/*
	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 || "";
		}
	}

	//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.HomePageAds = [];
			this.FooterAds = [];
			this.InternalAds = [];
			this.SkyScraperAds = [];
			
			this.HomepageAdConfig = {divId:"homepageAd",width:300,height:250};
			this.FooterAdConfig = {divId:"footerAd",width:728,height:90};
			this.InternalAdConfig = {divId:"internalAd",width:234,height:60};
			this.SkyScraperAdConfig ={divId:"skyscraperAd",width:120,height:600};
		},
		registerAd:function(oAd){
				this.Ads.push(oAd);
				},
		registerHomepageAd:function(oAd){
				this.HomePageAds.push(oAd);
				},
		registerInternalAd:function(oAd){
				this.InternalAds.push(oAd);
				},
		registerSkyScraperAd:function(oAd){
				this.SkyScraperAds.push(oAd);
				},
		registerFooterAd:function(oAd){
				this.FooterAds.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);
		},
		randerHomepageAd:function()		{
			if(this.HomePageAds.length <= 0)
			{
				return;	
			}
			ad = this.getRandomAd(this.HomePageAds);
			ad.width = this.HomepageAdConfig.width;
			ad.height =this.HomepageAdConfig.height ;
			ad.divId = this.HomepageAdConfig.divId;
			oWebTrends.isFlashAd(oWebTrends.getAdName(ad))? this.displayFlashAd(ad):this.displayImageAd(ad);
			
			oWebTrends.registerAdView(ad);
		},
		randerInternalAd:function()		{
			if(this.InternalAds.length <= 0)
			{
				return;	
			}
			ad = this.getRandomAd(this.InternalAds);
			ad.width = this.InternalAdConfig.width;
			ad.height =this.InternalAdConfig.height ;
			ad.divId = this.InternalAdConfig.divId;
			oWebTrends.isFlashAd(oWebTrends.getAdName(ad))? this.displayFlashAd(ad):this.displayImageAd(ad);
			
			oWebTrends.registerAdView(ad);
		},
		randerSkyScraperAd:function()		{
			if(this.SkyScraperAds.length <= 0)
			{
				return;	
			}
			ad = this.getRandomAd(this.SkyScraperAds);
			ad.width = this.SkyScraperAdConfig.width;
			ad.height =this.SkyScraperAdConfig.height ;
			ad.divId = this.SkyScraperAdConfig.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);
		},
		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","#ffffff");
			so.write(ad.divId);

		}
	};
/*
	//Usage
	oAdRotator1 = new cAdRotator();
	oAdRotator1.divId = "myAd";
	
	oAd1 = new cAd({
		url:"http://www.google.com",
		src:"homepageAd1.jpg",
		altText:"this is alt text"
	});
	oAdRotator1.registerAd(oAd1);

	oAd2 = new cAd({
		url:"http://www.yahoo.com",
		src:"homepageAd2.jpg",
		altText:"this is alt text"
	});
	oAdRotator1.registerAd(oAd2);
	
	oAd3 = new cAd({
		url:"http://www.sandesh.com",
		src:"homepageAd3.jpg",
		altText:"this is alt text"
	});
	oAdRotator1.registerAd(oAd3);


	oAdRotator1.randerAd();
*/

oAdrotator = new cAdRotator();
