// JavaScript Document

var InternetExplorer = navigator.appName=="Microsoft Internet Explorer";
var sImageRoot = "/img/Rotating%20Broker%20Logos/";
var arBrokerLogos = new Array(sImageRoot + "FreedomFinance.gif", sImageRoot + "Loanmakers.gif", sImageRoot + "CentralCapital.gif");
var iLogoDelay = 8000;
var divMainMessage;
var divBrokerMessage;
var arBrokerGraphics;

var iElementCount = 0;
var iLogoIndex = 0;
var bBrokerMessageShown = true;
var sQueue = "BrokerAnim";
var iDuration = 2;

RunAnimation(_TickAnimation_MainMessage, "Just some of the fine companies we work with:");
// new Array("Debts or CCJs", "Negative or low equity", "Self employed", "Arrears or credit card debits", "Results in 60 seconds")

function RunAnimation(sMainMessage, sBrokerMessage) {
	if ( typeof(sMainMessage) == "undefined" || sMainMessage == "")
		sMainMessage = "Homeowner loans at your fingertips!";
		
	var sAnimDiv = "AdvertDiv";
	var sFrame = "<div id='"+sAnimDiv+"' style=\"height:160px; width:202px;\"></div>";
	document.write(sFrame);
	
	divMainMessage = BuildTextPoint(sAnimDiv, sMainMessage);
	divBrokerMessage = BuildTextPoint(sAnimDiv, sBrokerMessage);
	arBrokerGraphics = BuildBrokerGraphicDivs(sAnimDiv, arBrokerLogos);
	
	setTimeout("BeginAnimation()", 8000);
}

function BeginAnimation() {
	if (bBrokerMessageShown) {
		new Effect.Appear(divBrokerMessage, {duration:iDuration, queue:{scope:sQueue, position:'end'}});	
	}
	new Effect.Appear(arBrokerGraphics[iLogoIndex], {duration:iDuration, queue:{scope:sQueue, position:'end'}});
	setTimeout("DoNextLogoImage()", iLogoDelay);
}

function DoNextLogoImage() {
	if (iLogoIndex < arBrokerGraphics.length) {
		new Effect.Fade(arBrokerGraphics[iLogoIndex], {duration:iDuration, queue:{scope:sQueue, position:'end'}, afterFinish:function(){
			iLogoIndex++;
			if (iLogoIndex < arBrokerGraphics.length) {
				new Effect.Appear(arBrokerGraphics[iLogoIndex], {duration:iDuration, queue:{scope:sQueue, position:'end'}});
				setTimeout("DoNextLogoImage()", iLogoDelay);
			} else {
				setTimeout("DoNextLogoImage()", 1000);
			}
		}
		});
	} else {
		if (bBrokerMessageShown) {
			 bBrokerMessageShown = false;
			 
			new Effect.Fade(divBrokerMessage, {duration:iDuration, queue:{scope:sQueue, position:'end'}, afterFinish:function() {
					new Effect.Appear(divMainMessage, {duration:iDuration, queue:{scope:sQueue, position:'end'}});
				}
			});
		}
		setTimeout("BeginAnimation()", iLogoDelay/2);
		iLogoIndex = 0;
	}
}

function BuildTextPoint(sAnimDiv, sMessage) {
	var sId = RandomID();
	var oTable = Builder.node('table', {id:sId, width:'100%',cellpadding:'2',cellspacing:'0',border:'0', style:"display:none;"});
	var oTbody = Builder.node('tbody');
	var oTR = Builder.node('tr');
	var oTD = Builder.node('td',{valign:"top"});
	oTD.style.verticalAlign="top";	// IE workaround;

	var oTickImg = Builder.node("IMG", {src:"/img/icons/Tick.jpg"});
	oTD.appendChild(oTickImg);
	oTR.appendChild(oTD);
	
	oTD = Builder.node('td',{valign:"top"}, [Builder.node("strong",{style:"background-color:white;font-family:Arial, Helvetica, sans-serif;font-size:12pt"}, sMessage)]);
	
	oTR.appendChild(oTD);
	oTbody.appendChild(oTR);
	oTable.appendChild(oTbody);

	$(sAnimDiv).appendChild(oTable);	
	return sId;

}


function BuildBrokerGraphicDivs(sAnimDiv, arBrokerLogos) {
	var arLogos = $A(arBrokerLogos);
	var arIDs = new Array();
	
	
	var sTarget = "";
	var sTarget_buildernode = "";
	if (location.href.indexOf("loanspage-menu.php")>-1) {
		sTarget = " target=\"_blank\" ";
		sTarget_buildernode = "_blank";
	}
	
	arLogos.each(function(sLogo) {
	 	var sId = RandomID();
		//var oLogo = Builder.node("DIV", {style:"text-align:center; width:100%"}, [Builder.node("IMG", {id:sId, src:sLogo, style:'display:none; margin-top:10px'})]);
		
		var oImage = Builder.node("IMG", {src:sLogo, border:0, style:'margin-top:10px; margin-left:auto; margin-right:auto;'});
		var oLogo = Builder.node("DIV", {id:sId, style:"text-align:center; width:100%; display:none;"}, [
							Builder.node("A", {href:'/h/loanspage-ourservice.php', target:sTarget_buildernode}, [
								oImage
							]), 
						]);
		$(sAnimDiv).appendChild(oLogo);
		
		new Insertion.Bottom(oLogo, "<div style=\"text-align:right; width:100%; font-weight:bold;\" class=\"Text_small\">[ <a href=\"/h/loanspage-ourservice.php\" " + sTarget + "><span class=\"Text_small\">read more</span></a> ]</div>");
		
		// DAR - December 24, 2007...
		//Reflection.add(oImage);	// , {height:0.1, opacity:0.2}
		// reflect rheight10 ropacity20
		
		arIDs.push(sId);
	});
	return arIDs;
}

function RandomID() {
	iElementCount++;
	
	return "el_" + iElementCount + "_" + Math.round( (Math.random()*100000)+1 );
}