ads = {
	size: 0,
	url : "",
	bannerObject: new Array(),
	init: function(url,i){
		ads.size = $(".wp_banner_ads").size();
		ads.url = url;
			ads.bannerObject[i] = new Array(5);
			ads.bannerObject[i][0] = 0;													//"size"
			ads.bannerObject[i][1] = $(".wp_banner_ads:eq("+i+")").children().size();	//"banner_count"
			ads.bannerObject[i][2] = 0;													//"currentAd"
			ads.bannerObject[i][3] = true;												//"saveFlag"
			ads.bannerObject[i][4] = $(".wp_banner_ads:eq("+i+")").attr("group-id");	//"groupId"
			ads.loadBanners(i);
		
		$(".wp_banner_ads").hide();
	},
	loadBanners: function(intehero){
		//if(ads.bannerObject[intehero][2] != ""){
			if(ads.bannerObject[intehero][2] > (ads.bannerObject[intehero][1]-1)){
				ads.bannerObject[intehero][2] = 0;
				ads.bannerObject[intehero][2]++;
				ads.bannerObject[intehero][3] = false;
			}else{
				ads.bannerObject[intehero][2]++;
			}
			banner = $("[group-id="+ads.bannerObject[intehero][4]+"] .banner_ad_"+ ads.bannerObject[intehero][2]);
			ads.save(ads.bannerObject[intehero][3], banner.attr("adId"), intehero);
			$("#wp_banner_ads_container_"+ads.bannerObject[intehero][4]).html(banner.html());
			//console.log(banner.html());
		//}
	},
	save: function(flag,ad_id,i){
		if(flag){
			$.post(ads.url+"save.php?action=imp&id="+ad_id, {},
					function(msg){
						ads.load(i);
					}
				);	
		}else{
			ads.load(i);
		}	
	},
	load:function(i){
		banMidTimer = 0;
		clearTimeout(banMidTimer);
		banMidTimer = setTimeout("ads.loadBanners("+i+")",6000);
	},
	loadGraph: function(){
		$(".msg").html("Loading Please wait..");
		$(".image-graph").attr("src","");
		$.post(ads.url+"save.php?", {
			"adAction":$("[name=adAction]").val(),
			"adName":$("[name=adName]").val(),
			"adMonth":$("[name=adMonth]").val(),
			"adYear":$("[name=adYear]").val(),
			"adYearTo":$("[name=adYearTo]").val()},
			function(msg){
				img = new Image();
				img.onload = function(){
					$(".image-graph").attr("src",img.src);
					$(".msg").html("");
				}	
				img.src = ads.url+"images/output.png?state="+msg;
			}
		);
	},
	clickPlus: function(ad_id){
		$.post(ads.url+"save.php?action=click&id="+ad_id, {},
			function(msg){
		
			}
		);
	},
	ws_ad: function(url,start,end){
		if(start == "" && end != ""){
			alert("From: Field is required.");
		}else if(end == "" && start != ""){
			alert("To: Field is required.");
		}else{
			$(".ws-reports").html("Getting Reports. Please wait..");
			$.post(ads.url+"adrenderer.php", {"url":url,"start":start,"end":end},
				function(msg){
					ads.ws_display(msg);
				},
				'json'
			);
		}
	},
	ws_display: function(wsData){
		tbl_head = '<table class="widefat post fixed" cellspacing="0">';
		tbl_head +=' <thead>';
		tbl_head +='	  <tr>';
		tbl_head +='	  	<td></td>';
		tbl_head +='	  	<td colspan="2" style="color:#000080;"><center><b>Unique</b></center></td>';
		tbl_head +='	  	<td colspan="2" style="color:#800000;"><center><b>Total</b></center></td>';
		tbl_head +='	  	<td colspan="2" ><center><b>Today</b></center></td>';
		tbl_head +='	  </tr>';
		tbl_head +='      <tr>';
		tbl_head +='		<th scope="col" id="title" class="manage-column column-title" style="">Ad Name</th>';
		tbl_head +='		<th scope="col" id="title" class="manage-column column-title" style="color:#000080;"">Clicks</th>';
		tbl_head +='		<th scope="col" id="title" class="manage-column column-title" style="color:#000080;"">Impressions</th>';
		tbl_head +='		<th scope="col" id="title" class="manage-column column-title" style="color:#800000;">Clicks</th>';
		tbl_head +='		<th scope="col" id="title" class="manage-column column-title" style="color:#800000;">Impressions</th>';
		tbl_head +='		<!-- <th scope="col" id="title" class="manage-column column-title" style="color:#800000;">Visitors</th>-->';
		tbl_head +='		<th scope="col" id="title" class="manage-column column-title" style="">Clicks</th>';
		tbl_head +='		<th scope="col" id="title" class="manage-column column-title" style="">Impressions</th>';
		tbl_head +='	  </tr>';
		tbl_head +='  </thead>';
		tbl_head +='  <tbody>';
		text ="";
		flag = false;
		for(var i in wsData){
			flag = true;
			text += "<tr>";
			text += "<td>" + wsData[i].name + "</td>";
			text += "<td>" + wsData[i].clicksUnique + "</td>";
			text += "<td>" + wsData[i].impressionsUnique + "</td>";
			text += "<td>" + wsData[i].clicksTotal + "</td>";
			text += "<td>" + wsData[i].impressionsTotal + "</td>";
			text += "<td>" + wsData[i].clicksToday + "</td>";
			text += "<td>" + wsData[i].impressionsToday + "</td>";
			text += "</tr>";
		}
		tbl_head += text;
		tbl_head +='  </tbody>';
		tbl_head +='  </table>';
		if(flag){
			tbl_head +='<input type="button" value="download" onclick="ads.download_report()" />';
		}	
		$(".ws-reports").html(tbl_head);
	},
	download_report: function(){
		$.post(ads.url+"save.php?action=download", {},
				function(msg){
					location.href = msg;
				}
			);	
	}	
}