
//jQuery onReady
var $jq = jQuery.noConflict();
$jq(document).ready(function($){

//--open case in lightbox
$('.col2 .twocol .second a').each(function(){ 
	$(this).attr("rel","lightbox");
});

$('.tx-tab-panel-wrap .news_list_item a').each(function(){
	$(this).attr("rel","lightbox");
});

//--wrap tabs med extra markup
$('.tx-tab-bar a').each(function(){
	$(this).wrapInner('<span><em></em></span>');
});
$('.tx-tab-pi1').wrapInner('<div class="holder">');

//--Insert title in film tabs
$('.aside .tx-tab-pi1 .tx-tab-bar ul').prepend('<li><h3>Produkt film:</h3></li>');

//--Insert title in film tabs
$('.products .tx-tab-pi1 .holder').prepend('<h3>Produkt film:</h3>');

//----
// Accesskeys
//----

$('#accesskeys a').focus(function(){
		$('#accesskeys').attr("style","left: 10px; z-index:999;");
});
$('#accesskeys a').blur(function(){
		$('#accesskeys').attr("style","left: -9999px; z-index:0;");
});

//------
//Empty input fields on focus
//------
$("input:text").each(function(){ 
	var ov = $(this).val();
	$(this).focus(function(){
	var fv = $(this).val();
	if (fv == ov) {
		$(this).val('');
		}
	}); 
	
	$(this).blur(function(){
	var bv = $(this).val();
	if (bv.length < 1) {
		$(this).val(ov);
		}
	});
}); 
	
//------
// Vil du vide mere / FCE
//------

var map = $("#find_kontor>img");
var mapsrc = map.attr("src");
var moimg = $(".area>img").hide();

// Hide all areas
$(".area").hide();

// Take alle links from map and make a dropdown
$("#find_kontor").prepend("<select>");
$("#find_kontor select").append("<option>- Vælg -</option>");

$("area").each(function(i){
	var area = $(this);
	var link = area.attr("href");
	var name = area.attr("alt");

	$("#find_kontor select").append("<option value='"+ link +"'>"+ name +"</option>");
			
	// Mouseover on the map
	area.hover(function(){
		map.attr("src",moimg.filter(":eq("+i+")").attr("src"));
	},function(){
		map.attr("src",mapsrc);
	});
	
	// Click on area, call function
	area.click(function(){
		showRegion(i+1);
		return false;
	});
});

// Click on option value in select, call function
$("#find_kontor select").change(function(){
	var Index = $("#find_kontor select option").index($(this).children("option:selected"));
	showRegion(Index);
	return false;
	
});
/*$("#find_kontor option").each(function(i){
	$(this).click(function(){
		showRegion(i);
		alert(i);
		return false;
	});
});*/

// On click, show content and hide map
var link = $(".submit_button a[href^='book']").attr("href");
function showRegion(i){

	if(i>0){
		map.hide();
		$(".area").hide().removeClass("activearea");
		$(".area").filter(":eq("+(i-1)+")").show().addClass("activearea");
		$("#find_kontor option").filter(":eq("+(i)+")").attr("selected","selected");
		
		// Update submit button
		var person = $(".activearea .person").attr("id");
		var personNo = person.substring(7);
		var newlink = link + '?person=' + personNo;
		$(".submit_button a[href^='book']").attr("href",newlink);
	} else {
		map.show();
		$(".area").hide();
		$(".submit_button a[href^='book']").attr("href",link);
	}	
		
}

// Take value from id and add to link, wrap persons in link

$(".areacontent .person").each(function(){
	var person = $(this);
	
	var personNo = person.attr("id").substring(7); // Get number from FCE, ex: 1
	var link = person.children("a").attr("href");
	
	link = link + '?person=' + personNo;
	
	person.children("a").attr("href",link);
	person.children(".persontext").wrapInner("<a rel='lightbox' href='"+link+"'></a>")
	
});

//----
// Cases, thumbnails
//----

var imgs = new Array;

if($(".news-single-img img").length > 0){
	imgs = $(".news-single-img img");
}

if(imgs.length > 0){
var firstImg = imgs.filter(":eq(0)");
var firstP = $(".news-single-img p:eq(0)");
}

if(imgs.length > 1){
	imgs.filter(":gt(0)").addClass("thumbnail").removeAttr("height").removeAttr("width");
	firstImg.clone().insertAfter(firstP).addClass("thumbnail").removeAttr("height").removeAttr("width").addClass("act_img");
	$(".news-single-img p:gt(0)").hide();
		
	$(".thumbnail").each(function(){
		$(this).mouseover(function(){
			// Switch image src
			var imgsrc = $(this).attr("src");
			firstImg.attr("src",imgsrc);
			// Switch tekst
			var txt = $(this).attr("title");
			firstP.text(txt);			
			// Give active class
			$("img.thumbnail").removeClass("act_img");
			$(this).addClass("act_img");
		});
	});
}


//----
// Accordion effekt til jobopslag
//----
	
		$('div.jobopslag_beskrivelse').hide();
		$('a.jobopslag_stilling').click(function()
		{
			$(this).parents('div.jobopslag_container').children('div.jobopslag_beskrivelse').slideToggle('fast');			
		});
			
// End Accordion effekt til jobopslag


}); // End jQuery onReady


// cookie functions http://www.quirksmode.org/js/cookies.html

function createCookie(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 readCookie(name){
	var nameEQ = 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(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name){
	createCookie(name,"",-1);
}

// cookie functions end

