// start gallery section
var Gallery = {
	images : null,
	imagePrev : null,
	imageNext : null,
	imageWidth : 940,
	imageContainer : null,
	totalImage: 0,
	animating: null,
	pause : false,
	
	init : function(callback) {
        $(document).ready(function() {
			Gallery.preloadImage(callback)
		});
    },
	
	preloadImage: function(callback) {
		var images = $('#headerImage .images img');
		var nmb_images = images.length;
		var loaded = 0;
		
		images.each(function(i){
			var $image = $(this);
			$('<img />').load(function(){
				++loaded;
				if(loaded == nmb_images){
					if (typeof(callback) == 'function')
						callback();
						
					Gallery._init();
				}
			}).attr('src', $image.attr('src'));
		});
	},
    
    _init : function () {
		Gallery.imageContainer = $('#headerImage .images');
		Gallery.images = $('#headerImage .images .image');
	
		if ($('#headerImage a.nextImage') != null)
			Gallery.imageNext = $($('#headerImage a.nextImage')[0]);
			
		if ($('#headerImage a.previousImage') != null) {
			Gallery.imagePrev = $($('#headerImage a.previousImage')[0]);
		}
		
		if (!Gallery.images || !Gallery.imageContainer || !Gallery.imageNext || !Gallery.imagePrev) return;
		
		Gallery.totalImage = Gallery.images.length;
		
		if(Gallery.totalImage <= 1) {
			Gallery.imageNext.fadeOut(500);
			Gallery.imagePrev.fadeOut(500);
			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);
		
		//$('#headerImage').css({'visibility':'visible'});
		
		$("#headerImage").everyTime(5000,Gallery.autoPlay);
		
		$('#headerImage').mouseenter(function() {
			$("#headerImage").stopTime();
		});
		
		$('#headerImage').mouseleave(function() {
			$("#headerImage").everyTime(5000,Gallery.autoPlay);
		});

	},
	autoPlay : function() {
		if (Gallery.pause) return;
		
		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;
			}
		});
	}
}

Gallery.init();
// end gallery section
