/*
 * Pallant House Gallery
 * Bureau for Visual Affairs
 */

// CAROUSEL
function Carousel(type,displayContainer, dataSource, controls){
	
	var ACTIVE_CLASS = 'active',
		MAIN_SLIDE_WIDTH = 945;
	
	var display = $(displayContainer);
	var controls = (controls) ? $(controls) : display;
	var dataSource = (dataSource) ? $(dataSource) : display;
	var data = $('li', dataSource);

	// updated the main page carousel view for infinite scrolling
	if (type == 'main') {
		dataSource.html(dataSource.html() + dataSource.html());
		dataSource.find('.' + ACTIVE_CLASS + ':last').removeClass(ACTIVE_CLASS);
		data = $('li', dataSource);
		$(dataSource).width(945 * data.size());
	}
	else if (type == 'text') {
		if($('li',dataSource).size() > 0){
			var img = $('img:first', dataSource);
			$('ul',dataSource).prepend('<li class="active"><a href="'+	 img.attr('src') +'"><span></span><img title="'+ img.attr('alt')+'" alt="'+ img.attr('alt')+'" src="'+ img.attr('src') +'"></a></li>');
			
			overlay = $('li:first', dataSource).find('span');
			overlay.width(overlay.closest('li').find('img').width());
		}
	}
	else if (type == 'lightbox') {
		$('<img>').attr('src','/layout/img/loader-lightbox.gif');
		$('#lb-mask, #lb-container .close').live('click',function(){
			$('#lb-mask, #lb-container').animate({'opacity':'0'}, 'fast', function(){
				$('#lb-container img').attr('src','');
				$('body').removeClass('lb');
				$('.image-gallery .active').removeClass(ACTIVE_CLASS);
			});
			$(document).unbind('keydown.lb');
		});
		$(window).resize(function(){
			var root = $('#lb-container');
			var marginTop = ($('img', root).height() + 64 > $(window).height()) ? $(window).height() / -2 : -1 * ($('img', root).height() + 64) / 2;
			root.stop().animate({'margin-top' : marginTop + 'px', 'margin-left' : -1 * ($('img', root).width() + 20) / 2 + 'px' }, 'fast');
		});
	}

	// thumbnail list click
	// triggered by .next/.prev to load data
	$('a', data).live('click',function(e){
		if(type == 'main') return;
		if($(this).closest('li').hasClass(ACTIVE_CLASS)) return false;
		e.preventDefault();
		var el = this,
			src = el.href;
			
			if(src.indexOf('jpg') == -1)
				src = $(el).find('img').attr('src');
		
		$(this).closest('ul').find('.' + ACTIVE_CLASS).removeClass(ACTIVE_CLASS);
		$(this).closest('li').addClass(ACTIVE_CLASS);
		
		// type:MICRO-MAIN CAROUSEL micro-main
		if(type == 'micro-main'){
			overlay = display.find('span');
			overlay.addClass('loading').animate({'opacity':'0.5'},'slow');
			controls.slideUp('fast',function(){
				$.get(src,function(data){
					var elDetails = $('img',$(el))[0].title.split('|');
					$('img', display).attr('src',src);
					$('.title', controls).html(elDetails[0]);
					$('.author', controls).html(elDetails[1]);
					$('.description', controls).html($(el).next().html());
					
					controls.delay(1000).slideDown('fast');
					overlay.stop().animate({opacity : '0'}, 'fast', function(){
						overlay.removeClass('loading');
					});
				});
			});
		}
		// type:ARTICLE/TEXT CAROUSEL
		else if(type == 'text'){
			overlay = $(this).closest('li').find('span');
			overlay.width($('img', el).width());
			overlay.addClass('loading');
			var iecachefix = (!jQuery.support.leadingWhitespace) ? '?' + Math.random() : '';
			$('div:first img', display).fadeTo(20,.1);
			$('<img>').attr('src', src + iecachefix).load(function(data){
				var elDetails = $('img',$(el))[0].title.split('|');
				$('div:first img', display).attr('src',src);
				$('div:first img', display).fadeTo(600,1);
				if(!elDetails[0]) elDetails[0] = '';
				if(!elDetails[1]) elDetails[1] = '&nbsp;';
				$('.description', controls).html('<em>' + elDetails[0] + '</em> ' + elDetails[1]);
				overlay.removeClass('loading');
			});
		} else if(type == 'lightbox'){
			if($('#lb-mask').size() < 1){
				var structure = '';
				structure += '<div id="lb-mask"></div>';
				structure += '<div id="lb-container">';
				structure += '<div class="bgfix">';
				structure += '  <div class="wrap">';
				structure += '  	<ul>';
				structure += '  		<li class="prev"><a href="#">prev</a></li>';
				structure += '  		<li class="next"><a href="#">next</a></li>';
				structure += '  	</ul>';
				structure += '  	<div class="dc">';
				structure += '  	    <div class="d">';
				structure += '      	</div>';
				structure += '  	</div>';
				structure += '  	<a href="#" class="close">x</a>';
				structure += '  </div>';
				structure += '  <div id="lb-view">';
				structure += '	  <img src="" alt="">';
				structure += '  </div>';
				structure += '</div>';
				structure += '</div>';
				$('body').addClass('lb').prepend(structure);
			}
			var root = $('#lb-container');
			if(root.is(':animated'))
				return false;
				
				
			if(!$('body').hasClass('lb')){
				$('body').addClass('lb');
			} 
			var Ymod = window.scrollY;
			
			$(document).unbind('keydown.lb').bind('keydown.lb',function(e){
				if (e.keyCode == '37') {// left
					$('.prev', root).trigger('click');
				}
				else if (e.keyCode == '39') {// right
					$('.next', root).trigger('click');
				}
				
				switch(e.keyCode){
					case 37 : case 39 : case 40 : case 38 : case 27 : case 67 :
						e.preventDefault();
						return false;
					default :
						return;
				}
			});
			
			$('#lb-mask').css({height : $(window).height() + 'px', width : $(window).width(), top : Ymod + 'px'}).addClass('loading').stop().animate({'opacity':'0.95'}, 'fast');
			root.css({opacity: 0 })
			root.animate({'opacity': 0}, 'fast', function(){
				var iecachefix = (!jQuery.support.leadingWhitespace) ? '?' + Math.random() : '';
				$('<img>').attr('src', src + iecachefix).load(function(data){
					$('img', root).attr('src',src);
					$('.d', root).html($('.d', el).html());
					root.removeClass('loading');
					
					$('img', root).width('auto');
					$('img', root).height('auto');
					
					var winHeight = $(window).height() - 20,
						winWidth = $(window).width() - 20,
						imgHeight = $('img', root).height(),
						imgWidth = $('img', root).width();
					if(imgHeight + 64 > winHeight || imgWidth + 20 > winWidth){
						if(imgHeight / winHeight > imgWidth / winWidth){
							imgHeight = winHeight - 64;
							imgWidth = imgWidth * imgHeight / $('img', root).height();
						} else {
							imgWidth = winWidth - 20;
							imgHeight = imgHeight * imgWidth / $('img', root).width();
						}
						$('img', root).width(imgWidth);
						$('img', root).height(imgHeight);
					}
					
					var marginTop = (imgHeight + 64) / -2 + Ymod,
						marginLeft = -1 * ($('img', root).width() + 20) / 2;
						
					root.css({'width':imgWidth + 20 + 'px','height':imgHeight + 64 + 'px', 'margin-top' : marginTop + 'px', 'margin-left' : marginLeft + 'px' });
					root.animate({'opacity': 1},'fast');
					$('#lb-mask').removeClass('loading')
					return false;
				});
			});
			
			return false;
		}
		
		return false;
	});
	
	// .next/.prev item
	if(type != 'main'){
		$('.next, .prev', controls).live('click',function(){
			thumbs = data.length,
			ix = data.index(dataSource.find('.' + ACTIVE_CLASS));
			if($(this).hasClass('next')){
				next = (ix + 1 == thumbs) ? 0 : ix + 1;	
				$(data[next]).find('a:first').trigger('click');
			} else {
				prev = (ix - 1 < 0) ? thumbs - 1 : ix - 1;
				$(data[prev]).find('a:first').trigger('click');
			}
			return false;
		});
	} 
	else if (type == 'main'){
		$('.next, .prev', controls).live('click',function(){
			if(dataSource.is(':animated'))
				return false;
			thumbs = data.length,
			ix = data.index(dataSource.find('.' + ACTIVE_CLASS));
			if($(this).hasClass('next')){
				next = (ix + 1 == thumbs) ? 0 : ix + 1;	
				$(data)[ix].className = '';
				if(next == 0){
					dataSource.find('li:lt((thumbs/2)').insertAfter(dataSource.find('li:last'));
					dataSource.stop().css({left : ((thumbs / 2 - 1) * -MAIN_SLIDE_WIDTH) + 'px'});
					next = thumbs/2;
				}
				$(data)[next].className = ACTIVE_CLASS;
				dataSource.stop().animate({'left':next * -MAIN_SLIDE_WIDTH + 'px'},'slow');
			} else {
				prev = (ix - 1 < 0) ? thumbs - 1 : ix - 1;
				$(data)[ix].className = '';
				if(prev == thumbs - 1){
					dataSource.find('li:gt((thumbs/2 - 1)').insertBefore(dataSource.find('li:first'));
					dataSource.stop().css({left : ((thumbs / 2) * -MAIN_SLIDE_WIDTH) + 'px'});
					prev = thumbs/2 - 1;
				}
				$(data)[prev].className = ACTIVE_CLASS;
				dataSource.stop().animate({'left':prev * -MAIN_SLIDE_WIDTH + 'px'},'slow');
			}
			return false;
		});
	}
	
}


// HORIZONTAL SLIDE
function Slide(slideContainer){
	
	slides = $(slideContainer);
	slidesCount = $('li', slides).size();
	slideWidth = $('li:first', slides).width();
	slides.width(slidesCount * slideWidth);
	wrapperWidth = slides.parent().width();
	controls = slides.closest('.container').find('.slide-horizontal-controls');
	
	if (slides.width() > wrapperWidth) {
		controls.find('.next-inactive').removeClass('next-inactive');
	}
	
	$('.next, .prev', controls).bind('click',function(){
		if(slideWidth * slidesCount < wrapperWidth)
			return false;
		if(slides.is(':animated')){
			return false;
		}
		else {
			slides.stop();
			var leftOffset = parseInt(slides.css('left'));
			if ($(this).hasClass('next')) {
				if(slides.width() + leftOffset > wrapperWidth){
					controls.find('.prev-inactive').removeClass('prev-inactive');
					slides.stop().animate({left: leftOffset - slideWidth + 'px'},'slow');
					if(slides.width() + leftOffset - wrapperWidth < slideWidth){
						controls.find('.next').addClass('next-inactive');	
					}
				}
				else {
					controls.find('.next').addClass('next-inactive');
				}
			
			} else if ($(this).hasClass('prev')){
				if(leftOffset < 0){
					controls.find('.next-inactive').removeClass('next-inactive');
					slides.stop().animate({left: leftOffset + slideWidth + 'px'},'slow');
					if(leftOffset + slideWidth >= 0) {
						controls.find('.prev').addClass('prev-inactive');
					}
				}
				else {
					controls.find('.prev').addClass('prev-inactive');
				}
			}
		}
		
		return false;
	});
}

function Expander(displayContainer){
	
	var root = $(displayContainer);
	
	$('.container-list .container').removeClass('expanded').find('.container-shadow-body').css({height: '178px'});
	
	$('.arrows-expand a', root).click(function(){
		var container = $(this).closest('div.container')
		if(container.hasClass('expanded')){
			container.removeClass('expanded');
			$('.container-shadow-body', container).stop().animate({height: '178px'}, 'slow');
		} else {
			container.addClass('expanded');
			var imageHeight = $('img', container).height();
			var imageHeight = $('.col-right', container).height();
			$('.container-shadow-body', container).stop().animate({height: imageHeight + 'px'}, 'slow');
		}
		return false;
	})
	
}
 
 
 // init on doc ready
 
$(function(){
	
	$('body').addClass('js-enabled');
	
	$('table tr').mouseover(function(){
		$(this).addClass('hover');
	}).mouseout(function(){
		$(this).removeClass('hover');
	}).click(function(){
		window.location = $(this).find('a')[0].href;
	});
	
	$('#submit-artwork').submit(function(){
		$(this).addClass('loading').children().css('visibility','hidden');
	});
	
	if(document.getElementById('micro-main-carousel-view'))
		document.getElementById('micro-main-carousel-view').carousel = new Carousel('micro-main','#micro-main-carousel-view','#micro-main-carousel-data','#micro-descr');
		
	if(document.getElementById('carousel-main'))
		document.getElementById('carousel-main').carousel = new Carousel('main','#carousel-main','#carousel-main .carousel-big');
		
	$('.text-carousel').each(function(){
		this.carousel = new Carousel('text','.text-carousel')
	});
	
	$('.expander-list').each(function(){
		this.expander = new Expander(this);
	});
	
	$('.slide-horizontal').each(function(){
		this.slide = new Slide('.slide-horizontal');
	})
	
	$('.image-gallery').not('.competition').each(function(){
		this.gallery = new Carousel('lightbox', '#lb-container', '.image-gallery')
	})
		
	if ($("#micro-navi-top .active a")[0] != null) {
		$('#micro-body h2:first').html('<a href="'+$("#micro-navi-top .active a")[0].href + '">' + $('#micro-body h2:first').text() + '</a>' );
	}
		
});

