/*
script.js
ensenta.com (version 1.1)
website by LaMa Media
copyright (c) 2009, 2010 - all rights reserved
*/


// ****************************************************************
// GLOBALS
// ****************************************************************
/*global document, navigator, Image, clearTimeout, setTimeout*/

var vPage, vBrowser, vVersion, vIE;
var vDepth = '';
var vDelim = '|';
var vPages = ['ezadmin', 'atm', 'kiosk', 'retail', 'consumer', 'branchcap', 'merchant', 'mobile', 
			  'company', 'partners', 'platform', 'news', 'ezlogon', 'support', 'contact', 'events'];

if(!Array.indexOf) {
	Array.prototype.indexOf = function(obj) {
		for (var t = 0; t < this.length; t++) {
		if (this[t] == obj) {
                return t;
            }
        }
        return -1;
    };
}


// ****************************************************************
// UTILITIES
// ****************************************************************

function fBrowser() {
	vIE = 0;
 	vBrowser = navigator.appName;
    var tAppVersion = navigator.appVersion.toLowerCase();
    var vVersion = parseFloat(tAppVersion);
    var tIEPos = tAppVersion.indexOf('msie');
    if (tIEPos != -1) {
		vIE = 1;
		vVersion = parseFloat(tAppVersion.substring(tIEPos+5,tAppVersion.indexOf(';',tIEPos)));
	}
}

function fGetItem(rString, rNum, rDelim) {
	var tCount, tPos, tItem;
	for (tCount = 0; tCount <= rNum; tCount++) {
		tPos = rString.indexOf(rDelim);
		if (tPos < 0) {
			tPos = rString.length;
		}
		tItem = rString.substring(0, tPos);
		tPos = tItem.length + 1;
		rString = rString.substring(tPos, rString.length);
	}
	return (tItem);
}

function fOpacity(rID, rValue) { 
    var tObject = document.getElementById(rID).style; 
	var tValue = (rValue / 100);
    tObject.opacity = tValue; 
    tObject.MozOpacity = tValue; 
    tObject.KhtmlOpacity = tValue; 
    tObject.filter = 'alpha(opacity=' + rValue + ')'; 
} 


// ****************************************************************
// CSS RULES
// ****************************************************************

function fCSSRule(rName) {
	var tSNum, tSheet, tRNum, tRule, tLast;
	rName = rName.toLowerCase();
	if (document.styleSheets) {
		for (tSNum = 0; tSNum < document.styleSheets.length; tSNum++) {
			tRNum = 0;
			tRule = false;
			tLast = false;
			tSheet = document.styleSheets[tSNum];
			do {
				if (tSheet.cssRules) {
					tRule = tSheet.cssRules[tRNum];
				} else {
					tRule = tSheet.rules[tRNum];
				}
				if (tRule)  {
					if (tRule.selectorText.toLowerCase() == rName) {
						return tRule;
					}
					// Chrome Fix
					if (tRule == tLast) {
						tRule = false;
					}
					tLast = tRule;
				}
				tRNum++;
			} while (tRule);
		}
	}
	return false;
}

function fCSSRuleAdd(rName) {
	if (document.styleSheets) {
		if (!fCSSRule(rName)) {
			if (document.styleSheets[0].addRule) {
				document.styleSheets[0].addRule(rName, null, 0);
			} else {
				document.styleSheets[0].insertRule(rName + ' { }', 0);
			}
		}
	}
	return fCSSRule(rName);
} 


// ****************************************************************
// CSS BUTTON
// ****************************************************************

function fCSSButton(rID, rImage, rWidth, rHeight, rLeft0, rTop0, rLeft1, rTop1, rPosL, rPosT, rPosR, rPosB) {
	var tRule;
	tRule = fCSSRuleAdd('#' + rID);
	tRule.style.position = 'absolute';
	tRule.style.display = 'block';
	tRule.style.outline = 'none';
	tRule.style.width = rWidth;
	tRule.style.height = rHeight;
	tRule.style.background = 'url(' + rImage + ')';
	tRule.style.backgroundRepeat = 'no-repeat';
	tRule.style.backgroundPosition = rLeft0 + 'px ' + rTop0 + 'px';
	if (rPosL) { tRule.style.left = rPosL; }
	if (rPosT) { tRule.style.top = rPosT; }
	if (rPosR) { tRule.style.right = rPosR; }
	if (rPosB) { tRule.style.bottom = rPosB; }

	tRule = fCSSRuleAdd('#' + rID + ':hover');
	tRule.style.backgroundPosition = rLeft1 + 'px ' + rTop1 + 'px';

	tRule = fCSSRuleAdd('#' + rID + ' span');
	tRule.style.display = 'none';
}

function fCSSButtonLock(rID, rLeft, rTop) {
	var tRule;
	
	tRule = fCSSRule('#' + rID);
	tRule.style.backgroundPosition = rLeft + 'px ' + rTop + 'px';
	tRule.style.cursor = 'default';

	tRule = fCSSRule('#' + rID + ':hover');
	tRule.style.backgroundPosition = rLeft + 'px ' + rTop + 'px';
}


// ****************************************************************
// PAGES
// ****************************************************************

function fPageNav() {
	document.getElementById('pagenav').innerHTML = '<map name="pagehome"><area shape="circle" coords="58,58,58" href="' + vDepth + '../index.html" onmouseover="fPageHome(1);" onmouseout="fPageHome(0);" alt=""></map><div id="top"><div id="topbar"><div id="topleft" class="topside"></div><a id="tb-company" class="topbutton" href="' + vDepth + 'company.html"><span>COMPANY</span></a><a id="tb-partners" class="topbutton" href="' + vDepth + 'partners.html"><span>PARTNERS</span></a><a id="tb-platform" class="topbutton" href="' + vDepth + 'platform.html"><span>PLATFORM</span></a><a id="tb-news" class="topbutton" href="' + vDepth + 'news.html"><span>NEWS</span></a><a id="tb-ezlogin" class="topbutton" href="https://admin.ensenta.com/ECAdmin"><span>EZ LOGIN</span></a><a id="tb-support" class="topbutton" href="' + vDepth + 'support.shtml"><span>SUPPORT</span></a><a id="tb-contact" class="topbutton" href="' + vDepth + 'contact.html"><span>CONTACT</span></a><a id="tb-events" class="topbutton" href="' + vDepth + 'events.html"><span>EVENTS</span></a><div id="topright" class="topside"></div></div></div><div id="left"></div><div id="leftnav"><div id="leftlogo"><img usemap="#pagehome" src="' + vDepth + '../media/blank.gif" width="116" height="116" alt="" border="0"></div><a id="lb-ezadmin" class="leftbutton" href="' + vDepth + 'ezadmin.html"><span>EZ Admin</span></a><a id="lb-atm" class="leftbutton" href="' + vDepth + 'atm.html"><span>ATM</span></a><a id="lb-kiosk" class="leftbutton" href="' + vDepth + 'kiosk.html"><span>Kiosk</span></a><a id="lb-retail" class="leftbutton" href="' + vDepth + 'retail.html"><span>Retail</span></a><a id="lb-consumer" class="leftbutton" href="' + vDepth + 'consumer.html"><span>Consumer</span></a><a id="lb-branchcap" class="leftbutton" href="' + vDepth + 'branchcap.html"><span>Branch Capture</span></a><a id="lb-merchant" class="leftbutton" href="' + vDepth + 'merchant.html"><span>Merchant Capture</span></a><a id="lb-mobile" class="leftbutton" href="' + vDepth + 'mobile.html"><span>Mobile</span></a></div>';
}

function fPageStart(rPage, rDepth) {
	fBrowser();
	vPage = rPage;
	if (typeof(rDepth) != 'undefined') {
		while (rDepth--) {
			vDepth+='../';
		}
	}
}

function fPageEnd() {
	var tTopOffs = [0, -97, -190, -283, -353, -454, -541, -631];
	fPageNav();
	var tPageNum = vPages.indexOf(vPage);
	if (tPageNum >= 0) {
		if (tPageNum <= 7) {
			fCSSButtonLock(('lb-' + vPage), -392, (tPageNum * -30));
		} else {
			fCSSButtonLock(('tb-' + vPage), tTopOffs[(tPageNum - 8)], -58);
		}
	}
}

function fPageHome(rOver) {
	if (rOver) {
		document.getElementById('leftlogo').style.backgroundPosition = '0 -117px';
	} else {
		document.getElementById('leftlogo').style.backgroundPosition = '0 0';
	}
}

function fEmail(rWho) {
	var tEmail = (rWho + String.fromCharCode(64) + 'ensenta' + '.com');
	document.getElementById('email').innerHTML = ('<a href="mai' + 'lto:' + tEmail + '">' + tEmail + '</a>');
}

function fMap(rWho) {
	document.getElementById('map').innerHTML = ('<a href="http://maps.google.com/maps?&q=303+Twin+Dolphin+Drive,+Suite+550,++Redwood+City,+CA&z=15" target="_blank">MAP</a>');
}


// ****************************************************************
// PARTNERS
// ****************************************************************

var vPartners, vPartnerPtr;
var fPartnerHide, fPartnerNext;
var vPartnerShowTime = 1500;
var vPartnerSpeed = 20;
var vPartnerDelta = 5;
var vPartnerDelay = 100;
var vPartnerOpacity = 100;

function fPartnerShow() {
	vPartnerOpacity += vPartnerDelta;
	if (vPartnerOpacity >= 100) {
		vPartnerOpacity = 100;
		setTimeout(fPartnerHide, vPartnerShowTime);
	} else {
		setTimeout(fPartnerShow, vPartnerSpeed);
	}
	fOpacity(vPartners[vPartnerPtr], vPartnerOpacity);
}

fPartnerHide = function () { //JSLint Fix
	vPartnerOpacity -= vPartnerDelta;
	if (vPartnerOpacity <= 0) {
		vPartnerOpacity = 0;
		fPartnerNext();
	} else {
		setTimeout(fPartnerHide, vPartnerSpeed);
	}
	fOpacity(vPartners[vPartnerPtr], vPartnerOpacity);
};

fPartnerNext = function () { //JSLint Fix
	var tRule;
	for (var t = 0; t < vPartners.length; t++) {
		tRule = fCSSRuleAdd('#' + vPartners[t]);
		tRule.style.display = 'none';
		fOpacity(vPartners[t], 0);
	}
	if (++vPartnerPtr >= vPartners.length) {
		vPartnerPtr = 0;
	}
	tRule = fCSSRule('#' + vPartners[vPartnerPtr]);
	tRule.style.display = 'inline';
	setTimeout(fPartnerShow, vPartnerDelay);
};

function fPartnerStart() {
	var tPartner;
	var tString = '';
	var tMax = (vPartners.length);
	vPartnerPtr = (Math.floor(Math.random()*tMax)-1);
	for (var t = 0; t < vPartners.length; t++) {
		tPartner = vPartners[t];
		tString += '<img id="' + tPartner + '" class="partnerlogo" src="../media/page/partners/' + tPartner + '.jpg" width="280" height="200" alt="">';
	}
	document.getElementById('partnerframe').innerHTML = tString;
	fPartnerNext();
}


// ****************************************************************
// NEWS
// ****************************************************************

var vNews, vNewsTimeout, vNewsPtr;
var fNewsHide;
var vNewsShowTime = 3000;
var vNewsSpeed = 10;
var vNewsDelta = 5;
var vNewsDelay = 300;
var vNewsOpacity = -100;


function fNewsHeadline() {
	var tString, tHeadline, tLink, tATag;
	if (++vNewsPtr >= (vNews.length - 1)) {
		vNewsPtr = 0;
	}
	tString = vNews[vNewsPtr];
	tHeadline = fGetItem(tString, 0, vDelim);
	tLink = fGetItem(tString, 1, vDelim);
	tATag = '<a href="' + tLink + '" onmouseover="fNewsMouse(1);" onmouseout="fNewsMouse(0);">';
	document.getElementById('homenews').innerHTML = tATag + tHeadline + '</a>';
}

function fNewsShow() {
	vNewsTimeout = 0;
	vNewsOpacity += vNewsDelta;
	if (vNewsOpacity >= 100) {
		vNewsOpacity = 100;
		vNewsTimeout = setTimeout(fNewsHide, vNewsShowTime);
	} else {
		setTimeout(fNewsShow, vNewsSpeed);
	}
	fOpacity('homenews', vNewsOpacity);
}

fNewsHide = function () { //JSLint Fix
	vNewsTimeout = 0;
	vNewsOpacity -= vNewsDelta;
	if (vNewsOpacity <= 0) {
		vNewsOpacity = 0;
		fNewsHeadline();
		setTimeout(fNewsShow, vNewsDelay);
	} else {
		setTimeout(fNewsHide, vNewsSpeed);
	}
	fOpacity('homenews', vNewsOpacity);
};

function fNewsMouse(rOver) {
	if (vNewsTimeout !== 0) {
		clearTimeout(vNewsTimeout);
		if (!rOver) {
			fNewsHide();
		}
	}
}

function fNewsStart() {
	var tMax = (vNews.length - 1);
	vNewsPtr = Math.floor(Math.random()*tMax);
	fNewsHeadline();
	fNewsShow();
}


// ****************************************************************
// HOME
// ****************************************************************

var vHomeButtons = [];
var vHomeImages = [];
var vHomeLayer = -1;
var vHomeTimeout;

function fHomeMap() {
	var tString = '';
	var tNumCircles = 7;
	for (var t = 0; t <= tNumCircles; t++) {
		tString += '<map name="homecircle' + t + '"><area shape="circle" coords="52,52,50" href="content/' + vPages[t+1] + '.html" onmouseover="fHome(' + t + ', 1);" onmouseout="fHome(' + t + ', 0);" alt=""></map>';
	}
	document.getElementById('homemap').innerHTML = tString;
}

function fHomePreload() {
	var tImageNames = ['botback', 'botcen', 'headback', 'homeback', 'homeicon', 'homelogo', 'hometext', 
	                   'leftback', 'leftcurve', 'leftlogo', 'leftnav', 'topback', 'topcover', 'topnav', 'topsides'];
	for (var t = 0; t < tImageNames.length; t++) {
		vHomeImages[t] = new Image();
		vHomeImages[t].src = ('../media/nav/' + tImageNames[t] + '.gif');
	}
}

function fHomePreloaded() {
	for (var t = 0; t < vHomeImages.length; t++) {
		if (vIE) {
			if (vHomeImages[t].width === 0) {
				return 0;
			}
		} else {
			if (vHomeImages[t].complete === false) {
				return 0;
			}
		}
	}
	return 1;
}

function fHomeDisplay() {
	if (fHomePreloaded()) {
		document.getElementById('homewheel').style.display = 'block';
		document.getElementById('home-default').style.display = 'inline';
		document.getElementById('homebottom').style.display = 'block';
		document.getElementById('homecover').style.display = 'none';
		fNewsStart();
	} else {
		setTimeout(fHomeDisplay, 100);
	}
}

function fHomeStart() {
	fBrowser();
	//[rID, rTextLeft, rIconLeft];
	vHomeButtons.push([vPages[1], 0, 0]);
	vHomeButtons.push([vPages[2], -141, -104]);
	vHomeButtons.push([vPages[3], -282, -208]);
	vHomeButtons.push([vPages[4], -438, -312]);
	vHomeButtons.push([vPages[5], -594, -416]);
	vHomeButtons.push([vPages[6], -793, -520]);
	vHomeButtons.push([vPages[7], -992, -624]);
}

function fHomeEnd() {
	fHomeMap();
	fHomePreload();
	fHomeDisplay();
}

function fHomeDefault() {
	if (vHomeLayer >= 0) {
		document.getElementById('home-default').style.display = 'inline';
		document.getElementById('home-' + vHomeLayer).style.display = 'none';
		vHomeLayer = -1;
	}
}

function fHomeLayer(rWhich, rOver) {
	if (rOver) {
		if (vHomeLayer == rWhich) {
			clearTimeout(vHomeTimeout);
		} else {
			if (vHomeLayer >= 0) {
				document.getElementById('home-' + vHomeLayer).style.display = 'none';
			}
			vHomeLayer = rWhich;
			document.getElementById('home-default').style.display = 'none';
			document.getElementById('home-' + rWhich).style.display = 'inline';
		}
	} else {
		vHomeTimeout = setTimeout(fHomeDefault, 10);
	}
}

function fHome(rWhich, rOver) {
	var tTextOn = -45;
	var tIconOn = -104;
	var tData = vHomeButtons[rWhich];
	var tTextRule = fCSSRule('#ht-' + tData[0]);
	var tIconRule = fCSSRule('#hi-' + tData[0]);
	var tCircleRule = fCSSRule('#hc-' + tData[0]);
	
	if (rOver) {
		tTextRule.style.backgroundPosition = tData[1] + 'px ' + tTextOn + 'px';
		tIconRule.style.backgroundPosition = tData[2] + 'px ' + tIconOn + 'px';
		tCircleRule.style.cursor = 'pointer';
	} else {
		tTextRule.style.backgroundPosition = tData[1] + 'px 0px';
		tIconRule.style.backgroundPosition = tData[2] + 'px 0px';
		tCircleRule.style.cursor = 'default';
	}
	fHomeLayer(rWhich, rOver);
}


