// function to pop open larger taster
function newTasterWindow() {
	var newTasterURL = 'http://flashimp.cachefly.net/imp/index300.html#'+IMP_tasterid;
	window.open(newTasterURL,'popped','width=300,height=311,toolbar=no,scrollbars=no,resizable=no,menubar=no,status=no,directories=no,location=no');
}

/* ===== SETTINGS ========================================================== */

var IMP_w=120;	// taster width default. (passed in on query string for this script src)

var IMP_i = IMP_i || 0; // taster count. Used if multiple tasters are on the same page. Immediately increase for next count;

// URL to pull feed data from
var IMP_cacheKiller = new Date().getTime();
//var IMP_feedurl='https://admin2.poweredbyimp.com/impweb/com/get.php?msg=taster-list&iid=cTptmBjmHJVMpszs9f6p&brand=1&version=0.47&countryCode=US&getAll=1&cacheKiller='+IMP_cacheKiller;
var IMP_feedurl='http://flashimp.cachefly.net/imp/feed.txt?cachekiller='+IMP_cacheKiller;

// URL to post share data to
var IMP_shareproxyurl='https://admin.poweredbyimp.com/impweb/com/eishare.php';

// External JavaScript files to load before anything else - must be on the same domain as this file
var IMP_externalJS = [
	'./swfobject.js'
];

// Create a container div, IMP_i allows for unique IDs.
var IMP_nodeid='IMP_taster_container_'+IMP_i;
document.write('<div id="IMP_taster_container_'+IMP_i+'"></div>')

// Google AdSense
// Note: size and format information depend on taster size, those are set below.
/*
google_ad_client = "pub-7411287217540783";
google_ad_type = "text_image";
google_ad_channel = "5974379315";
google_color_border = "6699CC";
google_color_bg = "003366";
google_color_link = "FFFFFF";
google_color_text = "AECCEB";
google_color_url = "AECCEB";
*/
/* ===== END SETTINGS ====================================================== */





var IMP_context=this;
var IMP_tc={};	// taster container node

// Tags for sharing
var IMP_shareproxyhtml='<script type="text/javascript" src="__JS__"></script>';

// ID of taster - REQUIRED (passed in on the query string for this script src)
var IMP_tasterid = 32;

// URL of this script - populated by IMP_getArgs() below
var IMP_js_url='';
var IMP_js='imp.js';

var IMP_args = {};
var IMP_getArgs = function(){
	var args = [];
	var impsrc = [];
	var ssrc = [];

	var nodes = document.getElementsByTagName("script");
	for (var i=0; i<nodes.length; i++) {
		var s = nodes[i];
		var re = new RegExp(IMP_js+'\\?.*$');
		if (s.src && s.src.match(re)) {
			impsrc.push(nodes[i]);
		};
	}

	s = impsrc[IMP_i]; // get the correct script tag
	ssrc = s.src.split( /\?/ );
	IMP_js_url = ssrc[0];
	args = ssrc[1].split('&');
	
	for (i=0; i<args.length; i++) {
		if (args[i].match( /=/ )) {
			var parts = args[i].split( /=/ );
			if (parts[0]=='tid') 		IMP_tasterid = parts[1];
			if (parts[0]=='tw')			IMP_w = parts[1];
		}
	}

	// Set the sharing code to include this script and sharing info
	var d = new Date();
	IMP_shareproxyhtml=IMP_shareproxyhtml.replace(/__JS__/, ssrc[0]+'?tid='+IMP_tasterid+'&tw='+IMP_w+'&sid='+d.getTime()+'_'+window.location.hostname);
}
IMP_getArgs();

// Now that we know our taster version we can set up the g-ad format and the correct taster swf
var IMP_tc_w=IMP_w;	// taster container width
/*
var IMP_ad_w=IMP_w;	// ad width
google_ad_width = IMP_w;
google_ad_height = (IMP_w == 120) ? 240 : 250;
google_ad_format = (IMP_w == 120) ? "120x240_as" : "300x250_as";
*/
// Path to taster SWF
var IMP_taster_uri= (IMP_w == 120) ? "http://flashimp.cachefly.net/imp/e-imp.swf" : "http://flashimp.cachefly.net/imp/e-imp300.swf";


var IMP_getTasterContainer = function(){
// TODO: handle if node does not exist
	return document.getElementById(IMP_nodeid);
}
	
var IMP_addImpNodes = function(){
// NOTE: currently IMP_tasterid is required, which means tid= is required in the script URL.  do we want to have a fallback position if no tid= is passed?
	if (IMP_tasterid!='') {
		var taster_id='IMP_taster_'+IMP_i;
		var taster = document.createElement('div');
		taster.id=taster_id;
		var taster_a = (IMP_w == 120) ? 357/120 : 311/300 // aspect ratio of flash movie. 
//taster.style.cssFloat='left';	// alternate layout
		taster.style.width=IMP_w+'px';
		IMP_tc.appendChild(taster);
		var so = new SWFObject(IMP_taster_uri, "eimp", IMP_w, IMP_w*taster_a, "8", "#000000");
		so.addVariable("feedurl", encodeURIComponent(IMP_feedurl));
		so.addVariable("tasterid", encodeURIComponent(IMP_tasterid));
		so.addVariable("shareproxyurl", encodeURIComponent(IMP_shareproxyurl));
		so.addVariable("shareproxyhtml", encodeURIComponent(IMP_shareproxyhtml));
		so.addParam("allowScriptAccess","always");
		so.write(taster_id);
		/*
		var ads = document.createElement('div');
		ads.id='IMP_ads';
		*/
//ads.style.cssFloat='left';	// alternate layout
//ads.style.width=IMP_w+'px';	// alternate layout
//ads.style.height='400px';	// alternate layout
		/*
		ads.style.width=IMP_ad_w+'px';
		ads.style.marginTop = '3px'; // small gap between taster and ads
		ads.style.height='auto';
		*/
// TODO: include ad content
		/*
		IMP_tc.appendChild(ads);
		*/
	}
	// After everything is done, update the imp counter
	IMP_i++
}

var IMP_related_path = function(path) {
	var IMP_js_dir = IMP_js_url.replace(/\/[^\/]*\.js$/, '/');
	if (path.match(/^\.(\.)?\//)) {
		if (path.match(/^\.\//)) {
			path = path.replace(/^\.\//, '');
		}
		while (path.match(/^\.\.\//)) {
			IMP_js_dir = IMP_js_dir.replace(/\/[^\/]*\/$/, '/');
			path = path.replace(/^\.\.\//, '');
		}
	}

	return IMP_js_dir+path;
}





/* ===== MAKE THE CALL ===================================================== */

// setup our container
IMP_tc = IMP_getTasterContainer();
//IMP_tc.style.width=(IMP_w*2)+'px';	// alternate layout
IMP_tc.style.width=IMP_tc_w+'px';
IMP_tc.style.height='auto';
IMP_tc.style.overflow='hidden';

// load external JS
for (var i=0; i<IMP_externalJS.length; i++) {
	// Brute force approach
	document.write('<script type="text/javascript" src="'+IMP_related_path(IMP_externalJS[i])+'"><'+'/script>');
}

// now let's do some good - time to output the actual interface (after our prerequisite SWFObject has been initialized)
var i=0;
var IMP_load = function(){
	if ((typeof SWFObject)=='function') IMP_addImpNodes();
	else {
		if (i++<50) {
			// try again in 1/10th of a second
			var t = window.setTimeout(function(){IMP_load();}, 100);
		}
	}
}
IMP_load();

// Brute force for Google Ads
/*
var IMP_gads = function(){
	document.write('<script type="text/javascript"\n');
	document.write('  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">\n');
	document.write('<'+'/script>');
}
IMP_gads.call(window);
*/
/* created by Electric Pulp - http://electricpulp.com/ */
