
var blaat = 1;
var oldimgnr = -1;

$(document).ready(function() {
	if (jQuery.browser.safari && document.readyState != "complete"){
	    //console.info('ready...');
	    setTimeout( arguments.callee, 100 );
	    return;
	}
	

	cycleImage();
	setInterval(cycleImage, 20000);
	
	$(".sub").hide();
	
	
	var url = window.location.toString().split("#")[1];
	if(url === undefined){
		url = "home.html";
	}
	getUrl(url);


	bindClicks();
	


});

var bindClicks = function(){
	$("a").live("click", function(event){
		if($(this).is(".external")){
			return;
		}else{
			event.preventDefault();
		
			var url = this.href.split("/").pop();
		
			var currentUrl = window.location.toString().split("#")[1];
			if(currentUrl === undefined){
				currentUrl = "home.html";
			}
			if(currentUrl == url){ return; }
	
			var superUrl = "";
			superUrl = url.split("-")[0];
			$("a").removeClass("selected");	
			$("a[href="+url+"]").addClass("selected");
			$("a[href="+superUrl+".html]").addClass("selected");
			getUrl(url);
		}
		
	});
};

var getMenu = function(myurl){

};

var getUrl = function(myurl){
	var height = $("#bodyText").height();
	$("#bodyText").css({"opacity": "0"});
	$("#bodyTemp").css({display:"block"});
	$("#bodyTemp").height(height);
	
	var superUrl = myurl.split("-")[0].split(".html")[0];
	
	if(!$("#"+superUrl).is(":visible")){
		$(".sub").hide();
	}
		
	$("#bodyContainer2").load(myurl,'', function(){
		$("#bodyText").css({"opacity": "0"});
		
		var newHeight = $("#bodyText").height();
		$("#bodyText").css({"display":"none"});

		$("#bodyText,#bodyTemp").animate({"height":"toggle"}, Math.abs(newHeight-height)/2+500, function(){
				$("#bodyText").animate({"opacity": "100"}, 2000);	
				$("a").removeClass("selected");	
				$("a[href="+myurl+"]").addClass("selected");
				
				var superUrl = myurl.split("-")[0].split(".html")[0];
				$("#" +superUrl).show();
				$("#" +superUrl).css({"opacity": "0"});
				$("#"+superUrl).animate({"opacity": "100"}, 2000);
				$("a[href="+superUrl+".html]").addClass("selected");
		});
	});
	window.location.hash = myurl;
	
};

var cycleImage = function(){
	var images = ["slide1.jpg", "slide2.jpg", "slide3.jpg", "slide4.jpg", "slide5.jpg", "slide6.jpg", "slide7.jpg"];
	var imgnr = Math.floor(Math.random()*images.length);
	while(imgnr == oldimgnr){
		imgnr = Math.floor(Math.random()*images.length);
	}
	var img = images[imgnr];
	oldimgnr = imgnr;
	addImage(img);
	
}

var addImage = function(imageName){
	var imageId;
	var oldImageId;
	var wrapperId;
	var oldWrapperId;
	
	if(blaat == 1){
		blaat = 2
		imageId = "bgImage2";
		oldImageId = "bgImage";
		wrapperId = "bg2";
	}else{
		blaat = 1
		imageId = "bgImage";
		oldImageId = "bgImage2";
		wrapperId = "bg"
	}
	
	var img = new Image();

	  // wrap our new image in jQuery, then:
	  $(img).load(function () {
	      // set the image hidden by default    
	      $(this).hide();
			
	    $('#' + wrapperId).children().remove();
		$('#' + wrapperId).append(this);
	
			resize(this);

	      // fade our image in to create a nice effect
	      $(this).fadeIn(3000);
		  $("." + oldImageId).fadeOut(3000);
	    })

	    // if there was an error loading the image, react accordingly
	    .error(function () {
	      // notify the user that the image could not be loaded
	    })

	    // *finally*, set the src attribute of the new image to our image
	    .attr('src', imageName)
		.addClass(imageId);
	
};

var resize = function(img){
	
	var Wwidth = $(window).width();
	var Wheight = $(window).height();
	var Iwidth = $(img).width();
	var Iheight = $(img).height();

	if(Wwidth/Iwidth >= Wheight/Iheight){
		$(img).width(Wwidth);
		$(img).height("auto");
	}else{
		$(img).height(Wheight);
		$(img).width("auto");
	}
	
};


$(window).resize(function(){
  resize($('.bgImage'));
  resize($('.bgImage2'));
});



