$(document).ready(function() {
	/*
	addPrintLink function by Roger Johansson, www.456bereastreet.com
	*/
	var addPrintLink = {
		init:function(sTargetEl,sLinkText) {
			if (!document.getElementById || !document.createTextNode) {return;} // Check for DOM support
			if (!document.getElementById(sTargetEl)) {return;} // Check that the target element actually exists
			if (!window.print) {return;} // Check that the browser supports window.print
			var oTarget = document.getElementById(sTargetEl);
			var oLink = document.createElement('a');
			oLink.id = 'print-link'; // Give the link an id to allow styling
			oLink.href = '#'; // Make the link focusable for keyboard users
			oLink.appendChild(document.createTextNode(sLinkText));
			oLink.onclick = function() {window.print(); return false;} // Return false prevents the browser from following the link and jumping to the top of the page after printing
			oTarget.appendChild(oLink);
		},
	/* addEvent function from http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html */
		addEvent:function(obj, type, fn) {
			if (obj.addEventListener)
				obj.addEventListener(type, fn, false);
			else if (obj.attachEvent) {
				obj["e"+type+fn] = fn;
				obj[type+fn] = function() {obj["e"+type+fn](window.event);}
				obj.attachEvent("on"+type, obj[type+fn]);
			}
		}
	};
	addPrintLink.addEvent(window, 'load', function(){addPrintLink.init('article','Print this page');});
	$('ul.menu li').hover(
		function() {
			$(this).addClass('hover'); 
		}, function() {
			$(this).removeClass('hover');
		}
	);
	
	/// Text size functions
	$("a.small").click(function()
	{
		$('body').css('font-size','62.5%');
		set_cookie('page_size', '0', 30);
		$("a.medium").removeClass('selected');
		$("a.large").removeClass('selected');
		$(this).addClass('selected');
	});

	$("a.medium").click(function()
	{
		$('body').css('font-size','0.7em');
		set_cookie('page_size', '0.7', 30);
		$("a.smalle").removeClass('selected');
		$("a.large").removeClass('selected');
		$(this).addClass('selected');
	});

	$("a.large").click(function()
	{
		$('body').css('font-size','0.9em'); 
		set_cookie('page_size', '0.9', 30);
		$("a.small").removeClass('selected');
		$("a.medium").removeClass('selected');
		$(this).addClass('selected');
	});
	
	$('div.search input.submit').hover(
		function() {
			$(this).addClass('hover'); 
		}, function() {
			$(this).removeClass('hover');
		}
	);	
	$('ul.second-menu li').hover(
		function() 
		{
			$(this).addClass('hover'); 
		}, function() 
		{
			$(this).removeClass('hover');
		}
	);
  	$('div.right-col div.box ul li a').hover(
		function() {
			$(this).parent().addClass('hover'); 
		}, function() {
			$(this).parent().removeClass('hover');
		}
	);
 	$('div.left-col div.box ul li a').hover(
		function() {
			$(this).parent().addClass('hover'); 
		}, function() {
			$(this).parent().removeClass('hover');
		}
	);
	$('p.button').hover(
		function() {
			$(this).addClass('hover'); 
		}, function() {
			$(this).removeClass('hover');
		}
	);
	$('ul.third-menu li a').hover(
		function() {
			$(this).parent().addClass('hover'); 
		}, function() {
			$(this).parent().removeClass('hover');
		}
	);
	$('body.contact div.content div.left-col fieldset dl.big-row dd input#sendcomment').hover(
		function() 
		{
			$(this).addClass('hover');
		}, function() 
		{
			$(this).removeClass('hover');
		}
	);

	if(get_cookie("page_size") == "0")
	{
	    $("head").append('<style>');
	    $("head").append('body');
	    $("head").append('{');
	    $("head").append('	font-size:62.5%;');
	    $("head").append('}');
	    $("head").append('</style>')
		$("a.small").addClass('selected');
	}
	else if(get_cookie("page_size") != null)
	{
	    $("head").append('<style> body {font-size:'+ get_cookie("page_size") + 'em;}</style>')
		if(get_cookie("page_size") == "0.7")
		{
			$("a.medium").addClass('selected');
		}	
		else if(get_cookie("page_size") == "0.9")
		{
			$("a.large").addClass('selected');
		}
	}
	else
	{
		$("head").append('<style>');
	    $("head").append('body{');
	    $("head").append('font-size: 62.5em;');
	    $("head").append('}');
	    $("head").append('</style>')
		$("a.small").addClass('selected');
	}


	
});
	
function set_cookie(name,value,days) 
{
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function get_cookie(name) 
{
    var name_eq = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(name_eq) == 0) return c.substring(name_eq.length,c.length);
    }
    return null;
}
function setImage(thisImage,i) {
  	//Get img-tag reference and attributes
	var imgVar = thisImage;
	if(!imgVar.hasClass('noframe'))
	{
		var widthVar = imgVar.attr('width')+10;
		var heightVar = imgVar.attr('height')+10;
		var urlVar = imgVar.attr('src');
		var clrCheck = 	$('div.content ').css('background-color');
	
		if($.isRGBColor(clrCheck) == true)
		{
			//var clr =  new Array();
			/*var test = $('div.content ').css('background-color').replace("rgb(").replace(")");
	
			alert(test);
			clr = test.split(',');*/
		 	var clr = clrCheck.split("(")[1];
			clr = clr.split(")")[0];
			var colour = [clr.split((","))[0],clr.split((","))[1],clr.split((","))[2]];

		    function rgb2hex(rgb) 
			{
				function hex(x) 
				{
					hexDigits = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8","9", "A", "B", "C", "D", "E", "F");
					return isNaN(x) ? "00" : hexDigits[(x - x % 16) / 16] + hexDigits[x	% 16];
				}
		    	return "0x" + hex(rgb[0]) + hex(rgb[1]) + hex(rgb[2]);
		    }
	
			//Fetches color
			var colorVar = rgb2hex(colour);
	
		}
		else
		{
			var colorVar = "0x"+ clrCheck.split("#")[1];
		}

		var flashvars = {};
		flashvars.imageUrl = urlVar;
		flashvars.imageClr = colorVar;
		var params = {};
		params.play = "true";
		params.loop = "false";
		params.menu = "false";
		params.quality = "high";
		params.scale = "noscale";
		params.wmode = "window";
		params.bgcolor = "#ffffff";
		params.devicefont = "false";
		params.swliveconnect = "false";
		params.allowfullscreen = "false";
		params.allowscriptaccess = "sameDomain";
	
		//Add a div
		$("<div style=\"width:"+widthVar+"\;height:"+heightVar+";\" width=\""+widthVar+"\" height=\""+heightVar+"\"id=\"img"+i+"\"><img src=\""+urlVar+"\" alt=\"Image\"/></div>").insertAfter(imgVar);
		//imgVar.css("display", "none");  
		imgVar.addClass('hide');
	
		swfobject.embedSWF("/flash/imageFrame.swf", "img"+i, widthVar, heightVar, "9.0.0", false, flashvars,params);
		//alert(imgVar.attr("src"));
	}
};