/*
 * OpenXAds post page load ad loader.
 *
 * Usage:
 *    <script language="JavaScript" src="/openx_ads.js?ver=1" type="text/javascript"></script>
 *
 *    <script type="text/javascript">
 *    // Override defaults
 *    OpenXAds.settings.site = 'streetfire.net';
 *    OpenXAds.settings.url = 'http://d.cardomain.com/spc.php';
 *
 *    // Define the Ad Units to load
 *    OpenXAds.settings.positions.push('top_728x90');
 *    OpenXAds.settings.positions.push('frame1_300x250');
 *
 *    // Make the request to the ad server
 *    OpenXAds.fetchAds();  // Must close the Script tag before rendering ad units
 *  </script>
 *
 *  <script type="text/javascript">OpenXAds.showAd('top_728x90');</script>
 *  <script type="text/javascript">OpenXAds.moveAd('top_728x90');</script>
 *
 *
 * Include this file, preferably near the bottom of an HTML source page/template.
 *
 * Notes:
 *  Because OpenXAds.fetchAds() uses document.write, you must close the script
 *  tag in which it's called before calling OpenXAds.showAd()/OpenXAds.moveAd().
 *
 *  OpenXAds.showAd and OpenXAds.moveAd must be in there own script tags to work
 *  properly.  If they are not only the first ad will reposition correctly.
 *
 */

OpenXAds = {
	settings : {
				// Ad Server Config Params
				site      : 'www.cardomain.com', // This can be overridden -- OpenXAds.settings.site = 'streetfire.net';
				url       : 'http://d.cardomain.com/spc.php', // This can be overridden -- OpenXAds.settings.url = 'http://d.cardomain.com/spc.php';
				positions : [],
				source    : null
	},
	specAbbr : {
				// User Info
				member_id    : 'mid',          // 'rollin_on_dubz'
				logged_in    : 'l',            // 'yes' / 'no'
				age          : 'a',            // number from registration data
				gender       : 'g',            // male, female

				// Vehicle Info
				ride_id      : 'r_id',      // 2114050
				vehicle      : 'v',         // 'honda' or 'honda.civic'
				year         : 'yr',        // 2009
				mods         : 'mods',      // modifications (CarDomain)
				ride_state   : 'r_state',   // State (if known)
				ride_country : 'r_country', // Country (if known)

				// Other
				event_name   : 'e_name',      // 'SEMA_2009'
				franchise    : 'f',           // drift,4x4,girls,bike,cotw,burnout
				category     : 'cat',
				sort         : 'sort',        // 'Rating'
				seo          : 'seo',         // yes, no
				sotw         : 'sotw',        // 'true' / 'false'
				keywords     : 'key'          // keywords (Streetfire) Always want this one last.
	},
	specs : {
				// User Info
				member_id    : null,
				logged_in    : null,
				age          : null,
				gender       : null,

				// Vehicle Info
				ride_id      : null,
				vehicle      : null,
				year         : null,
				mods         : null,
				ride_state   : null,
				ride_country : null,

				// Other
				event_name   : null,
				franchise    : null,
				category     : null,
				sort         : null,
				seo          : null,
				sotw         : null,
				keywords     : null
	},
	adZones : {
				'www.cardomain.com' : {
										site_id   : 3,
										zones : {
													bottom     : 20,
													expand     : 55,
													frame1     : 17,
													frame2     : 18,
													frame4     : 19,
													hp_frame3  : 47,
													inline1    : 53,
													inline_x21 : 56,
													inline_x22 : 57,
													interstit  : 58,
													skin       : 60,
													top        : 16
												}
									}
	},

	buildQueryString : function()
	{
		var url = "";
		var site = OpenXAds.settings['site'];

		// Make sure that they are hitting a real site, otherwise fail
		if (site != null && site.length > 0)
		{
			// Define the site ID
			var siteId = OpenXAds.adZones[site].site_id;

			// Verify that there is a valid siteid of fail
			if (siteId != null && siteId > 0)
			{
				// Define the
				var url = '&amp;id=' + siteId
							+ '&amp;source=' + OpenXAds.settings['source'];

				// Append all parameters to the Url
				for (prop in OpenXAds.specs) {

					var propAbbr = OpenXAds.specAbbr[prop];
					var propValue = OpenXAds.specs[prop];

					if (propAbbr != null
						&& propValue != null
						&& propValue.length > 0) {
						url += '&amp;' + propAbbr + '=' + escape(propValue);
					}
				}
			}
		}
		return url;
	},
	
	fetchAds : function()
	{
		var site = OpenXAds.settings['site'];

		// Make sure that they are hitting a real site, otherwise fail
		if(site != null && site.length > 0)
		{
			// Define the site ID
			var siteId = OpenXAds.adZones[site].site_id;

			// Verify that there is a valid siteid of fail
			if(siteId != null && siteId > 0)
			{
				// Define the Zones
				var zoneIds = [];
				for(var x=0, len=OpenXAds.settings.positions.length; x<len; ++x )
				{
					var zoneId = OpenXAds.adZones[site].zones[OpenXAds.settings.positions[x]];
					
					if(zoneId != null && zoneId > 0)
						zoneIds.push(zoneId);
				}
				
				// Define the 
				var url = OpenXAds.settings['url']
							+ '?id=' + siteId
							+ '&zones=' + zoneIds.join('|')
							+ '&source=' + OpenXAds.settings['source'];
				
				// Append all parameters to the Url
				for(prop in OpenXAds.specs)
				{
					
					var propAbbr = OpenXAds.specAbbr[prop];
					var propValue = OpenXAds.specs[prop];
					
					if(propAbbr != null
						&& propValue != null
						&& propValue.length > 0)
					{
						url += '&' + propAbbr + '=' + escape(propValue);
					}
				}
			
				document.write(unescape("%3Cscript src='" + url + "' type='text/javascript'%3E%3C/script%3E"));
			}
		}
	},
	
	/*
	 *  This must run after fetchAds
	 *
	 *  showAd
	 *   - Loads the ad into a div
	 */
	showAd : function(name)
	{
		var adVar = OA_output[OpenXAds.adZones[OpenXAds.settings['site']]['zones'][name]];
		
		if (typeof adVar == 'string')
		{
			document.writeln('<div id="' + name + '_tmp">' + adVar + '<\/div>');
		}
	},
	   
	moveAd : function(name)
	{
		var homeDiv = document.getElementById(name + '_spacer'),
			fromDiv = document.getElementById(name + '_tmp'),
			child,
			next,
			ad_temp_container;
	
		if (homeDiv && fromDiv)
		{
			ad_temp_container = fromDiv.parentNode;	// don't depend on id='ad_handling_container'
		
			for (child = fromDiv.firstChild; child; child = next)
			{
				next = child.nextSibling;
				homeDiv.appendChild(child);
			}
		
			fromDiv.parentNode.removeChild(fromDiv);
		}
	
		// Sometimes (multiply-nested document.writes?) elements break out of the _tmp div.
		// This catches those.
		if (ad_temp_container)
		{   
			for (child = ad_temp_container.firstChild; child; child = next)
			{
				next = child.nextSibling;
	
				if (child.nodeName.toUpperCase() == 'SCRIPT');  // skip moving extra SCRIPTS
				else if (/.*_tmp$/.test(child.id));			 // or our own temp div
				else homeDiv.appendChild(child);
			}
		}
	}
};