var isAdCom = false;
var afterHalf = false;
var g_duration = 0;
var g_preAudits = null;
var g_midAudits = null;
var g_postAudits = null;
var g_percentAudits = null;

var adTimer = null;
var adCheckCount = 0;
var adStarted = false;

var imgTopRightTag = null;
var imgTopRightHref = null;
var imgTopRightInterval = null;

var img0Tag = null;
var img0Href = null;
var img0Type = null;
var img0Interval = null;
var img1Tag = null;
var img1Href = null;
var img1Type = null;
var img1Interval = null;
var comp0Tag = null;
var comp1Tag = null;
var compAdi0Tag = null;
var compAdi1Tag = null;

var adNowIsShown = false;

// executeAcudeoAd in acudeoplayer swf application
function executeAcudeoAd(volume, programId, videoUrl, videoId, videoName, adParams) {
    var flash = document.getElementById("flashPreroll");
    flash.style.zIndex = 10;/*
    var windowWidth = parseInt(document.body.clientWidth);
    if (!g_embed) {
        if (windowWidth <= 860)
            flash.style.left = 10 + "px";
        else
            flash.style.left = (windowWidth - 860) / 2 + 10 + "px";
    }*/

    var acudeoplayer = document.getElementById("acudeoplayer");
    //programId = "49ca635be9d33";
    //alert(acudeoplayer + volume + "," + programId + "," + videoUrl + "," + videoId + "," + videoName);
    if (acudeoplayer && acudeoplayer.executeAcudeoAd)
        acudeoplayer.executeAcudeoAd("" + volume, "" + programId, "" + videoUrl, "" + videoId, "" + videoName, "" + adParams);
}

function resetAdCom() {
    isAdCom = false;
    afterHalf = false;
    g_duration = 0;
    g_preAudits = null;
    g_midAudits = null;
    g_postAudits = null;
    g_percentAudits = null;

    adCheckCount = 0;
    adStarted = false;
}

function playAdComWmv(path, preAudits, midAudits, postAudits, percentAudits, x, y, volume)
{
	//alert("playAdComWmv " + path);
	resetAdCom();
	playWMV(path, x, y, volume);
	
	isAdCom = true;
	g_preAudits = preAudits;
	g_midAudits = midAudits;
	g_postAudits = postAudits;
	g_percentAudits = percentAudits;	

	if (adTimer) window.clearTimeout(adTimer);
	adTimer = window.setTimeout("checkPlayerStatus()", 500);
}

function playAdWmv(path, x, y, volume)
{
	//alert("playAdWmv " + path);
	resetAdCom();
	playWMV(path, x, y, volume);
	if (adTimer) window.clearTimeout(adTimer);
	adTimer = window.setTimeout("checkPlayerStatus()", 500);
}

function trackAudit(type)
{	
	var x = 0;
	document.getElementById('AD_0_hidden').innerHTML = "";
	if (type == "pre")
	{
		if (g_preAudits != null)
			for (x = 0; x < g_preAudits.length; x++) 
			 	appendAuditImg(g_preAudits[x]);
	}
	else if (type == "mid")
	{
		if (g_midAudits != null)
			for (x = 0; x < g_midAudits.length; x++) 
				appendAuditImg(g_midAudits[x]);
		if (g_percentAudits != null) 
			appendAuditImg(g_percentAudits[0]+"&pct=50");
	}
	else if (type == "post")
	{
		if (g_postAudits != null)
			for (x = 0; x < g_postAudits.length; x++) 
				appendAuditImg(g_postAudits[x]);
		if (g_percentAudits != null) 
			appendAuditImg(g_percentAudits[0]+"&pct=100&pctf=t");
	}
}

function appendAuditImg(url)
{
	var img = document.createElement("img");
	img.src = url;
	document.getElementById('AD_0_hidden').appendChild(img);
}

function checkPlayerStatus()
{
	var wmp = getPlayer();
	if (wmp == null) return;
	if (!g_isIE) wmp.focus();
	if (!adStarted) adCheckCount++;
	var ps = wmp.playState;
	
	if (ps == 3 && !adStarted) 
	{
		// alert("started");
		adStarted = true;
		adCheckCount = 0;
		if (isAdCom) trackAudit("pre");
	}

	if (isAdCom && adStarted && g_duration == 0 && wmp.currentMedia)
		g_duration = wmp.currentMedia.duration;
	
	if (isAdCom && !afterHalf && g_duration > 0)
	{
		var position = wmp.controls.currentPosition;
		if (((position * 100) / g_duration) >= 50)
		{
			// alert("g_duration" + g_duration + ", pos " + position);
			trackAudit("mid");
			afterHalf = true;
		}
	}
	
	if (ps == 8 || ps == 1 || adCheckCount > 50)
	{
		//alert("playernext , " + ps + ", " + adCheckCount);
		if (isAdCom) trackAudit("post");
		consoleCallback("compAdTag", "next");
	}
	else
		adTimer = window.setTimeout("checkPlayerStatus()", 500);	
}

function alertjs(str)
{
	alert(str);
}

function showImgAdTopRight(src, href) {
    clearInterval(imgTopRightInterval);
	if (g_embed) return;
    imgTopRightTag = src;
    imgTopRightHref = href;
    refreshImgAdTopRight();
    //imgTopRightInterval = setInterval("refreshImgAdTopRight()", 300000);
}

function refreshImgAdTopRight() {
     //alert("refreshImg0Ad src - " + imgTopRightTag + ", href - " + imgTopRightHref);
    document.getElementById('AD_Right').style.width = "120px";
    document.getElementById('AD_0').style.marginLeft = "6px";
    document.getElementById('AD_0_adi').style.marginLeft = "6px";
    if (g_isIE) {
        document.getElementById('AD_0').style.styleFloat = "left";
        document.getElementById('AD_0_adi').style.styleFloat = "left";
    }
    else {
        document.getElementById('AD_0').style.cssFloat = "left";
        document.getElementById('AD_0_adi').style.cssFloat = "left"; 
    }
    var bannerHTML = imgTopRightTag;
    if (imgTopRightTag.search(/ext=\.swf$/) != -1 || imgTopRightTag.search(/ext=\.html$/) != -1 || imgTopRightHref == "") {
        bannerHTML = '<HTML><IFRAME src="' + imgTopRightTag + '" frameborder="0" scrolling="no" width="120" height="90" marginwidth="0" marginheight="0"></iframe></HTML>';
    }
    else if (0 == bannerHTML.indexOf("http")) {
        bannerHTML = '<HTML><CENTER><A HREF="' + imgTopRightHref + '" target="_blank"><IMG SRC="' + imgTopRightTag + '" border="0"></A></CENTER></HTML>';
    }
    else {
        bannerHTML = '<HTML>' + imgTopRightTag + '</HTML>';
    }

    //alert("showImgAdTopRight " + bannerHTML); 	
    document.getElementById('AD_Right').innerHTML = bannerHTML;
}

function showImgAd0(src, href, type) {
    clearInterval(img0Interval);
	if (g_embed) return;
    img0Tag = src;
    img0Href = href;
    img0Type = type;
    document.getElementById("AD_0_adi").style.display = "none";
    document.getElementById('AD_0').style.display = "";
    refreshImgAd0();
    //img0Interval = setInterval("refreshImgAd0()", 300000);
}

function refreshImgAd0() {
    // alert("refreshImgAd0 src - " + img0Tag + ", href - " + img0Href);
    var bannerHTML = img0Tag;
    //if (img0Tag.search(/\/adiframe\//) != -1 || img0Tag.search(/ext=\.swf$/) != -1 || img0Tag.search(/ext=\.html$/) != -1 || img0Href == "") 
    if (img0Type == "img")
    {
        bannerHTML = '<HTML><IFRAME src="' + img0Tag + '" frameborder="0" scrolling="no" width="728" height="90" marginwidth="0" marginheight="0"></iframe></HTML>';
    }
    //else if (0 == bannerHTML.indexOf("http")) 
    else if (img0Type == "fimg")
    {
        bannerHTML = '<HTML><CENTER><A HREF="' + img0Href + '" target="_blank"><IMG SRC="' + img0Tag + '" border="0"></A></CENTER></HTML>';
    }
    else if (img0Type == "fswf")
    {
    	bannerHTML = '<object width="728" height="90" id="embed" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000">'
    	            + '<param name="movie" value="' + img0Tag + '" />'
    	            + '<embed name="embed" pluginspage="http://www.adobe.com/go/getflashplayer" src="'
    	            + img0Tag + '" type="application/x-shockwave-flash" width="728" height="90"></embed>'
    	            + '</object>';
    }
    else {
        bannerHTML = '<HTML>' + img0Tag + '</HTML>';
    }

    //alert("showImg0Ad " + bannerHTML); 	
    document.getElementById('AD_0').innerHTML = bannerHTML;
}

function showImgAd1(src, href, type) {
    clearInterval(img1Interval);
	if (g_embed) return;
    img1Tag = src;
    img1Href = href;
    img1Type = type;

    document.getElementById("AD_1_adi").style.display = "none";
    adNowIsShown = true;
    
	var fb = document.getElementById('facebook_container');
    if (!fb || fb.style.display != "") {
        document.getElementById('AD_1').style.display = "";
        refreshImgAd1();
    }
    //img1Interval = setInterval("refreshImgAd1()", 300000);
}

function refreshImgAd1() {
    // alert("refreshImgAd1 src - " + img1Tag + ", href - " + img1Href);
    var bannerHTML = img1Tag;
    //if (img1Tag.search(/\/adiframe\//) != -1 || img1Tag.search(/ext=\.swf$/) != -1 || img1Tag.search(/ext=\.html$/) != -1 || img1Href == "") 
    if (img1Type == "img")
    {
        bannerHTML = '<HTML><IFRAME src="' + img1Tag + '" frameborder="0" scrolling="no" width="300" height="250" marginwidth="0" marginheight="0"></iframe></HTML>';
    }
    //else if (0 == bannerHTML.indexOf("http")) 
    else if (img1Type == "fimg")
    {
        bannerHTML = '<HTML><CENTER><A HREF="' + img1Href + '" target="_blank"><IMG SRC="' + img1Tag + '" border="0"></A></CENTER></HTML>';
    }
    else if (img1Type == "fswf")
    {
    	bannerHTML = '<object width="300" height="250" id="embed" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000">'
    	            + '<param name="movie" value="' + img1Tag + '" />'
    	            + '<embed name="embed" pluginspage="http://www.adobe.com/go/getflashplayer" src="'
    	            + img1Tag + '" type="application/x-shockwave-flash" width="300" height="250"></embed>'
    	            + '</object>';
    }
    else {
        bannerHTML = '<HTML>' + img1Tag + '</HTML>';
    }
    
    //alert("showImg1Ad " + bannerHTML);
    document.getElementById('AD_1').innerHTML = bannerHTML;
}

function repcCompAd0() {
    //alert(frames['iframe_AD_0'].document.getElementById('getAdCall').innerHTML);
    var e = frames['iframe_AD_0'].document.getElementById('getAdCall');
    if (e) document.getElementById('AD_0').innerHTML = e.innerHTML;
}

function showCompAd0(adtag) {
    clearInterval(img0Interval);
	if (g_embed) return;
	comp0Tag = adtag;
	document.getElementById("AD_0_adi").style.display = "none";
	document.getElementById('AD_0').style.display = "";
    refreshCompAd0();
    //img0Interval = setInterval("refreshCompAd0()", 300000);
}

function refreshCompAd0() {
    // alert("refreshCompAd0 " + comp0Tag);
    if (document.getElementById("iframe_AD_0")) {
        if (document.images) {
            frames["iframe_AD_0"].location.replace(adTagCall + '?adtag=' + encodeURIComponent(comp0Tag));
        }
        else {
            //Old Browsers
            //frames[iframe.name].location.href = '/'+src;
        }
    }
    else {
        document.getElementById('AD_0_hidden').innerHTML = '<if' + 'rame id="iframe_AD_0" name="iframe_AD_0" src="' + adTagCall + '?adtag=' + encodeURIComponent(comp0Tag) + '"><\/ifr' + 'ame>';
    }

    var ad_timer_0 = setTimeout('repcCompAd0()', 5000);
}

function repcCompAd1() {
    //alert(frames['iframe_AD_1'].document.getElementById('getAdCall').innerHTML);
    var e = frames['iframe_AD_1'].document.getElementById('getAdCall');
    if (e) document.getElementById('AD_1').innerHTML = e.innerHTML;
}

function showCompAd1(adtag) {
    clearInterval(img1Interval);
	if (g_embed) return;
	comp1Tag = adtag;
	document.getElementById("AD_1_adi").style.display = "none";
	document.getElementById('AD_1').style.display = "";
    refreshCompAd1();
    //img1Interval = setInterval("refreshCompAd1()", 300000);
}

function refreshCompAd1() {
    // alert("refreshCompAd1 " + comp1Tag);
    if (document.getElementById("iframe_AD_1")) {
        if (document.images) {
            frames["iframe_AD_1"].location.replace(adTagCall + '?adtag=' + encodeURIComponent(comp1Tag));
        }
        else {
            //Old Browsers
            //frames[iframe.name].location.href = '/'+src;
        }
    }
    else {
        document.getElementById('AD_1_hidden').innerHTML = '<if' + 'rame id="iframe_AD_1" name="iframe_AD_1" src="' + adTagCall + '?adtag=' + encodeURIComponent(comp1Tag) + '"><\/ifr' + 'ame>';
    }

    var ad_timer_1 = setTimeout('repcCompAd1()', 5000);
}

function showCompAdi0(adtag) {
    clearInterval(img0Interval);
	if (g_embed) return;
	compAdi0Tag = adtag;
	document.getElementById("AD_0").style.display = "none";
	document.getElementById('AD_0_adi').style.display = "";
    refreshCompAdi0();
    //img0Interval = setInterval("refreshCompAdi0()", 300000);
}

function refreshCompAdi0() {
    // alert("refreshCompAdi0 " + compAdi0Tag);
    document.getElementById('AD_0_adi_iframe').src = compAdi0Tag;
}

function showCompAdi1(adtag) {
    // alert("showCompAd1");
    clearInterval(img1Interval);
	if (g_embed) return;
	compAdi1Tag = adtag;
	document.getElementById("AD_1").style.display = "none";
	document.getElementById('AD_1_adi').style.display = "";
    refreshCompAdi1();
    //img1Interval = setInterval("refreshCompAdi1()", 300000);
}

function refreshCompAdi1() {
    // alert("refreshCompAdi1 " + compAdi1Tag);
    document.getElementById('AD_1_adi_iframe').src = compAdi1Tag;
}

function hideAcudeoPlayer() {
    document.getElementById("flashPreroll").style.zIndex = -2;
}

function hideAdComp() {
    adNowIsShown = false;
	document.getElementById("AD_1").innerHTML = "";
	document.getElementById("AD_1").style.display = "none";
	document.getElementById("AD_1_adi").style.display = "none";
}

function isEmbedSameDomain()
{
    var url1 = window.location.href;
    var url2 = "";
    try{
	    url2 = parent.location.href;
	} catch (e) { }
	var ret = (getDomain(url1) == getDomain(url2) ? "same" : "");
	// alert("domain " + ret + ", self " + url1 + ", parent " + url2);
	return ret;
}

function getDomain(url)
{
	if (!url) return "";
	var i = url.indexOf("://");
	if (i > 0) url = url.substr(i + 3);
	i = url.indexOf("/");
	if (i > 0) url = url.substring(0, i);
	i = url.indexOf(":");
	if (i > 0) url = url.substring(0, i);
	return url;
}