// JavaScript Document
init = function(){
	startList();
	//Check if secound navigation exist, if so change the height on maincontet.
	if (document.getElementById('secoundnav'))
		if (document.getElementById('maincontent'))
			document.getElementById('maincontent').style.height='95.3%';

	//Handle the second level navigation hover and active link issue
	setSecondLevelNav()
	
	//Open active menu and mark the active link, only in IE
	if (navigator.appName.indexOf("Microsoft")>-1) zonalHandle()
	
	//Init the top menu. Activate top navigation items by looking for patterns in current page path
	
}
window.onload=init;

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

/* This is for the first navigation (top) */
function startList() {
	if (document.all&&document.getElementById) {
		navRoot = document.getElementById("nav");
		if (navRoot!=null){
			for (i=0; i<navRoot.childNodes.length; i++) {
				node = navRoot.childNodes[i];
				if (node.nodeName=="LI") {
					node.onmouseover=function() {
						this.className+=" over";
					}
					node.onmouseout=function() {
						this.className=this.className.replace(" over", "");
					}
				}
			}
		}
		navRoot = document.getElementById("secnav");
		if (navRoot!=null){
			for (i=0; i<navRoot.childNodes.length; i++) {
				node = navRoot.childNodes[i];
				if (node.nodeName=="LI") {
					node.onmouseover=function() {
						this.className+=" over";
					}
					node.onmouseout=function() {
						this.className=this.className.replace(" over", "");
					}
				}
			}
		}
	}
}


function checkBtn(pattern, btnId, activeImg){
	/*
	Activate top navigation items by looking for patterns in current page path
	pattern = The pattern in the path to look for
	btnId = the id of the menu item activate
	activeImg = path to the image to set on active
	*/
	if (location.pathname.indexOf(pattern)>0)document.getElementById(btnId).src=activeImg
}

function setSecondLevelNav(){
	//Handle the secon level navigation hover and active link issue
	var hoverColor, activeColor
	hoverColor=activeColor="#B5B5B5" //Here's the color to use
	if (document.getElementById("secnav")){ //If the secnav don't exist don't do anything
		oSN=document.getElementById("secnav")
		
		oLinks=oSN.getElementsByTagName("A") //Get all links in the div
		for (i = 0; i <= oLinks.length-1; i++){
			if (oLinks[i].href==location.href) //Set to active color if the link is the same as current page
				oLinks[i].style.color=activeColor
		}
		
		//Add some events to all spans in the secnav div
		oSPAN=oSN.getElementsByTagName("SPAN")
		for (i = 0; i <= oSPAN.length-1; i++){
			oSPAN[i].style.cursor="default" //Set to deafult, the arrow, cursor in the text
			oSPAN[i].onmouseover = function(){
				this.style.color=hoverColor
			}
			oSPAN[i].onmouseout = function(){
				this.style.color=''
			}
		}
	}
}


function zonalHandle(){
	//Open active menu and mark the active link
	//This function do's only work in IE
	if (document.getElementById("zonal")){ //If the zonalnav don't exist don't do anything
		oZN=document.getElementById("zonal")
		for(i=0;i<oZN.children.length;i++){
			oUL=oZN.children[i].getElementsByTagName("UL")
			for(ULCount=0;ULCount<oUL.length;ULCount++){
				currentUL=oUL[ULCount]
				oLI=currentUL.getElementsByTagName("LI")
				for(liCount=0;liCount<oLI.length;liCount++){
					currentLI=oLI[liCount]
					oA=currentLI.getElementsByTagName("A")
					for(aCount=0;aCount<oA.length;aCount++){
						if(oA[aCount].href==location.href){
							showExtended(currentUL.id)
							oA[aCount].innerHTML="<strong>" + oA[aCount].innerHTML + "</strong>"
						}
					}
				}
			}
		}
	}
}



function replace(inStr, repStr, newStr){
	eval("re = \/"+repStr+"\/g")
	return inStr.replace(re, newStr)
}

		
		
		
function getCookieVal (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}
function GetCookie (name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return getCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break; 
}
return null;
}
function SetCookie (name, value) {
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}
function DeleteCookie(name) {
	var exp = new Date();
	FixCookieDate (exp);
	exp.setTime (exp.getTime() - 1);
	var cval = GetCookie (name);
	if (cval != null)
	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}


function showExtended(s)
{
	x=document.getElementById(s);
	ss='p' + s
	y=document.getElementById(ss);
	
	if (x.style.display=='none')
	{
	
		x.style.display    = 'block';
		y.style.background = "#FFF200 url(none) no-repeat 4px";
		x.style.background = "#FFF200 url(none) no-repeat 4px";
	}	
	else if (x.style.display=='block')
	{

		x.style.display='none';
		y.style.background="#FFF200 url(none) no-repeat 6px";
	}
	else
	{

		x.style.display='block';
		y.style.background="#FFF200 url(none) no-repeat 4px";
	}	
}

/*==== OPEN WINDOW */
function NewWindow(mypage,myname,w,h,scroll){
	var win = null;
	if (w > screen.width) { w = screen.width - 30;}
	if (h > screen.height) { h = screen.height - 120;}
	LeftPosition = (screen.width) ? (screen.width-w)/4 : 0;
	TopPosition  = (screen.height) ? (screen.height-h)/4 : 0;
	settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable=no';
	win = window.open(mypage,myname,settings);
}

/*=== POPUPWINDOW ======
Funktionen kan ta emot 5 parametrar: 
 - url som skall öppnas, kan vara html eller bilder
 - namn på popup
 - mode, har tre alternativ basic, full eller dev (utvecklaren skickar med engna alternativ som sträng (window.open)
 - width i pixlar (standard 800)
 - height i pixlar (standard 600)
*/
function PopupWindow(url_img,pName,pMode,pW,pH){

	var alreadyOpen = false;
	var pwidth  = 800;
	var pheight = 600;
	var popupWin;
	var popupName;
	var url_imgType = url_img.substring((url_img.lastIndexOf(".")+1),url_img.length).toUpperCase();
	
	if(pName == ""){
		popupName = 'Popup Window';
	}else{
		popupName = pName;
	}
	if(pW != ""){
		pwidth = pW;
	}
	if(pH != ""){
		pheight= pH;
	}
	if(pMode == ""){
		popupMode = 'basic';
	}else{
		popupMode = pMode;
	}
	
	// CENTER THE WINDOW
		if (pwidth > screen.width) { pwidth = screen.width - 30;}
		if (pheight > screen.height) { pheight = screen.height - 120;}
		LeftPosition = (screen.width) ? (screen.width-pwidth)/4 : 0;
		TopPosition  = (screen.height) ? (screen.height-pheight)/4 : 0;
		
		//var LeftPosition = (screen.width) ? (screen.width-pwidth)/2 : 0;
		//var TopPosition  = (screen.height) ? (screen.height-pheight)/2 : 0;
	
	// Adds +20 to the pwidht when it is a image because of the scrollbar
	if(url_imgType == "JPG" || url_imgType == "PNG" || url_imgType == "GIF"){
		pwidth  = (pwidth+20);
	}	

	// Window Mode
	if( (pMode != 'basic') && (pMode != 'full') && (pMode != 'none') ){
		var popupAttr = pMode + ",width=" + pwidth + ",height=" + pheight;
	}
	if(pMode == "basic"){
		var popupAttr = "menubar=yes,dependent=yes,resizable=yes,scrollbars=yes,top="+ TopPosition +",left="+ LeftPosition +",width="+ pwidth +",height="+ pheight;
	}
	if(pMode == "full"){
		var popupAttr = "toolbar=yes,statusbar=yes,menubar=yes,dependent=yes,location=yes,resizable=no,scrollbars=yes,top="+TopPosition+",left="+LeftPosition+",width="+pwidth+",height="+pheight;
	}
	if(pMode == "none"){
		var popupAttr = "toolbar=no,statusbar=no,menubar=no,dependent=no,location=no,resizable=no,scrollbars=no,top="+TopPosition+",left="+LeftPosition+",width="+pwidth+",height="+pheight;
	}
	if(pMode == " "){
		var popupAttr = "menubar=yes,dependent=yes,resizable=no,scrollbars=yes,top="+TopPosition+",left="+LeftPosition+",width="+pwidth+",height="+pheight;
	}
	
	// CHECKS IF IT IS A IMAGE OR A HTML FILE THAT WILL BE OPEN
	if(url_imgType == "JPG" || url_imgType == "PNG" || url_imgType == "GIF"){
			popupWin = window.open('', popupName, popupAttr);
			popupWin.document.writeln("<html><head><title>"+popupName+"</title></head>");
			popupWin.document.writeln("<body marginheight=0 leftmargin=0 topmargin=0>");
			popupWin.document.writeln("<img src='"+ url_img +"' width='"+ (pwidth-20) +"' height='"+ pheight +"' alt='"+popupName+"'>");
			popupWin.document.writeln("</body></html>");
			popupWin.document.close();
	}else{
	
		if(alreadyOpen){
			if (popupWin.closed){
				popupWin = window.open(url_img, popupName, popupAttr);
			}else{
				popupWin.close();
				popupWin = window.open(url_img, popupName, popupAttr);
			}
		}else {
			popupWin = window.open(url_img,popupName,popupAttr);
			alreadyOpen = true;
		}
	}
}
/*### PRINTER FRIENDLY SCRIPTS - LAYER 
Old functin call "function PrintThisPage(idLayer,repValue)" 
*/
function PrintThisPage(idLayer){ 
   var sOption="toolbar=yes,location=no,directories=yes,menubar=yes,"; 
       sOption+="scrollbars=yes,width=750,height=600,left=100,top=25"; 
	
   var sWinHTML = document.getElementById(""+idLayer+"").innerHTML;
   var repValue = document.getElementById("aPrint").innerHTML;
     
   var winprint = window.open("","",sOption); 
       winprint.document.open(); 
       winprint.document.write('<html><LINK href=kungsbacka_bmx_files/kungsbacka_bmx.css rel=Stylesheet type=text/css><LINK href=kungsbacka_bmx_files/mainnav.css rel=Stylesheet type=text/css>');
	   winprint.document.write('<LINK href=kungsbacka_bmx_files/secoundnav.css rel=Stylesheet type=text/css><LINK href=kungsbacka_bmx_files/zonalnav.css rel=Stylesheet type=text/css>');  
       winprint.document.write('<body leftmargin=10 onload=window.print();window.close()>'); 
       winprint.document.write(replace(sWinHTML,repValue,""));          
       winprint.document.write('</body></html>'); 
       winprint.document.close(); 
       winprint.focus(); 
}
