//iFrame src now handled in javascript. doubleclick double requests fixed. 1x1 placements ignored for refreshes. 
var debug=false;

var currentWidth = 0;
var currentHeight = 0;

//writes out the iFrame but uses the pseudo-URL construction src="javascript:parent.getIFrame()" in order load the iFrame.
function writeIFrame(tile, tag, width, height, isDcoptNecessary, ord){

	if ( isIFrameEnabled ()) {		
    
		arg="javascript:parent.getIFrame(" + width + ", " + height + ", " + tile + ")";
		var placer="";
		placer='<scr';
		placer+='ipt language="JavaScript" ';
		placer+='src="';
		placer+=unescape(tag);
		placer+='tile=';
		placer+=tile;
		placer+=';';
		if (isDcoptNecessary){
			placer+='dcopt=ist;'
		}
		placer+='sz=' + this.width + 'x' + this.height + ';ord=' + ord + '?" type="text/javascript"></scr' + 'ipt>';
		
		document.write('<div id="adTag' + tile + '"><iframe id="iframe' + tile + '" src="' + arg + '" width="' + this.width + '" height="' + this.height + '" frameborder="1" scrolling="no" marginheight="0" marginwidth="0"></iframe></div>');
		if(tile==1){
			callAfterDelay ( 'Reload ()', 180 );
		}
	
	} else {

   		if (isDcoptNecessary==1){
			var dcopt="dcopt=ist;";
		} else {
			var dcopt="";
		}
        //alert ( 'width: ' + width + ' and height: ' + height );

   		var adPlacement ='<script language="JavaScript" src="' + unescape(tag) + 'tile=' + tile + ';' + dcopt + 'sz=' + width + 'x' + height + ';ord=' + ord + '?" type="text/javascript"></scr' + 'ipt>';
		document.write(adPlacement);

	}
}



function getIFrame( frameWidth, frameHeight, tile ){

	var placer="";
	placer='<scr';
	placer+='ipt language="JavaScript" ';
	placer+='src="';
	placer+=unescape(tag);
	placer+='tile=';
	placer+=tile;
	placer+=';';
	if (isDcoptNecessary){
		placer+='dcopt=ist;'
	}
	placer+='sz=' + frameWidth + 'x' + frameHeight + ';ord=' + ord + '?" type="text/javascript"></scr' + 'ipt>';
	return placer;
}
	
	
//delayed script calling, note parameter has been changed to delayInSeconds from delayInMinutes to reflect how it actually works.
function callAfterDelay ( signatureOfJavascriptFunction, delayInSeconds )
{
    var DEFAULT_DELAY = 5;
    if ( signatureOfJavascriptFunction == null )
    {
        return false;
    }
    else
    {
        jsSignature = signatureOfJavascriptFunction;
    }
    if ( ( delayInSeconds == null ) || ( isNaN ( delayInSeconds ) ) )
    {
        delayInSeconds = DEFAULT_DELAY;
    }
    else
    {
        // alert ( 'delayInMinutes: ' + delayInMinutes );
    } 
	var millisecs = convertSecondsToMilliseconds ( delayInSeconds );
    // alert ( 'miilisecs: ' + millisecs );

    var timerId = window.setInterval ( signatureOfJavascriptFunction, millisecs );
    //alert ( 'about to do the timer thing');
}
//utililty function for time conversion
function convertSecondsToMilliseconds ( numberOfSeconds )
{
    if ( ( numberOfSeconds == null ) || ( isNaN ( numberOfSeconds ) ) )
    {
        return 0;
    }
    else
    {
        return numberOfSeconds * 1000;
    }
}
//generates a new ord, then reloads all named iFrames on the page. 
function Reload ()
{
    if ( debug )
    {
        //ReloadDebug ();
    }
    else
    {
        var oldOrd = ord;
        //DoubleClick seems to like rounded numbers.
        ord = Math.round(Math.random()*10000000000000000);
        for ( var i=1; i <= tile; ++i )
        {
            var iFrame = document.getElementById( 'iframe' + i );
            
            if(document.getElementById( 'iframe' + i ).width==1 && document.getElementById( 'iframe' + i ).height==1){
            	//do nothing, ignore 1x1 placements for refreshing
            } else {                      
	            if ( iFrame != null )
	            {
	                var newSource = iFrame.src;
	                
	                var oldStr = '&ord=' + oldOrd;
	                var newStr = '&ord=' + ord;
	                newSource = newSource.replace ( oldStr, newStr );
	
	              // alert ( 'oldOrd=' + oldStr + ' new ord=' + newStr + ' and final url: ' + newSource );
	                iFrame.src = newSource;
	            }
            }
        }
    }
}

//can this browser handle frames?
function isIFrameEnabled()
{
    var inUserAgent = navigator.userAgent;

    //alert( "userAgent: " + inUserAgent );

 

    // Firefox 1.5 or greater
    if ( inUserAgent.indexOf( "Firefox/" ) != -1 )
    {
        var startIndex = inUserAgent.indexOf( "Firefox/") + 8;
        var version = parseFloat( inUserAgent.substring( startIndex, startIndex + 3 ) );
        if ( version >= 1.5 ) 
            return true;
    }

    // Safari 1.3 or greater
    if ( inUserAgent.indexOf( "Safari/" ) != -1 )
    {
        var startIndex = inUserAgent.indexOf( "Safari/") + 7;
        var version = parseFloat( inUserAgent.substring( startIndex, startIndex + 4 ) );
        // safari uses an odd version convention
        if ( parseFloat( inUserAgent.substring( startIndex, startIndex + 3 ) ) >= 312 ) 
            return true;
    }

    return false;
}
