﻿var indep_document;
var indep_window;
var indep_load_events;
var indep_load_timer;

function indepDomReady(func, newDocument, newWindow, adId)
{
    if (adId)
    {
        indep_document = newDocument ? newDocument : document;
        indep_window = newWindow ? newWindow : window;

	    if (!indep_load_events)
	    {
		    var init = function ()
		    {
				    if (arguments.callee.done) return;
				    arguments.callee.done = true;
				    if (indep_load_timer)
				    {
					    clearInterval(indep_load_timer);
					    indep_load_timer = null;
				    }
				    for (var i=0;i < indep_load_events.length;i++)
				    {   
					    indep_load_events[i].func(indep_load_events[i].document,indep_load_events[i].adId);
				    }
				    indep_load_events = null;
				    indep_document = document;
				    indep_window = window;
		    };
    		
		    if (indep_document.addEventListener)
		    {
			    indep_document.addEventListener("DOMContentLoaded", init, false);
		    }
    		
		    if (/WebKit/i.test(navigator.userAgent))
		    {
			    indep_load_timer = setInterval(function()
			    {
				    if (/loaded|complete/.test(indep_document.readyState))
				    {
					    init();
				    }
			    }
			    , 10);
		    }
    		
	        indep_window.onload = init;
	        indep_load_events = [];
	    }
	    indep_load_events.push({func:func,document:indep_document,adId:adId});
	}
	else
	    originalIndepDomReady(func);
}

function originalIndepDomReady(func)
{
 	if (!indep_load_events)
	{
        indep_document = document;
        indep_window = window;
	
		var init = function ()
		{
				if (arguments.callee.done) return;
				arguments.callee.done = true;
				if (indep_load_timer)
				{
					clearInterval(indep_load_timer);
					indep_load_timer = null;
				}
				for (var i=0;i < indep_load_events.length;i++)
				{   
					indep_load_events[i];
				}
				indep_load_events = null;
				indep_document = document;
				indep_window = window;
		};
		
		if (indep_document.addEventListener)
		{
			indep_document.addEventListener("DOMContentLoaded", init, false);
		}
		
		if (/WebKit/i.test(navigator.userAgent))
		{
			indep_load_timer = setInterval(function()
			{
				if (/loaded|complete/.test(indep_document.readyState))
				{
					init();
				}
			}
			, 10);
		}
		
	    indep_window.onload = init;
	    indep_load_events = [];
	}
	indep_load_events.push(func);
}

function moveAdToParent(targetDoc, id)
{
    targetDoc = targetDoc ? targetDoc : document;
    var divToMove = targetDoc.getElementById('adDiv');
    divToMove.removeChild(targetDoc.getElementById('removeMeAdScript'));
    if (PositionDCAd)
        PositionDCAd(divToMove, id);
}

function getAdQueryParams(queryString)
{
    var firstIndex = queryString.indexOf("&");
    var id = queryString.substring(0,firstIndex).split("=")[1];
    var adUrl = queryString.substring(firstIndex+1);
    adUrl = adUrl.replace("__ord__",Math.random()*10000000000000000);
    
    return {id:id,adUrl:adUrl};
}

function resizeAdFrame(iFrame)
{
    if (iFrame)
    {
        if(document.getElementById && !(document.all)) 
        {
            var contentHeight = (iFrame.contentDocument.getElementById('adDiv').clientHeight) - 5;
            iFrame.style.height = (contentHeight > 15 ? contentHeight : 0) + 'px';
        }
        else if(document.all) 
        {
            var contentHeight = iFrame.contentWindow.document.body.clientHeight;
            iFrame.style.height = (contentHeight > 15 ? contentHeight : 0) + 'px';
        }
    }
}