/* Author: 

*/

var slideshow = {	
	
	init:function() {
		var c = 0;
		window.addEventListener('focus', function(){c += 1; if (c >= 1) {slideshow.hide();}},false);
				
		jQuery('#accordion').dcAccordion();	
		
		currentItem = 0;
		
		// Thumbs fade-in one by one
		jQuery('section#gallery div.thumbsPanel div').hide().each(function(index) {
		    jQuery(this).delay(50*index).fadeIn();
		});
		
		// Load first thumb as main image
		jQuery('section#gallery div.thumbsPanel div:first a, section#gallery div.thumbsPanel div:first').addClass('active');
		jQuery('section#gallery div.thumbsPanel div:first a img.selected').css({zIndex: "9"});
		var initialImageURL = jQuery('section#gallery div.thumbsPanel div:first a').attr('href');
		jQuery('section#gallery .main-image img').hide().attr('src',initialImageURL).load(function() {
			jQuery(this).fadeIn();
		});
	
		// Hover and Active Thumbnail States
		jQuery('section#gallery div.thumbsPanel div a').hover(function() {
			jQuery(this).children('img.selected').css({zIndex: "9"});
		}, function() {
			if(jQuery(this).hasClass('active')) {
				
			} else {
				jQuery(this).children('img.selected').css({zIndex: "0"});
			}
		});
			
		// Load selected thumb as main image			
		jQuery('section#gallery div.thumbsPanel div a').click(function(e) {
			currentItem = jQuery(this).parent().index('section#gallery div.thumbsPanel div');
			var slideshowItem = jQuery('section#gallery div.liner div.slideshow div');
			
			if(slideshowItem.length > 0) {
				slideshow.hide();
			} 
			
			if(jQuery(this).hasClass('active')) {
				
			} else {
				jQuery('section#gallery div.thumbsPanel div a img.selected').css({zIndex: "0"});
				jQuery(this).children('img.selected').css({zIndex: "9"});
				jQuery('section#gallery div.thumbsPanel div a, section#gallery div.thumbsPanel div').removeClass('active');
				jQuery(this).addClass('active');
				jQuery(this).parent().addClass('active');
				var mainImageURL = jQuery(this).attr('href');
				jQuery('section#gallery .main-image img').hide().attr('src',mainImageURL).load(function() {
					jQuery(this).fadeIn();
				});		
				if (jQuery('body').hasClass('thumb-info')) {
					jQuery('section#gallery div.thumbsPanel div .description').animate({opacity: 0}, 300);
					jQuery('section#gallery div.thumbsPanel div.active .description').animate({opacity: 1.0}, 500);
				}	
			}
			listItem = currentItem+1;
			e.preventDefault();
		});
	
		// Slideshow Controls
		var slideshowControl = jQuery('section#gallery div#controls p.options a#slideshowControl');
		var imageContianer = jQuery('section#gallery div.liner');
		var slideshowContainer = jQuery('section#gallery div.liner div.slideshow'); 
		var mainImageContainer = jQuery('section#gallery div.liner div.main-image');
		
		slideshowControl.click(function(e) {
			if(slideshowControl.hasClass('active')) {
				slideshow.hide();
			} else {
				slideshow.show();
			}
			e.preventDefault();
		});	
	
		// Next and Previous Controls
		jQuery('section#gallery div#controls p.navigation a').click(function(e){
			var mainImage = jQuery('section#gallery .main-image img');
			var currentImage = jQuery('section#gallery div.thumbsPanel div a.active');
			var allThumbs = jQuery('section#gallery div.thumbsPanel div');
			var allDescription = jQuery('section#gallery div.thumbsPanel div .description');
			var prevDescription = jQuery('section#gallery div.thumbsPanel div.active').prev('div').children('.description');
			var nextDescription = jQuery('section#gallery div.thumbsPanel div.active').next('div').children('.description');
			var prevImage = jQuery('section#gallery div.thumbsPanel div a.active').parent().prev('div').children('a');
			var nextImage = jQuery('section#gallery div.thumbsPanel div a.active').parent().next('div').children('a');
			var slideshowItem = jQuery('section#gallery div.liner div.slideshow div');
			
			jQuery('section#gallery div.thumbsPanel div a img.selected').css({zIndex: "0"});	
	
			if(jQuery(this).hasClass('prev')) {
				if (prevImage.length > 0) {
					if (jQuery('body').hasClass('slideshow')) {
						slideshow.gallery("p");
					} else {
						currentItem = currentItem-1;
						prevImage.addClass('active');
						allThumbs.removeClass('active');
						prevImage.parent('div').addClass('active');
						prevImage.children('img.selected').css({zIndex: "9"});
						mainImage.hide().attr('src', prevImage.attr('href')).load(function() {
							jQuery(this).fadeIn();
						});					
						currentImage.removeClass('active');		
						if (jQuery('body').hasClass('thumb-info')) {
							allDescription.animate({opacity: 0}, 300);
							prevDescription.animate({opacity: 1.0}, 500);	
						}
					}
				}			
			} else if(jQuery(this).hasClass('next')) {
				if (nextImage.length > 0) {
					if (jQuery('body').hasClass('slideshow')) {
						slideshow.gallery("n");
					} else {
						currentItem = currentItem+1;
						nextImage.addClass('active');
						allThumbs.removeClass('active');
						nextImage.parent('div').addClass('active');
						nextImage.children('img.selected').css({zIndex: "9"});
						mainImage.hide().attr('src', nextImage.attr('href')).load(function() {
							jQuery(this).fadeIn();
						});	
						currentImage.removeClass('active');
						if (jQuery('body').hasClass('thumb-info')) {
							allDescription.animate({opacity: 0}, 300);
							nextDescription.animate({opacity: 1.0}, 500);
						}
					}
				}			
			}
			listItem = currentItem+1;
			e.preventDefault();
		});
	
		jQuery('#thumbnailVis').click(function(e) {
			if (jQuery(this).hasClass('no-thumbs')) {
				// Display Thumbnails
				jQuery(this).removeClass('no-thumbs');
				jQuery('section#gallery div.thumbsPanel div').stop().each(function(index) {
				    jQuery(this).delay(50*index).animate({opacity: 1}, 300);
				});
			} else {
				// Hide Thumbnails
				jQuery(this).addClass('no-thumbs');
				jQuery('section#gallery div.thumbsPanel div').stop().each(function(index) {
				    jQuery(this).delay(50*index).animate({opacity: 0}, 300);
				});
			}
			e.preventDefault();
		});
		
		jQuery('.info a').click(function(e) {
			if (jQuery('body').hasClass('thumb-info')) {
				// Hide Info
				jQuery(this).html('View Info');
				jQuery('body').removeClass('thumb-info');
				jQuery('section#gallery div.thumbsPanel div .description').animate({opacity: 0}, 100);
			} else {
				// Display Info
				jQuery(this).html('Hide Info');
				jQuery('body').addClass('thumb-info');
				jQuery('section#gallery div.thumbsPanel div.active .description').animate({opacity: 1.0}, 200);
				if (jQuery('body').hasClass('slideshow')) {
					slideshow.hide();
				}
			}
			e.preventDefault();
		});		
	},
	
	create:function() {
		var thumbsArray = new Array();
		var startArray = new Array();
		var imageContainer = jQuery('section#gallery div.liner div.slideshow');

		jQuery('section#gallery div.thumbsPanel div').each(function() {
			var thumbURL = jQuery(this).children('a').attr('href');
			thumbsArray.push(thumbURL);
		});
		
		jQuery.each(thumbsArray.slice(currentItem), function(intIndex, objValue){
			var thumbURL = objValue;
			startArray.push(thumbURL);
		});
		
		var i = startArray.length;
		mergeArray = jQuery.merge(startArray, thumbsArray);	
		while(i > 0) {
			mergeArray.pop();
			i--;
		}
		
		jQuery('section#gallery div.liner div.slideshow div').remove();
		jQuery.each(thumbsArray, function(intIndex, objValue){
			imageContainer.append(jQuery( "<div class='" + intIndex + "'><img src='" + objValue + "' /></div>"));
		});
		gallery = 1;
	},
	
	hide:function() {	
		jQuery('section#gallery div#controls p.options a#slideshowControl').removeClass('active').html('Start Slideshow');
		jQuery('section#gallery div.liner div.slideshow').css({zIndex: 0});
		slideshow.stop();
	},
	
	show:function() {
		jQuery('section#gallery div#controls p.options a#slideshowControl').addClass('active').html('Stop Slideshow');
		jQuery('section#gallery div.liner div.slideshow').css({zIndex: 10});
		slideshow.create();		
		slideshow.start();
		jQuery('section#gallery div.thumbsPanel div').removeClass('active');
		jQuery('section#gallery div.thumbsPanel div:first').addClass('active');
		jQuery('section#gallery div.thumbsPanel div a.active img.selected').css({zIndex: "0"});
	},
	
	start:function() {
		jQuery('section#gallery div.liner div.slideshow div').css({opacity: 0});
		jQuery('section#gallery div.liner div.slideshow div:first').css({opacity: 1}).addClass('active');
		slider = setInterval('slideshow.gallery("n")', 6000);
		jQuery('body').addClass('slideshow');
		jQuery('.info a').html('View Info');
		jQuery('body').removeClass('thumb-info');
		jQuery('section#gallery div.thumbsPanel div .description').css({opacity: "0"});
		jQuery('section#gallery div.thumbsPanel div:first').children('a').children('img.selected').css({zIndex: "9"});
	},
	
	stop:function() {
		clearInterval(slider);
		jQuery('section#gallery div.thumbsPanel div a img.selected').css({zIndex: "0"});
		jQuery('section#gallery div.thumbsPanel div a.active img.selected').css({zIndex: "9"});
		jQuery('body').removeClass('slideshow');
	},
	
	gallery:function(d) {
		var currentImage = (jQuery('section#gallery div.liner div.slideshow div.active') ? jQuery('section#gallery div.liner div.slideshow div.active') : jQuery('section#gallery div.liner div.slideshow div:first'));
		var currentThumb = (jQuery('section#gallery div.thumbsPanel div.active') ? jQuery('section#gallery div.thumbsPanel div.active') : jQuery('section#gallery div.thumbsPanel div:first'));

		var prevImage = ((currentImage.next('div').length) ? ((currentImage.prev('div').hasClass('active')) ? jQuery('section#gallery div.liner div.slideshow div.active') : currentImage.prev()) : jQuery('section#gallery div.liner div.slideshow div:first'));
		var prevThumb = (currentThumb.prev().length) ? currentThumb.prev() : jQuery('section#gallery div.thumbsPanel div:first');

		var nextImage = ((currentImage.next('div').length) ? ((currentImage.next('div').hasClass('active')) ? jQuery('section#gallery div.liner div.slideshow div.active') : currentImage.next()) : jQuery('section#gallery div.liner div.slideshow div:first'));
		var nextThumb = (currentThumb.next().length) ? currentThumb.next() : jQuery('section#gallery div.thumbsPanel div:first');
		
		if (d == "n") {
			nextImage.css({opacity: 0.0}).addClass('active').animate({opacity: 1.0}, 1000);
			currentImage.animate({opacity: 0.0}, 1000).removeClass('active');	
			
			jQuery('section#gallery div.thumbsPanel div').children('a').children('img.selected').css({zIndex: "0"});
			nextThumb.children('a').children('img.selected').css({zIndex: "9"});
			
			nextThumb.addClass('active');
			currentThumb.removeClass('active');
		} else if (d == "p") {
			prevImage.css({opacity: 0.0}).addClass('active').animate({opacity: 1.0}, 1000);
			currentImage.animate({opacity: 0.0}, 1000).removeClass('active');	
			
			jQuery('section#gallery div.thumbsPanel div').children('a').children('img.selected').css({zIndex: "0"});
			prevThumb.children('a').children('img.selected').css({zIndex: "9"});
			
			prevThumb.addClass('active');
			currentThumb.removeClass('active');
		}
	}
}


jQuery(document).ready(function(){	
	

});
