/* AFUP - AdForum UniversalPlayer Singleton
	@author: jmserra
	@date 2009/09/25
*/

var AFUP = new function() {

	/************************************************************
	 * Class Variables
	 ************************************************************/
	this.preUrl;
    this.embeddedTypes = {};
    this.initialized = false;
    this.batags = {};

	/************************************************************
	 * Class Functions
	 ************************************************************/
	
	/*
	  Sets the BAT Tracker Tag will be used for each event
	*/
	this.setEventTracker = function(event,tag)
	{
		if(event!='load' && event!='play' && event!='pause' && event!='complete')
		{
			return false;
		}
	
		this.batags[event] = tag;
		return true;
	}
	
    this.embed = function(type,contentid,placeholderAndSize,parameters)
	{
		//THERES NO Tracking in the Embed mode at this moment
		///this.init(); NOT CALLING INIT TO NOT RELOAD CSS AND JS THAT ALREADY LOADED THE PAGE
		//for the new player...
		
		var loadedMedias='';
		jQuery.each(this.embeddedTypes, function(key, value) {
			loadedMedias = loadedMedias+key+";";
		 });

		//parameters
		var strParameters='';
		if(parameters)
		{
			strParameters='&';
			jQuery.each(parameters, function(key, value) {
				strParameters = strParameters+key+"="+value+"&";
			 });
		}

		placeholder = placeholderAndSize[0];
		w = placeholderAndSize[1];
		h = placeholderAndSize[2];
		$('#'+placeholder).html('&nbsp;');
		$('#'+placeholder).load(this.preUrl+'/render.php?type='+type+'&id='+contentid+'&w='+w+'&h='+h+'&loaded='+loadedMedias+strParameters);
		
		//Set to true that we loaded the required resources for this type
		this.embeddedTypes[type] = true;

	}
	 
    this.overlay = function(type,contentid,position,parameters)
	{
		this.init();
		
		batags_str = this.getBatagsString();
	
		player_id = 'afup_'+contentid;
	
		x = position[0];
		y = position[1];
		w = position[2];
		h = position[3];
		ifh = h; 
		ifw = w;
		ovh = h+35;
		ovw = w+6;		
		
		
		//extra pixels are REQUIRED FOR Display properly on IE'S
		if ($.browser.msie) {
			ifh = ifh+10; 
			ovw = ovw+4;
		}
		
	
		title = parameters.title;
		
		iframe_code='<iframe allowtransparency="true" vspace="0" hspace="0" marginheight="0" marginwidth="0" src="'+this.preUrl+'/render.php?type='+type+'&id='+contentid+'&w='+w+'&h='+h+'&batags='+batags_str+'" frameborder="0" height="'+ifh+'px" scrolling="no" width="'+ifw+'"></iframe>';
		
				
		if ( $('#'+player_id).length )
		{
			$('#'+player_id).html(iframe_code);
			$('#'+player_id).dialog('open');
		}
		else
		{
			
			$("body").append(' <div id="'+player_id+'" > <center>'+iframe_code+'</center></div>');
			$('#'+player_id).dialog({ 
				height:ovh,
				width:ovw,
				position:[x,y],
				title:title,
				resizable: false,
				modal: true,
				beforeclose: function(event, ui) {
					$('#'+player_id).html('&nbsp;');
				}
			});
		}
		
		//force height
		$('#'+player_id).dialog('option' , 'height' , ovh );
		
		return true;
	}
	
    this.iframe = function(type,contentid,placeholderAndSize,parameters)
	{
		//force preurl to www, andyawards compatible
		this.preUrl = 'http://www.adforum.com/afup';
	
		placeholder = placeholderAndSize[0];
		w = placeholderAndSize[1];
		h = placeholderAndSize[2];
		ifh = h;
		ifw = w;
		
		//parameters
		var strParameters='';
		if(parameters)
		{
			strParameters='&';			 
			for(var key in parameters)
			{
				strParameters = strParameters+key+"="+parameters[key]+"&";
			}
		}		
		
		iframe_code='<iframe allowtransparency="true" vspace="0" hspace="0" marginheight="0" marginwidth="0" src="'+this.preUrl+'/render.php?type='+type+'&id='+contentid+'&w='+w+'&h='+h+strParameters+'" frameborder="0" height="'+ifh+'px" scrolling="no" width="'+ifw+'"></iframe>';
		
		
	
		//$('#'+placeholder).html(iframe_code);
		document.getElementById(placeholder).innerHTML = iframe_code; 

		return true;
	}
    
    /*
    	Serializes the TAG codes to send to the Render.php
    */
    this.getBatagsString = function()
    {
		batags_str = '';
		for (var key in this.batags)
		{
			batags_str = batags_str + key+':'+this.batags[key]+'|';
		}
		
		return batags_str;
	}
    
    /*Initializes the class, loads required JS and CSS*/
    this.init = function()
	{
		if(!this.initialized)
		{
			var headID = document.getElementsByTagName("head")[0];  
			       
			var newScript1 = document.createElement('script');
			newScript1.type = 'text/javascript';
			newScript1.src = this.preUrl+'/jquery132_ui172.js';
			headID.appendChild(newScript1);
		
			var cssNode = document.createElement('link');
			cssNode.type = 'text/css';
			cssNode.rel = 'stylesheet';
			cssNode.href = this.preUrl+'/smoothness/jquery-ui-1.7.2.custom.css';
			cssNode.media = 'screen';
			headID.appendChild(cssNode);

			this.initialized = true;
		}	
	}
	

	/************************************************************
	* Constructor
	************************************************************/
	
	//Set preurl
/*	if(window.location.hostname == 'localadforum.com' || window.location.hostname == 'www.localadforum.com' || window.location.hostname == 'qa.adforum.net' || window.location.hostname == 'tools.adforum.com' )
	{
		this.preUrl = '/afup';
	}
	else
	{
		this.preUrl = 'http://www.adforum.com/afup';
	}
*/
	this.preUrl = '/afup';
	
	// initialize the AFUP singleton  - AVOID IT because of the prototype compatibility problems
	///this.init();
}
