var timer = new Array();
var state = 0;
var folder = new Array();

$(window).unload(function()
{
        $("div[class='campagna']").each(function (i, element)
                {
			if ($.cookie($(this).attr('id')+"-"+self.window.name))
				updateCookie($(this).attr('id'));
                });
});

$(document).ready(function()
{
	//alert($("body").html()); 
        $("div[class='campagna']").each(function (i, element) {
                if (!$.cookie($(this).attr('id')+"-"+self.window.name)) {
                        timer[$(this).attr('id')] = 0;
                }
                var tabs = getTables($(this).attr('id'));
		folder[$(this).attr('id')] = tabs.image_folder;
                start($(this), tabs);
                $(this).click(function () {
                        clickedBanner($(this), tabs);
                });
        });
        $("div[class*='campagna_mini']").each(function (i, element) {
                var tabs = getTables($(this).attr('id'));
                var div = new Array();
                div[0] = $(this).children("div:eq(0)");
                div[1] = $(this).children("div:eq(1)");
                startPic($(this), div, tabs);
                div[0].click(function () {
			clickedBannerPic(0, $(this), tabs);
		});
		div[1].click(function () {
			clickedBannerPic(1, $(this), tabs);
		});

        });
	$("div[class*='campagna_big']").each(function (i, element) {
		var tabs = getTables($(this).attr('id'));
		startBig($(this), tabs);
	});
	$("div[class*='campagna_locale']").each(function (i, element) {
		var divid = $(this).attr('id');
		var tabs = getTables(divid);
		//tabs = JSON.parse(tabs);
		var cod_loc = $(this).attr('name');
		$.post("GMBanner/modules/get_scheda.php", {cod_loc: cod_loc, tabs: tabs.tab_camp}, function(scheda) {
			if (scheda != "nothing") {
				var dati = JSON.parse(scheda);
				//var dati = scheda;
				var html = "<a href=\"javascript:void(0);\"><img src=\""+tabs.image_folder+"/"+dati.image+"\" alt=\""+dati.alt+"\" title=\""+dati.title+"\" /></a>";
				$("#"+divid).html(html);
				$.post('GMBanner/modules/db_in.php', { cod: cod_loc, action: "displayed", table: tabs.tab_report });
				$("#"+divid).click(function() {
					$.post("GMBanner/modules/db_in.php", {table: tabs.tab_report, cod: cod_loc, action: 'clicked'}, function() {
						if ($.cookie('uid')) {
							var uuid = $.cookie('uid');
							$.post('GMBanner/modules/db_uid.php', { uid: uuid, banner: dati.image, table: tabs.tab_visual}, function(data) {
								location.href = dati.link;
							});
						} else {
							location.href = dati.link;
						}
					});
				});
			}
		});
	});
	$(".imgsponsor").live('click',function(event, ui) {
		var cod = $(this).attr('id');
		var link = $(this).attr('name');
		$.post("GMBanner/modules/db_in.php", {table: 'report_main_sponsor',cod: cod,action: 'clicked'}, function() {
			if ($.cookie('uid')) {
				var uuid = $.cookie('uid');
				var path = $("#"+cod).children().children().attr('src');
				var path_splitted = path.split(/\//);
				var banner = path_splitted[2];
				$.post('GMBanner/modules/db_uid.php', { uid: uuid, banner: banner, table: 'visual_main_sponsor'}, function(data) {
					location.href = link;
				});
			} else {
				location.href = link;
			}
		});
	});
	$("div[class*='campagna_ricerca']").each(function(j,element) {
		var divid = $(this).attr('id');
		var tabs = getTables(divid);
		var macro = $("#where-kind").val();
		var tml = "";
		var locs = new Array();
		$.post("GMBanner/modules/get_ricerche.php", {tabs: tabs.tab_camp, macro: macro}, function(ricerche) {
			var arrow = JSON.parse(ricerche);
			for(var i in arrow) {
				tml += "<a href=\"javascript:void(0);\" name=\""+i+"\" class=\"bannerini_ric\"><img id=\""+arrow[i].cod_loc+"\" src=\""+tabs.image_folder+"/"+arrow[i].image+"\" alt=\""+arrow[i].alt+"\" title=\""+arrow[i].title+"\" /></a>";
				locs[i] = arrow[i].cod_loc;
			}
			$("#"+divid).html(tml);
			$.post('GMBanner/modules/db_in_multi.php', { locs: locs, table: tabs.tab_report });
			$(".bannerini_ric").click(function() {
				var cod_loc_ = $(this).children().attr('id');
				var index = $(this).attr('name');
				$.post("GMBanner/modules/db_in.php", {table: tabs.tab_report, cod: cod_loc_, action: 'clicked'}, function() {
					if ($.cookie('uid')) {
						var uuid = $.cookie('uid');
						$.post('GMBanner/modules/db_uid.php', { uid: uuid, banner: arrow[index].image, table: tabs.tab_visual}, function(data) {
							location.href = arrow[index].link;
						});
					} else {
						location.href = arrow[index].link;
					}
				});
			});
		});
	});

});

