/*********************************
========= Script Notice ==========
**********************************
Script Title: Random Banner Rotation Script
	* Feature:
		- Supports image element (image)
		- Supports flash element (flash)
		- Configurable Banner info
		- Script automatically detects if flash is being passed.
	* Modifications:
		- June 23, 2009
			> Object Oriented
			> Will use only one script and configuration is easily configured
			> Supports scripts, flash and images (Script is witty to distinguish one from the other)
		- June 12, 2009
			> Added Cookie detection for banner showing in sequence
		- June 5, 2009
			> Fix the bottom border bug issue for caption
			> Fix the broken link issue
		- September 10, 2010
			> Allows config on first URL for SWF
			> Allows 5th configuration which is the SWF size
		- September 15, 2010
			> Allows a quick way to load all FLASH templates. Use loadFlash: true config.
			
Copyright: http://www.financial-spread-betting.com
Script Created by Raymond Angana (http://ray.ph/)
Date Created: June 5, 2009

This notice must stay intact for legal use
***********************************
========= End of Notice ===========
**********************************/

// Flash templates
var banners_must = [
	// [URL, Filename, Caption, TITLE, URL, Size]
	['http://www.financial-spread-betting.com/ccount/click.php?id=3','capital_spreads.gif','','','','300x250'],
	['http://ad.doubleclick.net/adi/N7118.367332.FINANCIAL-SPREAD-BE/B6212674;sz=300x250;ord=[timestamp]?','iframe','','','','']
];


////////// Edit at your own risk ////////
var ray = {
	ban: banners_must,
	loadRandomBannerScript : function ( obj ) { // loadRandomBannerScript
		obj.banners = obj.banners?obj.banners:[];
		var banners = (obj.loadTemplate)?(ray.ban.concat(obj.banners)):(obj.banners);
		var e = Number(this.checkCookie([banners,obj.bannerName])); // Set/Check cookie for banner
		e = (e==banners.length)?0:e; // Fix to ensure that the cookie doesn't overpass the banners length
		//alert('Page reloaded, array index "'+e+'" should be loaded!');
		var getBanner = banners[e]; // Get the banner configuration	
		// Check at this early point if it's a script
		if (getBanner[1].toLowerCase() == 'script') {
			document.write ('<script type="text/javascript" src="'+getBanner[0]+'"><\/script>');
			
			if(getBanner[2]) {
			document.write ('<p style="border:0px;margin-top:10px;text-align:center;clear:both;"><b><a href="'+getBanner[3]+'" target="_blank">'+getBanner[2]+'</b></a></p>');
			}
			return;
		}
		if(getBanner[1].toLowerCase() == 'iframe') {
			document.write ('<IFRAME SRC="'+getBanner[0]+'" width="'+(getBanner[5]?(getBanner[5].split('x')[0]+'px'):'300px')+'" height="'+(getBanner[5]?(getBanner[5].split('x')[1]+'px'):'250px')+'" MARGINWIDTH=0 MARGINHEIGHT=0 HSPACE=0 VSPACE=0 FRAMEBORDER=0 SCROLLING=no BORDERCOLOR="#000000"><SCRIPT language="JavaScript1.1" SRC="'+getBanner[0]+'"></SCRIPT></IFRAME>');
			
			if(getBanner[2]) {
			document.write ('<p style="border:0px;margin-top:10px;text-align:center;clear:both;"><b><a href="'+getBanner[3]+'" target="_blank">'+getBanner[2]+'</b></a></p>');
			}
			return;
		}
		
		// End of check
		var getExt = (obj.loadFlash)?'swf':(getBanner[1].substr(getBanner[1].length-3,getBanner[1].length)); // Gets the file extension
		var str = '<a href="'+getBanner[0]+'" target="_blank" class="text hover" id="mylink" onmouseover="this.style.textDecoration=\'none\'">';	
		var iHTML = (getExt.toLowerCase() == 'swf') ? 
			
			'<a href="'+getBanner[0]+'" class="flashover-bak" target="_blank">&nbsp;</a><object type="application/x-shockwave-flash" width="'+(getBanner[5]?(getBanner[5].split('x')[0]+'px'):'728px')+'" height="'+(getBanner[5]?(getBanner[5].split('x')[1]+'px'):'90px')+'" data="'+((obj.bannerFolder)+getBanner[1])+'" class="'+getBanner[6]+'"><param name="movie" value="'+((obj.bannerFolder)+getBanner[1])+'"><param name="quality" value="high"><param name="wmode" value="transparent"><h1>You don\'t have "'+getBanner[1]+'" flash file on your machine</h1></object>':
			
			(str+'<img id="myimg" onmouseover="this.style.textDecoration=\'none\'" src="'+((obj.bannerFolder)+getBanner[1])+'" style="border:0px;width:'+(getBanner[5]?(getBanner[5].split('x')[0]+'px'):'728px')+';height:'+(getBanner[5]?(getBanner[5].split('x')[1]+'px'):'90px')+';" alt="'+getBanner[2]+'"></a>'); // Construct the HTML code
		
		
		// Create a caption if one is defined
		iHTML += (getBanner[3])?
			'<p style="border:0px;margin-top:10px;text-align:center;"><b><a href="'+(getBanner[4]?getBanner[4]:getBanner[0])+'" target="_blank">'+getBanner[3]+'</b></a></p>':'';
		
		if (obj.debug) alert(iHTML);
		
		document.write(iHTML); // Write it now
	},
	
	checkCookie : function ( banners ) { // Checks if cookie exists
		
		var cookieName = (banners[1])?banners[1]:'banner';
		var banner=this.getCookie(cookieName); // Gets the cookie with name: banner
		if (banner > (banners[0].length) - 1) // If the cookie is greater than or equal to the configured banner
			this.setCookie(cookieName,0,365); // Reset the cookie
		else if (banner[0]!=null && banner[0]!="") // If cookie exists already
		  this.setCookie(cookieName,Number(banner[0])+1,365); // Increment the cookie
		else // If for some circumstance
			this.setCookie(cookieName,(Math.floor(Math.random()*(banners[0].length))),365); // Just reset the cooie
		
		return this.getCookie(cookieName); // Return the cookie
	},
	
	setCookie : function (c_name,value,expiredays) { // Sets cookie's value
		var exdate=new Date(); // Get the current date
		exdate.setDate(exdate.getDate()+expiredays); // Set cookie's expiration date
		document.cookie=c_name+ "=" +escape(value)+ 
		((expiredays==null) ? "" : ";expires="+exdate.toGMTString()); // Set cookie
	},
	
	getCookie : function (c_name) { // Gets cookie's value
		if (document.cookie.length>0) { // If a cookie exists
			c_start=document.cookie.indexOf(c_name + "="); // Check if the cookie exists
			if (c_start!=-1) { // If there's a match above
				c_start=c_start + c_name.length+1; // Set the start of cookie
				c_end=document.cookie.indexOf(";",c_start); // Set the end of cookie
				if (c_end==-1) // If doesn't exists
					c_end=document.cookie.length; // Just set it to cookie's length
			return unescape(document.cookie.substring(c_start,c_end)); // Set cookie
			}
		}
		return "";
	},
	
	// Custom function: Same as document.getElementById(element);
	getID : function (el) { return document.getElementById(el); }
}
