//$.log("start");
var curColId = -1;
var colWidth = 311;

var curColPosArray = [];

$(window).ready(function(){

	
	$("#col_mouse_scroll_down").hover(scrollDownMouseOver,scrollDownMouseOut)
	$("#col_mouse_scroll_up").hover(scrollUpMouseOver,scrollUpMouseOut)
	
	$("#content_mask").click(function(e){
		
		if(e.pageX - $(this).position().left - 318 > 10 ){
		
			var nId = Math.ceil((e.pageX - $(this).position().left - 319) / 310)
			updateCol(curColId + nId);
		}
	})

	//cookie voor popup met de key
	if($.cookie('show_popup_key') == null || $.cookie('font_size') == false){
		
		$.cookie('show_popup_key', 'true');
		showPopUpKey();
	}
	
	parseHash();
	
	setWindow();
	//setcol(curColId);
	
	//action mousedown
	$("#menu_side_left_background_img").mousedown(function(){updateCol(5)});
	$("#menu_side_left_background_img").css("cursor","pointer");
	
})

$(document).keydown(function(e){$(window).scrollLeft(0);keyboardDownEvent(e);})
$(document).keyup(function(e){$(window).scrollLeft(0);keyboardUpEvent(e);$(window).scrollLeft(0);})
$(document).keypress(function(e){$(window).scrollLeft(0)})



jQuery(window).hashchange(function() {
        // location hash has been updated
        // ...
       
    
       

       
       
        
    parseHash();
});

var curScrollPos = 0;

function parseHash(){
	
		
	
	   var hashArray = document.location.hash.replace(/#/,"").split("_");
       if(parseFloat(hashArray[0]) > 0){
			if(parseInt(hashArray[0]) != curColId){
				
				curColId = hashArray[0];
				if(curColId > nCol){curColId= 1}
				else if(curColId < 1){curColId = nCol}
				
				setcol(parseInt(hashArray[0]));
       		}
       }
       else{
       		
       		setcol(1);
       		
       }
       

       if(parseInt(hashArray[1]) > 0){
			curScrollPos = 0;
			if(hashArray[1] == colIdArray[hashArray[0]][1]){
				

				
				
				if($("#content_col_" + curColId).length > 0){
					$("#content_col_" + curColId)[0].scrollBy(0);
					
				}
				
				
			}
			else{
				
				
				if($("#col_"+curColId+"_"+hashArray[1]).length > 0){;
				
					if($("#content_col_" + curColId).length > 0){
						var pos = $("#col_"+curColId+"_"+hashArray[1]).position().top;
						$("#content_col_" + curColId)[0].scrollBy(pos);
						scrollId = hashArray[1];
						curScrollPos = pos;
						
						
					}
				}
			}
       }
       else if(hashArray[1] == "undefined"){
       	
       		window.location.href = "#"+curColId+"_"+1;
       }  
       
       
		
}




function updateCol(id){
	
	if(id > nCol){id = 1}
	else if(id < 1){id = nCol}

	if(curColPosArray[id] == null){
		
		curColPosArray[id] = colIdArray[id][1];	}
	
	
	if(curColPosArray[id] != undefined){
	
		window.location.hash = "#"+id+"_"+curColPosArray[id];
	}
	else{
	
		window.location.hash = "#"+id+"_"+1;	
	}
	
		//laat de pijltjes zien
	if((id == nCol && curColId == 1) | (id == 1 && curColId == nCol)){
			
		$("#col_mouse_scroll_left").css("background-image","url("+themeUrl+"/images/mouse_nav_round_left.png)").fadeIn("fast",function(){$(this).fadeOut("slow");});
		$("#col_mouse_scroll_right").css("background-image","url("+themeUrl+"/images/mouse_nav_round_right.png)").fadeIn("fast",function(){$(this).fadeOut("slow");});
	}
	else if(curColId < id){$("#col_mouse_scroll_right").css("background-image","url("+themeUrl+"/images/logo_arrow_round_right.png)").fadeIn("fast",function(){$(this).fadeOut("slow");})}
	else if(curColId > id){$("#col_mouse_scroll_left").css("background-image","url("+themeUrl+"/images/logo_arrow_round_left.png)").fadeIn("fast",function(){$(this).fadeOut("slow");})}
	
}

function setcol(id,scrollpos){



	
	if(scrollpos == true){
	
		curScrollPos = 0;
		if($("#content_col_" + curColId).length > 0){
		
			curScrollPos = -1 * $("#content_col_" + curColId).position().top;
		}
	}

	$.log(curScrollPos);

	$("#content_col_" + curColId).jScrollPaneRemove();
	
	if(id > nCol){id = 1}
	else if(id < 1){id = nCol}


	if(id > nCol-4){
		
		for(var i=0;i<4;i++){
			var itemId = i+1;
			if($("#content_col_tmp_" + itemId).html() == null){
				var startX = (nCol * colWidth) + (colWidth * i);
				$("#content_col_"+itemId ).clone().attr({"id":"content_col_tmp_" +itemId }).css("left",startX+"px").appendTo("#content_col_cnt");
			}
			else{$("#content_col_tmp_" + itemId).html($("#content_col_tmp_" + itemId).html())}
		}
	}
	
	if(id == nCol){
		
		if($("#content_col_tmp_" + nCol).html() == null){
			
			var startX = -1*colWidth;
			$("#content_col_"+nCol ).clone().attr({"id":"content_col_tmp_" +nCol }).css("left",startX+"px").appendTo("#content_col_cnt");
		}
		else{$("#content_col_tmp_" + nCol).html($("#content_col_tmp_" + nCol).html())}
	}
	
	var xStop = 0;
	
	if(id==1 && curColId == nCol){xStop = -1 * (nCol) * colWidth;}
	else if(id==nCol && curColId == 1){xStop = colWidth;}
	else{
		if($("#content_col_"+id).length > 0){
		
			xStop = 0 - $("#content_col_"+id).position().left
		}
	}
	

	$("#content_col_cnt").animate({left:xStop+"px"},100,"swing",function(){colAniReady()});;
	
	curColId = id;

	$(".menu_header_menu_col:not(#menu_header_menu_col_" +id + ")").removeClass("menu_header_menu_col_active").addClass("menu_header_menu_col_normal");	
	$("#menu_header_menu_col_" + id).removeClass("menu_header_menu_col_normal").addClass("menu_header_menu_col_active")
	$("#menu_side_left_current_text").html($("#menu_header_menu_col_"+id).html())
	$("#menu_side_left_current_1").attr("class","menu_side_left_current_1_"+id);
	$("#menu_side_left_current_2").attr("class","menu_side_left_current_2_"+id);
	$("#menu_side_left_current_text").attr("class","menu_side_left_current_text_"+id);
	$("#comments").attr("class","comment_col_" + id);




	$("#content_col_" + id).jScrollPane();	
	
	
	if($("#content_col_" + curColId).length > 0){
		
		
		$("#content_col_" + curColId)[0].scrollBy(curScrollPos);
	}
	
	hideComment();
}

function colAniReady(){
	
	if(curColId == 1){$("#content_col_cnt").css("left","0px");}
	else if(curColId == nCol){$("#content_col_cnt").css("left",(0 - (colWidth * (nCol-1))) +"px");}
}

function nextcol(){updateCol(curColId + 1);}

function prevcol(){updateCol(curColId - 1);}

keyDownBool = false;

function keyboardDownEvent(e){
	
////alert($("body").scrollLeft());

	switch(e.which){
		
		case 39: updateCol(curColId + 1);$(window).scrollLeft(0);break;
		case 37: updateCol(curColId - 1);$(window).scrollLeft(0);break;
		case 38: scrollUp();break;
		case 40: scrollDown();break;
	}
}

function keyboardUpEvent(e){
	
	switch(e.which){
		
		case 37:$(window).scrollLeft(0);break;
		case 39:$(window).scrollLeft(0);break;
		case 38: $("#content_col_"+curColId).stop();break;
		case 40: $("#content_col_"+curColId).stop();break;
	}
}



var scrollSpeed = 10;



function scrollDown(){

	//alert(colIdArray[curColId][1]);
	var curId = 1;
	for(var i=1;i<colIdArray[curColId].length +1 ;i++){
		
		if(curColPosArray[curColId] == colIdArray[curColId][i]){
			
			curId = i;
		}
	}
	
	
	//$.log(colIdArray[curColId][2]);
	if(curColPosArray[curColId] == null){
		
		if(colIdArray[curColId][2] != undefined){
			curColPosArray[curColId] = colIdArray[curColId][2];
			window.location.href = "#"+curColId+"_"+curColPosArray[curColId];
			$("#col_mouse_scroll_down").fadeIn("fast",function(){$(this).fadeOut("slow");})
		}
		
	}
	else if(colIdArray[curColId][curId + 1] != undefined) {
		
		curColPosArray[curColId] = colIdArray[curColId][curId + 1];
		window.location.href = "#"+curColId+"_"+curColPosArray[curColId];
		$("#col_mouse_scroll_down").css("background-image","url("+themeUrl+"/images/logo_arrow_round_down.png)");
		$("#col_mouse_scroll_down").fadeIn("fast",function(){$(this).fadeOut("slow");})
	}
	else{
		$("#col_mouse_scroll_down").css("background-image","url("+themeUrl+"/images/mouse_nav_round_up_down.png)");
		$("#col_mouse_scroll_down").fadeIn("fast",function(){$(this).fadeOut("slow");})
	}

}


function scrollUp(){
	
	
	
	var curId = 1;
	for(var i=1;i<colIdArray[curColId].length +1 ;i++){
		
		if(curColPosArray[curColId] == colIdArray[curColId][i]){
			
			curId = i;
		}
	}
	
	if(curColPosArray[curColId] == null){
		
		curColPosArray[curColId] = colIdArray[curColId][1];
		$("#col_mouse_scroll_up").fadeIn("fast",function(){$(this).fadeOut("slow");})
		window.location.href = "#"+curColId+"_"+curColPosArray[curColId];
	}
	else if(colIdArray[curColId][curId - 1] != undefined){
		
		curColPosArray[curColId] = colIdArray[curColId][curId -1];
		$("#col_mouse_scroll_up").css("background-image","url("+themeUrl+"/images/logo_arrow_round_up.png)");
		$("#col_mouse_scroll_up").fadeIn("fast",function(){$(this).fadeOut("slow");})
		window.location.href = "#"+curColId+"_"+curColPosArray[curColId];
	}
	else{
		$("#col_mouse_scroll_up").css("background-image","url("+themeUrl+"/images/mouse_nav_round_up_down.png)");
		$("#col_mouse_scroll_up").fadeIn("fast",function(){$(this).fadeOut("slow");})
	}
}









function scrollDownMouseOver(){}

function scrollDownMouseOut(){}

function scrollUpMouseOver(){}

function scrollUpMouseOut(){}




function getWindowSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }

	return([myHeight, myWidth]);
}



var resizeTimer = null;
$(window).bind('resize', function() {
    if (resizeTimer) clearTimeout(resizeTimer);
    resizeTimer = setTimeout(setWindow, 100);
});

function setWindow(){
	
	
	var windowHeight = getWindowSize()[0] - 150;

	var windowHeight = getWindowSize()[0] - 165;


	var menuSideHeight = windowHeight-5;

	var windowWidth = getWindowSize()[1]

	//$(".content_col").height(windowHeight);
	$("#content_col_cnt_mask").height(windowHeight);
	
	$("#content").height(windowHeight);
	$("#content_mask").height(windowHeight);
	
	
	$("#col_mouse_scroll_left").height(windowHeight);
	$("#col_mouse_scroll_right").height(windowHeight);
	
	
	jQuery.each($(".content_col"),function(){$(this).height(windowHeight)})
	
	
	$("#menu_side_left").height(menuSideHeight);
	$("#menu_side_right").height(menuSideHeight);
	
	$("#menu_side_right_background_top").hide();
	$("#menu_side_left_background_top").hide();
	
	$("#menu_side_left_background").height(windowHeight);
	$("#menu_side_right_background").height(windowHeight);
	
	
	
	if(windowHeight < $("#menu_side_right_background_img").height()){
		
		
		$("#menu_side_right_background_img").css("top","0px");
		$("#menu_side_right_background_img").css("bottom","");
	}
	else{
		
		$("#menu_side_right_background_img").css("top","");
		$("#menu_side_right_background_img").css("bottom,","0px");
	}
	
	
	if(windowHeight < $("#menu_side_left_background_img").height()){
			
		$("#menu_side_left_background_img").css("top","0px");
		$("#menu_side_left_background_img").css("bottom","");
	}
	else{
		
		$("#menu_side_left_background_img").css("top","");
		$("#menu_side_left_background_img").css("bottom,","0px");
	}
	
	
	
		
	
	var backgroundStart = 	$("#menu_side_right_background_bottom").height() - $("#menu_side_right_background").height() - 48;;
	

	
	
	$("#col_mouse_scroll_down").css("top",windowHeight - $("#col_mouse_scroll_down").height());
	
	
	if(windowWidth < 1556){
		
		$("#menu_side_right").css({left:"1245px"});
		$("#content").css({width:"956px"});
		$("#content_mask").css({width:"623px"});
		$("#content_col_cnt").css({width:"932px"});
		$("#content_col_cnt_mask").css({width:"932px"});
	}
	else{
		
		var menuSideRightPos = windowWidth - 310;
		$("#menu_side_right").css({left:(windowWidth - 310)+"px"});
		$("#content").css({width:(windowWidth - 599)+"px"});
		$("#content_mask").css({width:(windowWidth - 932)+"px"});
		$("#content_col_cnt").css({width:(windowWidth - 632)+"px"});
		$("#content_col_cnt_mask").css({width:(windowWidth - 622)+"px"});
	}
	
	//content_col_space_blank{}
	setcol(curColId);
}

/* Comments */

function loadComment(url) {
    
    $("#comments_cnt").html("Loading comments.")
    $("#comments").fadeIn("fast");
    //$("#comments").show()
    $.get("http://vespavita.com/"+url,{}, loadCommentReady, "html");
  	//$("#comments_cnt").html("").height(0);
}

function loadCommentReady(data,status){

	if(data != ""){$("#comments_cnt").html(data);}
}

function hideComment(){
	
	$("#comments").fadeOut("fast");
}

function postComment(val){

	//var val = $("#commentform");
	var post = {};
	var error = false;
	
	if(val.author != undefined){
		if(val.author.value == ""){error = true;alert("please fill in an author");}
		else{post.author = val.author.value;}
	}
	
	if(val.email != undefined){
		if(val.email.value == ""){error = true;alert("please fill in an email adress");}
		else{post.email = val.email.value;}
	}
	
	if(val.url != undefined){post.url = val.url.value;}
	
	if(val.comment.value == ""){alert("please fill in an comment");error = true;}
	else{post.comment = val.comment.value;}
	
	if(val.comment_post_ID != undefined){post.comment_post_ID = val.comment_post_ID.value;}
	if(val.redirect_to != undefined){post.redirect_to = val.redirect_to.value;}
	
	if(error == false){
		$.log("send"); 
		$.post("http://vespavita.com/wp-comments-post.php",post, loadCommentReady, "html"); 
		if(val.author != undefined){
			
			alert("comment has been sent, and it's hold for moderation.");
		}
		else{
			
			alert("comment has been sent");
		}
	}
	var returnValue = false;
	return(returnValue);
}


function readMoreContent(id){
	
	$("#content_read_more_"+id).slideDown('fast',function(){setcol(curColId,true);});
	$("#content_read_more_a_"+id).text('hide');
	$("#content_read_more_a_"+id).attr('href','javascript:hideReadMoreContent('+id+')');

}

function hideReadMoreContent(id){
	
	$("#content_read_more_"+id).slideUp('fast',function(){setcol(curColId,true);});
	$("#content_read_more_a_"+id).text('read more');
	$("#content_read_more_a_"+id).attr('href','javascript:readMoreContent('+id+')');		
}

function getSearch(){
	

		
	if($("#s_input").attr("value") != ""){
		$.get(pageUrl+"/",{"s":$("#s_input").attr("value")}, loadSearchReady, "html");
	}
	

	
	
	
	
	var returnvalue = false;
	return(returnvalue);
}

function loadSearchReady(data,status){
	
	$("#search_result").html(data);
	$("#content_col_" + curColId).jScrollPane();
}


function setDebug(message,clear){
	
	//if(clear == true){$("#menu_header_debug").html("");}
	//$("#menu_header_debug").append("<br />").append(message);
}

function closePopUpKey(){
	
	$("#popup_key").fadeOut("fast");
}

function showPopUpKey(){
	
	$("#popup_key").fadeIn("fast");
}
