function filemobile()
{}


filemobile.ad = function( pJsonRequestUrl )
{	
	this.StartTrackerImage = null;
	this.EndTrackerImage = null;
	
	this.IsLoaded = false;
	this.IsDestroyed = false;
	
	this.LoaderElement = null;
	
	this.InstanceIndex = filemobile.ad.setInstance( this );
	
	if( pJsonRequestUrl )
	{
		this.jsonRequest( pJsonRequestUrl );
	}
};

filemobile.ad.LOADER_TIMEOUT = 4; // seconds
filemobile.ad.START_TRACKER_VARIABLE = "StartPlayImage";
filemobile.ad.END_TRACKER_VARIABLE = "EndPlayImage";

filemobile.ad.getInstance = function( i )
{
	if( ! filemobile.ad.__instance )
	{
		throw "Trying to get an instance of the ad before it has been initialized";
		return;
	}
	else if( i != null && i >= 0 )
	{	
		return filemobile.ad.__instance[i];
	}
	else
	{
		return filemobile.ad.__instance[ filemobile.ad.__instance.length - 1 ];
	}
};

filemobile.ad.setInstance = function( pAd )
{
	if( ! filemobile.ad.__instance )
	{
		filemobile.ad.__instance = new Array();
	}
	
	filemobile.ad.__instance.push( pAd );
	
	return filemobile.ad.__instance.length - 1;
};

filemobile.ad.prototype.jsonRequest = function( pUrl )
{
	var ScriptLoader = document.createElement("script");
	ScriptLoader.src = pUrl;
	ScriptLoader.type = "text/javascript";
	document.getElementsByTagName("head")[0].appendChild( ScriptLoader );

	this.LoaderElement = ScriptLoader;
	
	setTimeout( "filemobile.ad.getInstance(" + this.InstanceIndex + ").checkForLoadFailure()", filemobile.ad.LOADER_TIMEOUT * 1000 );
};

filemobile.ad.prototype.onStart = function()
{
	if( this.StartTrackerImage ) ( new Image() ).src = this.StartTrackerImage;		
};


filemobile.ad.prototype.onEnd = function()
{
	if( this.EndTrackerImage ) ( new Image() ).src = this.EndTrackerImage;		
};

filemobile.ad.load = function( pAdDetails )
{
	var _Ad = filemobile.ad.getInstance();
	
	if( ! _Ad.IsDestroyed )
	{
		var StartTrackerImage = eval( "pAdDetails." + filemobile.ad.START_TRACKER_VARIABLE );
		if( StartTrackerImage )
		{
			_Ad.StartTrackerImage = StartTrackerImage;
		}
		
		var EndTrackerImage = eval( "pAdDetails." + filemobile.ad.END_TRACKER_VARIABLE );
		if( EndTrackerImage )
		{
			_Ad.EndTrackerImage = EndTrackerImage;
		}
        
        if( pAdDetails.Preroll )
		{
			_Ad.loadPreroll( pAdDetails );
		}
		else if( pAdDetails.Preroll )
		{
			_Ad.loadPostroll( pAdDetails );
		}
		else if( pAdDetails.Interstitial )
		{
			_Ad.loadInterstitial( pAdDetails );
		}
		
        _Ad.IsLoaded = true;
	}
};

filemobile.ad.prototype.checkForLoadFailure = function() 
{
	if( ! this.IsLoaded )
	{
		this.destroy();
		throw "Failed to load Ad " + this.InstanceIndex;
	}
	else
	{
		this.IsLoaded = true;
	}
};

filemobile.ad.prototype.destroy = function()
{
	this.IsDestroyed = true;
	
	try
	{
		this.LoaderElement.parentNode.removeChild( this.LoaderElement ); 
	}
	catch( Error )
	{}
    
	filemobile.ad.__instance[ this.InstanceIndex ] = null;	
};

filemobile.ad.prototype.play = function( pUrl, clicklink, pOrientation )
{
	//console.log(clicklink);
    // Call a function in all the Filemobile flash widgets on the page
    var Embeds = document.getElementsByTagName("embed");
    var Objects = document.getElementsByTagName("object");
    
    for( var i = 0; i < Embeds.length; i++ )
    {
        try
        {
            Embeds[i].sendAdInfo ( pUrl, clicklink, pOrientation );
        }
        catch( err ) {}
    }
    
    for( var i = 0; i < Objects.length; i++ )
    {
        try
        {
            Objects[i].sendAdInfo ( pUrl, clicklink, pOrientation );
        }
        catch( err ) {}
    }
};

//*****************************************************//
//	ALL FUNCTIONS AFTER THIS POINT CAN BE OVERWRITTEN
//*****************************************************//

filemobile.ad.LOADER_TIMEOUT = 4; // seconds
filemobile.ad.START_TRACKER_VARIABLE = "StartPlayImage";
filemobile.ad.END_TRACKER_VARIABLE = "EndPlayImage";

filemobile.ad.getPreroll = function( )
{
	//var AdUrl = "http://adserver.adtechus.com/?adrawdata|3.0|5152|507042|0|1293|ADTECH;loc=100;noperf=1;target=_blank;key=fmvidtest+key2+key3+key4;grp=[group];misc=";    
    var AdUrl = "http://adserver.adtechus.com/?adrawdata|3.0|5152|507072|0|1293|ADTECH;loc=100;noperf=1;target=_blank;key=key1+key2+key3+key4;grp=[group];misc=[TIMESTAMP]";    
    
	AdUrl += ";ord=" + Math.round( 10000000 * Math.random() );
    
	var PrerollAd = new filemobile.ad( AdUrl );
	
	//console.log(AdUrl);
	
	return true;	
};

filemobile.ad.getPostroll = function( )
{
    return false;
};

filemobile.ad.getInterstitial = function( )
{
    return false;
};

filemobile.ad.prototype.loadPreroll = function( pAdDetails )
{	
	//Log( "Got a preroll ad: " + pAdDetails.Preroll );
    
    this.play( pAdDetails.Preroll, pAdDetails.VideoClickURL);
};

filemobile.ad.prototype.loadPostroll = function( pAdDetails )
{		
    this.play( pAdDetails.Postroll );
};

filemobile.ad.prototype.loadInterstitial = function( pAdDetails )
{		
    this.play( pAdDetails.Interstitial, pAdDetails.Alignment );
};

///////////TESTING////////////
function Ad()
{
	
}

Ad.Load = function( pAdInfo )
{
	filemobile.ad.load( pAdInfo );
};