/* requires core.js */
ADCENTRIC.lang.namespace("ADCENTRIC.architectures")

ADCENTRIC.env.architectures = ADCENTRIC.env.architectures || { };
ADCENTRIC.env.architectures['DOM_id_prefix_banner'] = "adcentric_banner";
ADCENTRIC.env.architectures['DOM_id_prefix_tla'] = "adcentric_tla";
ADCENTRIC.env.architectures['SWF_LOADER_BANNER_URL'] = ADCENTRIC.env.creatives[__ADC_ID].placements[__SECTION__].assets["BANNER_LOADER_SWF"];
ADCENTRIC.env.architectures['SWF_LOADER_TLA_URL'] = ADCENTRIC.env.creatives[__ADC_ID].placements[__SECTION__].assets["TLA_LOADER_SWF"];


/* XXX TO BE DEPRECATED, LEGACY adcClick()/adcAction() global-namespace API: */
function adcClick(crID, _name) { }
function adcAction(crID, _name, _obj) {

	var event_pixel = new Image();
	var rn = Math.random();
	var _obj_to_string = "";

	if(_obj){
		for(var key in _obj){
			_obj_to_string += key + "=" + _obj[key] + '&';
		}
		event_pixel.src = ADCENTRIC.env.creatives[crID].placements[__SECTION__].interactions[_name] + '?' + _obj_to_string + 'num=' +rn;
	}else{
		event_pixel.src = ADCENTRIC.env.creatives[crID].placements[__SECTION__].interactions[_name] + '?num=' + rn;
	}
}

/*
 * AdCentric Banner Architecture for Ripple
 *
 * A TLA is an out of page SWF, with no backup image.  Like any Ripple creative unit,
 * it can have multiple assets (additional files) associated with it, along with AdCentric interaction-tracking URLs and click-tracking
 * redirection URLs.
 *
 * A TLA DIV container is written out when its setup() method is called.  Flash-detection is performed and an interaction 'tla_not_displayed'
 * is triggered should we not detect the presence of the Adobe plugin.  The setup() method also sets up a window.onload event to polite-load the
 * actual SWF TLA object.  Because of this, it's possible to call setup() on a TLA  instance and override width, height, zindex,
 * position, or other attributes prior to window.onload firing (i.e., directly from the tag template).  The simplest TLA
 * tag template will look roughly like this:
 *
 * //...
 * var my_tla = ADCENTRIC.architectures.TLA('__ADC_ID', '__ADC_PLACEMENT_ID');
 * my_tla.setup();
 * //...
 *
 * Note that ADCENTRIC.architectures.TLA is constructed with a (creative_id, placement_id) 2-tuple.  This allows the constructor
 * to extract required configuration structure (prepended automagically by AdCentric when the creative is served) via the
 * ADCENTRIC.env.creatives[].placements[] matrix.
 *
 * Note also that ADCENTRIC.architectures.TLA takes care of setting up the required ExternalInterfaceProxy for the TLA
 * creative as well, so this does not need to be done separately prior to instantiating ADCENTRIC.architectures.TLA
 *
 *
 * Expected ADCENTRIC.env.creatives[creative_id] for TLA architecture, general structure:
 *
 * ADCENTRIC.env.creatives[creative_id].placements[placement_id].interactions[INTERACTION_LABEL]
 * ADCENTRIC.env.creatives[creative_id].placements[placement_id].clicks[CLICK_LABEL]
 * ADCENTRIC.env.creatives[creative_id].placements[placement_id].assets[ASSET_LABEL]
 * ADCENTRIC.env.creatives[creative_id].placements[placement_id].variables[VARIABLE_LABEL]
 *
 * Specific labels required:
 *
 * - Assets: TLA
 * - Clicks: CLICK
 * - Variables: WIDTH, HEIGHT
 *
 * Optional labels:
 *
 * - Variables: ZINDEX (default = '1000000'), POSITION (default = 'absolute'), WMODE (default = 'opaque'),
 *
 */

ADCENTRIC.architectures.TLA= function(creative_id, placement_id) {
	var cr = ADCENTRIC.env.creatives[creative_id].placements[placement_id];
	var zindex = cr.variables['TLA_ZINDEX'] || "1000000";
	var position = cr.variables['TLA_POSITION'] || "absolute";
	var width = cr.variables['TLA_WIDTH'];
	var height = cr.variables['TLA_HEIGHT'];
	var msie_wmode = cr.variables['TLA_MSIE_WMODE'] || "opaque";
	var ff_wmode  = cr.variables['TLA_FF_WMODE'] || "opaque";
	var banner_swf_url = cr.assets['TLA'];
	var left_offset = cr.variables['TLA_OFFSET_LEFT'] || 0;
	var top_offset = cr.variables['TLA_OFFSET_TOP'] || 0;
	var x_coord = default_pos('x');
	var y_coord = default_pos('y');
	var appear_delay = cr.variables['TLA_APPEAR_AFTER_SECS'] || 0;
	var kill_delay = cr.variables['TLA_KILL_AFTER_SECS'] || 10000;
	var close_on_mouse_out = (cr.variables['CLOSE_ON_MOUSEOUT'] == "true" ? true : false);
	var swf_id = Math.floor(Math.random() * 1000000);

	/***************************************************************************************************
	 ** putting an iframe shim below a flash tla, in FF, causes the banner to be unclickable.          **
	 ** The browser seems to not understand what is layered over what, the TLA or the iframe.          **
	 ** To bypass this issue, when using a shim and viewing the TLA in FF, wmode for the embed object  **
	 ** must be equal to "window"                                                                      **
	 ***************************************************************************************************/
	if(cr.variables['USE_SHIM']){
		ff_wmode = "window";
	}

	//depending on the user's browser, function returns the x or y coordinate of the TLA div container
	//should creative variables offset_left and offset_top be set (by the traffiker), these values will
	//be added to the calculated x and y coordinates
	//Without offset_left and offset_top values, the default position of a TLA is the center of the browser
	//window.
	function default_pos(coordinate){
		if (parseInt(navigator.appVersion)>3) {
			if (navigator.appName=="Netscape") {
				if(coordinate == 'x'){
					return (top.window.innerWidth - width)/2 + left_offset;
				}else if(coordinate == 'y'){
					return (top.window.innerHeight - height)/2 + top_offset;
				}else{
					return 300;
				}
			}
			if (navigator.appName.indexOf("Microsoft")!=-1) {
				if(coordinate == 'x'){
					return (top.document.body.offsetWidth - width)/2 + left_offset;
				}else if(coordinate == 'y'){
					return (top.document.body.offsetHeight - height)/2 + top_offset;
				}else{
					return 300;
				}
			}
		}
	}

	cr.lcPix_TLA = ADCENTRIC.util.ExternalInterfaceProxy(creative_id, placement_id, swf_id);

	return {
		adcAction: function(name, obj){
			// do adcAction
		},
		adcClick: function(name, obj){
			// do adcAction
		},
id: function() {
	    return ADCENTRIC.env.architectures['DOM_id_prefix_tla'] + "_" + String(creative_id) + "_" + String(placement_id);
    },
container_id: function() {
		      return ADCENTRIC.env.architectures['DOM_id_prefix_tla'] + "_container_" + String(creative_id) + "_" + String(placement_id);
	      },
shim_id: function() {
		 return ADCENTRIC.env.architectures['DOM_id_prefix_tla'] + "_shim_" + String(creative_id) + "_" + String(placement_id);
	 },

override_left: function(){
		       x_coord = x_coord + cr.variables['OFFSET_LEFT'];
	       },

override_top: function(){
		      y_coord = y_coord + cr.variables['OFFSET_TOP'];
	      },

get_top: function() {
		 return y_coord;
	 },
get_left: function() {
		  return x_coord;
	  },
width: function() {
	       return width;
       },
set_width: function(new_width) {
		   width = new_width;
	   },

height: function() {
		return height;
	},
set_height: function(new_height) {
		    height = new_height;
	    },

zindex: function() {
		return zindex;
	},
set_zindex: function(new_zindex) {
		    zindex = new_zindex;
	    },

position: function() {
		  return position;
	  },
set_position: function(new_position) {
		      position = new_position;
	      },
set_msie_wmode: function(new_msie_wmode) {
			msie_wmode = new_msie_wmode;
		},
msie_wmode: function() {
		    return msie_wmode;
	    },
set_ff_wmode: function(new_ff_wmode) {
		      ff_wmode = new_ff_wmode;
	      },
ff_wmode: function() {
		  return ff_wmode;
	  },
set_kill_delay: function(new_delay){
	kill_delay = new_delay;
},
set_close_on_mouse_out: function(new_val){
	close_on_mouse_out = new_val;
},

to_string: function() {
		   var banner_swf = ADCENTRIC.util.FlashTag(ADCENTRIC.env.architectures['SWF_LOADER_TLA_URL'],
				   this.width(), this.height());

		   var clicks = "";
		   var asset_files = "";

		   for (var key in cr.clicks)
			   clicks += "&adcEvent_" + String(key) + "=__CLICKURL__" + cr.clicks[key];

		   for (var key in cr.assets)
			   asset_files += "&adcEvent_" + String(key) + "=" + cr.assets[key];

		   banner_swf.set_id(this.id());
		   banner_swf.set_msie_wmode(this.msie_wmode());
		   banner_swf.set_ff_wmode(this.ff_wmode());
		   banner_swf.set_flashvars("crID=" + String(creative_id) +
				   "&timeoutSecs=" + String(kill_delay) +	
					"&sectionID=" + String(placement_id) +
					"&ugt=false" +
					"&base_url_click=" +
					"&base_url_assets=" +
				   "&lcRandID=" + cr.lcPix_TLA.lc_id() +
				   "&isLive=true&bannerFileName=" + banner_swf_url + clicks + asset_files);
		   return banner_swf.to_string();
	   },

set_visible: function(){
		     var container = top.document.getElementById(this.container_id());
		     var shim = top.document.getElementById(this.shim_id());

		     if(cr.variables['USE_SHIM']){
			     shim.style.display = 'block';
			     shim.style.visibility = 'visible';
		     }

			 container.style.display = 'block';
		     container.style.visibility = "visible";
	     },

destroy: function(){
		 var container = top.document.getElementById(this.container_id());
		 var shim = top.document.getElementById(this.shim_id());

		 if(cr.variables['USE_SHIM']){
			 shim.style.display = 'none';
		 }

		 container.style.display = "none";
	 },	
local_setup: function(__container){
			__container.innerHTML = this.to_string();
},
setup: function() {
	       var that = this;

	       /*write container div for lcPix swf*/
	       cr.lcPix_TLA.write_container();



	       /* Setup the creative container and Banner polite-load: */
	       //IMPORTANT: top.document seems to trigger errors. This will make it very hard to iframe bust

	       var container = top.document.createElement('div');

	       container.id = this.container_id();
	       container.style.zIndex = this.zindex();
	       container.style.position = this.position();
	       container.style.visibility = "hidden";
	       container.style.display = "block";
	       container.style.width = this.width() + "px";
	       container.style.height = this.height() + "px";
	       container.style.top = this.get_top() + "px";
	       container.style.left = this.get_left() + "px";

	       var shim  = top.document.createElement('iframe');

	       shim.id = this.shim_id();
	       shim.src = "about:blank";
	       shim.scrolling = "no";
	       shim.frameborder = "0";
	       shim.style.borderTop = "0px";
	       shim.style.borderBottom = "0px";
	       shim.style.borderRight = "0px";
	       shim.style.borderLeft = "0px";
	       shim.style.zIndex = this.zindex() -1;
	       shim.style.position = this.position();
	       shim.style.visibility = "hidden";
	       shim.style.display = "none";
	       shim.style.width = this.width() + "px";
	       shim.style.height = this.height() + "px";
	       shim.style.top = this.get_top() + "px";
	       shim.style.left = this.get_left() + "px";
	       shim.style.opacity = 1;
	       shim.style.filter = 'alpha(opacity=' + 1 + ')';


			       ADCENTRIC.util.Events.add_window_onload(function() {

				       /* Setup the ExternalInterfaceProxy (local-connection pixel) for this creative: */
				       cr.lcPix_TLA.setup();

				       top.document.body.appendChild(container);
				       top.document.body.appendChild(shim);

				       container.innerHTML = that.to_string();
					   	container.onmouseout = function() { 
							cr.lcPix_TLA.call("rollOutHook"); 
						   	if(close_on_mouse_out){
								cr.lcPix_TLA.call("closeTLA"); 
							}
						};
					   
				       setTimeout('ADCENTRIC.env.creatives[' + String(creative_id) + '].placements[' + String(placement_id) + '].architecture_TLA.set_visible();', appear_delay);
				       });
			       }
			       };
			       };





/*
 * AdCentric Banner Architecture for Ripple
 *
 * A Banner is an in-page SWF (with a backup image, usually GIF or PNG + default click URL).  Like any Ripple creative unit,
 * it can have multiple assets (additional files) associated with it, along with AdCentric interaction-tracking URLs and click-tracking
 * redirection URLs.
 *
 * A Banner DIV container is written out when its setup() method is called.  Flash-detection is performed and the backup image is written
 * out should we not detect the presence of the Adobe plugin.  The setup() method also sets up a window.onload event to polite-load the
 * actual SWF banner object.  Because of this, it's possible to call setup() on a Banner instance and override width, height, zindex,
 * position, or other attributes prior to window.onload firing (i.e., directly from the tag template).  The simplest Banner
 * tag template will look roughly like this:
 *
 * //...
 * var my_banner = ADCENTRIC.architectures.Banner('__ADC_ID', '__ADC_PLACEMENT_ID');
 * my_banner.setup();
 * //...
 *
 * Note that ADCENTRIC.architectures.Banner is constructed with a (creative_id, placement_id) 2-tuple.  This allows the constructor
 * to extract required configuration structure (prepended automagically by AdCentric when the creative is served) via the
 * ADCENTRIC.env.creatives[].placements[] matrix.
 *
 * Note also that ADCENTRIC.architectures.Banner takes care of setting up the required ExternalInterfaceProxy for the Banner
 * creative as well, so this does not need to be done separately prior to instantiating ADCENTRIC.architectures.Banner
 *
 *
 * Expected ADCENTRIC.env.creatives[creative_id] for Banner architecture, general structure:
 *
 * ADCENTRIC.env.creatives[creative_id].placements[placement_id].interactions[INTERACTION_LABEL]
 * ADCENTRIC.env.creatives[creative_id].placements[placement_id].clicks[CLICK_LABEL]
 * ADCENTRIC.env.creatives[creative_id].placements[placement_id].assets[ASSET_LABEL]
 * ADCENTRIC.env.creatives[creative_id].placements[placement_id].variables[VARIABLE_LABEL]
 *
 * Specific labels required:
 *
 * - Assets: BANNER, BACKUP_BANNER
 * - Clicks: BACKUP_BANNER_CLICK
 * - Variables: WIDTH, HEIGHT
 *
 * Optional labels:
 *
 * - Variables: ZINDEX (default = '1000'), POSITION (default = 'absolute')
 *
 */
ADCENTRIC.architectures.Banner = function(creative_id, placement_id) {
	var cr = ADCENTRIC.env.creatives[creative_id].placements[placement_id];
	var zindex = cr.variables['BANNER_ZINDEX'] || "1000";
	var position = cr.variables['BANNER_POSITION'] || "absolute";
	var width = cr.variables['BANNER_WIDTH'];
	var height = cr.variables['BANNER_HEIGHT'];
	var banner_swf_url = cr.assets['BANNER'];
	var backup_img_url = cr.assets['BACKUP_BANNER'];
	var backup_banner_click = cr.clicks['BACKUP_BANNER_CLICK'];
	var msie_wmode = cr.variables['BANNER_MSIE_WMODE'] || "opaque";
	var ff_wmode = cr.variables['BANNER_FF_WMODE'] || "opaque";
	var swf_id = Math.floor(Math.random() * 1000000);

	cr.lcPix_banner = ADCENTRIC.util.ExternalInterfaceProxy(creative_id, placement_id, swf_id);

	return {
		adcAction: function(name, obj){
			// do adcAction
		},
		adcClick: function(name){
			// do adcAction
		},
id: function() {
	    return ADCENTRIC.env.architectures['DOM_id_prefix_banner'] + "_" + String(creative_id) + "_" + String(placement_id);
    },
container_id: function() {
		      return ADCENTRIC.env.architectures['DOM_id_prefix_banner'] + "_container_" + String(creative_id) + "_" + String(placement_id);
	      },

width: function() {
	       return width;
       },
set_width: function(new_width) {
		   width = new_width;
	   },

height: function() {
		return height;
	},
set_height: function(new_height) {
		    height = new_height;
	    },

zindex: function() {
		return zindex;
	},
set_zindex: function(new_zindex) {
		    zindex = new_zindex;
	    },

position: function() {
		  return position;
	  },
set_position: function(new_position) {
		      position = new_position;
	      },

to_string: function() {
		   var banner_swf = ADCENTRIC.util.FlashTag(ADCENTRIC.env.architectures['SWF_LOADER_BANNER_URL'],
				   this.width(), this.height());
		   var clicks = ""; 
		   var asset_files = "";

		   for (var key in cr.clicks)
			   clicks += "&adcEvent_" + String(key) + "=__CLICKURL__" + cr.clicks[key];

		   for (var key in cr.assets)
			   asset_files += "&adcEvent_" + String(key) + "=" + cr.assets[key];

		   banner_swf.set_id(this.id());
		   banner_swf.set_msie_wmode(msie_wmode);
		   banner_swf.set_ff_wmode(ff_wmode);
		   banner_swf.set_flashvars("crID=" + String(creative_id) +
				   "&lcRandID=" + cr.lcPix_banner.lc_id() +	
					"&sectionID=" + String(placement_id) +
					"&base_url_click=" +
					"&base_url_assets=" +
				   "&isLive=true&bannerFileName=" + banner_swf_url + clicks + asset_files);
		   return banner_swf.to_string();
	   },
local_setup: function(__container){
			__container.innerHTML = this.to_string();
},
setup: function() {
	       var that = this;


	       /*write container div for lcPix swf*/
	       cr.lcPix_banner.write_container();

	       /* Setup the creative container and Banner polite-load: */
	       var container = '<div id="' + this.container_id() + '" ' +
		       'style="z-index: ' + this.zindex() + '; ' +
		       'position: ' + this.position() + '; ' +
		       'visibility: visible; ' +
		       'display: block; ' +
		       'width: ' + this.width() + 'px; ' +
		       'height: ' + this.height() + 'px;"></div>';
	       document.writeln(container);

	       ADCENTRIC.util.Events.add_window_onload(function() {
			       /* Setup the ExternalInterfaceProxy (local-connection pixel) for this creative: */
			       cr.lcPix_banner.setup();
			       var container = document.getElementById(that.container_id());

			       if(ADCENTRIC.env.has_flash){
			       container.innerHTML = that.to_string();
			       container.onmouseout = function() { cr.lcPix_banner.call("rollOutHook"); };
			       }else{
			       container.innerHTML = '<a target="_blank" href="__ADC_CLICK[BACKUP_GIF_CLICK]?number=' +Math.random()+ '"><img width="__ADC_VARIABLE[WIDTH]" height="__ADC_VARIABLE[WIDTH]" border = "0" alt="" src="__ADC_FILE[BACKUP_GIF_FILE]"></a>';		                                
			       }

			       });
       }
	};
};

function closeLayer__ADC_ID(){
	ADCENTRIC.env.creatives[__ADC_ID].placements[__SECTION__].architecture_TLA.destroy()
}

function flashIsActive__ADC_ID(){
	return;
}

/* adcAction & adcClick scope */
ADCENTRIC.env.creatives[__ADC_ID].placements[__SECTION__].architecture = {}
ADCENTRIC.env.creatives[__ADC_ID].placements[__SECTION__].architecture.adcAction = function(){}
ADCENTRIC.env.creatives[__ADC_ID].placements[__SECTION__].architecture.adcClick = function(){}

/* finally, actual tag template code: */
ADCENTRIC.env.creatives[__ADC_ID].placements[__SECTION__].architecture_TLA = ADCENTRIC.architectures.TLA(__ADC_ID, __SECTION__);
ADCENTRIC.env.creatives[__ADC_ID].placements[__SECTION__].architecture_TLA.setup();
/* finally, actual tag template code: */
ADCENTRIC.env.creatives[__ADC_ID].placements[__SECTION__].architecture_Banner = ADCENTRIC.architectures.Banner(__ADC_ID, __SECTION__);
ADCENTRIC.env.creatives[__ADC_ID].placements[__SECTION__].architecture_Banner.setup();
