/* ------ start - document.ready ------ */
$(document).ready(function() {
	// open links in new window
	$("a[rel='external']").each(function() {
		$(this).click(function() {
			window.open($(this).attr("href"));
			return false;
		});		
	})

	// mailto-links to fool spam-bots
	var at	  = "@";
	if($("#rekryt").length > 0 || $("#yhteys").length > 0) {
		var email = "contact" + at + "re-union.fi";
		$("a[rel='mailto']").attr("href","mailto:" + email).html(email);
	}
	
	if($("#tarinat").length > 0) {
		var email = "thomas" + at + "re-union.fi";
		$("a[rel='mailto']").attr("href","mailto:" + email).html(email);
	}
	
	// remove margin of the last paragraph
	$("div#mainContent").find("p:last").addClass("noMargin");

	if ($(".caseLink").length > 0) {
		// hints-blocks click-chandler
		$(".caseLink").click(function() {
			// gets the destination from headings link
			var destination = $(this).find("h5 a").attr("href");
			// re-direct
			window.location = destination;
			// don't follow
			return false;
		});
	}

	if($("#tyot").length > 0) {
		// get URL variables
		var url		 =	getUrlVars();
		// set workCase variable
		var workCase =	url["case"];
		// load the right content
		if(workCase == "fsecure") {
			loadItem("fsecure", "tyot");	
		}
		else if(workCase == "sitra") {
			loadItem("sitra", "tyot");	
		}
		else if(workCase == "spa") {
			loadItem("spa", "tyot");	
		}
		else {
			// set default case
			loadItem("fsecure", "tyot");
		}
	
		// works click-handler
		$("div#dynamic a").click(function() {
			if(!$(this).hasClass("active")) {
				var thisItem = $(this).attr("id");
				var thisArea = "tyot";
				loadItem(thisItem, thisArea);
			}
			return false;	
		});
	}

	// don't follow active link
	$(".active").live("click", function() {
		return false;
	});	

});
/* ------- end - document.ready ------- */

/* ------- start - window.load ------- */
$(window).load(function() {
	// set the fade starting point
	$("div#dynamic a").each(function() {
		if(!$(this).hasClass("active")) {
			$(this).css("opacity","0.5");	
		}
		// fade 0%
		$(this).mouseover(function() {
			if(!$(this).hasClass("active")) {
				$(this).css("opacity","1.0");	
			}
		});
		// fade 50%
		$(this).mouseout(function() {
			if(!$(this).hasClass("active")) {
				$(this).css("opacity","0.5");
			}
		});
	});

	$("div#frontImages a").each(function() {
		if(!$(this).hasClass("active")) {
			$(this).css("opacity","1.0");				
		}
		// fade 0%
		$(this).mouseover(function() {
			if(!$(this).hasClass("active")) {
				$(this).css("opacity","0.5");	
			}
		});
		// fade 50%
		$(this).mouseout(function() {
			if(!$(this).hasClass("active")) {
				$(this).css("opacity","1.0");
			}
		});
	});
});
/* -------- end - window.load -------- */

/* -------- start - functions -------- */
function getUrlVars() {
	var map = {};
	var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
		map[key] = value;
	});
	return map;
}

function loadItem(thisItem, thisArea) {
	$("div#dynamic").find("a").removeClass();
	$("div#dynamic").find("a").css("opacity","0.5");
	$("div#include").load("includes/mini/" + thisArea + "_" + thisItem + ".php");
	$("div#dynamic").find("a#" + thisItem).addClass("active").css("opacity","1.0");
}
/* --------- end - functions --------- */