
var rememberedTL = ""; 

//init
$(document).ready(function() {
	
	
	//init section image
	//if (sectionCurrent == "secThink") {
	//	$("#sectionIcon").attr("src","/images/secThink.png");
	//}
	
	//init timeline
	if (sectionSubCurrent == "subsecHistory") {
		initTimeline();
		tl_select('linowski');
	}
	
	//sectionSelect(sectionCurrent);
	sectionSubSelect(sectionSubCurrent);
});





function sectionOver(which) {
	which = $(which).attr("id");
	$("#sectionIcon").attr("src","/images/" + which + ".png")
}

function sectionOut(which) {
	$("#sectionIcon").attr("src","/images/" + sectionCurrent + ".png")
}



function sectionSelect(which) {
	//remove old
	$("#" + sectionCurrent).removeClass("sectionCurrent");

	//select new
	sectionCurrent = which;
	$("#" + sectionCurrent).addClass("sectionCurrent");
}

function sectionSubSelect(which) {
	if (which == "subsecOverview") {
		//preload more images
		var urls = [ 'frontscreen_architecting.jpg', 'frontscreen_designing.jpg','frontscreen_envisioning.jpg','frontscreen_evaluating.jpg','frontscreen_inspiring.jpg','frontscreen_observing.jpg','frontscreen_programming.gif','frontscreen_prototyping.jpg','frontscreen_sketching.jpg','frontscreen_visualizing2.jpg','frontscreen_wireframing.jpg'];
		$.preload( urls, {
			base: '../images/front/',
			onComplete: function(data){}
		});
		
		//attach readmore images to recent logos
		$(".recent div").hover(function() {
				$(this).find(".readmore").addClass("readmoreHover");
			},
			function() {
				$(this).find(".readmore").removeClass("readmoreHover");
			}
		);
		
	}
	if (which == "subsecServices") {
		//set hover
		$(".sMenu").hover(
		function () {
			services.serviceOver(this);
			$(this).addClass("sSelected");
		}, 
		function () {
			if (services.rememberedService != $(this).attr("id")) {
				services.serviceOut(this);
				$(this).removeClass("sSelected");
			}
		}
		);
		//setclick
		$(".sMenu").bind("mousedown",function() { services.selectService($(this).attr("id")); });
		
		//pick a random default item
		services.selectService(services.getRandomService());
	}
	
		
	if(which == "subsecHistory") {
	//preload
		var urls = [ '/images/arrowLeft_on.png','/images/arrowRight_on.png','/images/tl_yorkuniv_on.png', '/images/tl_york_on.png','/images/tl_unikron_on.png','/images/tl_um_on.png','/images/tl_tudelft_on.png','/images/tl_tribler_on.png','/images/tl_sensis_on.png','/images/tl_sapient_on.png','/images/tl_insidewire_on.png','/images/tl_freelance_on.png','/images/tl_devlin2_on.png','/images/tl_devlin1_on.png','/images/tl_componentart_on.png'];
		$.preload( urls, {
			onComplete: function(data){}
		});
	}
	
	//remove old
	$("#" + which).removeClass("subSectionCurrent");

	//select new
	$("#" + which).addClass("subSectionCurrent");
}




function showEmail() {
	$("#fluidiaText").fadeOut(300);
	setTimeout("$('#woohoo').fadeIn(300);",300);
	//$("#woohoo").fadeIn(300);
	setTimeout("$('#woohoo').fadeOut(500)",20000);
	setTimeout("$('#fluidiaText').fadeIn(300)",20600);
}


var services = {
	rememberedService : null,
	serviceOver : function (what) {
		var replacetxt = $(what).find("img").attr("src").replace("_off","_over");
		$(what).find("img").attr("src",replacetxt);	
	},
	serviceOut : function (what) {
		var replacetxt = $(what).find("img").attr("src").replace("_over","_off");
		$(what).find("img").attr("src",replacetxt);
	},
	selectService : function (what) {
		//turn off old one
		if(this.rememberedService != null) {
			services.serviceOut($("#" + this.rememberedService));
			$("#" + this.rememberedService).removeClass("sSelected");
		}
		
		//turn on new one
		services.serviceOver($("#" + what));
		$("#" + what).addClass("sSelected");
		
		//remember the clicked �one
		this.rememberedService = what;
		
		//turn off all main content
		$(".servicesMain").hide();
		
		//turn on the right main content
		var turnOn = what.replace("sMenu_","sMain_");
		$("#" + turnOn).show();
		
		//alert(":" + what + ":");
	},
	getRandomService : function () {
		howmanychildren = $(".servicesLeft").children(".sMenu").size(); 
		var randomnumber = rand(howmanychildren); //between 0 and howmanychildren-1
		var returnwhich = $(".servicesLeft > div").eq(randomnumber);
		return $(returnwhich).attr("id");
	}
}

var frontscreen = {
	currentId : null,
	rememberedId : null,
	enabled : true,
	rotate : function(direction) {
		if (frontscreen.currentId == null) {
			if(direction == 'random') { frontscreen.currentId = frontscreen.getRandomItem(); }
			//frontscreen.currentId = '#frontscreen_prototyping';
			$(frontscreen.currentId).fadeIn(200);
			frontscreen.rememberedId = frontscreen.currentId;
		}
		else {
			if (this.enabled == true) {
				this.enabled = false; //lock
				$(frontscreen.rememberedId).fadeOut(200, function() {
					if(direction == 'forward') { frontscreen.currentId = frontscreen.getNextItem(); }
					else if(direction == 'back') { frontscreen.currentId = frontscreen.getPrevItem(); }
					else {frontscreen.currentId = "#" + direction; }
					
					$(frontscreen.currentId).fadeIn(200);
					frontscreen.rememberedId = frontscreen.currentId;
					frontscreen.enabled = true; //unlock
				});
			}
		}
	},
	getNextItem : function () {
		var returnwhich = $(frontscreen.currentId).next();
		if ($(returnwhich).attr("id") == undefined) {
			//grab first element
			returnwhich = "#" + $("#contentFront div").attr("id");
		}
		return returnwhich;
	},
	getPrevItem : function () {
		var returnwhich = $(frontscreen.currentId).prev();
		if ($(returnwhich).attr("id") == undefined) {
			//grab first element
			returnwhich = "#" + $("#contentFront > div:last").attr("id");
		}
		return returnwhich;
	},
	getRandomItem : function () {
		howmanychildren = $("#contentFront").children().size(); 
		var randomnumber = rand(howmanychildren); //between 0 and howmanychildren-1
		var returnwhich = $("#contentFront > div").eq(randomnumber);
		return returnwhich;
	}
}



function initTimeline() {
	//shift the timeline to the right
  $("#tl_main").scrollTo('+=500', {queue: false, axis:'x', duration: 400, easing: 'swing'});
    
  //assign scroll handlers
	$("#tl_left").click(
      function () {
      	$("#tl_main").scrollTo('-=200', {queue: false, duration: 200, axis:'x', easing: 'swing'});
      }
    );
    
  $("#tl_right").click(
    function () {
    	$("#tl_main").scrollTo('+=200', {queue: false, duration: 200, axis:'x', easing: 'swing'});
    }
  );
    
    
   
 // attach hovers to items on timeline 
 $(".tl_ln1").hover(
	  function () {
	  	strpass = $(this).attr("id");
	  	strpass = strpass.replace("tl_","");
	    if(strpass != rememberedTL) {
		    tl_on(strpass);
		  }
	  }, 
	  function () {
	  	strpass = $(this).attr("id");
	  	strpass = strpass.replace("tl_","");
	  	if(strpass != rememberedTL) {
	  		tl_off(strpass);
	  	}
	  }
 );
}


// select timeline item
function tl_select(what) {
	//hide remembered if there
	$("#tl_content_" + rememberedTL).hide();
	
	//show new item
	$("#tl_content_" + what).show();
	
	
	
	//visually select on timeline
	if (rememberedTL != "") { tl_off(rememberedTL); }
	tl_on(what);
	
	rememberedTL = what;
}


function tl_on(what) {
	//get image src of hovered img
	//alert(what);
  imgsrc = $("#tl_" + what).children().attr("src");
  //replace .png with _on.png
  imgsrc = imgsrc.replace("_off.png","_on.png");
  $("#tl_" + what).children().attr("src",imgsrc);
  
  //change textcolor
  $("#tl_" + what).addClass("tl_txtsel");
}

function tl_off(what) {
	//get image src of hovered img
	imgsrc = $("#tl_" + what).children().attr("src");
   //replace .png with _on.png
  imgsrc = imgsrc.replace("_on.png","_off.png");
  $("#tl_" + what).children().attr("src",imgsrc);
  
  //change textcolor
  $("#tl_" + what).removeClass("tl_txtsel");
}


function rand ( n )
{
  return ( Math.floor ( Math.random ( ) * n ) );
}



