/*
	Creator: Gaurav Saparia
	Company: GigabitIT
	Date-Created: 2009 06 05
*/
	//Class AD (represents singal ad)
	/*
		Structure
		cAd
			-url
			-altText
			-openInNewWindow
			-src
		
		cAdRotator
			-divId
			-Ads[] //array containing objects of cAd
			-registerAd(accepts cAd object)
			-renderAd()
			-getRandomAd() returns cAd object

	*/
	//Class Ad
		var cAd = Class.create();
	cAd.prototype = {
		initialize:function(ob)
		{
			this.url = ob.url || "";
			this.altText = ob.altText || "";
			this.openInNewWindow = ob.openInNewWindow=="True" ? true:false;
			this.src = ob.src || "";
			this.width = ob.width || "";
			this.height = ob.height ||  "";
			this.divId = ob.divId || "";
			this.webTrendzName = ob.webTrendzName || ""; 
		}
	}

	//Class ADRotator
	var cAdRotator = Class.create();
	cAdRotator.prototype ={
		initialize:function()
		{	
			this.divId = "mydiv", //default is mydiv can be changed eg obj.divId = "islandAd"
			this.Ads = []; //an array of cAd object will be stored here
			this.HeaderAds = [];
			this.FooterAds = [];
			this.IslandAds = [];
			
			this.HeaderAdConfig = {divId:"header-leaderboard",width:728,height:90};
			this.FooterAdConfig = {divId:"leaderboard",width:728,height:90};
			this.IslandAdConfig = {divId:"ad-island",width:300,height:250};
		},
		
		registerHeaderAd:function(oAd){
				this.HeaderAds.push(oAd);
				},
		registerFooterAd:function(oAd){
				this.FooterAds.push(oAd);
				},
		registerIslandAd:function(oAd){
				this.IslandAds.push(oAd);
				},
		
		alertInner:function(){
			alert(this.divId);
		},
		randerAd:function()		{
			if(this.Ads.length <= 0)
			{
				return;	
			}
			ad = this.getRandomAd();
			oWebTrends.isFlashAd(oWebTrends.getAdName(ad))? this.displayFlashAd(ad):this.displayImageAd(ad);
			
			oWebTrends.registerAdView(ad);
		},
		randerHeaderAd:function()		{
			if(this.HeaderAds.length <= 0)
			{
				return;	
			}
			ad = this.getRandomAd(this.HeaderAds);
			ad.width = this.HeaderAdConfig.width;
			ad.height =this.HeaderAdConfig.height ;
			ad.divId = this.HeaderAdConfig.divId;
			oWebTrends.isFlashAd(oWebTrends.getAdName(ad))? this.displayFlashAd(ad):this.displayImageAd(ad);
			
			oWebTrends.registerAdView(ad);
		},
		randerFooterAd:function()		{
			if(this.FooterAds.length <= 0)
			{
				return;	
			}
			ad = this.getRandomAd(this.FooterAds);
			ad.width = this.FooterAdConfig.width;
			ad.height =this.FooterAdConfig.height ;
			ad.divId = this.FooterAdConfig.divId;
			oWebTrends.isFlashAd(oWebTrends.getAdName(ad))? this.displayFlashAd(ad):this.displayImageAd(ad);
			
			oWebTrends.registerAdView(ad);
		},
		randerIslandAd:function()		{
			if(this.IslandAds.length <= 0)
			{
				return;	
			}
			ad = this.getRandomAd(this.IslandAds);
			ad.width = this.IslandAdConfig.width;
			ad.height =this.IslandAdConfig.height ;
			ad.divId = this.IslandAdConfig.divId;
			oWebTrends.isFlashAd(oWebTrends.getAdName(ad))? this.displayFlashAd(ad):this.displayImageAd(ad);
			
			oWebTrends.registerAdView(ad);
		},
		getRandomAd:function(AdArray)
		{
			rand_no = Math.random();
			randAdRef = Math.ceil((rand_no * AdArray.length)-1);
			return AdArray[randAdRef];			
		},
		displayImageAd:function(ad)
		{
			var a = document.createElement("a");
			//a.setAttribute("href",ad.url);			
			sObj = "{src:\""+ad.src+"\",openInNewWindow:"+ad.openInNewWindow+",url:\""+ad.url+"\"}";
			a.setAttribute("href","javascript:oWebTrends.registerAdClick("+sObj+");");
			//ad.openInNewWindow ? a.setAttribute("target","_new"):false;
			var i = document.createElement("img");
			i.setAttribute("src",ad.src);
			i.setAttribute("alt",ad.altText);
			//i.setAttribute("style","border:0");
			a.appendChild(i);
			$(ad.divId).appendChild(a);
			//htmlContent = "<a href=\"[url]\" target=\"[target]\"><img src=\"[src]\" style=\"border:0\" alt=\"[alt]\" title=\"[title]\"></a>".replace("[url]",ad.url).replace("[src]",ad.src).replace("[alt]",ad.altText).replace("[title]",ad.altText);
			//htmlContent = ad.openInNewWindow==true ? htmlContent.replace("[target]","_new"):htmlContent.replace("[target]","");		
			//$(this.divId).update(htmlContent);

			//register view
		},
		displayFlashAd:function(ad)
		{
			var so = new SWFObject(ad.src, "mymovie", ad.width, ad.height,"8","");
			so.addParam("wmode", "opaque");
			so.write(ad.divId);

		}
	};

oAdrotator = new cAdRotator();