// Javascript Document //
var htmlurl = 'http://www.lfcc.edu/';

function PromoajaxFunction(dowhat) {
	var newDate = new Date;
	var si = newDate.getTime();
	dowhat = './homepage-rotate-promos.php?'+dowhat+'&si='+newDate;

	var xmlHttp;
	try {
	// Firefox, Opera 8.0+, Safari
	xmlHttp=new XMLHttpRequest();
	}
	catch (e) {
	// Internet Explorer
		try {
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
			alert("Your browser does not support AJAX!");
			return false;
			}
		}
	}
	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			var myArr = xmlHttp.responseText.split('<!---->');
			togglePromo(myArr);
			if(ie6) { document.getElementById("promoshade").style.marginTop = '-137px'; }
		}
		else {
			if(document.getElementById("promoforeimg") && (document.getElementById("promoforeimg").src === undefined || document.getElementById("promoforeimg").src !== gimgurl+"solid-bg-ajax-loader.gif")) {
				//alert(document.getElementById("promoforeimg").src);
				var myLoad = document.createElement('img');
				myLoad.src = gimgurl+"solid-bg-ajax-loader.gif";
				myLoad.id = "promoforeimg";
				myLoad.alt = "The new item is loading.";
				document.getElementById("promoforeimg").parentNode.replaceChild(myLoad,document.getElementById("promoforeimg"));
			}
			else if(document.getElementById("promofore-embed")) {
				var myLoad = document.createElement('img');
				myLoad.src = gimgurl+"solid-bg-ajax-loader.gif";
				myLoad.id = "promoforeimg";
				myLoad.alt = "The new item is loading.";
				document.getElementById("promofore-embed").parentNode.replaceChild(myLoad,document.getElementById("promofore-embed"));
			}
		}
	}
	xmlHttp.open("GET",dowhat,true);
	xmlHttp.send(null);
	return true;
}

function goPromo(what) {
	var wh = what.href;
	var wa = wh.split('?');
	wh = wa.pop();
	//alert(what);
	PromoajaxFunction(wh);
	var pN = what.parentNode.parentNode;
	for(i in pN.childNodes) {
		if(pN.childNodes[i] !== undefined && pN.childNodes[i].tagName !== undefined && pN.childNodes[i].tagName.toLowerCase() == 'div') {
			pN.childNodes[i].className = 'SlideItMoo_element';
		}
	}
	what.parentNode.className = 'active-item SlideItMoo_element';
	return false;
}

function togglePromo(myArr){

	var prBack = myArr[0]; // Source of background image
	var prBackSize = myArr[1]; // Size of background image
	var prFore = myArr[2]; // Source of foreground image
	var prForeSize = myArr[3]; // Size of foreground image
	var prHeader = myArr[4]; // Title of promo item
	var prText = myArr[5]; // Body text of promo item
	var prLink = myArr[7]; // Link to promo item information
//	alert(prBack);
	
	var myImg = new Image();
	myImg.onload = function() {

		var myReSt = 'img src="([^"]+?)"';
		var myReLn = prFore;
		var rE = new RegExp(myReSt,"gim");
		//alert('myReLn is equal to '+myReLn);
		var m = rE.exec(myReLn); 
		if(m != null && m.length == 2) {
			//alert('We found a match');
			//var foreImgSrc = myReLn.replace(rE,"$1");
			var foreImgSrc = m[1];
			var foreImg = new Image();
			foreImg.onload = function() {
//				alert('We are loading an image');
				document.getElementById("main").style.background = '';
				document.getElementById("main").style.background = 'url('+myImg.src+')';
				/*if(prLink != undefined) {
					document.getElementById("main").onclick = function() { window.location.href=prLink; }
				}*/
				document.getElementById("promofore-inner").innerHTML = prFore;
				document.getElementById("promoheader").innerHTML = prHeader;
				document.getElementById("homepage-promotext").innerHTML = prText;
			}
			foreImg.src = foreImgSrc;
		}
		else {
			document.getElementById("main").style.backgroundImage = 'url('+myImg.src+')';
			/*if(prLink != undefined) {
				document.getElementById("main").onclick = function() { window.location.href=prLink; }
			}*/
			document.getElementById("promofore-inner").innerHTML = prFore;
			document.getElementById("promoheader").innerHTML = prHeader;
			document.getElementById("homepage-promotext").innerHTML = prText;
		}
	}
	myImg.src = prBack.replace(htmlurl,'');

/*
    //stop the current animation if it is running.
    if($("div1").fx){$("div1").fx.stop();}
    if($("div2").fx){$("div2").fx.stop();}
    //Decide which div to hide and which to show.
    if(visibleDiv == $("div1")){
        //change the hidden image's source
        $("image2").src = newSrc;
        //fade the visible out and the hidden in.
        $("div1").fx = new Fx.Style($("div1"), 'opacity', {duration: 2000}).start(0);
        $("div2").fx = new Fx.Style($("div2"), 'opacity', {duration: 2000}).start(1);
        //Set which div is visible.
        visibleDiv = $("div2");
    }else{
        //change the hidden image's source
        $("image1").src = newSrc;
        //fade the visible out and the hidden in.
        $("div1").fx = new Fx.Style($("div1"), 'opacity', {duration: 2000}).start(1);
        $("div2").fx = new Fx.Style($("div2"), 'opacity', {duration: 2000}).start(0);
        //Set which div is visible
        visibleDiv = $("div1");
    }
*/
}

function rearrangePromos(what) {
	if(document.getElementById("SlideItMoo_inner"))
		document.getElementById("SlideItMoo_inner").style.paddingLeft = "0";
	var pN = what.parentNode.parentNode; // Should return the SlideItMoo_items element
	var cN = pN.childNodes; // Should return the list of SlideItMoo_element elements
	var prev = Array(); // Will hold the list of elements that originally appeared before our "active" item
	var next = Array(); // Will hold the list of elements that originially appeared after out "active" item
	var curr = 0; // Will hold the element that is currently "active" 
	for(i in cN) { // Loop through all of our SlideItMoo_element elements
		if(cN[i].tagName !== undefined && cN[i].tagName.toLowerCase() == 'div') { // Check to make sure this is a div
			if(cN[i].className == 'SlideItMoo_element right-promo-list' || cN[i].className == 'SlideItMoo_element left-promo-list') {
				cN[i].className = 'SlideItMoo_element';
			}
			if(cN[i] == what.parentNode) { // Check to see if the current element is the "active" element
				curr = cN[i];
			}
			else if (curr == 0) { // If we have not yet set the "active" element, this element must come before
				prev.push(cN[i]);
			}
			else { // If we already set the "active" element, this element must come after
				next.push(cN[i]);
			}
		}
	}

	while(cN.length > 0) { // Loop through all of the SlideItMoo_element elements and remove them from the DOM
		pN.removeChild(cN[0]);
	}

	pN.appendChild(curr); // Add the "active" element back into the DOM as the first one
	if(next.length > 0) {
		for(i in next) {
			if(next[i].tagName !== undefined && next[i].tagName.toLowerCase() == 'div') {
				pN.appendChild(next[i]);
			}
		}
	}
	if(prev.length > 0) {
		for(i in prev) {
			if(prev[i].tagName !== undefined && prev[i].tagName.toLowerCase() == 'div') {
				pN.appendChild(prev[i]);
			}
		}
	}
}
