

function loadPage (obj, selector, target){
	if (!obj.length && !target.length) return;
	//alert (load(url + " #mainContent"));
	obj.click(function(e){
		e.preventDefault();
		var url = $(this).attr("href");
		obj.removeClass("active");
		$(this).addClass("active");
		target.fadeOut("fast", function(){
			target.load(url + " " + selector + " > *", {'load':'loadArtistPage'} , function(){
				target.fadeIn("fast");
			});
		});
		if (typeof window.history.pushState == 'function') {
  			history.pushState(new Object(), "seeyaproduktion", url);
		}
	    
	});
}

function doHomeAnimation (){
	$("#navigationArtistMenu ul li a").rand($("#navigationArtistMenu ul li a").length).each(function(){
		var url = $(this).attr('href');
		var linkTitle = $(this).attr('title');
		var linkText = $(this).html();
		$("#homeAnimation ul").append("<li></li>");
		$("#homeAnimation ul li:last").append('<a href="'+url+'" title="'+linkText+'"></a>');
		$("#homeAnimation ul li:last a").append('<div class="image"></div>');
		$("#homeAnimation ul li:last a").append('<div class="photographer">'+linkText+'</div>');
		$("#homeAnimation ul li:last a .image").load(url + " #mainContent .wt_gallery_single_inner_pic > *");
		
	});
	$("#homeAnimation ul").newsTicker();
}

function addToMyLightBox(imageUrl) {
	if ($.Jookie.Get("myLightBox", "images") == undefined){
		var images = new Array(imageUrl);
		$.Jookie.Set("myLightBox", "images", images);
		$("#addtolightboxlink a").html('Remove from Lightbox');
		$("#navigationFirstLevelMenu ul li.first a").show();
	}
	else {
		var images = $.Jookie.Get("myLightBox", "images");
		if (images.join().indexOf(imageUrl) == -1){
			images[images.length] = imageUrl;
			$.Jookie.Set("myLightBox", "images", images);
			$("#addtolightboxlink a").html('Remove from Lightbox');
			$("#navigationFirstLevelMenu ul li.first a").show();
		}
		else {
			images.splice(images.indexOf(imageUrl), 1);
			$.Jookie.Set("myLightBox", "images", images);
			$("#addtolightboxlink a").html('Add to Lightbox');
			if (images.length == 0) {
				$("#navigationFirstLevelMenu ul li.first a").hide();
			}
		}
	}
	//console.log(images);
}

function removeFromMyLightBox(imageUrl) {
	var images = $.Jookie.Get("myLightBox", "images");
	if (images.join().indexOf(imageUrl) == -1){
		images[images.length] = imageUrl;
		$.Jookie.Set("myLightBox", "images", images);
		$("#addtolightboxlink a").html('Remove from Lightbox');
		$("#navigationFirstLevelMenu ul li.first a").show();
	}
	else {
		images.splice(images.indexOf(imageUrl), 1);
		$.Jookie.Set("myLightBox", "images", images);
		$("#addtolightboxlink a").html('Add to Lightbox');
		if (images.length == 0) {
			$("#navigationFirstLevelMenu ul li.first a").hide();
		}
	}
	window.location.reload();
	//console.log(images);
}

$(document).ready(function (){
	$.Jookie.Initialise("myLightBox", 60); // in Minuten; 1440 = 1 Tag
	if ($.Jookie.Get("myLightBox", "images") == undefined || $.Jookie.Get("myLightBox", "images").length == 0){
		$("#navigationFirstLevelMenu ul li.first a").hide();
	}

	$(".scrollable").scrollable({size:10,loop:true, mousewheel:false});

	//loadPage($("#navigationArtistMenu ul li a").not("a.level3"), "#contentwrapper", $("#contentwrapper"));
	//loadPage($("#navigationFirstLevelMenu ul li a"), "#contentwrapper", $("#contentwrapper"));
	loadPage($(".wtgallery_list_area a"), ".wtgallery_single", $(".wtgallery_single"));
	loadPage($("#mainContent .wtgallery_single_browser_previous a"), "#mainContent .wtgallery_single", $("#mainContent .wtgallery_single"));
	loadPage($("#mainContent .wtgallery_single_browser_next a"), "#mainContent .wtgallery_single", $("#mainContent .wtgallery_single"));
	
	//loadPage($("#zoomlink a").not("#zoomlink a.close"), ".wtgallery_single", $("#zoomcontent"));
	//loadPage($("#zoomlink a.close"), ".wtgallery_single", $(".wtgallery_single"));

	if ($("#homeAnimation").length > 0){
		doHomeAnimation();
	}

	$('#addtolightboxlink a').removeAttr("href");
	if ($.Jookie.Get("myLightBox", "images") != undefined){
		var images = $.Jookie.Get("myLightBox", "images");
		var imageUrl = $('#addtolightboxlink a').attr('rel');
		if (images.join().indexOf(imageUrl) != -1){
			$("#addtolightboxlink a").html('Remove from Lightbox');
		}
	}	
	$('#addtolightboxlink a').click(function(){addToMyLightBox($('#addtolightboxlink a').attr('rel'));});
	
	$('#myLightBox .lightboxImage').click(function(){$(this).fadeOut('fast');removeFromMyLightBox($(this).attr('rel'));});
	
	$("body").ajaxComplete(function(event, request, settings){
		if (settings.data == 'load=loadArtistPage'){
			loadPage($("#mainContent .wtgallery_single_browser_previous a"), "#mainContent .wtgallery_single", $("#mainContent .wtgallery_single"));
			loadPage($("#mainContent .wtgallery_single_browser_next a"), "#mainContent .wtgallery_single", $("#mainContent .wtgallery_single"));
			$('#addtolightboxlink a').removeAttr("href");
			if ($.Jookie.Get("myLightBox", "images") != undefined){
				var images = $.Jookie.Get("myLightBox", "images");
				var imageUrl = $('#addtolightboxlink a').attr('rel');
				if (images.join().indexOf(imageUrl) != -1){
					$("#addtolightboxlink a").html('Remove from Lightbox');
				}
			}			
			$('#addtolightboxlink a').click(function(){addToMyLightBox($('#addtolightboxlink a').attr('rel'));});			
		}
	});	
});


