// JavaScript Document

function newWindow(theURL,winName,theStyle) {
  window.open(theURL,winName,theStyle);
}

/*
Name Type Description Example

width number Width, not including chrome (in pixels) width=640

height number Height, not including chrome (in pixels) height=480

left number Horizontal position wrt. screen (pixels) left=0

top number Vertical position wrt. screen (pixels) top=0

titlebar boolean Window has titlebar titlebar=no

menubar boolean Window has menubar menubar=yes

toolbar boolean Window has toolbar (e.g., back button) toolbar=yes

location boolean Window has location bar location=no

scrollbars boolean Window has scrollbars scrollbars=yes

status boolean Window has statusbar status=yes

resizable boolean Window can be resized resizable=no

*/


function selectNavBar(path) {
	
	//var url= window.parent.mainFrame.document.location.href;
	var nav="none";
	 	
	if((path=="")||(path==null)) {
		var url = document.location.href;	
		if (url.indexOf('/') != -1) {
		
			while (url.indexOf('/') != -1) {
				url = url.substring((url.indexOf('/')+1),url.length);
			}
			nav=url;
		}
	}
	else nav = path;
	
	 
	var select_nav = nav.substr(0,5);
	//var id = nav.substr(0,9);
	//alert("select_nav: "+select_nav);
	
	if(document.getElementById(select_nav)!=null) { 
	// check if it's a lyric page
		if(select_nav[0]=='l') {
			document.getElementById(select_nav).className="nav_lyric_selected";
		}
		else {
			document.getElementById(select_nav).className="nav_word_selected";
		}
	}
	else {
		
	 select_nav="sw_01";
	 document.getElementById(select_nav).className="nav_word_selected";
	}
}


function preloadImages(backgroundImageArray, offset) {
	
	var imgCount=10;
	var path="";
	
	if(offset==0) {
		for(i=0; i<imgCount; i++) {
			
			path = 'background/'+i+'.jpg';
			backgroundImageArray[i]=new Image;
			backgroundImageArray[i].src=path;
		}
	}
	else if(offset==99) {
	
	for(i=0; i<imgCount; i++) {
			
			path = 'background/'+offset+'.jpg';
			//alert(path);
			backgroundImageArray[i]=new Image;
			backgroundImageArray[i].src=path;
		}
	}
	else {
		for(i=0; i<imgCount; i++) {
			
			path = 'background/'+offset+i+'.jpg';
			//alert(path);
			backgroundImageArray[i]=new Image;
			backgroundImageArray[i].src=path;
		}
	}
}

 

function getCookie(name) {
	
	var search = name + "="
	var returnvalue = "";
	
		if (document.cookie.length > 0) {
			
			offset = document.cookie.indexOf(search);
			
			// if cookie exists
			if (offset != -1) { 
				offset += search.length;
				// set index of beginning of value
				end = document.cookie.indexOf(";", offset);
				// set index of end of cookie value
				if (end == -1) end = document.cookie.length;
				returnvalue=unescape(document.cookie.substring(offset, end))
			}
	}
	return returnvalue;
}

function loadBackGroundImage(backgroundImageArray) {
			
	var imgCount=10;
	var imgNumber;
	var path;
	var lastImageNumber=-1;
	
	var backgroundImage = document.getElementById('imgFrame');
	
	//store lastImageNumber in session cookie version
	//var tempNumber = getCookie("lin");
	
	//store lastImageNumber in window.name version
	var tempNumber = parseInt(window.name);
	
	//alert("tempNumber: "+tempNumber);
	 
	if(isNaN(tempNumber)==false) lastImageNumber=tempNumber;
	
	//alert("lastImageNumber: "+lastImageNumber);
	 
	imgNumber=Math.floor(Math.random()*imgCount);
	
	while(imgNumber==lastImageNumber) {
		
		imgNumber=Math.floor(Math.random()*imgCount);
	}
	// session cookie version store the value
	//document.cookie = 'lin=' + imgNumber;
	
	// window.name version store the value
	window.name = ""+imgNumber;
	
	
	backgroundImage.src = backgroundImageArray[imgNumber].src;

}
 


function sizeBackground() {
		
	var windowH;
	var windowW;
	var scaleFactor=1.0;
	var imageHeight;
	var imageWidth;
		
	var backgroundImage = document.getElementById('imgFrame');
	var container = document.documentElement;	
	
	//alert("path: " +backgroundImage.src);
	
	if(backgroundImage && container) {
		
	 
		if(backgroundImage.naturalHeight) imageHeight=backgroundImage.naturalHeight;
		else imageHeight=backgroundImage.height;
		
		if(backgroundImage.naturalWidth) imageWidth=backgroundImage.naturalWidth;
		else imageWidth=backgroundImage.width;
		 
		 
		if (container.innerHeight) windowH = container.innerHeight;
		else windowH = container.clientHeight;
		
		if (container.innerWidth) windowW = container.innerWidth;
		else windowW = container.clientWidth;
		
	//	alert("backgroundImage.complete: " +backgroundImage.complete+ "  windowW: " +windowW);
		
	// todo, make this a good value based on the site page layout bounds
	
		if(windowH<940) windowH=940;
		if(windowW<900) windowW=900;
	 
		  
		 if(windowW >= windowH) {  // window width is deciding scale factor
		 
			scaleFactor=windowW/imageWidth;

			// check that the scaled height will fill the window
			if((scaleFactor*imageHeight)<windowH) scaleFactor=windowH/imageHeight;
		 }
		 else {  // window height is decide scale factor
			
			scaleFactor=windowH/imageHeight;
		
			// check that the scaled width will fill the window
			if((scaleFactor*imageWidth)<windowW) scaleFactor=windowW/imageWidth;
		 }
		  
		 //alert("scaleFactor: " +scaleFactor);
		 
		 backgroundImage.height=scaleFactor*imageHeight;
		 backgroundImage.width=scaleFactor*imageWidth;
		
		 
		 
		 backgroundImage.style.visibility = 'visible';
	}
	
}





function addevent(obj,evt,fn,capt){

	if(obj.addEventListener){

		obj.addEventListener(evt, fn, capt);
		return true;
	}

	else if(obj.attachEvent){

		obj.attachEvent('on'+evt, fn);
		return true;
	}

	else return false;
}



/* now add the events to the event manager */


if (document.getElementById && document.getElementsByTagName){

	addevent(window, 'load', sizeBackground, false);
	addevent(window, 'resize', sizeBackground, false);
}



/************* dreamweaver javascript *************/



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];}

}


