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 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 adTagCall = "getAjaxAd.jsp";
var adiTagCall = "getAjaxAdi.jsp";

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();
	playFactWMV(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();
	playFactWMV(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 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";
    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) {
    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 hideAdComp() {
	document.getElementById("AD_0").innerHTML = "";
    document.getElementById("AD_0").style.display = "none";
    document.getElementById("AD_0_adi").style.display = "none";
	document.getElementById("AD_1").innerHTML = "";
    document.getElementById("AD_1").style.display = "none";
    document.getElementById("AD_1_adi").style.display = "none";
}

function hideAdCompAd1() {
	document.getElementById("AD_1").innerHTML = "";
    document.getElementById("AD_1").style.display = "none";
    document.getElementById("AD_1_adi").style.display = "none";
}

function repcAudioCompAd() {
    //alert(frames['iframe_AD_audio'].document.getElementById('getAdCall').innerHTML);
    var e = frames['iframe_AD_audio'].document.getElementById('getAdCall');
    if (e) document.getElementById('AD_audio').innerHTML = e.innerHTML;
}

function showAudioCompAd(adtag) {
	if (g_embed) return;
    document.getElementById('AD_audio').style.display = "";

    // alert("showAudioCompAdi " + adtag);
    if (document.getElementById("iframe_AD_audio")) {
        if (document.images) {
            frames["iframe_AD_audio"].location.replace(adTagCall + '?adtag=' + encodeURIComponent(adtag));
        }
        else {
            //Old Browsers
            //frames[iframe.name].location.href = '/'+src;
        }
    }
    else {
        document.getElementById('AD_audio_hidden').innerHTML = '<if' + 'rame id="iframe_AD_audio" name="iframe_AD_audio" src="' + adTagCall + '?adtag=' + encodeURIComponent(adtag) + '"><\/ifr' + 'ame>';
    }

    var ad_timer_1 = setTimeout('repcAudioCompAd()', 5000);
}

function repcAudioCompAdi() {
    //alert(frames['iframe_AD_audio'].document.getElementById('getAdCall').innerHTML);
    var e = frames['iframe_AD_audio'].document.getElementById('getAdCall');
    if (e) document.getElementById('AD_audio').innerHTML = e.innerHTML;
}

function showAudioCompAdi(adtag) {
	if (g_embed) return;
    document.getElementById('AD_audio').style.display = "";

    // alert("showAudioCompAdi " + adtag);
    if (document.getElementById("iframe_AD_audio")) {
        if (document.images) {
            frames["iframe_AD_audio"].location.replace(adiTagCall + '?adtag=' + encodeURIComponent(adtag) + '&width=300&height=250');
        }
        else {
            //Old Browsers
            //frames[iframe.name].location.href = '/'+src;
        }
    }
    else {
        document.getElementById('AD_audio_hidden').innerHTML = '<if' + 'rame id="iframe_AD_audio" name="iframe_AD_audio" src="' + adiTagCall + '?adtag=' + encodeURIComponent(adtag) + '&width=300&height=250"><\/ifr' + 'ame>';
    }

    var ad_timer_1 = setTimeout('repcAudioCompAdi()', 2000);
}

function showAudioImageAd(src, href) {
	if (g_embed) return;
    document.getElementById('AD_audio').style.display = "";

    // alert("showAudioImageAd src - " + src + ", href - " + href);
    var bannerHTML = src;
    if (src.search(/ext=\.swf$/) != -1 || src.search(/ext=\.html$/) != -1 || href == "") {
        bannerHTML = '<HTML><IFRAME src="' + src + '" frameborder="0" scrolling="no" width="728" height="90" marginwidth="0" marginheight="0"></iframe></HTML>';
    }
    else if (0 == bannerHTML.indexOf("http")) {
        bannerHTML = '<HTML><CENTER><A HREF="' + href + '" target="_blank"><IMG SRC="' + src + '" border="0"></A></CENTER></HTML>';
    }
    else {
        bannerHTML = '<HTML>' + AD_audio + '</HTML>';
    }

    //alert("showImg1Ad " + bannerHTML);
    document.getElementById('AD_audio').innerHTML = bannerHTML;
}

function hideAudioAdComp() {
	document.getElementById("AD_audio").innerHTML = "";
    document.getElementById("AD_audio").style.display = "none";
}