var useImages = false;
var IE = (navigator.appName=="Microsoft Internet Explorer");

function setAdCookie(c_name,value,expireminutes)
{
var exTime=new Date();
exTime.setTime( exTime.getTime() + (expireminutes * 60000) );
document.cookie=c_name+ "=" +escape(value)+
((expireminutes==null) ? "" : ";expires="+exTime.toUTCString());
}
function getAdCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}


// Called when a Rich Media IFRAME completes its loading
function adsRMIFOnL(win, doc)
{
//    var adFrame = win.frameElement;
    var adFrame = document.getElementById("adFrame");

    var div = adFrame.parentNode;

        var adSpan = doc.getElementById("adSpan");
        var adDiv = doc.getElementById("adDiv");

        if (adSpan)
        {
	    var w = adSpan.offsetWidth;

	    if (IE)
	        var h = adSpan.offsetHeight;
	    else
		var h = adDiv.offsetHeight;
	    adFrame.style.width = w + "px";
	    adFrame.style.height = h + "px";
        }

}


// Returns the URL of for a given frame. This assumes the IFRAME is in a
// div and the div has the adURL property set to the URL that the 
// IFRAME should use to load an ad in a <SCRIPT> tag. This is used by the
// addoc.htm file.
function adsGetAdURL(win)
{
    var adFrame = win.frameElement;
    var div = adFrame.parentNode;

    return div.adURL;
}

// Called by the Frame source when an ad frame is created. Loads the
// ad reference document into the IFrame
function LoadFrame(win, doc)
{
    var div = win.frameElement.parentNode;
    var content;

//    content = "<html><body onload='if (parent.adsRMIFOnL) parent.adsRMIFOnL(window, document);'>";
    content = "<html><body onload='if (parent.adsRMIFOnL) setTimeout(\"parent.adsRMIFOnL(window, document)\", 100);'>";

    content += "<scr"+"ipt>inFIF=true;</scr"+"ipt>";
    content += "<scr"+"ipt>inDapIF=true;</scr"+"ipt>";

    content += "<scr" + "ipt type='text/javascript'> function closeDocument() { if(event.srcElement.readyState && event.srcElement.readyState == 'complete') { window.setTimeout(\'document.close()\', 100); } }</scr" + "ipt>";

    content += "<scr"+"ipt id='adscr' type='text/javascript' src='"+div.adURL+"'";
    content += " onreadystatechange='closeDocument();'";
    content += "></scr"+"ipt>";

    content += "</body></html>";

    // Write the content string to the document
    doc.write(content);
}

function RemoveChildren(obj)
{
    var iframe = null;

    while (obj.childNodes.length > 0) 
    {
        var child = obj.childNodes[0];

	var id = child.id;

	if (id == "adFrame")
	{
	    iframe = child;
	    iframe.src = "about:blank";
	}

	if (id)
	    child.id = "";

        if (child.childNodes.length > 0)
	    RemoveChildren(child);

        obj.removeChild(child);
    }
}

// Clear the contents of the Ad DIV
function ClearDiv()
{
    RemoveChildren(this);
}

// Clear the ad DIV, add the IFRAME, and make the ad call
function LoadAd()
{
	 var pathName = location.pathname;
	 pathArray = window.location.pathname.split( '/' );
    	 if (pathArray[pathArray.length] != null)
		pathName =  pathArray[pathArray.length];
	else
		pathName =  pathArray[pathArray.length-1];
	// First clear any existing content in the DIV
        this.ClearAd();
	 
        closeAd=getAdCookie('TSJcloseAd_'+ pathName);
        if (!(closeAd)) {
        // Create close ad button
    	 //var spanCloseAdButton = document.createElement("span");
	 //spanCloseAdButton.id = "TSJ_closeAd";
    	 //spanCloseAdButton.innerHTML = "<a href='javascript:void(0);' onclick='TSJ_dynamicAD.CloseAd();' >X</a>"; 
    	 //this.appendChild(spanCloseAdButton);

        // Create the ad call IFRAME
        var iframe = document.createElement('iframe');

        // Set the properties of the iframe
        iframe.id = "adFrame";
        iframe.style.height = this.h;
        iframe.style.width = this.w;
        iframe.marginWidth = 0;
        iframe.marginHeight = 0;
        iframe.frameBorder = 0;
        iframe.scrolling = "no";
        iframe.width = this.w;
        iframe.height = this.h;

        // Put the new IFRAME into the ad div
        this.appendChild(iframe);

        // If there is a page based URL, use it instead of doc.writing into the
        // IFRAME
        if (true){
    	    iframe.src = this.adPage;
        } else
        {
	    // Load the frame's content (make the ad call)
	    iframe.src = "javascript:void(parent.LoadFrame(this, document))";
        }

}
}

function CloseAd() 
{
	 var pathName = location.pathname;
	 pathArray = window.location.pathname.split( '/' );
    	 if (pathArray[pathArray.length] != null)
		pathName =  pathArray[pathArray.length];
	else
		pathName =  pathArray[pathArray.length-1];
	closeAd=getAdCookie('TSJcloseAd_'+pathName);
       if (closeAd)
	    setAdCookie('TSJcloseAd_'+pathName,false,1);
       else
           setAdCookie('TSJcloseAd_'+pathName,true,1);
       this.LoadAd();
}


function DOMCreateAdDiv(adPosition, clickPosition, w, h, adURL, adPage)
{
    var did = "tsj_"+adPosition;

    // Create the ad call IFRAME
    var div = document.createElement('div');
    
    // Set the DIV ID
    div.id = did;
    div.className = "gbl_dynamic_adstruct";

    // Setup the DIV attributes
    SetupAdDiv(div, clickPosition, w, h, adURL, adPage);
    return div;
}

function DocWriteAdDiv(adPosition, clickPosition, w, h, adURL, adPage)
{
    var did = "tsj_"+adPosition;

    // Doc write the new DIV
    document.write("<div id='" + did + "' class='gbl_dynamic_adstruct'></div>");

    // Get the new DIV object
    var div = document.getElementById(did);

    // Setup the DIV attributes
    SetupAdDiv(div, clickPosition, w, h, adURL, adPage);
    return div;
}


function SetupAdDiv(div, clickPos, w, h, adURL, adPage)
{
    // Setup the onload handler

    div.RMIFOnLoad = adsRMIFOnL;
    div.LoadAd = LoadAd;
    div.CloseAd = CloseAd;
    div.ClearAd = ClearDiv;
    div.w = w;
    div.h = h;
    div.adURL = adURL;
    div.adPage = adPage;
    div.clickPosition = clickPos;
}

