
// This is called from the header include inline, not at doc ready
// so the IE classes will be set early and the page will load nice
var g_isIE6 = false;
var g_isIE7 = false;
function AddIEClassToPage() {
	var userAgent = navigator.userAgent.toLowerCase();
	g_isIE6 = (/msie/.test(userAgent) && !/opera/.test(userAgent)) &&	userAgent.match(/6.0/)
	g_isIE7 = (/msie/.test(userAgent) && !/opera/.test(userAgent)) &&	userAgent.match(/7.0/)
	if (g_isIE6) {
		var pageEl = document.getElementById("pageId");
		var x = Math.floor((document.body.clientWidth - 980) / 2);
		if (x < 4) x = 4;
		pageEl.style.marginLeft = x + "px";
	}
	if (g_isIE6 || g_isIE7) {
		var className = "IE6"
		if (g_isIE7) {
			className = "IE7";
		}
		var elem = document.getElementById("pageId");
		elem.className += (elem.className ? " " : "") + className;
		if (g_isIE6) {
			var elems = document.getElementsByName("s_i_aolcleanup");
			if (elems[0]) elems[0].style.position = "absolute";
		}
	}
}

function handleSearch(){
	clossErrorPane();
	var inpElem = document.getElementById("input2");
	if (inpElem.value == js_txt_defaultInputValue) return;
	var strQuery = inpElem.value;
	var str = trim(strQuery);

	if (!hasErrors(str, 20, 52)) {
		// report FindYourNewEmail action to Omniture
		//docommentOutReportAction("FindYourNewEmail", gOmnitureObject.reportingAccount, "", false, "startPage", 0);		
		srchDomain(str, false);
	}	
	return;
}

function redirectURL(domName, clickedFromIcon)
{
	var url = "addrSearch.jsp?search="+domName;
	if (clickedFromIcon)
	{
		url += "&icid=featured_main";
	}
	document.location = url;
}

function srchDomain(domName, clickedFromIcon)
{	
	setTimeout("redirectURL('" + domName +"'," + clickedFromIcon +")", 200);
}

function init(){
	//  margin: auto does not work well with IE6.  Center the page manually.
	var elem = document.getElementById("input2");
	elem.value = js_txt_defaultInputValue;
	elem.onblur = function() {
		if (this.value=='') this.value=js_txt_defaultInputValue;
	};
	elem.onfocus = function() {
		if (this.value==js_txt_defaultInputValue) this.value='';
	};
	elem.onkeydown = handleEnterKeys;
	makeOtherBanners();
	if (g_isIE6) transitionNoBlendSetup();	
	setTimeout("rotate()",8000);

	initOmniture();  // defined in footerInclude.jsp
	doPageReport();
}

function makeOtherBanners() {
	if (g_numRotatingImg <= 1) return;
	var bannerTemplate = '<div id="@id@" style="background:url('+g_ResourcePath+'/images/start-bg-@imgId@.jpg)" class="bannerBgOther"> <div class="tag1"> @tagLine@ </div> <div class="tag2"> @infoLine@ </div> <div class="example"> @exampleLine@ </div> </div>';
	html = "";
	for (var i = 2; i <= g_numRotatingImg; i++) {
		var banner = bannerTemplate.replace("@id@", "ban" + (i-1));
		banner = banner.replace("@imgId@", i);
		banner = banner.replace("@tagLine@",g_TagLine[i]);
		banner = banner.replace("@infoLine@",g_InfoLine[i]);
		banner = banner.replace("@exampleLine@",g_ExampleLine[i]);
		html += banner;	
	}
	
	var elem = document.getElementById("rotateImg");
	elem.innerHTML += html;
}

function handleEnterKeys(e) {
	var c;
	var evt = (e) ? e : window.event;
	if (evt.type == "keydown") {
		c = evt.keyCode;
		if (c == 13) {
			handleSearch();
		}
	}
}

var bannerIndex = -1;
function rotate(){
	if (g_numRotatingImg <= 1) return;
	bannerIndex++;
	bannerIndex = bannerIndex % g_numRotatingImg;
	transIndex = 0;
	if (g_isIE6)
		transitionNoBlend();
	else
		transition();
	setTimeout("rotate()",7000);
}

var transIndex = 0;
function transition(){
	if (transIndex >= 1) return;
	transIndex += 0.05;
	transIndex = (transIndex >= 1) ? 1 : transIndex;
	var currElem = document.getElementById("ban"+bannerIndex);
	var nextIndex = bannerIndex + 1;
	nextIndex = nextIndex % g_numRotatingImg;
	var nextElem = document.getElementById("ban"+nextIndex);
	
	setAlpa(currElem, 1-transIndex);
	setAlpa(nextElem, transIndex);		

	setTimeout("transition()",50);
}

function setAlpa(elem, num){
	var percent = parseInt(num * 100);
	var num = percent / 100;
	elem.style.opacity = num;
	elem.style.MozOpacity = num;
	elem.style.filter = "alpha(opacity=" + percent + ")";
}

function transitionNoBlendSetup(){
	var elem = document.getElementById("ban"+0);
	setAlpa(elem, 1);
	elem.style.visibility = 'visible';
	
	for (var i = 1; i < g_numRotatingImg; i++) {
		var elem = document.getElementById("ban"+i);
		elem.style.visibility = 'hidden';
		setAlpa(elem, 1);
	}
}

function transitionNoBlend(){
	for (var i = 0; i < g_numRotatingImg; i++) { 
		var currElem = document.getElementById("ban"+i);
		currElem.style.visibility = 'hidden';
	}
	
	var nextIndex = bannerIndex + 1;
	nextIndex = nextIndex % g_numRotatingImg;
	var nextElem = document.getElementById("ban"+nextIndex);
	nextElem.style.visibility = 'visible';
}
window.onload = init;