var wheight, wwidth, showbox, openonhomepage;
var showbox;
var stepsset = 0;
var siteurl = "http://www.elenabeelaerts.nl/";
$(function(){
	$('#showsinglesheet .page-content a[href^="'+siteurl+'"]').live('click',function(){
		
		
		thehref = $(this).attr("href");
		
		pageslug = thehref.replace(siteurl,"")
		_clickedwhat = pageslug.split("/");
		clickedwhat = _clickedwhat[0];
		thisid = _clickedwhat[1];
		
		
		if (clickedwhat == "tag"){
			clickedwhat = "tags";
		} else {
			if (clickedwhat == "category"){
				clickedwhat = "cats";
			} 
		}
		
		if (clickedwhat == "werk"){ // open een singel page
			
			thistype = "single";
			
			
			setNewPosition(hg_box,'widest',0);
			$("#mainshade").show();
			hg_box.show().css("z-index","2501");
			
			hg_box.load(siteurl+'?page_id=15&what='+thistype+'&which='+thisid, function() {
				stepsset = 0;
				totalsteps = 0;
				
				if ((clickedwhat == null) || (thistype == "single")){
					setSteps(0,0);
				} else {
					var listItem = $("#post-"+arrayItem);
					steps = $('.showwork > li').index(listItem) ;
					
					total = $(".showwork").attr("lis");
					setSteps(total,steps);
	
					amount = steps *628 
					$("#box ul.showwork").animate({ 
						left: '-='+amount
					}, 300, function() {		
					});
				}
				
				thisbut = $("#box ul > li:eq("+stepsset+") .showwerk")
				url = thisbut.attr("href");
				gotoslug = thisbut.attr("goto");
				setNewUrltoMeer(url,gotoslug); 
			});	
			
			
		} else if (clickedwhat == "cats" || clickedwhat == "tags") {
		
			setNewPosition(hg_box,'widest',0);
			hg_box.show().css("z-index","2501");
			
			hg_box.load(siteurl+'?page_id=15&what='+clickedwhat+'&which='+thisid, function() {	
				
				doAfterBoxLoaded();	
			});		
			
			
		
		
		} else { // open een verzamelnintendo
			
			//$('#scrollsingle').show();
				$('#showsinglesheet').load(siteurl+'?page_id=47&which='+_clickedwhat[0], function() {
					shadeheight = $(".showsingle").outerHeight()+(120);
					
					if (shadeheight < wheight){
						shadeheight = wheight;
					}
					$('#singleshade').css("height", shadeheight+"px").css("width","100%");
				});
			
			
			
		}
		
		return false;	
		
		
		
				
				
		
		
	});
	var hg_box,
		hg_ind = $(".indeling"),
		hg_ind_li = hg_ind.find("li"),
		showsinglesheet = $('#showsinglesheet');
	
	// start: stuff to do when page has loaded:
	
	
	
	wheight = $(window).height();
	wwidth = $(window).width();
	
	
	$("#container, #scrollsingle").css({"width" : wwidth+"px", "height" : wheight+"px"});
	//alert(wwidth+'-'+$('#container').width());
	$(".hoofd, .icons").find("li").each(function(){
		setNewPosition($(this),'',0);
	});
	
	hg_ind.find("li").each(function(){
		dezeli = $(this);
		dezeli.append("<div class='drag'></div>");
		setNewPosition(dezeli,'',0);
	});
	
	if ($("#box").length==0){
		$('#container').append('<div id="box"></div>');
		hg_box = $("#box");
	}	else {
		hg_box = $("#box");
	}
	if ($("#scrollsingle").length==0){
		$('#container').append('<div id="scrollsingle"><div id="singleshade"></div><div class="showsingle"><div id="showsinglesheet"></div></div></div>');
		showsinglesheet = $('#showsinglesheet');
	}
	
	// when hoofdindeling is clicked, show matching sub-names, and clone and move appropriate icons
	$(".hoofd li").click(function(){
		$(".hoofd li").removeClass("clicked");
		$(this).addClass("clicked");
		
		clickedwhat = $(this).attr("id");
		$(".clone").remove();
		$(".cloned").removeClass("cloned");
		hg_ind_li.hide('fast');
		$("ul[typ="+clickedwhat+"] li").show('fast', function() {
		     
		   		getid = $(this).attr("id");
		   		
		    	$('li['+clickedwhat+'^="'+getid+'"]').not(".clone, .cloned").each(function(){
		    		
	
		    		opties = $(this).attr(clickedwhat);
		    		arr_opties = opties.split(",");	
		    		if (arr_opties.length > 1){
			    		i = 0;
		    			for (i=1;i<=arr_opties.length-1;i++) {
		    				newelem = $(this).clone().appendTo(".icons").addClass("clone").attr(""+clickedwhat+"",""+arr_opties[i]+"");
		    				
		    				setNewPosition(newelem,$("#"+arr_opties[i]+""), 2500);
		    			}
		    			setNewPosition($(this),$("#"+getid), 2500);
		    		} else {	
		    			setNewPosition($(this),$("#"+getid), 2500);
		    		}
		    		
		    	});
		    	
		});	
	
	});

	// when a indeling-name is clicked, open #box (nintendo)
	hg_ind_li.find("a").click(function(){
		
		thisli = $(this).parent("li");
		thisid = thisli.attr("id");
		setNewPosition(hg_box,'widest',0);
		hg_box.show();
		
		hg_box.load(siteurl+'?page_id=15&what='+clickedwhat+'&which='+thisid, function() {	 	
			doAfterBoxLoaded();	
		});
		return false;	
	});
	
	
	
	// when hovering an cat/tag/chron-name, show highlight color for icons
	hg_ind_li.hover(function(){
		
		thisli = $(this);
		thisid = thisli.attr("id");
		thistype = thisli.parent("ul").attr("typ");
		
		selectedlis = $('li['+thistype+'*="'+thisid+'"]');
		selectedlis.each(function(){
			$(this).find('a').addClass('hover');	
		});
	
	}, 
	function(){
		selectedlis.each(function(){
			$(this).find('a').removeClass('hover');
		}); 
	});
	
	
// start: stuff used on detail or archive page
	
// iterate through items in nintendo
	$(".shownext a, .showprev a").live('click', function() {
		animemove ='-=628';
		if ($(this).parent().hasClass('shownext')){
			stepping = 1;
			animemove = '-=628';
		} else {
			stepping = -1;
			animemove = '+=628';
		}
		setSteps(totalsteps,stepping);
		newhgbox = $("#box");
		newhgbox.addClass("nomore");
		
		newhgbox.find("ul.showwork").animate({ 
			left: animemove
		}, 300, function() {
			if (stepsset == 0){	
			} else {
				thisbut = $("#box ul > li:eq("+stepsset+") .showwerk");
				url = thisbut.attr("href");
				gotoslug = thisbut.attr("goto");
				setNewUrltoMeer(url,gotoslug);
			}
		});
		return false;
	});
	
// show more button in nintendo
	$("a.meer").live('click', function() {
		thisquery = $(this).attr("goto");
		$('#scrollsingle').show();
		hg_box.css("z-index","");
		showsinglesheet.load(siteurl+'?page_id=30&which='+thisquery, function() {
			shadeheight = $(".showsingle").outerHeight()+(120);
			
			if (shadeheight < wheight){
				shadeheight = wheight;
			}
			$('#singleshade').css("height", shadeheight+"px").css("width","100%");
			
			$("#carousel").jCarouselLite({
			    btnNext: "#photos .next",
			    btnPrev: "#photos .prev",
			    vertical: true, 
			    circular: false
			});
			$("#carousel .photolist a").colorbox({width:"80%", height:"95%",slideshow:true,slideshowAuto:false});
			
			
		});
		return false;
	});
	
// closing stuff
	$("#box .close, #mainshade").live('click', function() {
		closeWidest();
	});
	showsinglesheet.find(".close").live('click', function() {
		closeSingleSheet();
	});
	$("#singleshade").live('click', function() {
		closeSingleSheet();
	});
	
// opening 'more info' when clicking in current nintendo	
	$(".post-preview").live('click',function(e){
		var clicked=$(e.target); // get the element clicked                 
	    if( clicked.hasClass('.video') || clicked.is("embed")) {	
	    } else {
			$("a.meer").trigger('click');
		}
	});
	
// open colorbox when clicking big foto on detailpage	
	$("#mainphoto img").live('click',function(){
		$("#carousel a:first").trigger('click');
	});


// opening 'normal' pages in nintendo	
	$(".biggest li a").click(function(){
		thehref = $(this).attr("href");
		pageslug = thehref.replace(siteurl,"")
		pageslug = pageslug.replace("/","")
		
		$('#scrollsingle').show();
		showsinglesheet.load(siteurl+'?page_id=47&which='+pageslug, function() {
		
		shadeheight = $(".showsingle").outerHeight()+(120);
		
		if (shadeheight < wheight){
			shadeheight = wheight;
		}
		$('#singleshade').css("height", shadeheight+"px").css("width","100%");
		});
		
		return false;
	});
	
// opening a single work page OR a collection of single depending on clicked cat/tag/chron, when clicking on icon
	$(".icons li a").live('click', function() {
		thisparentli = $(this).parent('li')
		if (clickedwhat == null){
			thistype = "single";
			thisid = thisparentli.attr("postid");
		} else {
			thistype = clickedwhat;
			theseids = thisparentli.attr(thistype);
			if (theseids == ""){
				thistype = "single";
				thisid = thisparentli.attr("postid");
			} else {
				theseids =theseids.split(",");
				thisid = theseids[0];
				arrayItem = thisparentli.attr('postid');
				arrayName = getArray(thistype,thisid).split(",");
				for(var i=0;i<arrayName.length;i++){ 
				   if(arrayName[i]==arrayItem)
				       steps = i;
				}
			}	
		}
				
		setNewPosition(hg_box,'widest',0);
		$("#mainshade").show();
		hg_box.show();
		
		hg_box.load(siteurl+'?page_id=15&what='+thistype+'&which='+thisid, function() {
			stepsset = 0;
			totalsteps = 0;
			
			if ((clickedwhat == null) || (thistype == "single")){
				setSteps(0,0);
			} else {
				var listItem = $("#post-"+arrayItem);
				steps = $('.showwork > li').index(listItem) ;
				
				total = $(".showwork").attr("lis");
				setSteps(total,steps);

				amount = steps *628 
				$("#box ul.showwork").animate({ 
					left: '-='+amount
				}, 300, function() {		
				});
			}
			
			thisbut = $("#box ul > li:eq("+stepsset+") .showwerk")
			url = thisbut.attr("href");
			gotoslug = thisbut.attr("goto");
			setNewUrltoMeer(url,gotoslug); 
		});	
		return false;
	});
	
// setting up drag on cat/tag/chron names	
	
	var countdrag = 0;
	var totalup = 0;
	var lasttotalup = 0;
	var totalleft = 0;
	var lasttotalleft = 0;
	var movedleft = 0;
	$(".indeling li").draggable({ handle: '.drag', stack:'.indeling li' },{
		start: function(event, ui){
			thisli = $(this);
			myid = thisli.attr("id");
			mytype = thisli.parent("ul").attr("typ");
			thispos = thisli.position();
			startX = thispos.left;
			startY = thispos.top;
			 totalup = 0;
			 lasttotalup = 0;
			 totalleft = 0;
			lasttotalleft = 0;
		},
		drag: function(event, ui){
			countdrag ++;
			if (countdrag%60 == 0 ){
				totalup =  Math.round(ui.position.top - startY);
				if (lasttotalleft == 0 ) {
					lasttotalup = totalup;
				}
				stepsup = totalup - lasttotalup;
				totalleft =  Math.round(ui.position.left - startX);
				stepsleft = totalleft - lasttotalleft;
				lasttotalup = totalup;
				lasttotalleft = totalleft;
				followMe(mytype,myid,stepsleft, stepsup);
			}
		},
		stop: function(event,ui){
			
			totalup =  Math.round(ui.position.top - startY);
			stepsup = totalup - lasttotalup;
			totalleft =  Math.round(ui.position.left - startX);
			stepsleft = totalleft - lasttotalleft;
			followMe(mytype,myid,stepsleft, stepsup);
		}
	});
	
	//$(".indeling").disableSelection();
	
	

getTaboo();	

if ($("#bovenin").length > 0 ){
	setTimeout("moveBovenin()",1000);
	
}

if ($("#plaatje").length > 0 ){
	
	setNewPosition($("#plaatje"),'',0);
}
if (showbox == "yes"){
	$("#box").show();
	doAfterBoxLoaded();
	
}
setTimeout(function(){
$(".hoofd li#cats").trigger('click');
}, '1500');

if (openonhomepage){
	$('.icons').find("li[postid="+openonhomepage+"] a").trigger('click');
}

		
}); // end document ready


var totalsteps  = 0;
var thistype = "";
var clickedwhat;

var tabooFromLeft;
var tabooTillLeft;
var tabooFromTop;
var tabooTillTop;

function doAfterBoxLoaded(){
	
	total = $(".showwork").attr("lis");
	stepsset = 0;
	totalsteps = 0;

	if (total == 1) {
		stepsset = 1;
		$("#box ul.showwork").animate({
			left: '-=628'
		}, 300, function() {
			thisbut = $("#box .showwerk");
			url = thisbut.attr("href");
			gotoslug = thisbut.attr("goto");
			setNewUrltoMeer(url,gotoslug);
			
		});
	}
	
	$("#mainshade").show();
	setSteps(total,0);
	jumpToAnker();
}

// click on anchor link in overview-ul in nintendo
function jumpToAnker(){

	//$("ul.ankers li a").live('click', function(event) {
	$("ul.ankers li a").click(function(event) {
		welke = $(this).parents("li");
		
		tel = $("ul.ankers li").index(welke) + 1;
		
		hoeveel = tel*628;
		setSteps(totalsteps,tel);
		$("#box").find("ul.showwork").animate({
		  left: '-='+hoeveel
		}, 300, function() {
			thisbut = $("#box ul > li:eq("+stepsset+") .showwerk")
			url = thisbut.attr("href");
			gotoslug = thisbut.attr("goto");
			setNewUrltoMeer(url,gotoslug);
			
		});		
		return false;
	});
}
// when a next or prev work is shown in nintendo, 'meer info' should get new URL
function setNewUrltoMeer(urlx,gotoslug){
	newhgbox = $("#box");
	elem = newhgbox.find(".meer");
	
	elem.attr("href",urlx).attr("goto",gotoslug);
	newhgbox.removeClass("nomore");
	
}
function closeWidest(){
	stepsset  = 0;
	totalsteps  = 0;
	$("#box").hide().html("");
	$("#mainshade").hide();
}
function closeSingleSheet(){
	$('#showsinglesheet').html("");
	$("#scrollsingle").hide();
}

// counting on which work we are in nintendo
function setSteps(total,amount){
	
	stepsset = stepsset + amount;
	totalsteps = total;
	shownext = $(".shownext");
	showprev = $(".showprev");
	newhgbox = $("#box");
	newhgbox.removeClass("nomore");
	if (stepsset == 0 ) {
		showprev.addClass("off");
		if (thistype != "single"){
			newhgbox.addClass("nomore");
		}
	} else {
		showprev.removeClass("off");
	}
	
	if (stepsset == totalsteps){
		shownext.addClass("off");;
	} else {
		shownext.removeClass("off");
	}
}
function getArray(type,spec){
	queryarray = "";
	sep = "";
	$('li['+type+'*="'+spec+'"]').not(".clone").each(function(){
		queryarray = queryarray + sep + $(this).attr("postid");
		sep = ",";
	});
	return queryarray;
}
function followMe(wat,wie,x,y){
	$('li['+wat+'^="'+wie+'"]').each(function(){
		if (x < 0 ){
			moveX = "-="+-x;
		} else if (x > 0){
			moveX = "+="+x;
		}else {
			moveX = "+=0";
		}
		$(this).stop(true, true).animate({
				left: moveX,
				top: "+="+y   
		  }, getRandom(200,1500), function() {
	  });
	})	
}
function setNewPosition(elem,relativeto,speed){	
	
	if (relativeto == ""){
		toppos = getRandom(60,wheight-50);
		leftpos = getRandom(60,wwidth-90);
		checkTaboo(elem,leftpos, toppos);
	} else if (relativeto == "widest"){
		toppos = getRandom(60,wheight-507);
		leftpos = getRandom(60,wwidth-660);
	} else {
		relpos = relativeto.position()
		relleft = relpos.left;
		reltop = relpos.top;
		relheight = relativeto.outerHeight();
		relwidth = relativeto.outerWidth();
		leftpx = (120 - relwidth)/2;
		rightpx = leftpx + relwidth;
		toppx = (120 - relheight)/2;
		botpx = toppx +relheight;
		toppos = getRandom(reltop-toppx,reltop+botpx);
		leftpos = getRandom(relleft-leftpx,relleft+rightpx);
		checkTaboo(toppos, leftpos);
	}
	if (speed == 0 ){
		elem.css({"left" : leftpos+"px", "top" : toppos+"px"});
	} else {
		elem.animate({
		    left: leftpos+"px",
		    top: toppos+"px"
		  }, speed, function() {
		});
	}
}
function getRandom(minVal,maxVal){
	var randVal = minVal+(Math.random()*(maxVal-minVal));
	return Math.round(randVal);
}
function checkTaboo(elem,left,top){
	if ((top>tabooFromTop) && (top < tabooTillTop) && (left > tabooFromLeft) && (left < tabooTillLeft)) {
	setNewPosition(elem,'',0);
	} 
}
function getTaboo(){
	tabooFromLeft = $("#hoofdtekst").position().left - 16;
	tabooTillLeft = tabooFromLeft + $("#hoofdtekst").outerWidth();
	tabooFromTop = $("#hoofdtekst").position().top - 16;
	tabooTillTop = tabooFromTop + $("#hoofdtekst").outerHeight();
}

function moveBovenin(){
	heightbovenin =  parseInt($("#bovenin").outerHeight());
	heightboveninx =  parseInt(heightbovenin+ 15);
	$("#bovenin").css("top","-"+heightboveninx+"px");
		$("#bovenin").animate({
		   top: "-10px"
		  }, 1000, function() {
		    
		  });
	
	
	$("#bovenin .close").click(function(){
		
		
		$("#bovenin").animate({
		   top: "-"+heightbovenin+"px"
		  }, 500, function() {
		    $(this).css("display","none");
		  });
	});

}
