/* global jQuery, $ */
/* HEADLINE INTERACTIVE WEBSITE BEHAVIOUR */

// INIT SITE *************************************************
function baseInit() {
	//check if on homepage
	if(!($("body").hasClass("subpage"))) {
		// remove all scrollbars
		$("html").css({"overflow":"hidden"});
		//load page in lightbox
		if (location.href.split(".nl/#/")[1]) {
			var currenturl = location.href.split(".nl/#/")[1];
			var currenttype = (currenturl.split("/")[1]) ? currenturl.split("/")[0] : "startnew";
			if (currenttype == "startnew") {	pagetype = "startnew";} 
			else if (currenttype == "startnew") {	pagetype = "startnew";}
			window.nonAnimOverlay(pagetype, currenturl, 1);
			window.prettyTitles(currenturl);
		}
	} else {
		//relocate to homepage
		location.href="/#/"+location.href.split(".nl/")[1];
	}
	
	// implement center for large screens
	if($.browser.version != 6) {
		if($(window).height() > 700) { $("body").addClass("largescreen");}
		$(window).resize(function(){
			if($(window).height() > 700) { $("body").addClass("largescreen");}
			else {$("body").removeClass("largescreen");}
		});
	}
	//position the dragging handles
	var buttonSpacing = 800;// 1 button every 790px
	var numberOfDragbuttons = ($(".content").width()/buttonSpacing); 
	var delta = $(".content").width()/numberOfDragbuttons;
	var i;
	for(i=0;i<=numberOfDragbuttons;i++) {
		$(".content").append("<a class=\"dragbutton\" id=\"button"+i+"\"></a>");
		$("#button"+i).css({"left":(i*delta-($("#button"+i).width()/2))});
	}

	// guess...
	$("body").addClass("hasjs");	
	//position left and right buttons
	$("#wrap").append("<a href=\"#\" class=\"clickleft click\"><img class=\"arrow\" src=\"/assets/images/arrow-l.png\"></a><a href=\"#\" class=\"clickright click\"><img class=\"arrow\" src=\"/assets/images/arrow-r.png\"></a>");		
	//offset site to the left
	$(".content").css("left",-(delta)*-0.10);
				
}
function ieInit() {
	// if ie, move blog
	if(jQuery.browser.msie) {
		$(".blog").css({"left":"-300px"});	
		$("#wrap").css({"backgroundPosition":-($(window).width()/6)+" 1px"});
	}
}
function ie6Init() {
	// if ie6, add pngfix.
	if(jQuery.browser.version == 6 && jQuery.browser.msie) {
		$(".dragbutton").ifixpng();
	}
}
// DRAGGABLE SITE FUNCTIONS *************************************************
function pageDragFunction() {
	// drag function
	$(".content").draggable({
		axis: 'x', 
		stop: function dragCheck() {//check if the content borders stay within the screen									
			var offset = $(this).offset();
			if (offset.left > 0) 											{$(".content").animate({ left:"0px" }, 800, "easeOutBack");}
			if (-offset.left > ($(".content").width()-$(window).width())) 	{$(".content").animate({ left:-($(".content").width()-$(window).width()) }, 800, "easeOutBack");}
		}
		}).mousedown(function(){	$(this).css({"cursor":"url(\"/resources/images/closedhand.cur\"), -moz-grabbing"});
		}).mouseup(function(){	$(this).css({"cursor":"url(\"/resources/images/openhand.cur\"), -moz-grab"}); 
	});

	//scroll 1 screenwidth or to the border.
	$(".clickleft").click(function() {
		var offset = $(".content").offset();
		if (-offset.left <= ($(window).width()/2)) 											{$(".content").animate({ left:"0px" }, 800, "easeOutBack");}
		if (-offset.left > ($(window).width()/2) && -offset.left < $(".content").width()) 	{$(".content").animate({left: offset.left+($(window).width()/2)}, 800, "easeOutBack");}
		$(this).blur();
		
	});	
	$(".clickright").click(function() {
		var offset = $(".content").offset();
		if (-offset.left >= ($(".content").width()-(1.5*$(window).width()))) 				{$(".content").animate({ left:-($(".content").width()-$(window).width()) }, 800, "easeOutBack");}
		else																			{$(".content").animate({left: offset.left-($(window).width()/2)}, 800, "easeOutBack");}
		$(this).blur();
	});
	$(".leftscroll").click(function() {
		itemgo = $(this).attr("href");
		itemoffset = $(itemgo).offset().left;
		contentoffset = $(".content").offset().left;
		scrolltooffset = contentoffset-itemoffset+80;
		if (scrolltooffset < -($(".content").width()-$(window).width())) {
			$(".content").animate({left:-$(".content").width()+$(window).width()}, 800, "easeOutBack");
		} else {
			$(".content").animate({left:scrolltooffset}, 1000, "easeOutBack");
		}
		return false;
	});
}

// OTHER FUNCTIONS ****************************************

function ie7scroller() {
	if($.browser.msie && $.browser.version == 7) {
	}
}
function removePng() {
	if(jQuery.browser.version == 6 && jQuery.browser.msie) {
		$(".person div, .speach").removeAttr("style");
	}
}
// INIT JQUERY ****************************************
$(document).ready(function(){
		window.baseInit();
		window.ieInit();
		window.ie6Init();
		window.pageDragFunction();
});
/* fin. */
