//Activate FancyBox
var gallery = {
	makeGallery: function(whichOnes) {
		$(whichOnes+' a').livequery(function(){
			$(this).fancybox({
				'overlayShow': true
			});
		}, function (){
			$(this).unbind('fancybox'); 	
		});
	}
};
var webStart = {
	initializeWeb: function () {
		$("#innerNavLeftSliding").livequery(function(){
			slide("#innerNavLeftSliding", 20, 10, 150, .8, "ori", "-85", "0", "left");
		});	
		gallery.makeGallery('.screenshot');
		gallery.makeGallery('.photos');
		gallery.makeGallery('.smallPhotos');
		$('#cpr').click(function() {
			if($(this).children('a').is(':hidden')) {
				$(this).children('a').show();
				$(this).animate({ left: "30px" }, 'slow');
			}
			else {
				$(this).animate({ left: "130px" }, 'slow', function (){
					$(this).children('a').hide();													 
				});	
			}
		});
		/*$("#siteLoading").prepend('<div id="siteLoadOverlay"></div>');
		$("#siteLoadOverlay").css({'width': $(window).width(), 'height': $(document).height()});

		if ($.browser.msie) {
			$("#siteLoading").prepend('<iframe id="siteLoadOverlay_Frame" scrolling="no" frameborder="0"></iframe>');
			$("#siteLoadOverlay_Frame").css({'width': $(window).width(), 'height': $(document).height()});
		}*/
		//$("#siteLoading").fadeIn('normal', function (){});
		$(window).load(function () {
			//$("#siteLoading,#siteLoadOverlay_Frame").fadeOut("fast").remove();
			$("#siteLoading").fadeOut('normal', function (){
				$(this).remove();											
			});
		});
		
	}	
};
var content = {
	loadsData: function(urlAddress, whichBox, speed, callback) {
		var newContentToSee = urlAddress + ' ' + whichBox + ' > *';
		if(speed === undefined) {
			speed = 'normal';	
		}
		$(whichBox).fadeOut(speed, function() {
			$(whichBox).load(newContentToSee,'', function(data, status) {
				/*if(status == 'error'){
					$("#innerNavLeft").fadeIn('normal');
					$("#innerNavRight").fadeIn("normal");	
					$('#hookContent').html("<div id=\"error\"><h2>Oops!</h2><p>something wrong happend<p/><p>you either typed something wrong<br /> or we have made a mistake and we do not know it yet.</p><p>for now you can return <a href=\"/\" title=\"home\">home</a></p></div><div id=\"errorCreature\"></div>").fadeIn('normal', function () {
						$("#content").fadeIn('slow');																																																																											   					});
					$("#logo").fadeIn('slow');
				}
				else {*/
					$(whichBox).fadeIn(speed);
					document.title="outRandom | " + urlAddress.substr(11,urlAddress.length - 15);
					if(callback !== undefined){
						callback();
					}
				//}
			});
		});
	},
	loadEventContent: function() {
		$('.sliding-element a').livequery('click', function(event) { 
			var selfSlide = this;
			$('.sliding-element').each(function () {
				$(this).removeClass('activeSlide');
				$(this).children().css("padding-right", 10);
					//$(this).children().animate({paddingRight: 10});
			});
			$(this).parent().addClass('activeSlide');
			$(this).css("padding-right", 20);
			var eventToSaw = $(this).attr('href');
			window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-4);
			content.loadsData('ajaxedData/' + eventToSaw, '#hookContent', 'normal');
			return false;	
		});
	}, 
	fadeContent: function () {
		$("#container > *").hide();
		$("a.ajaxfied").click(function () {
			var newUrltoGo = $(this).attr('href');
			$("#logo").fadeIn('slow');
			window.location.hash = newUrltoGo.substr(0,$(this).attr('href').length-4);
			content.loadsData('ajaxedData/' + newUrltoGo, '#navContentLeft', 'normal', function(){
				//content.loadsData('ajaxedData/' + newUrltoGo, '#navContentRight', 'normal');
				content.loadsData.call(content, 'ajaxedData/' +  newUrltoGo, '#hookContent', 'fast');
				$("#innerNavRight").fadeIn("normal");
				$("#innerNavLeft").fadeIn("normal");
				$("#content").fadeIn("slow");
				window.setTimeout(function() { changeOpticalWindow.doChange('#content', true); }, 300);
				if (newUrltoGo === 'vangogh-vr.php' || newUrltoGo === 'interview.php'){ // here we put what is in the link in the animated menu at the bottom
					content.loadEventContent();		
				}
				else if(newUrltoGo === "index.php") {
					window.location.hash = "";	
				}
			});
			return false;			
		});		
	},
	redirectUrl: function(){
		var hash = window.location.hash.substr(1);
		var noEvents = 0;
		$("#innerNavLeft").hide();
		$("#innerNavRight").hide();
		$("#content").hide();
		$("#logo").hide();
		
		$('a.ajaxfied').each(function(){
			var href = $(this).attr('href');
			if(hash === href.substr(0,href.length-4)){
				content.loadsData('ajaxedData/' + href, '#hookContent', 'fast', function(){
					content.loadsData.call(content, 'ajaxedData/' +  href, '#navContentLeft', 'fast');
					//content.loadsData('ajaxedData/' + href, '#navContentRight', 'normal');
					$("#innerNavLeft").fadeIn('normal');
					$("#innerNavRight").fadeIn("normal");	
					$("#logo").fadeIn('slow');	
					$("#content").fadeIn('slow');
					window.setTimeout(function() { changeOpticalWindow.doChange('#content', true); }, 300);
					if (href === "index.php") {
						window.location.hash = "";
					}
					if (href === 'vangogh-vr.php' || href === 'interview.php'){ // here we put what is in the link in the animated menu at the bottom
						content.loadEventContent();			
					}
				});
			}
			else if (hash === "") {
				noEvents = 0;
			}
			else {
				noEvents++;
			}
		});
		if (noEvents === 10) {//the same number as the number of links in all buttonContent
		  content.loadsData('ajaxedData/' + hash + ".php", '#navContentLeft', 'normal', function(){
			  //content.loadsData('ajaxedData/' + hash, '#navContentRight', 'normal');																  
			  content.loadsData.call(content, 'ajaxedData/' + hash + ".php", '#hookContent', 'fast');
			  $('#content').fadeIn(700);
			  $("#innerNavLeft").fadeIn('normal');
			  $("#innerNavRight").fadeIn("normal");	
			  $("#logo").fadeIn('slow');	
			  window.setTimeout(function() { changeOpticalWindow.doChange('#content', true); }, 300);
			  content.loadEventContent();	
		  });
		}
	},
	init: function () {
		this.fadeContent();	
		this.redirectUrl();
	}
};

var navBoxes = {
	controlMove: function() {
		this.root = $("#navigation");
		$(".buttonContent").hide();
		$(".navButton", this.root).each(function(index) {
			var self = $(this);
			if(self.animationIn)window.clearInterval(self.animationIn);	
			if(self.animationOut)window.clearInterval(self.animationOut);
			self.i = 0;
			self.j = 21;
			self.hover(
			  function () {
				window.clearInterval(self.animationIn);	
				self.animationOut = window.setInterval(function(){
					self.children(".buttonOpen").css("background-position", "0px "+(self.i * -204)+"px");
					if(self.i<=21){
						self.i++;
						self.j = self.i;
					}
					else {
						window.clearInterval(self.animationOut);
						self.children(".buttonOpen").children().show('fast');
						self.j = 21;
						self.i = 0;
					}
				},50);
			  }, 
			  function () {
				self.animationIn = window.setInterval(function(){
					window.clearInterval(self.animationOut);
					self.children(".buttonOpen").children().hide('fast');
					self.children(".buttonOpen").css("background-position", "0px "+(self.j * -204)+"px");
					if(self.j>=0){
						self.j--;
						self.i = self.j;
					}
					else {
						window.clearInterval(self.animationIn);	
						self.j = 21; 
						self.i = 0;
					}
				},30);
			  }
			);
			
		});
	},
	init: function(){
		this.controlMove();
	}
};
var changeOpticalWindow = {
	doChange: function(inContrastWith, notInitial) {
		if($('#content').is(':hidden') || notInitial) {
			var targetOffset = $(inContrastWith).offset().top;
			var docHeight = $(document).height();
			var screenHeight = $(window).height();
			if (docHeight > screenHeight) {
				$('html,body').animate({scrollTop: targetOffset}, 1500);
			}
		}
	}
}; 

//a function for the sliding menu effect
function slide (navigation_id, pad_out, pad_in, time, multiplier, orientation, totalPad, pad, direction) {
	
	var list_elements = navigation_id + " .sliding-element";
	var link_elements = list_elements + " a";
	var timer = 0;
	$("#innerNavLeftSliding").show("fast");
	if (orientation === "ori") {
		if(direction === "right") {
		  $(list_elements).each(function(i) {
			$(this).css("margin-left", totalPad);
			timer = (timer*multiplier + time);
			$(this).animate({ marginLeft: "-15px" }, timer);
			$(this).animate({ marginLeft: pad }, timer);
			$(this).animate({ marginLeft: "0" }, timer);
			  
		  });
		  $(link_elements).each(function(i) {
			$(this).hover(
			function()
			{
				$(this).animate({ paddingLeft: pad_out }, 150);
			},		
			function()
			{
				$(this).animate({ paddingLeft: pad_in }, 150);
			});
		  });
		}
		else if(direction === "left") {
		  $(list_elements).each(function(i) {
			$(this).css("margin-right", totalPad);
			timer = (timer*multiplier + time);
			$(this).animate({ marginRight: "-10px" }, timer);
			$(this).animate({ marginRight: pad }, timer);
			$(this).animate({ marginRight: "0" }, timer);
			  
		  });
		  $(link_elements).each(function(i) {
			$(this).hover(
			function()
			{
				if ($(this).parent().hasClass('activeSlide') === false){
					$(this).animate({ paddingRight: pad_out }, 150);
				}
			},		
			function()
			{
				if ($(this).parent().hasClass('activeSlide') === false){
					$(this).animate({ paddingRight: pad_in }, 150);
				}
			});
		  });
		}
	}
	else if (orientation === "ver") {
		$(list_elements).each(function(i) {
			$(this).css("margin-top", totalPad);
			timer = (timer*multiplier + time);
			$(this).animate({ marginTop: "0" }, timer);
			$(this).animate({ marginTop: pad }, timer);
			$(this).animate({ marginTop: "0" }, timer);
		});
		$(link_elements).each(function(i) {
			$(this).hover(
			function()
			{
				if ($(this).parent().hasClass('activeSlide') === false){
					$(this).animate({ marginTop: pad_out }, 150);
				}
			},		
			function()
			{	
				if ($(this).parent().hasClass('activeSlide') === false){
					$(this).animate({ marginTop: pad_in }, 150);
				}
			});
		});	
	}
};
$(function extrnalLinks () {
	$("a.externalLink").livequery(function (){
		$(this).attr("target","_blank");							
	});
});
//a function to preload some images
function preloadImages () {
	if (document.images) {
	   	img1 = new Image();
		img2 = new Image();
		img3 = new Image();
		img4 = new Image();
		img5 = new Image();
		img6 = new Image();
		img7 = new Image();
		img8 = new Image();
		img9 = new Image();
		img10 = new Image();
		img11 = new Image();
		img12 = new Image();
		img13 = new Image();
		img14 = new Image();
		img15 = new Image();
		img16 = new Image();
		img17 = new Image();
		img18 = new Image();
		img19 = new Image();
		img20 = new Image();
		img21 = new Image();
		img22 = new Image();
		img23 = new Image();
		img24 = new Image();
		img25 = new Image();
		img26 = new Image();
		img27 = new Image();
		img28 = new Image();
		img29 = new Image();
		img30 = new Image();
		img31 = new Image();
		img32 = new Image();
		img33 = new Image();
		img34 = new Image();
		
		img1.src = "images/events/leiden-studiefestival-2008-.jpg";
		img2.src = "images/events/discovery-07.jpg";
		img3.src = "images/events/mt07.jpg";
		img4.src = "images/events/mt08.jpg";
		img5.src = "images/events/crackthecode.jpg";
	   	img6.src = "images/events/clubsociety.jpg";
		
		img7.src = "images/thumbs/arcade1.jpg";
		img8.src = "images/thumbs/arcade2.jpg";			   	
		img9.src = "images/thumbs/arcade3.jpg";
	   	img10.src = "images/thumbs/arcade4.jpg";

	   	img11.src = "images/thumbs/controls1.jpg";
	   	img12.src = "images/thumbs/controls2.jpg";
	   	img13.src = "images/thumbs/controls3.jpg";
	   	img14.src = "images/thumbs/controls4.jpg";

	   	img15.src = "images/thumbs/backstage1.jpg";
	   	img16.src = "images/thumbs/backstage2.jpg";
	   	img17.src = "images/thumbs/backstage3.jpg";
	   	img18.src = "images/thumbs/backstage4.jpg";
		
	   	img19.src = "images/thumbs/battles1.jpg";
	   	img20.src = "images/thumbs/battles2.jpg";
	   	img21.src = "images/thumbs/battles3.jpg";
	   	img22.src = "images/thumbs/battles4.jpg";
		
	   	img23.src = "images/thumbs/people1.jpg";
	   	img24.src = "images/thumbs/people2.jpg";
	   	img25.src = "images/thumbs/people3.jpg";
	   	img26.src = "images/thumbs/people4.jpg";
		img27.src = "images/thumbs/people5.jpg";
	   	img28.src = "images/thumbs/people6.jpg";
	   	img29.src = "images/thumbs/people7.jpg";
	   	img30.src = "images/thumbs/people8.jpg";
		img31.src = "images/thumbs/people9.jpg";
		
		img32.src = "images/thumbs/setup1.jpg";
	   	img33.src = "images/thumbs/setup2.jpg";
	   	img34.src = "images/thumbs/setup3.jpg";
	}
};
$(document).ready(function() {
	webStart.initializeWeb();					   
	navBoxes.init();
	content.init();
	if(window.location.hash.substr(1) === ""){
		window.setTimeout(function() { changeOpticalWindow.doChange('#navigation', false); }, 300);
	}
	preloadImages();
});
