var animationstarted = false;

$(document).ready(function(){

	$("#map").scrollview({
		grab:"http://www.bureauzee.nl/wp-content/themes/bureauzee/js/images/openhand.cur",
		grabbing:"http://www.bureauzee.nl/wp-content/themes/bureauzee/js/images/closedhand.cur"
	});
	
	$("#footer").click(function(event){
		clickVorm("contact",event.pageX-60,0,"tekstvakgroot");
	});

	$.address.init(function(event) {
		}).change(function(event) {
			vanlinks = $("#map").scrollLeft();
			vanboven = $("#map").scrollTop();
			linkAdress=$.address.value().substr(1);
			xthisID = linkAdress.replace("/","");
			thisID = xthisID.replace("/","");
		
			if (thisID) {
				$.address.title( $("#postid_"+thisID).find("h4 a").text() + ' | Bureau Zee' );
			}
 
		}).internalChange(function(event) { //als er geklikt wordt
			style = $("#postid_"+thisID).find("h4 a").attr('class');
			xpos = $("#postid_"+thisID).position().left - vanlinks + 20;
			ypos = $("#postid_"+thisID).position().top - vanboven;
			clickVorm(thisID,xpos,ypos,style);
		
		}).externalChange(function(event) { // met de backbutton
	
			if (thisID) {
				animationstarted = true;
				posleft = ($(window).width()/2);
				postop = ($(window).height()/2);
				style = $("#postid_"+thisID).find("h4 a").attr('class');
				xpos = posleft ;
				ypos = postop;
				animateScrollTo($("#postid_"+thisID).position().top,$("#postid_"+thisID).position().left + 20);
				clickVorm(thisID,xpos,ypos,style);
			}
	});

	$('.vorm h4 a').bind('click', function(event) {
		slug = jQuery(this).attr('slug');
		style = jQuery(this).attr('class');
		
		if ( $("#tekstvak").attr("slug") == slug){
			$("#tekstvak").show();
		}
		
		var contentLink=$(this).attr('href'); 
        var contentLink=contentLink.replace('http://www.bureauzee.nl/','');  
        $.address.value(contentLink); 
        
		return false;
	});



	
	$('.vorm').bind('click', function(event) {
		slug = $(this).find("h4 a").attr('slug');
				
		if ( $("#tekstvak").attr("slug") == slug){
			$("#tekstvak").show();
		}
		
		var contentLink=$(this).find("h4 a").attr('href'); 
		var contentLink=contentLink.replace('http://www.bureauzee.nl/','');  
		$.address.value(contentLink); 
		
		return false;
	});
	
	$.sifr({
		path: 'http://www.bureauzee.nl/wp-content/themes/bureauzee/sifr/',
		save: true,
		build: 436,
		version: 3
		
	});		
	setFont();
	makeGallery();


	var type = "tri-s1";
	$(document).bind('keypress',function(e){ 
		//alert(e.charCode);
		if (e.charCode == 104)  {
			type = "hex-s1";	
		}
		if (e.charCode == 72)  {
			type = "hex-s2";
		}
		if (e.charCode == 100)  {
			type = "tri-s1";
		}
		if (e.charCode == 68)  {
			type = "tri-s2";
		}
		if (e.charCode == 114)  {
			type = "ruit-s1";
		}
		if (e.charCode == 82)  {
			type = "ruit-s2";
		}
		if (e.charCode == 120) {
			$.cookies.del( 'driehoek' );
		}
		
	});  

	$(document).mousemove(function(event) {
		if (event.shiftKey) {
			$("#lozenge").css("display","block");
			moveMouse(event.pageX,event.pageY,type);
		} else {
			$("#lozenge").css("display","none").removeClass();
		}
	});

	$(document).dblclick(function(event) {
		if (event.shiftKey) {
			moveMouse(event.pageX,event.pageY,type,'dblclick');
		}
	});	

	$(document).click(function(event) {
		if (event.altKey) {
			moveMouse(event.pageX,event.pageY,type,'click');
		} else {
			vanlinks = $("#map").scrollLeft();
			vanboven = $("#map").scrollTop();
			xcoord = vanlinks + event.pageX;
			ycoord = vanboven + event.pageY;	
			if (  (xcoord > 3400 && xcoord < 3950)  && (ycoord > 2500 && ycoord < 2900) ){
				moveMouse(event.pageX,event.pageY,type,'singleclick');
			}
		}
	});	

	$(".t-h").hover(
		function () {
			$(this).find("h4").fadeIn(150);
		}, 
		function () {
			$(this).find("h4").fadeOut(150);
		}
	);

	$(".muster, #header h1 a").click(function(){
		animateScrollTo(1500,2000);
		hideTekstvak();
		
		return false;
	});

	
	if ($("#movetotekstvak").length){
		
		$("#tekstvak").html($("#movetotekstvak").html());
		coords = $("#movetotekstvak").attr("rel");
		slug = $("#movetotekstvak").attr("slug");
		style = $("#movetotekstvak").attr("class");
		
		leftoparray = coords.split("-");
		xcoord = leftoparray[1];
		ycoord = leftoparray[0];
		
		
		xposW = $("#postid_"+slug).position().left;
		yposW = $("#postid_"+slug).position().top;
		
		xpos = ( $(window).width() / 2 )+20	;
		ypos =( $(window).height() / 2 )+20	;
		
		animateScrollTo(xcoord,ycoord);
		setSizeTekstvak(xpos,ypos,style);
		showClickedArrow(slug,xposW,yposW)
		setFont();
		
	} else if (animationstarted = true) {
		//$("#map").scrollLeft("2000");
		//$("#map").scrollTop("1900");
		//setTimeout( "animateScrollTo(1500,2000,1500)", 100);
	} else {
		$("#map").scrollLeft("2000");
		$("#map").scrollTop("1900");
		setTimeout( "animateScrollTo(1500,2000,1500)", 100);
	}

	createCookieHoekie();

	updateArrayFile();


	$(".cat-parent").hover(function(){
		cat = $(this).attr("rel");
		vleft = $("#map").scrollLeft() + $(this).offset().left + 16;
		vtop =  $("#map").scrollTop() + $(this).offset().top + 14;
		scleft =  $("#map").scrollLeft(); 
		sctop =  $("#map").scrollTop(); 
		n = 0;
		
		$(".cat-"+cat+":not(.cat-parentx)").each(function(){
			thisdiv = $(this).parents(".vorm");
			cloneddiv = $(this).parents(".vorm").clone().appendTo("#stippels").addClass("tag-clone");
			cloneddiv.css("opacity","0.7");//.animate({
			cloneddiv.animate({ 
					left: vleft,
					top: vtop
				}, {
					duration: 2000, 
					easing: 'linear',
					complete: function() {
						$(".tag-clone").hide('slow');
					}
				});
			});
		}, 
		  function(){
		  		$(".tag-clone").remove();		
		  }
		);

	$('a.scrollto-link').live('click', function(e) {
		lefttop = $(this).attr("rel");
		leftoparray = lefttop.split("-");
		fromleft = leftoparray[0];
		fromtop = leftoparray[1];
		classs = leftoparray[2];
		postid = leftoparray[3];
		postslug = $(this).attr("slug");
		posleft = ($(window).width()/2) + 20;
		postop = ($(window).height()/2);
		e.preventDefault();
		
		var contentLink=$(this).attr('href'); 
		var contentLink=contentLink.replace('http://www.bureauzee.nl/',''); 
		$.address.value(contentLink); 
		
		return false;
	});
	
	$('span.close').live('click', function(e) {
		hideTekstvak();
	});
	
	
	stopmove = false;

	
	$("#moveLeftBottom,#moveRightBottom,#moveLeftTop,#moveRightTop,#moveBottom,#moveRight,#moveTop,#moveLeft")
		.mouseover(function(){
			stopmove = false;
			moveMap($(this).attr("id"));
		})
		.mouseout(function(){
			stopmove = true;
			$("#map").stop();
		});
	
	
		
}); // end docu ready

function makeGallery() {
	$(".gallery").each(function(){
		$(this).find("dl:first").addClass("show");
		$(this).append("<div class='gal-nav'><div class='next-img'></div><div class='prev-img'></div>image <span class='cnt'>1</span> of "+$(this).find("dl").size()+"</div>");
		$(this).find(".prev-img").css("display","none");
		
		dezegallery = $(this);
		dezeID = dezegallery.attr("id");	
			$(this).find(".next-img").click(function() {
			  	dezegallery.find(".show").removeClass("show").next().addClass("show");	
			  	
			  	if ( dezegallery.find("dl:last").hasClass("show") ){
			  		dezegallery.find(".next-img").css("display","none");
			  		dezegallery.find(".prev-img").css("display","block");
			  	} else if ( dezegallery.find("dl:first").hasClass("show") ){
				  	dezegallery.find(".next-img").css("display","block");
				  	dezegallery.find(".prev-img").css("display","none");
			  	} else {
				  	dezegallery.find(".next-img").css("display","block");
				  	dezegallery.find(".prev-img").css("display","block");
			  	}
			  	showing = $('dl.show').index('.gallery dl');
			  	dezegallery.find(".cnt").html(showing+1);
			});
		
			$(this).find(".prev-img").click(function() {
			  	dezegallery.find(".show").removeClass("show").prev().addClass("show");
			  	
			  	if ( dezegallery.find("dl:last").hasClass("show") ){
			  		dezegallery.find(".next-img").css("display","none");
			  		dezegallery.find(".prev-img").css("display","block");
			  	} else if ( dezegallery.find("dl:first").hasClass("show") ){
			  		dezegallery.find(".next-img").css("display","block");
			  		dezegallery.find(".prev-img").css("display","none");
			  	} else {
			  		dezegallery.find(".next-img").css("display","block");
			  		dezegallery.find(".prev-img").css("display","block");
			  	}
			  	showing = $('dl.show').index('.gallery dl');
			  	dezegallery.find(".cnt").html(showing+1);
			  	
			  	
			});
	});
}

function moveMap(direction){
	leftright = "+=0";
	updown = "+=0";
	
	switch (direction) {
		case "moveLeft":
			leftright = "-=150";
		break;
		case "moveRight":
			leftright = "+=150";
		break;
		case "moveTop":
			updown = "-=150";
		break;
		case "moveBottom":
			updown = "+=150";
		break;
		case "moveLeftTop":
			leftright = "-=150";
			updown = "-=150";
		break;
		case "moveRightTop":
			leftright = "+=150";
			updown = "-=150";
		break;
		case "moveLeftBottom":
			updown = "+=150";
			leftright = "-=150";
		break;
		case "moveRightBottom":
			updown = "+=150";
			leftright = "+=150";
		break;
		default: updown = "-=100";
	}

	$("#map").animate({
		scrollLeft: leftright,
		scrollTop: updown
	}, {
	
		duration: 200,
		easing: 'linear',
		complete: function() {
			if (stopmove == false){
				moveMap(direction);
			} else {
				$("#map").stop();
			}
		}
	});
		
	
}

//function goCentre(){
//	$("#map")
//		.scrollLeft(2000 - ($(window).width()/2) )
//		.scrollTop(1500 - ($(window).height()/2) );
//}
//function animateBorders(){
//if (event.pageX > ($(window).width() - 20 )){
//	$("#map").animate({
//		scrollLeft: "+=150"
//	}, {duration: 200, easing: 'linear'});			
//}
//if (event.pageX < ( 20 )){
//	$("#map").animate({
//		scrollLeft: "-=150"
//	}, {duration: 200, easing: 'linear'});			
//}
//
//}
function animateScrollTo(topX, leftX, speed){
	if (!speed){
		speed = 1000;
	}
	
	if ((topX =="") && (leftX = "")){
		topX = 2000 - ($(window).width()/2);
		leftX = 1500 - ($(window).height()/2);
	}
	tTop = topX - ($(window).height()/2);
	tLeft = leftX - ($(window).width()/2);
	$("#map").animate(
		{scrollLeft: tLeft,
		scrollTop: tTop
		},
		{ 	queue: false, 
			duration: speed
		});
}
function setDriehoekCookie(x,y,t){
	if ($.cookies.get( 'driehoek' )){
		oudecookie = $.cookies.get( 'driehoek' );
		$.cookies.set( 'driehoek', oudecookie+'|'+x+','+y+','+t );
	} else {
		$.cookies.set( 'driehoek', x+','+y+','+t );
	}
}
function createCookieHoekie(){
	if ($.cookies.get( 'driehoek' ) ){
		driehoeken = $.cookies.get( 'driehoek' );
		var arrDriehoeken = driehoeken.split( "|" );
		for ( var i in arrDriehoeken ){
			var arrCoord = arrDriehoeken[i].split(",");
			driehoekId = arrCoord[0]+'-'+arrCoord[1];
			driehoekLeft = arrCoord[0];
			driehoekTop = arrCoord[1];
			driehoekSoort = arrCoord[2]
			$("#cookiehoekies").append('<div id="id'+driehoekId+'" class="vorm  tri-vol tri-s1 '+driehoekSoort+' t-h" style="left:'+driehoekLeft+'px;top:'+driehoekTop+'px"></div>');	    
		} 
	}
}
function hideTekstvak(){
	$("#tekstvak").fadeOut(200);
	$("#clickeditem").hide();	
}
function newCookieHoekie(left,top,soort){
	if ( $("#id"+left+""+top).length == 0 ) {
		$("#cookiehoekies").append('<div id="id'+left+'-'+top+'" class="vorm tri tri-vol tri-s1 '+soort+' t-h" style="left:'+left+'px;top:'+top+'px"></div>');
		setDriehoekCookie(left,top,soort);
	} else {
		$("#id"+left+""+top).remove();
		myString = $.cookies.get( 'driehoek' );
		myArray = myString.split(''+left+','+top+','+soort+'');
		newdriehoek = myArray.join('');
		newArray = newdriehoek.split('||');
		$.cookies.set( 'driehoek',newArray.join('|'));
	}
}
function moveMouse(pgX,pgY,type,klik){
	vanlinks = $("#map").scrollLeft();
	vanboven = $("#map").scrollTop();
	xcoord = vanlinks + pgX;
	ycoord = vanboven + pgY;
	

	$("#lozenge").removeClass();
	$("#lozenge").addClass("vorm t-h "+type+"");


	restwaardeX = xcoord % 34;
	if (type == "tri-s1"){ 
		restwaardeX = xcoord % 17;
	}
	
	
	if (type == "hex-s1"){ 
		restwaardeX = xcoord % 34;
	}

	lozleft = xcoord - restwaardeX;
	
	restwaardeY = ycoord % 30;
	if (type == "tri-s2"){ 
		restwaardeY = ycoord % 30;
		
	}
	loztop = ycoord - restwaardeY;
	
	soort = "";
		if ((loztop % 60) == 0){ // bepaalt of het op een even of oneven rij (horizontaal) is
		if ((lozleft % 34) == 0){ // bepaalt of het een op z'n kop of rechtop-driehoek is
			soort = "tri-s1-b";
			if (type == "tri-s1"){ 
				$("#lozenge").css("background-position","left -31px");
				//$("#lozenge").addClass("tri-1");
				type = "tri-s1-b";
			}	
			if (type == "hex-s1"){ 
				$("#lozenge").css("background-position","-70px 0px");
				lozleft = xcoord - restwaardeX -17;
				type = "hex-s1";
			}
			if (type == "ruit-s1"){ 
				$("#lozenge").css("background-position","-35px 1px");
				lozleft = xcoord - restwaardeX -17;
				type = "ruit-s1";
			}
			if (type == "tri-s2"){ 
				$("#lozenge").css("background-position","-138px 0px");
				type = "tri-s2-a";
			}	
			if (type == "hex-s2"){ 
				$("#lozenge").css("background-position","0px 0px");
				type = "hex-s2";
			}
			if (type == "ruit-s2"){ 
				$("#lozenge").css("background-position","-208px 0px");
				type = "ruit-s2";
			}
		} else { // 
			soort = "tri-s1-a";
			if (type == "tri-s1"){ 
				$("#lozenge").css("background-position","left 0px");
				type = "tri-s1-a";	
			}
			if (type == "hex-s1"){ 
				$("#lozenge").css("background-position","-70px -0px");
				type = "hex-s1";
			}
			if (type == "ruit-s1"){ 
				$("#lozenge").css("background-position","-35px 1px");
				type = "ruit-s1";
			}
			
			if (type == "tri-s2"){ 
				$("#lozenge").css("background-position","-138px -0px");
				
				type = "tri-s2-b";	
			}
			if (type == "hex-s2"){ 
				$("#lozenge").css("background-position","0px -0px");
				
				type = "hex-s2";
			}
			if (type == "ruit-s2"){ 
				$("#lozenge").css("background-position","-208px 0px");
				
				type = "ruit-s2";
			}
		}
	} else {
		if ((lozleft % 34) == 0){
			soort = "tri-s1-a";
			if (type == "tri-s1"){ 
				$("#lozenge").css("background-position","left 0px");
				//$("#lozenge").addClass("tri-2");
				type = "tri-s1-a";
			}
			if (type == "hex-s1"){ 
				$("#lozenge").css("background-position","-70px 0px");
				type = "hex-s1";
			}	
			if (type == "ruit-s1"){  
				$("#lozenge").css("background-position","-35px 1px");
				type = "ruit-s1";
			}
			if (type == "tri-s2"){ 
				$("#lozenge").css("background-position","-138px -61px");
				//$("#lozenge").addClass("tri-2");
				lozleft = xcoord - restwaardeX -17;
				type = "tri-s2-b";
			}
			if (type == "hex-s2"){ 
				$("#lozenge").css("background-position","0px 0px");
				type = "hex-s2";
				lozleft = xcoord - restwaardeX -17;
			}	
			if (type == "ruit-s2"){  
				$("#lozenge").css("background-position","-208px 0px");
				lozleft = xcoord - restwaardeX -17;
				type = "ruit-s2";
			}
		} else {
			soort = "tri-s1-b";
			if (type == "tri-s1"){ 
				$("#lozenge").css("background-position","left -31px");
				//$("#lozenge").addClass("tri-t1");
				type = "tri-s1-b";
				
			}
			if (type == "hex-s1"){  
				$("#lozenge").css("background-position","-70px -0px");
				type = "hex-s1";
			}
			if (type == "ruit-s1"){  
				$("#lozenge").css("background-position","-35px 1px");
				type = "ruit-s1";
			}
			if (type == "tri-s2"){ 
				$("#lozenge").css("background-position","-138px -61px");
				//$("#lozenge").addClass("tri-t1");
				type = "tri-s2-b";
			}
			if (type == "hex-s2"){  
				$("#lozenge").css("background-position","0px -0px");
				type = "hex-s2";
			}
			if (type == "ruit-s2"){  
				$("#lozenge").css("background-position","-208px 0px");
				type = "ruit-s2";
			}
		}
	}
	$("#lozenge").css({
		'top' : loztop+'px',
		'left' : lozleft+'px'
	});
	
	if (klik == "dblclick"){			
		alert("Kopieer onderstaande in het input-veld in Wordpress:\n"+loztop+","+lozleft+","+type+"")
	}
	if (klik == "click"){	
		newCookieHoekie(lozleft,loztop,soort);
	} 
	if (klik == "singleclick") {
		writeHTMLfile(lozleft,loztop,soort);
	}	
}

function updateArrayFile(){
	$.ajax({
		  url: '/showtestfile.php',
		  success: function(data) {
		  	$("#getplayground").html(data); 
		  }
	 });
}
function writeHTMLfile(xpos,ypos,soort){
	//tsTimeStamp= new Date().getTime();
	$.ajax({
		url: "/showarray2.php",
		data: "left="+xpos+"&top="+ypos+"&type="+soort,
		success: function(msg){
			updateArrayFile();
		}
	});
}

function setSizeTekstvak(xcoord,ycoord,style){
	$("#tekstvak").removeClass();
	windowheight = $(window).height(); 
	vanlinks = $("#map").scrollLeft();
	vanboven = $("#map").scrollTop();
	hoogte = $("#map").height();
	
		if (style == "tekstvakgroot"){
			maxwidth = 482;
		} else if (style == "tekstvakklein"){
			maxwidth = 300;
		} else {
			maxwidth = 482;
		}
		
		if ( (xcoord < 0) || (xcoord > $(window).width() ) || (ycoord < 0) || (ycoord > $(window).height() ) ){
			xcoord = $(window).width() / 2;	
				myslug = $("#tekstvak").attr("slug");
				xpos = $("#postid_"+myslug).position().left + 20;	
				ypos = $("#postid_"+myslug).position().top;
			animateScrollTo(ypos,xpos);
		}
		if ( xcoord + maxwidth > $(window).width() ){
			xcoord = $(window).width() - maxwidth;
		}
	$("#tekstvak").addClass(style);
	$("#tekstvak").css("left", xcoord+"px");
	
	if (style == "tekstvakgroot"){
		$(".tekstcontent").css("height",windowheight - 143+"px");	
		$("#tekstvak").css("top","0px");
	}
	if (style == "tekstvakklein"){
		$("#tekstvak").css("top",ycoord - 50+"px");
	}
	$("#tekstvak").fadeIn(200);
}

//function groupCats(cat){
//	$("cat-"+cat+"").addClass("hoera");
//}
function setFont(){
		$('h2').sifr({ font: 'documentasanstot' });
	}
function lightboxImages(){
	$("a img.size-medium, a img.size-thumbnail").each(function(){
		$(this).parent("a").attr("title",$(this).attr("title"))
	})
	$("a img.attachment-thumbnail").parent("a").lightBox({fixedNavigation:true});
	$("a img.size-medium, a img.size-thumbnail").parent("a").lightBox();
}
function clickVorm(slug,xpos,ypos,style){
	showSpinner(xpos,ypos);
	$("#tekstvak").attr("slug",slug);
	
	$.ajax({
		url: 'http://www.bureauzee.nl/ajax-handler/?slug='+slug,
		success: function(data) {
			$("#tekstvak").html(data);
			setSizeTekstvak(xpos,ypos,style);
			setFont();
			lightboxImages();
			makeGallery();
			$("#spinner").hide();
		}
	});
	xposW = $("#postid_"+slug).position().left;
	yposW = $("#postid_"+slug).position().top;
	showClickedArrow(slug,xposW,yposW);
}
function showSpinner(left,top){
	if (!$('#spinner').size()) {
	        $('<div id="spinner" />').appendTo('body');
	    }
	$('#spinner').show().css("left",left+"px").css("top",top+"px");
}
function showClickedArrow(slug,xposW,yposW){
	if ( $("#postid_"+slug).hasClass("tri-s1-b") || $("#postid_"+slug).hasClass("tri-s1-a") ) {
		xposW = xposW -8;
		yposW = yposW + 12;
	} else if ( $("#postid_"+slug).hasClass("hex-s2") || $("#postid_"+slug).hasClass("ruit-s2") ) {
		xposW = xposW -13 ;
		yposW = yposW + 58;
	} else if ( $("#postid_"+slug).hasClass("tri-s2-a") || $("#postid_"+slug).hasClass("tri-s2-b") ) {
		xposW = xposW ;
		yposW = yposW + 28;
	} else if ( $("#postid_"+slug).hasClass("hex-s1") || $("#postid_"+slug).hasClass("ruit-s1") ) {
		xposW = xposW -13;
		yposW = yposW + 28;
	}
	$("#clickeditem").show().css("left",xposW+"px").css("top",yposW+"px");
}


