var ADCENTRIC = ADCENTRIC || { };
ADCENTRIC.env = ADCENTRIC.env || { };
ADCENTRIC.env.creatives = ADCENTRIC.env.creatives || { };
ADCENTRIC.env.creatives[9141] = { };
ADCENTRIC.env.creatives[9141].placements = { };
ADCENTRIC.env.creatives[9141].placements[9572] = { };
ADCENTRIC.env.creatives[9141].placements[9572].assets = { };
ADCENTRIC.env.creatives[9141].placements[9572].clicks = { };
ADCENTRIC.env.creatives[9141].placements[9572].interactions = { };
ADCENTRIC.env.creatives[9141].placements[9572].variables = { };
ADCENTRIC.env.creatives[9141].placements[9572].variables['GIF_WIDTH'] = '160';
ADCENTRIC.env.creatives[9141].placements[9572].variables['GIF_HEIGHT'] = '600';
ADCENTRIC.env.creatives[9141].placements[9572].variables['WIDTH'] = '160';
ADCENTRIC.env.creatives[9141].placements[9572].variables['HEIGHT'] = '600';
ADCENTRIC.env.creatives[9141].placements[9572].variables['IFRAME_BUST'] = 'false';
ADCENTRIC.env.creatives[9141].placements[9572].variables['IS_DAP'] = 'false';
ADCENTRIC.env.creatives[9141].placements[9572].variables['MSIE_WMODE'] = 'opaque';
ADCENTRIC.env.creatives[9141].placements[9572].variables['FF_WMODE'] = 'opaque';
ADCENTRIC.env.creatives[9141].placements[9572].clicks['clickthru'] = 'http://';
ADCENTRIC.env.creatives[9141].placements[9572].assets['BANNER'] = '160x600';
ADCENTRIC.env.creatives[9141].placements[9572].assets['BANNER_LOADER_SWF'] = 'swfLoaders/Banner.swf';
/* requires core.js */
/* requires architectures/banner.js */

/* Preamble served by AdCentric looks like this:
ADCENTRIC.env.creatives[1] = { };
ADCENTRIC.env.creatives[1].placements = { };
ADCENTRIC.env.creatives[1].placements[1] = { };
ADCENTRIC.env.creatives[1].placements[1].assets = { };
ADCENTRIC.env.creatives[1].placements[1].clicks = { };
ADCENTRIC.env.creatives[1].placements[1].interactions = { };
ADCENTRIC.env.creatives[1].placements[1].variables = { };

ADCENTRIC.env.creatives[1].placements[1].assets['BANNER'] = "";
ADCENTRIC.env.creatives[1].placements[1].assets['BACKUP_BANNER'] = "";
ADCENTRIC.env.creatives[1].placements[1].clicks['BACKUP_BANNER_CLICK'] = "";
ADCENTRIC.env.creatives[1].placements[1].variables['WIDTH'] = "300";
ADCENTRIC.env.creatives[1].placements[1].variables['HEIGHT'] = "250";
*/
/* requires core.js */
ADCENTRIC.lang.namespace("ADCENTRIC.architectures")

ADCENTRIC.env.architectures = ADCENTRIC.env.architectures || { };
ADCENTRIC.env.architectures['DOM_id_prefix'] = "adcentric";
ADCENTRIC.env.architectures['SWF_LOADER_BANNER_URL'] = ADCENTRIC.env.creatives[9141].placements[9572].assets["BANNER_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[9572].interactions[_name] + '?' + _obj_to_string + 'num=' +rn;
	}else{
		event_pixel.src = ADCENTRIC.env.creatives[crID].placements[9572].interactions[_name] + '?num=' + rn;
	}
}


/*
 * 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('9141', '__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['ZINDEX'] || "1000";
	var position = cr.variables['POSITION'] || "absolute";
	var width = cr.variables['WIDTH'];
	var height = cr.variables['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['MSIE_WMODE'] || "opaque";
	var ff_wmode = cr.variables['FF_WMODE'] || "opaque";
	var swf_id = Math.floor(Math.random() * 1000000);

	cr.lcPix = 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'] + "_" + String(creative_id) + "_" + String(placement_id);
		},
		container_id: function() {
			return ADCENTRIC.env.architectures['DOM_id_prefix'] + "_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) + "=" + 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.lc_id()  + ""+
					"&sectionID=" + String(placement_id) +
					"&ugt=false" +
					"&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.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.setup();
				
				var container = document.getElementById(that.container_id());

				if(ADCENTRIC.env.has_flash){
				container.innerHTML = that.to_string();
				container.onmouseout = function() { cr.lcPix.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 flashIsActive9141(){
	return;
}


/* 
 * This marker is replaced when FEX publish the local test page
 * //__LOCAL_MARKER__ must be write before the instantiation of the architecture
*/
//__LOCAL_MARKER__


/* finally, actual tag template code: */
ADCENTRIC.env.creatives[9141].placements[9572].architecture = ADCENTRIC.architectures.Banner(9141, 9572);
ADCENTRIC.env.creatives[9141].placements[9572].architecture.setup();
