var toggled = true; // extra image-info is on

function sendContact(form,parstemplate) {
	   		var resultCheck = formChecker.checkForm(form);
			if(resultCheck) {
							     $.ajax({
								 url: '/nl/c/'+parstemplate,
								 cache: false,
								 type: "POST",
								 data: $(form).serialize(),
								 success: function(html){
										$("#content").empty();
										$("#content").append(html);
										$('.ContentBox').show().animate({"left": "100px"}, "fast").addClass('Shown');
										
										noContent();
										executeScrollPanes();
										executeDraggable();
										document.title=$('h2',html).text()+' ~ Caroline Biss';
										Cufon.replace('h2');			
								 }
					});
					return false;
			   } else {
					return false;
			   }
}


function activatePhotoHolder() {
	
	/**
	 * Bind events on photoHolder, mouseenter and mouseleave
	 */
	 
	$("#photoHolder,.PN").css('height',($(window).height()-40));
	
	$("#photoHolder").bind({ 
		mouseenter: function(){
			$("#bottomBar").stop().animate({"bottom": "0"}, 550);
			if (toggled) {$(".ImageInfo").stop().animate({"bottom": "33"}, 550);} // only animate this on mouseenter if it's needed ...
			$("#next").stop().animate({"right": "0"}, "medium");
			$("#prev").stop().animate({"left": "0"}, "medium");
			
		},
		mouseleave: function(){	
		
			$("#bottomBar").stop().animate({"bottom": "-110px"}, "fast");
			if (toggled) {$(".ImageInfo").stop().animate({"bottom": "-51px"}, "fast");} // only animate this on mouseleave if it's needed ...
			$("#next").stop().animate({"right": "-100px"}, "fast");
			$("#prev").stop().animate({"left": "-100px"}, "fast");
			
		}
	});
	
	/**
	 * Show info button in bottom bar of website
	 */
	 
	$('.ShowInfo').bind('click', function() {
		if(!toggled){
			// show the info
			$(this).addClass("HideInfo");
			$(".ImageInfo").fadeIn(100,
				function(){
				  	$(this).animate({"bottom": "33px"}, 400);
				 }
			);
			
			toggled = true;
		}
		else {
			// hide the info 
			$(this).removeClass("HideInfo");
			$(".ImageInfo").animate({"bottom": "-30px"}, 500, 
					function(){
						$(".ImageInfo").fadeOut(300);
																										
					}
			);
			toggled = false;
		}
		return false;
	});

}

function toggleCheck () {
	if (toggled) {
		$('.ShowInfo').addClass("HideInfo");
		
	} else {
		$('.ShowInfo').removeClass("HideInfo");
	}
}

function onBefore(curr,next,opts) {
	$('.InfoText.empty').parent().hide();
	}

function onAfter(curr,next,opts) {
	var caption = (opts.currSlide + 1) + '/' + opts.slideCount;
	$('#counter').html(caption);
	}

function executeTabs() {
	$('.ContentBox div.TabContent').hide(); // Hide all divs
	$('#tab1').show();
	$('ul.Tabs li:first').addClass('Active'); // Set the class for active state
	$('ul.Tabs li a').click(function(){ // When link is clicked
			$('ul.Tabs li').removeClass('Active'); // Remove active class from links
			$(this).parent().addClass('Active'); //Set parent of clicked link class to active

			var urlParams = $(this).attr('href').split("#");
			var tabId = '#'+urlParams[1];

			$('.ContentBox div.TabContent').hide(); // Hide all divs
			$(tabId).show();
			$('div',$(tabId)).show(); // Show div with id equal to variable currentTab
			return false;
	});
		
}

function executePostcodeSearch() {
	
	if ($('#overview_offices').length > 0) {
	
		
		$('#overview_offices').attr('style','visibilty:visible;');
		
		$('#postcode_search').keypress(function(event) {						 
			  if (event.keyCode == '13') {
				 event.preventDefault(); // if enter is pressed do nothing
				 $('#btnZoekPostcode').click();
			   }
		});
			
		$('#overview_offices tr').hide();
		executeScrollPanes();
		$('#btnZoekPostcode').bind('click',function () {
					
			country = $('#country_search').val();
			postcode = $('#postcode_search').val().substr(0,2);
	
			/* if theres no good postcode show none ... */
			if ((isNaN(postcode)) | (postcode == 0)) {
				$('#overview_offices tr').hide();
				$('#overview_offices table').append('<tr><td>postcode is not valid</td></tr>');
			} else {
				$('#overview_offices tr').show();
				/* hide irrelevant postcodes */	
				$('#overview_offices tr:not(.postcode'+postcode+')').hide();
				/* hide irrelevant countries */	
				$('#overview_offices tr:not(.land'+country+')').hide();
				if ($('#overview_offices tr:visible').length == 0) {
					$('#overview_offices table').append('<tr><td>no results found for postcode: '+$('#postcode_search').val()+'</td></tr>');
				};	
				executeScrollPanes();			
			}
			return false;
		});
	}
}

function executeDraggable() {
	/**
	 * Define draggable areas and execute jScrollPane
	 */
	$(".ContentBox").draggable({ handle: 'h2', containment: 'parent' });
	
}

function executeScrollPanes() {
	$('.ScrollContent').jScrollPane({
			showArrows:true,
			scrollbarWidth:19,
			scrollbarMargin:0
	})
}

function noContent() {
	$(".CloseContent").click(function(evt) {
			$(".Overlay").hide().animate({"opacity": "0"}, "fast").css('display','none');
			$('.ContentBox').animate({"left": "-700px"}, 500,function(){$("#content").hide();});				
			$('#menuNavigation li').removeClass('Active');
	}); 
	
}


function imagePosition() {
	// $("#photoHolder,.PN").css('height',($(window).height()-40)); 
	// $('#photos .slide').css('display','block'); // images must be visible to calculate width
	var imageSize = $('#photos .slide img').width();
	$('#photos .slide img').css({'margin-left' : -(imageSize/2)});								  
	$("#loading").fadeOut(500, function(){$("#photos").css('visibility','visible');});	
}

function resizingImages () {
	$("#photoHolder,.PN").css('height',($(window).height()-40));
	$('#photos .slide').css('display','block'); // images must be visible to calculate width
	$('#photos .slide img').each(function(){								  
		var imageSize = $(this).width();
		if (imageSize > 0) {
			$(this).css({'margin-left' : -(imageSize/2)});
		} else {			
			$(this).css({'margin-left' : '-1040px'});
		}
	});
}

function goCycle (startframe) {
	/**
	 * Execute the cycle plugin 
	 */
	$('#imageslider').cycle({
		timeout: 0,
		speed: 500,
		pager:  '#cycleNav',
		pagerAnchorBuilder: function(idx, slide) { 
        return '<a href="#" id="link'+(idx)+'">thumb'+(idx)+'</a>'; 
    	}, 
		width: '100%',
		pause: 1,
		fit: 1,
		startingSlide: startframe,
		next: '#next',
		prev: '#prev',
		before: onBefore,
		after: onAfter
	});
}

function goThumbnails () {
	$('#cycleNav a').bind('mouseenter mouseleave',
		
		function () {
			var link = '#'+($(this).text());
			var position = $(this).position();
			var positionleft = position.left;
			var startingposition = $('#cycleHolder').position();
			var startingpositionleft = startingposition.left;
			var thumbwith = ($(link).width()/2);
			$(link).css('left',(startingpositionleft+positionleft)-thumbwith+6 + 'px');
			$(link).toggleClass('thumbactive');			
		}
	);
}

function cycleStuff () {
	var clickCount = 0;
	var cycleCount = $("#cycleNav a").size();
	var cycleLength = $("#cycleHolder").width();
	var maxClicks = Math.floor(cycleCount / 15);

	$("#cycleNav").css('margin-left','0px');
	
	if (maxClicks == 0) {
		$('.cyclePN').hide();} else {
		$('.cyclePN').show();
	}
	
	$('#cycleNext').bind('click',
						
		function() {
			
			if ((clickCount < maxClicks) && ($("#cycleNext.disabled").length == 0)) {
				$("#cycleNext").addClass('disabled');
				$("#cycleNav").stop().animate({"margin-left": "-="+cycleLength}, "slow", function(){clickCount ++;$("#cycleNext").removeClass('disabled');});
				return false;
			} 
			
		}
	);
	$('#cyclePrev').bind('click',
						
		function() {
			
			if ((clickCount > 0) && ($("#cyclePrev.disabled").length == 0)) {
				$("#cyclePrev").addClass('disabled');
				$("#cycleNav").stop().animate({"margin-left": "+="+cycleLength}, "slow", function(){clickCount --;$("#cyclePrev").removeClass('disabled');});
				return false;	
			}
			
		}
	);
}


$(document).ready(function() {
	
	var homeFotoGalleryUrl = '/en/a/22'; //  set home fotogallery
	var loadingGall = true;
	
	$(".Overlay").hide().css({"opacity": "0"});
	$("#content").hide();
	$('#menuCatalogue li:first').addClass('Active');
	
	$(window).resize(function(){
		resizingImages ();
	});
	
	activatePhotoHolder();
	toggleCheck ();	

	// loading home gallery
	
	$("#loading").fadeIn(function(){
	
		$.ajax({
			url: homeFotoGalleryUrl,
			cache: true,
			type: "GET",
			success: function(html) {
				document.title='Home ~ Caroline Biss';
				$(".galleryTitle").html($('#menuCatalogue li a:first').text()); // set gallery title in bottombar 
				// $("#photos").hide().html(html); // set fotocontent
				$("#photos").css('visibility','hidden').html(html); // set fotocontent
			}
		});
	});
	
		
	/**
	 * Define mouseenter and mouseleave events for dropdown navigation
	 */
	$(".MenuItem").bind({
		mouseenter: function(){		
			var height = $("ul",this).height();
			$(this).addClass("Hover");
			$(this).children(".Dropdown").stop().animate({"height": + height + 25 + "px"}, "fast");
		},
		mouseleave: function(){
			$(this).children(".Dropdown").stop().animate({"height": "0"}, "medium");
			$(this).removeClass("Hover");
		}
	});
	
	$("#closeie6warning").bind({
		click: function (){
			$("#ie6_banner").hide();
			$("#topBar").css('top','0px');
		}
	});
	
	/**
	 * Bind prev en next button animations
	 */
	 
	$("#next").bind({
		mouseenter: function(){
			$(this).animate({"right": "-10px","opacity": "0.8"}, "fast");
		},
		mouseleave: function(){
			$(this).animate({"right": "0","opacity": "0.5"}, "fast");
		}
	});
	
	$("#prev").bind({
		mouseenter: function(){
			$(this).animate({"left": "-10px","opacity": "0.8"}, "fast");
		},
		mouseleave: function(){
			$(this).animate({"left": "0","opacity": "0.5"}, "fast");
		}
	}); 
	
	/**
	 * Define custom easing pattern
	 */
	/* $.easing.custom = function (x, t, b, c, d) {
			return c*(t/=d)*t*t*t + b;
	}*/
	

	/**
	 * ## loading imagegalleries ...
	 */

	$(".menuNavigation a").bind('click', function() {
		navigationid = $(this).attr("href");
		// Clicked anchor element;
		var navElm = this;	
		$.ajax({
			url: navigationid,
			cache: false,
			type: "GET",
			success: function(html) {
																			
				// $("#photoHolder").unbind(); // unbind events for fotoholder
				
				$(".Overlay").show().animate({"opacity": "0.5"}, "fast");
				$("#content").html(html).show();
				$('.menuNavigation li').removeClass('Active');
										
				$(navElm).parent().addClass('Active');
				
				var currentTab = $('div:first',$(this).attr("href"));
				
				$("#content div.Shown").css({"left": "-650px"}).removeClass('Shown').hide();
				$('.ContentBox').attr('style','visibility:visible;').show().animate({"left": "100px"}, "fast").addClass('Shown');
				$('div:first',$(navElm).attr("href")).show();
				$('ul.Tabs li:first',$(navElm).attr("href")).addClass('Active');
				noContent();
				executeScrollPanes();
				executeTabs();
				executeDraggable();
				executePostcodeSearch();
				document.title=$('h2',html).text()+' ~ Caroline Biss';
				Cufon.replace('h2');		
			}
		});
			
		return false;
		
	});
	
	$("#tellafriend").bind('click', function() {
		navigationid = $(this).attr("href");
		// Clicked anchor element;
		var navElm = this;
			
		$.ajax({
			url: navigationid,
			cache: false,
			type: "GET",
			success: function(html) {
																			
				$("#photoHolder").unbind(); // unbind events for fotoholder
				
				$(".Overlay").show().animate({"opacity": "0.5"}, "fast");
				$("#content").html(html).show();
										
				$("#content div.Shown").css({"left": "-650px"}).removeClass('Shown').hide();
				$('.ContentBox').show().animate({"left": "100px"}, "fast").addClass('Shown');
	
				noContent();
				executeScrollPanes();
				executeDraggable();
				document.title=$('h2',html).text()+' ~ Caroline Biss';
				Cufon.replace('h2');	
				
			}
		});
			
		return false;
		
	});
	
	
	
	/**
	 * Loading the gallery's
	 */
	
	$(".menuGallery a").bind('click', function() {
		
		$(".CloseContent").triggerHandler('click');
		
		loadingGall = true;
		var navElm = this;
		var navigationid = $(this).attr("href");
			
		$("#photos").fadeOut(200, function(){
			$("#cycleNav").empty();	// clear pager
			$('.menuGallery li').removeClass('Active');
			$(navElm).parent().addClass('Active');
			$("#loading").fadeIn(function(){
				$.ajax({
					url: navigationid,
					cache: true,
					type: "GET",
					success: function(html) {						
						$(".galleryTitle").html($(navElm).text()); // set gallery title in bottombar 
						document.title=$(navElm).text()+' ~ Caroline Biss';
						// $("#photos").hide().html(html); // set fotocontent
						$("#photos").css({'display':'block','visibility':'hidden'}).html(html); // set fotocontent
					} 
				});
			});
		});

		return false;
		
	});
	
	$("#photos").ajaxComplete(function(){
		if(loadingGall) {	
			// images should be loaded first ...
			$('#photos .slide img:last').load(function (){
				goCycle(0);
				goThumbnails();
				cycleStuff();
				loadingGall = false;
				toggleCheck();
				imagePosition();
				});
		}
	});
				
});