/* Global Functions for LA Avengers */


/*************************************************************************************
* DOM scripting by brothercake -- http://www.brothercake.com/
* GNU Lesser General Public License -- http://www.gnu.org/licenses/lgpl.html
* allows other DOM scripting to run before window.onload;
**************************************************************************************/
function domFunction(f, a)	{
	var n = 0;
	var t = setInterval(function() {
		var c = true;
		n++;
		if(typeof document.getElementsByTagName != 'undefined' && (document.getElementsByTagName('body')[0] != null || document.body != null)) {
			c = false;
			if(typeof a == 'object') {
				for(var i in a) {
					if((a[i] == 'id' && document.getElementById(i) == null) || (a[i] == 'tag' && document.getElementsByTagName(i).length < 1))	{ 
						c = true; 
						break; 
					}
				}
			}
			if(!c) { f(); clearInterval(t); }
		}
		if(n >= 60) { clearInterval(t);}
	}, 250);
};

/*************************************************************************************
* Get all elements by class name
    Written by Jonathan Snook, http://www.snook.ca/jonathan
    Add-ons by Robert Nyman, http://www.robertnyman.com
**************************************************************************************/
function getElementsByClassName(oElm, strTagName, strClassName){
    var arrElements = (strTagName == "*" && document.all)? document.all : 
    oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];      
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }   
    }
    return (arrReturnElements)
}

Array.prototype.push = ArrayPush;
function ArrayPush(value){
    this[this.length] = value;
}


/*************************************************************************************
* SIFR
* must include sifr.js in the page
**************************************************************************************/
function doSifr() {
	if(typeof sIFR == "function"){
//		sIFR.bAutoInit = true;
		sIFR.replaceElement(named({sSelector:"#content h1", sFlashSrc:"/cmn/swf/knockout69FullLightweight.swf", sCase:"upper", sBgColor:"#041C49", sColor:"#ffffff",nPaddingBottom:"2",sWmode:"transparent"}));
		sIFR.replaceElement(named({sSelector:".promo h5", sFlashSrc:"/cmn/swf/knockout49Lightweight.swf", sCase:"upper", sBgColor:"#ffffff", sColor:"#212121"}));
		sIFR.replaceElement(named({sSelector:".viewSched h4", sFlashSrc:"/cmn/swf/knockout49Lightweight.swf", sCase:"upper", sBgColor:"#072D77", sColor:"#ffffff",sWmode:"transparent"}));
//		sIFR.replaceElement(named({sSelector:"#inTheComm h5 a", sFlashSrc:"/cmn/swf/knockout29JrLightweight.swf", sCase:"", sBgColor:"#ffffff", sColor:"#62000A", sWmode:"transparent"}));
		sIFR.replaceElement(named({sSelector:"#boxContent h4", sFlashSrc:"/cmn/swf/knockout49Lightweight.swf", sCase:"upper", sBgColor:"#072D77", sColor:"#ffffff", nPaddingTop: "10", nPaddingLeft: "10", nPaddingRight: "10", nPaddingBottom: "8"}));
		sIFR.replaceElement(named({sSelector:"body#game #content h4", sFlashSrc:"/cmn/swf/knockout49Lightweight.swf", sCase:"upper", sBgColor:"#072D77", sColor:"#ffffff", sWmode:"transparent", sFlashVars:"textalign=right"}));
		sIFR.replaceElement(named({sSelector:"h4", sFlashSrc:"/cmn/swf/knockout49Lightweight.swf", sCase:"upper", sBgColor:"#072D77", sColor:"#ffffff", sWmode:"transparent" }));
		sIFR.replaceElement(named({sSelector:"h2", sFlashSrc:"/cmn/swf/knockout69FullLightweight.swf", sCase:"upper", sBgColor:"#ffffff", sColor:"#ffffff", sWmode:"transparent"}));
		/* Add h3 to above if want to affect all headlines. Use h3.hdrArticle only for article. */
		sIFR.replaceElement(named({sSelector:"#viewSchedSm h5, #viewSchedLg h5", sFlashSrc:"/cmn/swf/knockout69FullLightweight.swf", sCase:"upper", sBgColor:"#ffffff", sColor:"#000", sWmode:"transparent", sFlashVars:"textalign=center"}));
		sIFR.replaceElement(named({sSelector:"#viewSchedSm h6, #viewSchedLg h6", sFlashSrc:"/cmn/swf/knockout69FullLightweight.swf", sCase:"upper", sBgColor:"#ffffff", sColor:"#000", sWmode:"transparent", sFlashVars:"textalign=center"}));
	}
}

new domFunction(doSifr,{'body':'tag'});

/*************************************************************************************
* Loops through all imgs looking for "rollover" class and assigns the appropriate functions.
* Images must be named as image_on.* and image_off.*
**************************************************************************************/
function attachRollOvers() {
	// Get all images and input buttons with class name "rollover"
	var oImgs = getElementsByClassName(document,'img','rollover');
	var oInputs = getElementsByClassName(document,'input','rollover');
	
	var oElements = oImgs.concat(oInputs);
	
	for(var i=0; i<oElements.length; i++) {
		// Preload the rollover image!
		var tmpimg = new Image();
		tmpimg.src = oElements[i].src.replace("_off.","_on.");
		
		oElements[i].onmouseover = function() {
      	this.src = this.src.replace("_off.","_on.");
			if(this.runtimeStyle) { this.runtimeStyle.filter = this.runtimeStyle.filter.toString().replace("_off.","_on."); }
		}
		oElements[i].onmouseout= function() {
      	this.src = this.src.replace("_on.","_off.");
			if(this.runtimeStyle) { this.runtimeStyle.filter = this.runtimeStyle.filter.toString().replace("_on.","_off."); }
		}
 	}
}
new domFunction(attachRollOvers,{'img':'tag','input':'tag'});

/*************************************************************************************
* Loops through all LIs looking for "article" and "editor" ids and adds an onclick event so it links
* to the appropriate article
**************************************************************************************/
function linkArticles() {
   var oLi = document.getElementsByTagName('li');
   for(var i=0; i<oLi.length; i++) {
		reArticle = new RegExp("^article_|^editor");
		if(reArticle.test(oLi[i].id)) {
			oLi[i].onclick = function() { 
				var aArticle = this.id.split('_');
				var sArticleType = aArticle[1];
	         var iArticleId = aArticle[2];
	         document.location.href = '/buzz/editorial.php?strUserCategory='+sArticleType+'&intBlogID='+iArticleId;
	      }
		}
   }
}
new domFunction(linkArticles);

/*************************************************************************************
* Make rel="external" anchors pop up to new window. 
**************************************************************************************/
function makeExternalLinks() {
	//if (!document.getElementsByTagName) return;
	var aExtAnchors = document.getElementsByTagName('a');
	for (var i=0; i<aExtAnchors.length; i++) {
		var anchor = aExtAnchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
			anchor.target = "_blank";
		}
	}
}
new domFunction(makeExternalLinks);

/*************************************************************************************
* Make rel="print" anchors pop up print dialog box
**************************************************************************************/
function makePrintLinks() {
	//if (!document.getElementsByTagName) return;
	var aExtAnchors = document.getElementsByTagName('a');
	for (var i=0; i<aExtAnchors.length; i++) {
		var anchor = aExtAnchors[i];
		if (anchor.getAttribute("rel") == "print") {
			anchor.onclick = function() {
				if (window.print) {
				    window.print();
				} else{
					alert("Please go to your browser's FILE menu and choose PRINT");
				}
			}
		}
	}
}
new domFunction(makePrintLinks);



/*************************************************************************************
* Loops through all LIs looking for "profile" ids and adds an onclick event so it links
* to the appropriate page
**************************************************************************************/
function linkProfiles() {
   var oLi = document.getElementsByTagName('li');
   for(var i=0; i<oLi.length; i++) {
      reProfile = new RegExp("^profile");
      if(reProfile.test(oLi[i].id)) {
         oLi[i].onclick = function() { 
            var aProfile = this.id.split('_');
            var sProfileType = aProfile[1];
            var iProfileId = aProfile[2];
	         
            switch(sProfileType) {
	           case 'staff':
	              sProfileLink = '/team/operations/profile.php?strProfileType=Login&intProfileID='+iProfileId;
	              break;
	              
	           case 'player':
	              sProfileLink = '/team/about/profile.php?strProfileType=Players&intProfileID='+iProfileId;
	              break;
	              
	           case 'coach':
	              sProfileLink = '/team/about/profile.php?strProfileType=Coaches&intProfileID='+iProfileId;
	              break;
	              
	           default:
	              sProfileLink = '/fans/profiles/profile.php?strProfileType=Login&intProfileID='+iProfileId;
	              break;
            }
	         
            document.location.href = sProfileLink;
         }
      }
   }
}
new domFunction(linkProfiles);

/******************************************
* Add an event onload - by Simon Willison
*******************************************/
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}
/* example of code needed to run a function on page load: */ 
/* addLoadEvent(function() {
}); */


// Displays showId and hides hideId. Vals are optional.
function showHide(strShowId,strHideId) {
	if (strHideId) {
		var oHide = document.getElementById(strHideId);
		oHide.style.display = 'none';
	}
	if (strShowId) {
		var oShow = document.getElementById(strShowId);
		oShow.style.display = 'block';	
	}
}

/**
 * Define namespace
 */
var Avengers = window.Avengers || {};

/**
 * Random rotating ad unit.
 * @constructor
 * @param id {String} The id of div that contains the ad
 * @param ads {Array} Array containing ad data 
 * @interval {Int} Frequency in milliseconds of how often to rotate ad
 */
Avengers.AdRotator = function(id,ads,interval) {
	this.container = document.getElementById(id);
	this.ads = ads;
	this.interval = interval;

	if (this.container) {
		this.init();
	}
};

Avengers.AdRotator.prototype = {
	init : function() {
		thisRotator = this;
		this.totalAds = this.ads.length;
		this.adTracker = "|"; // keep track of ads so impression not registered more than once per page
		
		// determine default ad that loaded
		if (this.container.getElementsByTagName('div')[0]) {
			var beacon = this.container.getElementsByTagName('div')[0].id;
			var defaultAdId = beacon.substring(beacon.indexOf("_")+1,beacon.length);
			this.adTracker += defaultAdId + "|";
		}
		
		setInterval("thisRotator.rotateAd()",this.interval);
	},
	
	rotateAd : function() {
		var newAd = [];
		var adHTML;
		var randomNum = Math.floor(Math.random()*(this.totalAds));
		var ad = this.ads[randomNum]; // pick an ad at random
		
		// create new ad HTML
		newAd.push('<a href="' + Avengers.URL + '/adclick.php?bannerid=' + ad.bannerid + '&amp;zoneid=' + ad.zoneid + '&amp;source=' + ad.source + '&amp;dest=' + ad.dest + '" target="_blank">');
		newAd.push('<img src="' + Avengers.URL + '/adimage.php?' + ad.queryAdImage +'" width="' + ad.width + '" height="' + ad.height + '" alt="Opens site in new window." title="Opens site in new window." border="0" /></a>');

		if (this.adTracker.indexOf("|" + randomNum + "|") == -1) { // ad hasn't shown before
			newAd.push('<div id="beacon_' + ad.bannerid + '" style="position: absolute; left: 0px; top: 0px; visibility: hidden;">');
			newAd.push('<img src="' + Avengers.URL + '/adlog.php?bannerid=' + ad.bannerid + '&amp;clientid=' + ad.clientid + '&amp;zoneid=' + ad.zoneid + '&amp;source=' + ad.source + '&amp;' + ad.queryAdLog + '" width="0" height="0" alt="" style="width: 0px; height: 0px;">');
			newAd.push('</div>');

			this.adTracker += randomNum + "|";
		}
	
		adHTML = newAd.join('');

		// insert new ad in page
		this.container.innerHTML = adHTML;
	}
};

/**
 * Instantiate rotating ad unit onload
 */
addLoadEvent(function() {
	var adUnit1 = new Avengers.AdRotator("adUnit1", Avengers.ads, 10000);
});
