//global variables
var path = location.pathname;

$(document).ready(function(){
	rotateBanners();

	//IE6 transparent .png fix
	$(document).pngFix();

	//rounded corners
	$("#searchBox").corner();

	//SEARCH
	$("#searchButton").click(function(e){
		e.stopPropagation();
		$("#searchForm").submit();
	});
	$("#searchWhat, #searchWhere").keypress(function (e){
		if(e.which == 13){
			e.stopPropagation();
			$("#searchForm").submit();
			return false;
		}
	});
	//SEARCH

	//Function for striping lines
	$("div[class^='scfContactLine'], div[class^='mpfPhoneLine']").each(function(x){
		var cls = $(this).attr("class");
		cls = cls.split(" ");
		if(x%2 == 0){
			$(this).removeClass(cls[0]).addClass(cls[0] + "A");
		}
		else if(x%2 > 0){
			$(this).removeClass(cls[0]).addClass(cls[0] + "B");
		}
	});

	/*$("#top").click(function(e){
		e.stopPropagation();
		document.location.href = "http://www.nrespons.se/";
	});*/
});

function populateElement(selector, defvalue){
	if($.trim($(selector).val()) == ""){
		$(selector).val(defvalue);
	}

	$(selector).focus(function(){
		if($(selector).val() == defvalue){
			$(selector).val("");
			$(selector).toggleClass("exampleInputText");
		}
	});

	$(selector).blur(function(){
		if($.trim($(selector).val()) == ""){
			$(selector).val(defvalue);
			$(selector).toggleClass("exampleInputText");
		}
	});
}

function isValidEmail(mailStr){
	var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
	return emailPattern.test(mailStr);
}

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=' + expires + 'path=/;domain=.www.operatorone.eu';
//	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,"",-2);
}

function xmlDecode(str){
	var str;
	str = str.replace(/&amp;/g, "&");
	str = str.replace(/&lt;/g, "<");
	str = str.replace(/&gt;/g, ">");
	str = str.replace(/&apos;/g, "\'");
	str = str.replace(/&quot;/g, "\"");
	return str;
}
var w3c, ns4, ie4, currentLayer, currentOject, lastObjectColor, objMove;

function mainInit(){
	getBrowser();
	window.onresize = calcMainDivPos;
	calcMainDivPos();
}

function getBrowser(){
	ie4 = (document.all)?true:false;           
	ns4 = (document.layers)?true:false;        
	w3c = (document.getElementById)?true:false;  
}

function swapLayer(layer){
	if(currentLayer){
		if(w3c){
			document.getElementById(currentLayer).style.visibility = 'hidden';
		}else if(ie4){
			document.all[currentLayer].style.visibility = 'hidden';
		}else if(ns4){
			document.layers[currentLayer].visibility = 'hidden';
		}
	}

	if(layer){
		if(w3c){
			document.getElementById(layer).style.visibility = 'visible';
		}else if(ie4){
			document.all[layer].style.visibility = 'visible';
		}else if(ns4){
			document.layers[layer].visibility = 'visible';
		}
	}
	currentLayer=layer;
}

function changeBgColor(object){
	if(currentOject){
		if(w3c){
			document.getElementById(currentOject).style.backgroundColor=lastObjectColor;
		}else if(ie4){
			document.all[currentOject].style.backgroundColor=lastObjectColor;
		}else if(ns4){
			document.layers[currentOject].style.backgroundColor=lastObjectColor;
		}
	}

	if(object){
		if(w3c){
			lastObjectColor = document.getElementById(object).style.backgroundColor;
			document.getElementById(object).style.backgroundColor = '#FFFFFF';
		}else if(ie4){
			lastObjectColor = document.all[object].style.backgroundColor;
			document.all[object].style.backgroundColor = '#FFFFFF';
		}else if(ns4){
			lastObjectColor = document.layers[object].style.backgroundColor;
			document.layers[object].style.backgroundColor = '#FFFFFF';
		}
	}
	currentOject=object;
}   

function goPost(uri){
	if(w3c){
		document.getElementById('navform').action = uri;
		document.getElementById('navform').submit();
	}else if(ie4){
		document.forms.navform.action = uri
		document.forms.navform.submit();
	}else if(ns4){
		document.forms["navform"].action = uri
		document.forms["navform"].submit();
	}
}

function go(uri){
	window.location = uri + "/";
}

function calcMainDivPos(){
	if(ie4 || ns4 || w3c){
		windowHeight = (ie4)?document.body.clientHeight:window.innerHeight;
		windowWidth = (ie4)?document.body.clientWidth:window.innerWidth;
		searchHelpTop = windowHeight/2 - 162;
		searchHelpLeft = windowWidth/2 + 187;
		posObj('searchHelpArea',searchHelpLeft,searchHelpTop);
	}
}

function posObj(objName,objLeft,objTop){
	if(w3c){
		objMove = document.getElementById(objName).style;
	}else if(ie4){
		objMove = document.all[objName].style;
	}else if(ns4){
		objMove = document.layers[objName];
	}
	objMove.left = objLeft; objMove.top = objTop;
}

function getRichEditText(){
	var form = document.forms.postForm;
	form.headlineText.value = document.all.text1.getHTML(); 
	form.preambleText.value = document.all.text2.getHTML(); 
	form.bodyText.value     = document.all.text3.getHTML(); 
}

function confirmUrl(uri, message){
	var is_confirmed = confirm(message);
	if(is_confirmed){
		window.location = uri;
		return 1;
	}
}

function openPopUpWindow(url, name, width, height){
	window.open(url,name,'left=60,top=60,width='+width+',height='+height+',toolbar=0,resizable=0,location=0,directories=0,status=0,menubar=0,scrollbars=1');
}

function openPopUpWindow2(url, name, width, height){
	window.open(url,name,'left=60,top=60,width='+width+',height='+height+',toolbar=0,resizable=1,location=0,directories=0,status=0,menubar=0,scrollbars=1');
}

function changeFrame(frame, uri){
	if(w3c){
		top.frames[frame].location.href = uri;
	}else if(ie4){
		parent[frame].location.href = uri;
	}else if(ns4){
		top.frames[frame].location = uri;
	}
}
var selected, prevBgColor, prevColor;
function chSelected(el, bgColor, color){
	if(selected){
		selected.style.backgroundColor=prevBgColor;
		selected.style.color=prevColor;
	}
	if(el){
		prevBgColor = el.style.backgroundColor;
		el.style.backgroundColor=bgColor;
		prevColor = el.style.color;
		el.style.color=color;
	}
	selected=el;
}
function rotateBanners(){
	var impulses = new Array(
		//{alt:'STH-ARN 20 MINUTER', url:'http://www.sj.se/', src:'/images/wideBanners/368249.gif'},
		{alt:'CALLECAT', url:'http://www.callecat.com/', src:'/images/wideBanners/callecat980x120.gif'});
	for(var i = Math.round(Math.random() * impulses.length); i > 0; i--){
		impulses.push(impulses.shift());
	}
	while(impulses.length > 1){
		impulses.splice(Math.round(Math.random() * (impulses.length - 1)), 1);
	}
	for(var i = 0; i < impulses.length; i++){
		impulses[i] = '<a href="'+impulses[i].url+'"><img alt="'+impulses[i].alt+'" src="'+impulses[i].src+'" /></a>';
	}
	document.getElementById("bottomBanner").innerHTML = impulses.join('');
}