//IDOL Global Functions
if (console == null) {
	var console = {};
	console.log = function(obj){};
}

// IDOL FUNCTIONS
var IDOL = {
	// Performs symetrical adjustment to column heights
	equalHeights: function(arr, colNum){
		// check to see if array is DOM Array
		if(!arr[0]){
			arr = $$(arr);
		}
		var ulObj = arr;
		var height = 0;
		var vMod = colNum - 1;
		for(var liCount=0;liCount<ulObj.length;liCount++){
			if(ulObj[liCount].offsetHeight>height){
				height = ulObj[liCount].offsetHeight;
			}
			if((liCount%colNum) == vMod){
				var startPos = liCount - (colNum - 1);
				for(var formatCount=startPos; formatCount<=liCount; formatCount++){
					ulObj[formatCount].style.height=height+"px";
				}
				height = 0;
			}
		}
	},

	// onDOMLoad Event Handler
	onDOM: function(func){
		window.addEvent('domready', func);
	}

};

// String parser (e.g. used to extract pre-roll data from companion ad) - *** Move into IDOL FUNCTIONS
midString = function(strContent, strBeginVal, strEndVal){
	var myRe = eval("/"+strBeginVal+"(.*?)"+strEndVal+"/gm");
	var myArray = myRe.exec(strContent);
	if(myArray){
		return myArray[1];
	}else{
		return false;
	}
}

// MyIdol Rounded Corner fix for Buttons on Themed Pages - *** Move into IDOL FUNCTIONS
function fixThemedButtons() {
	var aHeaderBtns = $$('div.headerBtn');
	aHeaderBtns.forEach(function(oBtn){
		//oBtn.style.width = "auto";
		if (oBtn.offsetWidth != 0) {
			if (navigator.userAgent.toLowerCase().indexOf("safari") == -1) {
				oBtn.style.width = oBtn.offsetWidth+"px";
			}
			oBtn.innerHTML = '<div class="corners"><b><b><b><b><b></b></b></b></b></b></div>'+oBtn.innerHTML+'<div class="corners bottom-corners"><b><b><b><b><b></b></b></b></b></b></div>';
		}
	});
}

var bDomLoaded = false;
IDOL.onDOM(function(){
	fixThemedButtons();
	bDomLoaded = true;
});

// Global Flash Generation - *** Move into IDOL FUNCTIONS
function makeFlash(url,w,h,trans,vars,bReturn){
	/*if(trans==false || trans==null){trans = "opaque"}else{trans = "transparent"}*/
	if(vars==null) vars = "";
	/* Add allowFullScreen parameter once a fix for the subnav has been found */
	var flashObjectTag='<object id="idolvideoplayer" type="application/x-shockwave-flash" data="'+url+'" width="'+w+'" height="'+h+'"><param name="movie" value="'+url+'?'+vars+'" /><param name="wmode" value="transparent" /><param name="flashvars" value="'+vars+'" /><param name="menu" value="false" /><embed src="'+url+'" width="'+w+'" height="'+h+'" wmode="transparent" menu="false" flashvars="'+vars+'" /></object>';
	if(bReturn){
		return flashObjectTag;
	} else {
		document.write(flashObjectTag);
		flashObjectTag=null;
		vars=null;
		trans=null;
	}
};
// New Global Flash Generation for use with DFP ad-serving -JF 9/08
function writeVideoPlayer(playerURL, w, h, flashVars, adVars, bReturn) {
    // This will be the full flash varaible string
    var vars = "";
    
    // Detect PreRoll and add to flashVars if necessary
    if(adVars == null || adVars['vidURL'] == '' )
        vars = flashVars;
    else
        vars = flashVars + "&adID=" + adVars['vidURL'];
        
    // Build Video Player Object/Embed String
    var flashObjectTag='<object id="idolvideoplayer" type="application/x-shockwave-flash" data="'+playerURL+'" width="'+w+'" height="'+h+'"><param name="movie" value="'+playerURL+'?'+vars+'" /><param name="wmode" value="transparent" /><param name="flashvars" value="'+vars+'" /><param name="menu" value="false" /><embed src="'+playerURL+'" width="'+w+'" height="'+h+'" wmode="transparent" menu="false" flashvars="'+vars+'" /></object><img src="'+adVars["1x1URL"]+'" height="1" width="1" />';

 
    // Either return as a string or write to the page where requested.
    if(bReturn) {
        return flashObjectTag;
    }
    else {
        document.write(flashObjectTag);
    }
};
// Generate Rating Functionality - *** Move into IDOL FUNCTIONS
function enableRating(sElement, sType, iId){
	$(sElement).getElements('a').forEach(function(oVoteLink){
		oVoteLink.onmouseover = function() {
			$(sElement).getElement('span').className = oVoteLink.className;
		};
		oVoteLink.onmouseout = function() {
			$(sElement).getElement('span').className = '';
		};
		oVoteLink.onclick = function() {
			score = parseInt(oVoteLink.getElement('b').innerHTML);
			if(sType == 'photos'){
				var sAjaxUrl = '/includes/legacy/photos/incPhotosStarRater.php?mediaID='+iId+'&score='+score;
			} else {
				var sAjaxUrl = '/includes/legacy/videos/incVideosStarRater.php?mediaID='+iId+'&score='+score;
			}
			
			var xmlHttp = GetXmlHttpObject();
			xmlHttp.onreadystatechange = function() {
				if (xmlHttp.readyState==4) { 
					document.getElementById('vote-rating').innerHTML = xmlHttp.responseText;
				}	
			}
			xmlHttp.open("get",sAjaxUrl,true);
			xmlHttp.send(null);
		};
	});
};
// Manages multiple onLoad events
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
};

function GetXmlHttpObject() {
	var objXMLHttp=null;
	if (window.XMLHttpRequest) {
  		objXMLHttp=new XMLHttpRequest();
  	} else if (window.ActiveXObject) {
  		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
  	}
	return objXMLHttp;
}// legacy Season 6 JS code

function getDropValJump(div){var v1 = $(div).options[$(div).selectedIndex].value;if(v1) document.location = v1;}
function sendToAFriend(l,ltype){if(l==null){var l = document.location} if(ltype==null){var ltype=""} window.open('/a/_main/friend.asp?c='+ltype+'&l='+l+'','sendtofriend','width=420,height=320,scrollbars=no');}
function getVar(_1){var _2=window.location.search.substring(1);var _3=_2.split("&");for(var i=0;i<_3.length;i++){var _5=_3[i].split("=");if(_5[0]==_1){return _5[1];}}return false;}

function listMargins(arr,columns){
	var v1 = arr;
	for(var i=0;i<v1.length;i++){
		if(!(i%2)){v1[i].className+=" even";}
	}
}

// selector is the array
// colNum is integer of columns
// cellpadding is total vertical padding
function equalHeights(arr, colNum){
	// check to see if array is DOM Array
	// if(!arr[0]){
	// 		arr = LOT.convertDOM(arr);
	// 	}
	var ulObj = arr;
	var height = 0;
	var vMod = colNum - 1;
	for(var liCount=0;liCount<ulObj.length;liCount++){
		if(ulObj[liCount].offsetHeight>height){
			height = ulObj[liCount].offsetHeight;
		}
		if((liCount%colNum) == vMod){
			var startPos = liCount - (colNum - 1);
			for(var formatCount=startPos; formatCount<=liCount; formatCount++){
				ulObj[formatCount].style.height=height+"px";
			}
			height = 0;
		}
	}
}

function equalLineHeights(listArray,colNum,cellPadding){
	var ulObj = listArray;
	var height = 0;
	var vMod = colNum - 1;
	for(var liCount=0;liCount<ulObj.length;liCount++){
		if(ulObj[liCount].offsetHeight>height){
			height=ulObj[liCount].offsetHeight;
			if(cellPadding > 0){
				height=height-cellPadding;
			}
		}
		var liPos = (liCount%colNum);
		if(liPos == vMod){
			var startPos = liCount-(colNum - 1);
			for(var formatCount=startPos; formatCount<=liCount; formatCount++){
				//console.log("ELH height: "+height+"   lineHeight: "+ulObj[formatCount].style.lineHeight);
				if(ulObj[formatCount].offsetHeight<height+cellPadding){
					ulObj[formatCount].style.lineHeight=height+"px";
				}
				ulObj[formatCount].style.height=height+"px";
			}
			height=0;
		}
	}
}

// fixPNG
var fixPNG = function(_1){
	if(!document.XMLHttpRequest && window.attachEvent){var _2=(_1.id)?"id='"+_1.id+"' ":"";var _3=(_1.className)?"class='"+_1.className+"' ":"";var _4=(_1.title)?"title='"+_1.title+"' ":"title='"+_1.alt+"' ";var _5="display:inline-block;"+_1.style.cssText;var _6="<span "+_2+_3+_4;_6+=" style=\""+"width:"+_1.width+"px; height:"+_1.height+"px;"+_5+";";_6+="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader";_6+="(src='"+_1.src+"', sizingMethod='crop');\"></span>";_1.outerHTML=_6;_1=null;_6=null;}
};
