﻿/*
* OpenXAds Post page load ad loader.
*
* Useage
*	<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 			: 'streetfire.net', // 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)

				// Video Info
				video_member_id : 'vid_mid',	// 'TheBoz'
				video_id		: 'vid_id',		// 197990
				video_hd		: 'vid_hd',		// 'true' / 'false'

				// Other
				profile_id		: 'p_id',		// 'TheBoz'
				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'
				companion		: 'companion',	// streetfire companion ad target value
				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,
				
				// Video Info
				video_member_id : null,
				video_id		: null,
				video_hd		: null,

				// Other
				profile_id		: null,
				event_name 		: null,
				franchise 		: null,
				category 		: null,
				sort 			: null,
				seo 			: null,
				sotw 			: null,
				companion		: null,
				keywords 		: null
	},
	adZones : {
				'streetfire.net' : {
										site_id : 2,
										zones : {
												top_728x90 				: 2, //[id2] StreetFire - Top - 728x90 - ATF 
												frame1_300x250 			: 3, //[id3] StreetFire - Frame 1 - 300x250 - ATF
												frame2_300x250 			: 4, //[id4] StreetFire - Frame 2 - 300x250 - ATF 
												frame4_300x250 			: 5, //[id5] StreetFire - Frame 4 - 300x250 - BTF 
												bottom_728x90 			: 6, //[id6] StreetFire - Bottom - 728x90 - BTF 
												sidebar1_125x125 		: 7, //[id7] StreetFire - Sidebar 1 
												sidebar2_125x125 		: 8, //[id8] StreetFire - Sidebar 2 
												sidebar3_125x125 		: 9, //[id9] StreetFire - Sidebar 3 
												sidebar4_125x125 		: 10, //[id10] StreetFire - Sidebar 4 
												sidebar5_125x125 		: 11, //[id11] StreetFire - Sidebar 5 
												sidebar6_125x125 		: 12, //[id12] StreetFire - Sidebar 6 
												sidebar7_125x125 		: 13, //[id13] StreetFire - Sidebar 7 
												sidebar8_125x125 		: 14, //[id14] StreetFire - Sidebar 8 
												interstitial_fullpage 	: 15, //[id15] StreetFire - Interstitial - Fullpage 
												button_234x90 			: 34, //[id34] StreetFire - Button - 234x90 
												site_skin				: 62, //[id62] StreetFire - Skin 
												hp_breakapart_990x300	: 74, //[id74] StreetFire - HP Breakapart 990x300 
												hp_breakapart_300x600	: 75, //[id75] StreetFire - HP Breakapart 300x600
												sidebar9_160x600		: 86 //[id86] Video List 160x600 
												}
									}
	},

	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);
			}
		}
	}
};

