﻿var winLoad = window.onload;
if(winLoad != null) window.onload = function(){ winLoad(); myInit(); }
else window.onload = function(){ myInit(); }

var promoIds = new Array;
var promoInterval;

function myInit(){
	if(navigator.appName == "Microsoft Internet Explorer")
		if(parseFloat(navigator.appVersion.substring(navigator.appVersion.indexOf("MSIE") + 4)) < 7)
			imgSwap();
}

function registerPromo(id)
{
    if(document.getElementById(id))
    {
        if(promoIds.length > 0)
        {
            promoIds[promoIds.length] = new Array(id, false); 
            document.getElementById(id).style.display = "none";
        }
        else
        {
            promoIds[promoIds.length] = new Array(id, true); 
        }
    }
}

function promoRotate()
{
    if(promoIds.length > 1)
    {    
        var show = true;
        
        for(i=0; i< promoIds.length; i++)
        {
            promo = document.getElementById(promoIds[i][0]);
            
            if(promo.style.display == "none")
            {
                if(show && !promoIds[i][1])
                {
                    promo.style.display = "block";
                    show = false;
                    promoIds[i][1] = true;
                }
            }
            else
            {
                if(show)
                {
                    promo.style.display = "none";
                }
            }
        }
        
        if(show)
        {
            document.getElementById(promoIds[0][0]).style.display="block";
            for(i=0; i< promoIds.length; i++)
            {
                promoIds[i][1] = false;
            }
        }
    }
    else
        clearInterval(promoInterval);
}

function imgSwap(){ // (png friendly)
	var as = document.getElementsByTagName("a");
	var irs = new Array();
	for(var a = 0; a < as.length; a++)
		if(as[a].className.indexOf("png") > -1)
			irs[irs.length] = as[a];
		for (a = 0; a < irs.length; a++){
			if(irs[a].className){
				if(irs[a].className.indexOf("png") >-1){
					// The cloneNode method is no good because the cloned item will be stuck with the same
					// background as the original. 
					if(irs[a].parentNode.tagName != "LI"){
					var clone = irs[a].parentNode.insertBefore(document.createElement("a"),irs[a]);
					clone.className = irs[a].className.replace("ir","hover");
					clone.innerHTML = irs[a].innerHTML;
					clone.target = irs[a].target;
					clone.href = irs[a].href;}
					irs[a].parentNode.className = "irc";
					irs[a].parentNode.onmouseover	= function(){ this.className = this.className.replace("irc","hover"); }
					irs[a].parentNode.onmouseout	= function(){ this.className = this.className.replace("hover","irc"); }
				}
			}
		}
}

function preloadImages(path,imgs){
	window.preloadedImgs = window.preloadedImgs || document.createElement("div");
	for(a in imgs){
		var img = preloadedImgs.appendChild(document.createElement("img"));
		img.src = path + imgs[a];
		preloadedImgs.width = preloadedImgs.height = img.style.width = img.style.height = "1px";
		preloadedImgs.style.position = "absolute";
		preloadedImgs.style.left = preloadedImgs.style.top = "-9999px";
	}
	if(!preloadedImgs.parentNode)document.getElementsByTagName("body")[0].appendChild(preloadedImgs);
}

/**
* Formats the number according to the 'format' string; adherses to the american number standard where a comma is inserted after every 3 digits.
*  note: there should be only 1 contiguous number in the format, where a number consists of digits, period, and commas
*        any other characters can be wrapped around this number, including '$', '%', or text
*        examples (123456.789):
*          '0' - (123456) show only digits, no precision
*          '0.00' - (123456.78) show only digits, 2 precision
*          '0.0000' - (123456.7890) show only digits, 4 precision
*          '0,000' - (123,456) show comma and digits, no precision
*          '0,000.00' - (123,456.78) show comma and digits, 2 precision
*          '0,0.00' - (123,456.78) shortcut method, show comma and digits, 2 precision
*
* @method format
* @param format {string} the way you would like to format this text
* @return {string} the formatted number
* @public
*/

Number.prototype.format = function(format) {
    var hasComma = -1 < format.indexOf(','),
		psplit = format.replace(/[^0-9\.]/g, "").split('.'),
		that = this;

    // compute precision
    if (1 < psplit.length) {
        // fix number precision
        that = that.toFixed(psplit[1].length);
    }
    // error: too many periods
    else if (2 < psplit.length) {
        throw ('NumberFormatException: invalid format, formats should have no more than 1 period: ' + format);
    }
    // remove precision
    else {
        that = that.toFixed(0);
    } // get the string now that precision is correct
    var fnum = that.toString();

    // format has comma, then compute commas
    if (hasComma) {
        // remove precision for computation
        psplit = fnum.split('.');

        var cnum = psplit[0],
			parr = [],
			j = cnum.length,
			m = Math.floor(j / 3),
			n = cnum.length % 3 || 3; // n cannot be ZERO or causes infinite loop

        // break the number into chunks of 3 digits; first chunk may be less than 3
        for (var i = 0; i < j; i += n) {
            if (i != 0) { n = 3; }
            parr[parr.length] = cnum.substr(i, n);
            m -= 1;
        }

        // put chunks back together, separated by comma
        fnum = parr.join(',');

        // add the precision back in
        if (psplit[1]) { fnum += '.' + psplit[1]; }
    }

    // replace the number portion of the format with fnum
    return format.replace(/[\d,?\.?]+/, fnum);
}