// a few variables for the following scripts to use
var imagePath = '/laqwww/common/images/rotatingImages/header';
var imageExt = ".jpg";
var totalImageCount = 5; 
var delay = 8000; 		// 1000 = 1 second delay between image swaps
var currentImageNumber = Math.floor(Math.random() * totalImageCount) + 1; // generate a random image to start on
var startFadeOut = false; // don't fad out first image, let all images load


// preload each of the images required for the rotating images script
function preloadImages(){
	for(var i=0;i<=totalImageCount;i++) {
		imageURL = imagePath + i + imageExt;
		MM_preloadImages(imageURL);
	}
}

// rotate the images
function rotateImages(){
	var animationDiv = document.getElementById('header');
	//if (startFadeOut == true) fadeOut(animationDiv);  
	
	startFadeOut = true; // 
	
	fileName = imagePath + currentImageNumber + imageExt;
	
	animationDiv.style.backgroundImage = 'url(' + fileName + ')';

	//if (document.all) {	// if IE then fade pics 
		//setTimeout('fadeIn(animationDiv)', delay);
		
		if (currentImageNumber == totalImageCount) {
			currentImageNumber = 1
		} else {
			currentImageNumber = currentImageNumber + 1
		}
	
		setTimeout('rotateImages()', delay);	
	//} else { // set randon image as background in header in FireFox et al. Tools div also needs resizing
	//}
}

// fade out an object (div or image)
function fadeOut(obj) {
    obj.style.filter="blendTrans(duration=2)";
	// Make sure filter is not playing.
	if ((obj.visibility != "hidden") && (obj.filters.blendTrans.status != 2)) {
        obj.filters.blendTrans.Apply();
        obj.style.visibility="hidden";
	    obj.filters.blendTrans.Play();
	}
}

// fade in an object
function fadeIn(obj) {
	 obj.style.filter="blendTrans(duration=2)";
	// Make sure filter is not playing.
	if ((obj.visibility != "visible") && (obj.filters.blendTrans.status != 2)) {
      obj.filters.blendTrans.Apply();
      obj.style.visibility="visible";
	  obj.filters.blendTrans.Play();
	}
}

// Dreamweaver stock-standard preload images script
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];}}
}

// if the length of the content area is greater than the window heigh, the top link is shown. This functions is also called onResize, so it can recalculate if the top link should be shown or not
function showTopLink() {
	if (document.getElementById('content').offsetHeight < document.documentElement.clientHeight) {
		document.getElementById('topLink').style.display = 'none'
	} else {
		document.getElementById('topLink').style.display = 'block'
	}
}

// if the www site isn't in an edit mode, the page is initialised. Otherwise the edit graphic is shown in the header.
function initialisePage(showRotatingImages) {	
	var animationDiv = document.getElementById('header');
	
	currentURL = document.location.href.toLowerCase();
	
	if (currentURL.indexOf('wbcmode') < 0) {
		preloadImages(); // load images required for rotating images
		checkForCookie() // check for font size cookie
		
		if (showRotatingImages) rotateImages(); // start rotating images
		
		showTopLink(); // show top link if required
		
		// make adjustments for browsers
		var browserInfo = navigator.appName.toLowerCase() + ' ' + navigator.appVersion.toLowerCase();
		
		if (browserInfo.indexOf('netscape') > -1) { // For firefox etc
			animationDiv.style.backgroundPosition = '0px -1px';
			animationDiv.style.border = '1px solid white';

			changeCSS('div.treeNav .treeNavExpand', 'width', '19px');
			changeCSS('div.treeNav .treeNavCollapse', 'width', '19px');
			changeCSS('#feature .dottedBorder ul', 'marginLeft', '-1.5em');
			changeCSS('#nav2 .dottedBorder ul', 'marginLeft', '-1.5em');
		}
		if (browserInfo.indexOf('msie 7') > -1) { // For MS IE 7 etc
			changeCSS('DIV.treeNav .treeNavExpand', 'width', '19px');
			changeCSS('DIV.treeNav .treeNavCollapse', 'width', '19px');
		}
	} else { // Display image for Edit mode
		fileName = imagePath + 'Edit.gif';
		animationDiv.style.backgroundImage = 'url(' + fileName + ')';
	}	
	
}


function changeCSS(theClass,element,value) {
//documentation for this script at http://www.shawnolson.net/a/503/
	var cssRules;
	
	if (document.all) {
		cssRules = 'rules';
	} else if (document.getElementById) {
		cssRules = 'cssRules';
	}

	for (var S = 0; S < document.styleSheets.length; S++){
		for (var R = 0; R < document.styleSheets[S][cssRules].length; R++) {
			//alert(R + ': ' + document.styleSheets[S][cssRules][R].selectorText);
			if (document.styleSheets[S][cssRules][R].selectorText == theClass) {
				//alert(document.styleSheets[S][cssRules][R].selectorText + ' ' + document.styleSheets[S][cssRules][R].style[element]);
				document.styleSheets[S][cssRules][R].style[element] = value;
			}
		}
	}	
}
