$(document).ready(function(){
	$("#container").fadeOut(0);
	$("#loader").css("display", "block");
	
	//PROJECTS AND FRONTPAGE INTERACTION
	$(".overlay2").mouseover(function() {
			$(".effect_right").fadeTo(0, 0.0);
			$(".effect_left").fadeTo(0, 0.0);
			$(".text_left").fadeTo(0, 0.0);
			$(".text_right").fadeTo(0, 0.0);
		    $(this).css('cursor','pointer');
			var id = $(this).attr("id");
			$("#"+id+"_effect").css("background-color", "rgb(40, 40, 40)");
			$("#"+id+"_effect").fadeTo(0, 0.0);
			$("#"+id+"_effect").fadeTo(600, 0.61);
			$("#"+id+"_text").fadeTo(300, 1.0);
			/*
			if(isBW($("#"+id+"_image").attr("src"))){
				var new_image_src = getColorImagePath($("#"+id+"_image").attr("src"));
				$("#"+id+"_image").attr("src", new_image_src);
			}
			*/
			
	});
	$(".overlay2").mouseout(function() {
		var id = $(this).attr("id");
		$("#"+id+"_effect").fadeTo(300, 0.0);
		$("#"+id+"_text").fadeTo(150, 0.0);
		/*
		if(!isBW($("#"+id+"_image").attr("src"))){
			var new_image_src = getBWImagePath($("#"+id+"_image").attr("src"));
			$("#"+id+"_image").attr("src", new_image_src);
		}
		*/
	}); 
	
	//ABOUT INTERACTION
	$(".about_div").mouseover(function() { 
		$(this).css("background-color", "rgb(218, 218, 218)");
		$(this).find("h1").css("color", "rgb(0, 181, 241)");
	});
	$(".about_div").mouseout(function() { 
		$(this).find("h1").css("color", "rgb(90, 90, 100)");
		$(this).css("background-color", "rgb(240, 240, 240)");
	});
	$(".yoke_contact_div").mouseover(function() { 
		$(this).css("background-color", "rgb(218, 218, 218)");
		$(this).find("h1").css("color", "rgb(0, 181, 241)");
	});
	$(".yoke_contact_div").mouseout(function() { 
		$(this).find("h1").css("color", "rgb(90, 90, 100)");
		$(this).css("background-color", "rgb(240, 240, 240)");
	});
	//$(".about_bg_image").fadeTo(0.2, 0.1);
	$(".about_bg_image_image").fadeTo(0.2, 0.1);
	
	//$("#client_logos").stop(true).fadeTo(0.2, 0.1);
	
	//CONTACT INTERACTION
	$(".business_card").mouseover(function() { 
		$(this).css("background-color", "rgb(240, 240, 240)");
		$(this).find("h3").css("color", "rgb(0, 181, 241)");
	});
	$(".business_card").mouseout(function() { 
		$(this).find("h3").css("color", "rgb(90, 90, 100)");
		$(this).css("background-color", "rgb(190, 191, 190)");
	});
	
	//PROJECT INTERACTION
	$("#project_text").mouseover(function() { 
		//$(this).css("background-color", "rgb(218, 218, 218)");
		$(this).find("h1").css("color", "rgb(0, 181, 241)");
	});
	$("#project_text").mouseout(function() { 
		//$(this).css("background-color", "rgb(218, 218, 218)");
		$(this).find("h1").css("color", "rgb(90, 90, 100)");
	});
	$(".project_related").fadeTo(0, 0.8);
	$(".project_related").mouseover(function() {
		$(this).fadeTo(100, 1.0);
	});
	$(".project_related").mouseout(function() {
		$(this).fadeTo(200, 0.8);
	});
	
	//NEWS
	//NEWS INTERACTION
	$(".news_div").mouseover(function() { 
		$(this).css("background-color", "rgb(218, 218, 218)");
		$(this).find("h3").css("color", "rgb(0, 181, 241)");
		$(this).find(".news_read_more_link").css("color", "rgb(0, 181, 241)");
	});
	$(".news_div").mouseout(function() { 
		$(this).find("h3").css("color", "rgb(90, 90, 100)");
		$(this).css("background-color", "rgb(240, 240, 240)");
	});
	
	$(".news_read_more_link").css("cursor", "pointer");
	$(".news_bg_image_image").fadeTo(0.2, 0.1);
	
	
	
	$(".news_read_more_link").mousedown(function(){
		
		
		
		var id = $(this).attr("name");
		var frame = $(this).attr("frame");
		var link = $(this);
		
		$(".news_read_more_link").each(function(){
			if($(this).html() == "read less" && $(this).attr("name") != id){
				var _id = $(this).attr("name");
				var _frame = $(this).attr("frame");

				$("."+_frame).animate({height: '188px'}, 300, '', function(){$("#"+_id).css("height", '148px');});
				$(this).animate({top: '165px'}, 300, '', function(){
					$("."+_frame).css("z-index", "0");
				});
				$(this).html("read more");
				cropText(_id);
			}
		});
		
		if($(this).text() == "read more"){
			$("."+frame).css("z-index", "10");
			$("#"+id).css("z-index", "10");
			$("#"+id).animate({height: "337px"}, 300);
			$("."+frame).animate({height: '357px'}, 300);
			$(this).html("read less").delay(100);
			$(link).animate({top: '354px'}, 300, '', function(){
				$("#"+id+"_news_text").html($("#"+id+"_news_text").attr("news_text"));
				$("#"+id).css('background-color', 'rgb(218, 218, 218)');
			});
			
		}
		else{
			$("."+frame).animate({height: '188px'}, 300, '', function(){$("#"+id).css("height", '148px');});
			$(this).animate({top: '165px'}, 300, '', function(){
				$("."+frame).css("z-index", "0");
			});
			$(this).html("read more");
			cropText(id);
		}
		
		
	});
	
	//link selector
	$(".link_selector").mouseup(function(){
		if($(this).attr("src") !=  $(this).attr("active")){
			$(".info_selector").attr("src", $(".info_selector").attr("passive"));
			$(".link_selector").each(function(){
				$(this).attr("src", $(this).attr("passive"));
			});
			$(this).attr("src", $(this).attr("active"));

			var media_id =  $(this).attr("project_id");
			$("#project_text_area p").fadeOut(300, function(){
				ajaxFunction("project_text_area", "projects", "get_links", media_id);
				$("#project_text_area").fadeIn(100);
			});
		}
	});
	$("#link_link").mouseup(function(){
		var media_id =  $(this).attr("project_id");
		$(".info_selector").attr("src", $(".info_selector").attr("active"));
		$(".info_selector").attr("src", $(".info_selector").attr("passive"));
		$("#link_selector_0").attr("src", $("#link_selector_0").attr("active"));
		$("#project_text_area p").fadeOut(300, function(){
			ajaxFunction("project_text_area", "projects", "get_links", media_id);
			$("#project_text_area").fadeIn(100);
		});
	});
	
	//info selector
	$(".info_selector").mouseup(function(){
		if($(this).attr("src") !=  $(this).attr("active")){
			$(".link_selector").attr("src", $(".link_selector").attr("passive"));
			$(this).attr("src", $(this).attr("active"));

			var media_id =  $(this).attr("project_id");
			$("#project_text_area a").fadeOut(300, function(){
				ajaxFunction("project_text_area", "projects", "get_info", media_id);
				$("#project_text_area p").fadeIn(100);
			});
		}
	});
	$("#info_link").mouseup(function(){
		var media_id =  $(this).attr("project_id");
		$(".info_selector").attr("src", $(".info_selector").attr("active"));
		$(".link_selector").attr("src", $(".link_selector").attr("passive"));
		$("#project_text_area a").fadeOut(300, function(){
			ajaxFunction("project_text_area", "projects", "get_info", media_id);
			$("#project_text_area p").fadeIn(100);
		});
		
	});
	
	
	//media selectors
	$(".media_selector").mouseup(function(){
		if($(this).attr("src") !=  $(this).attr("active")){
			$(".media_selector").each(function(){
				$(this).attr("src", $(this).attr("passive"));
			});
			$(this).attr("src", $(this).attr("active"));

			var media_id =  $(this).attr("media_id");
			var image_index =  $(this).attr("image_index");
			var image_count =  $(this).attr("image_count");
			if(image_index > -1){
				$("#media_div").html('<div id="new_media"></div>');
				$("#new_media").fadeOut(0);
				$("#new_media").html('<img src="'+images[image_index]+'"/>');
			}
			else{
				$("#media_div").html('<div id="new_media"></div>');
				$("#new_media").fadeOut(0);
				ajaxFunction("new_media", "projects", "get_media", media_id);
			}
			$("#new_media").fadeIn(1000);		
		}
	});
	$("#media_link").mouseup(function(){
		$(".media_selector").each(function(){
			$(this).attr("src", $(this).attr("passive"));
		});
		$("#media_selector_0").attr("src", $("#media_selector_0").attr("active"));
		var media_id =  $(this).attr("media_id");
		var image_index =  $(this).attr("image_index");
		var image_count =  $(this).attr("image_count");
		if(image_index > -1){
			$("#media_div").html('<div id="new_media"></div>');
			$("#new_media").fadeOut(0);
			$("#new_media").html('<img src="'+images[image_index]+'"/>');
		}
		else{
			$("#media_div").html('<div id="new_media"></div>');
			$("#new_media").fadeOut(0);
			ajaxFunction("new_media", "projects", "get_media", media_id);
		}
		$("#media_div img").fadeOut(500);
		$("#new_media").fadeIn(1000);
		
	});

	
	$(".media_selector").each(function(){
		var id = $(this).attr("id");
	});
	
	$("#media_div").mouseover(function(){
		$(this).css("cursor", "pointer");
	});
	$("#media_div").mouseout(function(){
		$(this).css("cursor", "default");
	});
	$("#media_div").mouseup(function(){
		var next_selector = "";
		$(".media_selector").each(function(){
			
			if($(this).attr("src") == $(this).attr("active")){
				$(this).attr("src", $(this).attr("passive"));
				var next_index = parseInt($(this).attr("id").substring(15)) + 1;
				var index_count = parseInt($(this).attr("image_count"));
				if(next_index < index_count){
					next_selector = "media_selector_"+next_index;
				}
				else{
					next_selector = "media_selector_0";
				}
				
			}
		});
		$("#"+next_selector).attr("src", $("#"+next_selector).attr("active"));
		var media_id =  $("#"+next_selector).attr("media_id");
		var image_index =  parseInt($("#"+next_selector).attr("image_index"));
		var image_count =  $("#"+next_selector).attr("image_count");
		if(image_index > -1){
			$("#media_div").html('<div id="new_media"></div>');
			$("#new_media").fadeOut(0);
			$("#new_media").html('<img src="'+images[image_index]+'"/>');
		}
		else{
			$("#media_div").html('<div id="new_media"></div>');
			$("#new_media").fadeOut(0);
			ajaxFunction("new_media", "projects", "get_media", media_id);
		}
		$("#new_media").fadeIn(1000);
	});
	
	//link behaviour
	$("a").mouseover(function() {
		if($(this).attr("id") != "menupoint_top"){
			$(this).css("color", "rgb(0, 181, 241)");
			//$(this).css("text_decoration", "none");
		}
	});
	$("a").mouseout(function() {
		if($(this).attr("id") != "menupoint_top"){
			$(this).css("color", "black");
			//$(this).css("text_decoration", "underline");
		}
	});
	
	$(window).load(function(){
		$("#footer").css("top", ($(document).height()+100)+"px");
		$("#footer").css("height", "100px");
		$("#footer").css("width", "100px");
		$("#footer").css("display", "block");
		
		$("#container").fadeIn(100);
		$("#loader").css("display", "none");
		
		$(".news_div").each(function(){
			var id = $(this).attr("id");
			cropText(id);
		});
	});
	
	function cropText(id){
		var header_height = $("#"+id+"_news_header").height();
		var text_height = $("#"+id+"_news_text").height();
		
		if(header_height+text_height < 120){
			$("#"+id+"_news_read_more_link").css("display", "none");
		}
		else if(header_height < 40){
			$("#"+id+"_news_text").attr("news_text", $("#"+id+"_news_text").html());
			$("#"+id+"_news_text").textOverflow({"lines":4, "titleAttr": false});
		}
		else if(header_height < 60){
			$("#"+id+"_news_text").attr("news_text", $("#"+id+"_news_text").html());
			$("#"+id+"_news_text").textOverflow({"lines":3, "titleAttr": false});
		}
		else if(header_height < 80){
			$("#"+id+"_news_text").attr("news_text", $("#"+id+"_news_text").html());
			$("#"+id+"_news_text").textOverflow({"lines":2, "titleAttr": false});
		}
	}
	
	function isBW(string){
		var image_src = string.split("/");
		if(image_src[image_src.length-1].substring(0, 2) == "bw"){
			return true;
		}
		return false;
	}

	function getBWImagePath(string){
		var image_src = string.split("/");
		var new_image_src = "";
		for(i=0; i<image_src.length-1; i++){
			new_image_src = new_image_src + (i==0 ? "" : "/") + image_src[i];
		}
		new_image_src = new_image_src + "/bw_" + image_src[image_src.length-1];
		return new_image_src;
	}

	function getColorImagePath(string){
		var image_src = string.split("/");
		var new_image_src = "";
		for(i=0; i<image_src.length-1; i++){
			new_image_src = new_image_src + (i==0 ? "" : "/") + image_src[i];
		}
		new_image_src = new_image_src + "/" + image_src[image_src.length-1].substring(3);
		return new_image_src;
	}
	
	function ajaxFunction(container, menu_point, action, id)
	{	

		var xmlHttp;
		try
		  {
		  // Firefox, Opera 8.0+, Safari
		  xmlHttp=new XMLHttpRequest();

		  }
		catch (e)
		  {
		  // Internet Explorer
		  try
		    {
		    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			}
		  catch (e)
		    {
		    try
		      {
		      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		      }
		    catch (e)
		      {
		      alert("Your browser does not support AJAX! Go get Firefox :-)");
		      return false;
		      }
		    }
		  }

		  xmlHttp.onreadystatechange=function()
		  {
		    if(xmlHttp.readyState==4)
		    {	
			  var response = xmlHttp.responseText;	
			  document.getElementById(container).innerHTML = response;
			  afterAjax();
			}
		  }

		  var url = "getContent.php?menu=" + menu_point + "&action=" + action + "&id=" + id;
		  //alert(url);
		  xmlHttp.open("GET",url,true);
		  xmlHttp.send(null);
	}
});

function afterAjax(){
	//link behaviour
	$("a").mouseover(function() {
		$(this).css("text-decoration", "underline");
	});
	$("a").mouseout(function() {
		$(this).css("text-decoration", "none");
	});
}




