function addDelayedEvent(functionref) {
	if (navigator.userAgent.toLowerCase().indexOf('applewebkit')!=-1) //for Safari
		window.addEventListener("load", functionref, false)
	else if (window.addEventListener) //for Firefox
		window.addEventListener("DOMContentLoaded", functionref, false)
	else if (window.attachEvent) //for Explorer
		window.attachEvent("onload", functionref)
}

function SetCookie (name,value,expires,path,domain,secure) {
	document.cookie = name + "=" + escape (value) +
	((expires) ? "; expires=" + expires.toGMTString() : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");
}

function getCookieVal (offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
		endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	if ((document.cookie == null) || (document.cookie.length == null))
	{
		return null;
	}
	var i = 0;
	while (i < clen)
	{
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
			return getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break; 
	}
	return null;
}

function valueStrip(regSearch,features) {
	var results =  features.match(regSearch);
	if (results != null) return results[2];
	else return 0;
}

function popper(theURL,winName,features) {
	var posWin = null;
	var gotIE = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;	
	var gotNS = (navigator.appName == 'Netscape') ? true : false;
	var gotNN6 = (gotNS && (parseInt(navigator.appVersion) >= 5)) ? true : false;
// REGULAR EXPRESSIONS TO STRIP OUT THE WIDTH AND HEIGHT VARS -----------------
	var widthMatch = /(width=)(\d+)/i;
	var heightMatch = /(height=)(\d+)/i;
	var scrollMatch = /(scrollbars=)([a-zA-Z0-9])/i;
	var numMatch = /\d+/;
	
	var width = valueStrip(widthMatch,features);
	var height = valueStrip(heightMatch,features);
	var scrollbars = valueStrip(scrollMatch,features);
	
	if (posWin!=null && !posWin.closed) posWin.close();
	
	if (gotIE) {
	var left = (screen.Width/2) - width/2;
	var top = (screen.Height/2) - height/2;
	posWin = window.open(theURL,winName,"scrollbars=0,left="+left+",top="+top+",width="+width+",height="+height+",scrollbars="+scrollbars);
	posWin.focus();
	}
	else if (gotNS && !gotNN6) {
	var left = (screen.availWidth/2) - width/2;
	var top = (screen.availHeight/2) - height/2;
	posWin = window.open(theURL,winName,"screenX="+left+",screenY="+top+",innerWidth="+width+",innerHeight="+height+",scrollbars="+scrollbars);
	posWin.focus();
	}
	else if (gotNN6) {
	var left = (screen.availWidth/2) - width/2;
	var top = (screen.availHeight/2) - height/2;
	posWin = window.open(theURL,winName,"screenX="+left+",screenY="+top+",width="+width+"px,height="+height+"px,scrollbars="+scrollbars);
	posWin.focus();
	}
	else {
	posWin = window.open(theURL,winName,"scrollbars=0,width="+width+",height="+height+",scrollbars="+scrollbars);
	posWin.focus();
	}
}

function popCheck(obj) {
	if (!obj) {
		alert("Some content on CMT.com is displayed using pop-ups.  \n\nIf you're using a Pop-up Blocker, you'll need to permit pop-ups \nfrom CMT.com in order to view Videos, photos and other content on our site.");
	}
}

/* new flipbook js for popping flipbook media from external photo searches */
function popFlipMedia(fid, mid) {
	pWin = window.open('/sitewide/apps/flipbook/index.jhtml?fid=' + fid + '&mid=' + mid,'flipbook' + fid,'width=748,height=500,scrollbars=0');
}

/* Hack for Firefox/Flash bug (https://bugzilla.mozilla.org/show_bug.cgi?id=362193) */
try {
  if(navigator.userAgent.toLowerCase().indexOf("firefox")>=0){
    var _el=parent.document.getElementById('contentarea');
    var _cd=parent.document.createElement('div');
    _cd.style.cssText='clear:both;';
    _el.appendChild(_cd);
    _el.style.overflow='visible';
  }
}catch(e){}

/* Search switch for MSN search the web */
function submitSearch(formRef,target) {
  var trimed = formRef.searchterm.value.replace(/^\s*|\s(?=\s)|\s*$/g, "").toLowerCase();
	formRef.searchterm.value = trimed;
	if (trimed.length == 0 || trimed == "search") {
		return;
	} else {
		if(target == "web") {
			var msUrl ='http://search.live.com/results.aspx?q=' + trimed + '&amp;mkt=en-us&amp;FORM=VCM001';
			var sWin = window.open(msUrl);
			return false;
		} else {
			formRef.submit();
		}
	}
}

function catchEnter(formRef,e) {
	if (!e) e = window.event;
	var code = (e.keyCode) ? e.keyCode : e.which;
	if (code == 13 || code == 3) submitSearch(formRef);
	else return true;
}

function refreshAds() {
	if (document.getElementById('header_ad')) {
		var a = document.getElementById('header_ad').getElementsByTagName('iframe');
		a[0].src = a[0].src;
	}
	if (document.getElementById('billboard_column')) {
		var b = document.getElementById('billboard_column').getElementsByTagName('iframe');
		b[0].src = b[0].src;
	}
	if (document.getElementById('skyscraper_column')) {
		var c = document.getElementById('skyscraper_column').getElementsByTagName('iframe');
		c[0].src = c[0].src;
	}
	setTimeout("refreshAds()", 60000);
}
/* Exclude some pages from ad rotation */
if ((location.href != "http://www.cmt.com/") && 
	(location.href != "http://www.cmt.com/index.jhtml") && 
	(location.href.indexOf("http://www.cmt.com/cmt-music-awards/") < 0) && 
	(location.href.indexOf("http://blog.cmt.com/") < 0) && 
	(location.href.indexOf("testmode") < 0)) {
	setTimeout("refreshAds()", 180000);
}

function menuInit() { 
	if(navigator.appVersion.indexOf("MSIE")==-1){return;}
	var i,k,g,lg,r=/\s*p7hvr/,nn='',c,cs='p7hvr',bv='header_menu';
	for(i=0;i<10;i++){g=document.getElementById(bv+nn);if(g){
	lg=g.getElementsByTagName("LI");if(lg){for(k=0;k<lg.length;k++){
	lg[k].onmouseover=function(){c=this.className;cl=(c)?c+' '+cs:cs;
	this.className=cl;};lg[k].onmouseout=function(){c=this.className;
	this.className=(c)?c.replace(r,''):'';};}}}nn=i+1;}
}

addDelayedEvent(menuInit); // initialize the menus after page load is complete
window.onresize=menuInit; // reinitialize the menus after window is resized

/* Page object */
Page = {};
page = Page;
Page.getIEVersion = function()
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
	var ua = navigator.userAgent;
	var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
	if (re.exec(ua) != null)
	  rv = parseFloat( RegExp.$1 );
  }
  return rv;
}
Page.getElementWidth = function(Elem){
	var elem = {};
	if (document.all){
		elem = document.all[Elem];
	}
	else if(document.getElementById) {
		elem = document.getElementById(Elem);
	}
	if(elem != null && elem != 'undefined'){
		var xPos = elem.offsetWidth;
		return xPos;
	}
	else
		return false;
}

$j = jQuery;
function checkForOPA(){
	var ad_width = Page.getElementWidth('headerAd');
	var headerPromoElement = document.getElementById('headerPromo');
	if(ad_width) {
		if(ad_width < 800) {
			try {
				var iev = Page.getIEVersion();
				if(iev == -1 || iev > 6) {
					document.getElementById('headerPromo').style.display = 'block';
					var numberOfPromos = $j('.header-promo').size();
					var randomnumber = Math.floor(Math.random()*Number(numberOfPromos));
					$j('.header-promo:eq('+randomnumber+')').css('display','block');
				}
			}
			catch(e) { 
				//System.out.println('ERROR: Getting the ie version');
			}
		} else if (headerPromoElement != null){
			document.getElementById('headerPromo').style.display = 'none';	
		}
	}
}

/* Detect non-OPA ad and display header promo area */
$j(document).ready(function(){
	setTimeout("checkForOPA()",1250);
});
