//// ROLLOVER FUNCTION ////
function roll(img_id, img_src){
	document.getElementById(img_id).src = img_src;
}

//// CHANGE MENU CLASS ////
var last_bar = null;

function change(id){	
	if(last_bar != null) {
		document.getElementById(last_bar).setAttribute('class', 'off');
		document.getElementById(last_bar).setAttribute('className', 'off');
	}
	
	document.getElementById(id).setAttribute('class', 'on');
	document.getElementById(id).setAttribute('className', 'on');
	last_bar = id;
}

//// PRELOAD IMAGES ////

function isIE(){
  return /msie/i.test(navigator.userAgent);
}

function loadImg(){

	if(isIE() == false){
		img_aktiviteter = new Image();
		img_aktiviteter.src = "wp-content/themes/fsf/img/menu1b.png";
		
		img_forskning = new Image();
		img_forskning.src = "wp-content/themes/fsf/img/menu2b.png";
		
		img_natverk = new Image();
		img_natverk.src = "wp-content/themes/fsf/img/menu3b.png";
		
		img_publikationer = new Image();
		img_publikationer.src = "wp-content/themes/fsf/img/menu4b.png";
		
		img_priser = new Image();
		img_priser.src = "wp-content/themes/fsf/img/menu5b.png";
		
		img_media = new Image();
		img_media.src = "wp-content/themes/fsf/img/menu6b.png";
		
		img_om = new Image();
		img_om.src = "wp-content/themes/fsf/img/menu7b.png";
	}
}

///////// FORM VALIDATION /////////
function validate_required(value,alertxt){
	if(value == null || value == ""){
		//alert(alertxt);
		return alertxt;
	}else{
		return "";
	}
}

function validate_email(value,alertxt){
	apos = value.lastIndexOf("@");
	dotpos = value.lastIndexOf(".");
		
	if(apos < 1 || dotpos-apos < 2 || value == "" ||  value == null){
		//alert(alertxt);
		return alertxt;
	}else{
		return "";
	}
}

function validate_form(thisform){
	var name = document.getElementById("wpnewsletter_name");
	var email = document.getElementById("wpnewsletter_email");
	var code = document.getElementById("security_code");
	var hidden_code = document.getElementById("harry");
	
	var code_string =  hidden_code.value;
	var broken_code = code_string.split("|");
	
	var errors = new Array();
	
	if(validate_required(name.value," - Du måste fylla i ditt namn!") != ""){
		errors[errors.length] = validate_required(name.value," - Du måste fylla i ditt namn!");
	}

	if(validate_email(email.value," - Felaktig e-mailadress!") != ""){
		errors[errors.length] = validate_email(email.value," - Felaktig e-mailadress!");
	}
	
	if(code.value != broken_code[1]){
		errors[errors.length] = " - Felaktig säkerhetskod!";
	}	
		
	if(errors.length > 0) {
		// loopa igenom errors och lägg dem i en alert
		var alert_text = "";
			
		for(var i = 0; i < errors.length; i++)
			alert_text += errors[i]+"\n\n";
				
		alert("Det uppkom ett fel när formuläret skickades:\n\n"+alert_text);
			
		return false;
	}else{
		alert("Tack för att du registrerade dig för vår nyhetsbrev! Ett aktiveringsmail har skickats till dig. Var noga med att kolla din skräppost!")
	}
}


////////     JQUERY POPUPS      ////////
var popupStatus = 0;

function loadPopup(id){
	if(popupStatus == 0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		
		$("#backgroundPopup").fadeIn("slow");
		
		$(id).fadeIn("slow");
		
		popupStatus = 1;
	}
}

function centerPopup(id){
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $(id).height();
	var popupWidth = $(id).width();

	$(id).css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
}

function disablePopup(){
	if(popupStatus == 1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popup").fadeOut("slow");
		popupStatus = 0;
	}
}

$(document).ready(function(){
	
	$("#newsletter").click(function(){
		centerPopup('#popup');
		loadPopup('#popup');
	});
	
	$(".close").click(function(){
		disablePopup();
	});
	
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	
	$(document).keypress(function(e){
		if(e.keyCode == 27 && popupStatus == 1){
			disablePopup();
		}
	});
});
