var mainImageClicked = false;
var imageLoaded = false;
$(document).ready(function(){
	
		jQuery("#closeAuthError").click(function(){
			//jQuery(this).fadeOut();
			jQuery(this).parent().fadeOut();
		});
	$('#subscribeLink').click(function(event){
		event.preventDefault();
	
		if($("#subscribeBox").css('display')=='none')
		{
			$("#subscribeBox").fadeIn('slow');
		}else {
			$("#subscribeBox").fadeOut('slow');
		}
	});
	
	$("#topMenu>ul>li:last").hide();
	
	var myTimeout;
	
	$("#topMenu>ul>li.menuItem").hover(function(){
		//menuNotHover = false;
		if(myTimeout) clearTimeout(myTimeout);
		$(".subMenu").hide();
		var sub = $(this).children('.subMenu');	
		sub.show();		
		$(this).prev().children('img').attr('src','img/common/menu_item_left.jpg');
		$(this).next().children('img').attr('src','img/common/menu_item_right.jpg');
	if(sub.offset().left > 596) {
		
			sub.css("left","-170px");
		};
	},
	function(){
	var sub = $(this).children('.subMenu');
	myTimeout = setTimeout(function(){
			
				sub.hide();
			
		},700);
		
		$(this).prev().children('img').attr('src','img/common/menu_hidden.gif');
		$(this).next().children('img').attr('src','img/common/menu_hidden.gif');		
	}
	);
	
	$('select[name=filter_year]').change(function(){

		$("form#filterForm").submit();
	});
	
	$('select[name=filter_month]').change(function(){
		if($('select[name=filter_year] option:selected').val())
		{
			$("form#filterForm").submit();
		}
	});
	
	
	var CECount = jQuery('#carouselBox ul').size();		
	var CEWidth =324;
	var switchInterval;
	
	jQuery('#galleryForward img').mousedown(function(event){
	    clearInterval(switchInterval);
		switchInterval = setInterval("nextSwitch()",10000);
	animating = true;	
	animateGalleryForward();
		
	}).mouseup(function(){
        animating = false;
    });
	
	jQuery('#galleryBack img').mousedown(function(event){
	clearInterval(switchInterval);
		switchInterval = setInterval("nextSwitch()",10000);
	
		animating = true;		
		animateGalleryBack();
			
	}).mouseup(function(){
        animating = false;

    });
;
	
	 $("#galleryMainImageContent").click(function(){
	 if(mainImageClicked==false){
		mainImageClicked = true;
		}else {
		mainImageClicked = false;
		}
		if(mainImageClicked==false) {
			$("#galleryPause").show();
			$("#galleryPlay").hide();
		}else {
			$("#galleryPause").hide();
			$("#galleryPlay").show();
		}
	 });
	 
	 $("#galleryMainImageContent").hover(function(){
	 if(imageLoaded==true){
		if(mainImageClicked==false) {
			$("#galleryPause").show();
		}else {
			$("#galleryPlay").show();
		}
		}
	 },function(){
		if(mainImageClicked==false) {
			$("#galleryPause").hide();
		}else {
			$("#galleryPlay").hide();
		}
	 });	 

	$('.thumbItem a').click(function(event){
		imageLoaded = false;
		mainImageClicked = false;
	    clearInterval(switchInterval);
		
		event.preventDefault();
	$('#mainImage').attr('src',"img_thumb.php?src="+$(this).attr('href')+"&height=312&width=450");
	$("#mainImage").hide();
	$(".ajaxLoad").show();
	});
	
	$('#mainImage').load(function(){
	imageLoaded = true;
	$(".ajaxLoad").hide();
	$(this).fadeIn();
	
	 switchInterval = setInterval("nextSwitch()",10000);
	 
	})
	
	$('.thumbItem').click(function(event){
	
		
	$('#carouselBox').stop();
		$('.thumbItem.selected').removeClass('selected');
		$(this).addClass('selected');
		if($('.thumbItem').length>4){
		if($(this).position().left>168&&$(this).position().left< $(".thumbItem:last").prev().position().left)
		{
		
			jQuery('#carouselBox').animate({
				right:"+="+($(this).offset().left-465)
			},{
			duration:1000,
			queue:false
			});
		}

		if($(this).position().left<=168){
			jQuery('#carouselBox').animate({
				right: 0
			},{
			duration:1000,
			queue:false
			});
		}
		
		if($(this).position().left>= $(".thumbItem:last").prev().position().left){	
			jQuery('#carouselBox').animate({
				right: $(".thumbItem:last").position().left - 502
			},{
			duration:1000,
			queue:false
			});
		}
		}
		
	});
	

	$("#sendToFriendLink").click(function(){
		$("#sendToFriend").toggle();
	});
	
	$('.thumbItem a:first').click();
	
	//clearInterval(switchInterval);
	
	
	// Sharing 
	
	$(".contentShare>img").click(function(event){
		event.preventDefault();
		$(".shareSub").toggle();
	});

	
   
});

function nextSwitch()
{
	if(mainImageClicked == false){
	if($('.thumbItem.selected').next().children('a').attr("href")!=undefined){	
	$('.thumbItem.selected').next().children('a').click();
	}else {
			$('.thumbItem:first').children("a").click();
	}
	}
}

var animating = false;
	function animateGalleryForward()
	{
				if($("#carouselBox .thumbItem:last").offset().left+ 160 > $('#carouselWrapper').offset().left + 672){
		jQuery('#carouselBox').animate(
					{
						right : "+=5px",
					},
					{
						duration: 10,
						step: function( currentLeft ){							
							if($("#carouselBox .thumbItem:last").offset().left+ 160 < $('#carouselWrapper').offset().left + 672){
							$('#carouselBox').stop();
							}
						},
						complete: function()
						{
							if(animating)
							{
								animateGalleryForward();
							}
						}
					}
				);	
				}
	}
	
	function animateGalleryBack()
	{
	if($("#carouselBox .thumbItem:first").offset().left < $('#carouselWrapper').offset().left+6){
				jQuery('#carouselBox').animate(
					{
						right : "-=5px",
					},
					{
						duration: 10,
						step: function( currentLeft ){
							
							if($("#carouselBox .thumbItem:first").offset().left > $('#carouselWrapper').offset().left+6){
							$('#carouselBox').stop();
							}
						},
						complete: function()
						{
							if(animating)
							{
								animateGalleryBack();
							}
						}
					}
				);
				}
	}
