$(function(){
	// start video section
	$('#feature').data('originalHeight',$('#feature').outerHeight());
	
	$('#feature a.close').click(function() {
		$('#feature').animate({ height: 33 }, { duration: 1000, queue: false, complete: function() {
				$('#feature .detail').hide();
				$('#feature .showVideo').fadeIn(500);
			}
		});
		return false;
	});
	
	$('#feature .showVideo a').click(function() {
		$('#feature .detail').show(500);
		$('#feature').animate({ height: $('#feature').data('originalHeight') }, { duration: 1000, queue: false, complete: function() {
				$('#feature .showVideo').fadeOut(500);
			}
		});
		return false;
	});
	// end video section
});

// start gallery section
var Gallery = {
	images : null,
	imagePrev : null,
	imageNext : null,
	imageWidth : 950,
	imageContainer : null,
	totalImage: 0,
	animating: false,
	
	init : function() {
        $(document).ready(Gallery._init);
    },
    
    _init : function () {
		Gallery.imageContainer = $('#container .images');
		Gallery.images = $('#container .images .image');
	
		if ($('#container #nextImage') != null)
			Gallery.imageNext = $($('#container #nextImage')[0]);
			
		if ($('#container #previousImage') != null) {
			Gallery.imagePrev = $($('#container #previousImage')[0]);
		}
		
		if (!Gallery.images || !Gallery.imageContainer || !Gallery.imageNext || !Gallery.imagePrev) return;
		
		Gallery.totalImage = Gallery.images.length;
		
		if(Gallery.totalImage <= 1) {
			Gallery.imageNext.fadeOut(0);
			Gallery.imagePrev.fadeOut(0);
			return;
		}
		
		Gallery.imageContainer.css({'width' : (Gallery.totalImage * Gallery.imageWidth) + 'px'});
		
		Gallery.images.each(function(index) {
			this.index = index;
		});
		
		Gallery.imageNext.attr('rel','next');
		Gallery.imageNext.bind('click',Gallery.onImageMoveClick);
		Gallery.imagePrev.attr('rel','prev');
		Gallery.imagePrev.bind('click',Gallery.onImageMoveClick);
		
		//$('#container').css({'visibility':'visible'});
		
		$("#container").everyTime(5000,Gallery.autoPlay);
		
		$('#container').mouseenter(function() {
			$("#container").stopTime();
		});
		
		$('#container').mouseleave(function() {
			$("#container").everyTime(5000,Gallery.autoPlay);
		});
	},
	autoPlay : function() {
		Gallery.animating = true;
		
		targetMargin = -1*Gallery.imageWidth;
		
		$(Gallery.imageContainer).animate({ marginLeft: targetMargin}, { 
			queue:false, 
			duration:1500,
			complete: function() {
				Gallery.imageContainer.children(':first-child').detach().appendTo(Gallery.imageContainer).parent().css({marginLeft: 0});
				Gallery.animating = false;
			}
		});
	},
	onImageMoveClick : function() {
		if (Gallery.animating) return false;
		
		Gallery.animating = true;
		
		var direction = '';
		
		if ($(this).attr('rel') == 'next') {
			targetMargin = -1*Gallery.imageWidth;
			direction = 'right';
		} else {
			targetMargin = 0;
			direction = 'left';
			
			Gallery.imageContainer.children(':last-child').detach().prependTo(Gallery.imageContainer).parent().css({marginLeft: -1*Gallery.imageWidth});
		}
		
		$(Gallery.imageContainer).animate({ marginLeft: targetMargin}, { 
			queue:false, 
			duration:1500,
			complete: function() {
				if (direction == 'right') {
					Gallery.imageContainer.children(':first-child').detach().appendTo(Gallery.imageContainer).parent().css({marginLeft: 0});
				}
				Gallery.animating = false;
			}
		});
		
		return false;
	}
}

Gallery.init();
// end gallery section
