(function($) {

	  if ((/referendum/gi).test(document.location.toString())) {return}
   
    var link_comunali = "/Speciali/Politica/2009/elezioni09/SEAS/amministrative/comunali/40090607000000_2_ITALIA_IT.shtml";
    var link_provinciali = "/Speciali/Politica/2009/elezioni09/SEAS/amministrative/provinciali/40090607000000_9_ITALIA_IT.shtml";


    jQuery("ul.telecomando_europee").each(function() {

        var arr = ["AUSTRIA", "BELGIO", "BULGARIA", "CIPRO", "DANIMARCA", "ESTONIA", "FINLANDIA", "FRANCIA", "GERMANIA", "GRECIA", "IRLANDA", "LETTONIA", "LITUANIA", "LUSSEMBURGO", "MALTA", "PAESI BASSI", "POLONIA", "PORTOGALLO", "REGNO UNITO", "REPUBBLICA CECA", "ROMANIA", "SLOVACCHIA", "SLOVENIA", "SPAGNA", "SVEZIA", "UNGHERIA"];
        var self = jQuery(this);

        jQuery(arr).each(function() {
            self.append("<li class='level-0'><a href=\"" + this.replace(' ', '_')
			 + ".shtml\">" + this + "</a></li>");
        });

    
    });
   
    
    $('body').attr('class', document.location.host.split('.')[0]);
	$('#header-bar').append('<a id="fake_home_link" href="/"/><a id="fake_election_link" href="/politica/speciali/2009/elezioni/"/>');


    //$("head").append("<style>.dark_box table.stats td.circle {padding:none !important  ;background-color:#ffffff !important  } </style>");

    //$("div.elections_container div.elections_col2 div.dark_box p:first-child").css("padding-top", "18px");
    // $("table.stats").each(function() {
    //     $("tr:only-child:last", this).each(function() {
    //         $(this).after("<tr class='last_tr'><td></td></tr>");
    //     });
    //  });
    //$("head").append("<style>.dark_box {padding:none !important  ;background-color:#ffffff !important  } </style>");
    $(".darkbox table").css("background-color", "#ffffff");
    //$(".place:first h1").css("font-size", "22px");
    var rgText = {
        "/comunali/": function() {
            //$(".col_tabs a").removeClass("active").eq(0).addClass("active");
            $(".main_menu:first .level0:last a").map(function() {
                return $(this).html() != "ITALIA INSULARE" ? this: null;
            }).each(function() {

                $(".main_menu:first").append((function() {
                    return $(".level0:last")
                    .clone()
                    .removeClass("current")
                    .find("a:first")
                    .html("ITALIA INSULARE").each(function() {
                        this["href"] = "CALTANISETTA.shtml";
                    }).end();

                })());

            });

            $("li.level1 a").map(function() {
                return $(this).html() == "FRIULI-VENEZIA GIULIA" ? this: null;
            }).parent().remove();

        },
        "/provinciali/": function() {

            $(".legend li.ico3").each(function() {
                var self = $(this);
                self.html((function() {
                    return (self.html() != "undefined") ? self.html().replace("comuni al", "<span style=\"white-space:nowrap;\">province al</span>") : "";
                })());
            });

            $("li.level1 a").map(function() {
                return $(this).html() == "FRIULI-VENEZIA GIULIA" ? this: null;
            }).each(function() {
                this["href"] = "PORDENONE.shtml"
            });


            //$(".col_tabs a").removeClass("active").eq(1).addClass("active");
            $(".legend .ico1,.legend .ico2").css("visibility", "hidden");

            setTimeout(function() {
                $("head").append('<link rel="stylesheet" type="text/css" href="http://css.corriere.it/includes2007/static/css/elezioni09_provinciali.css">');
            });

        }
    };

    /*   
 $("span.ente:first").parent().each(function() {
        var self = this;
        $(self).html((function() {
            return ($(self).html() != "undefined") ? $(self).html()
            .replace("Provincia di", "<span class='ente'>Provincia di</span>")
            .replace("Comune di", "<span class='ente'>Comune di</span>") : "";
        })());
    });

    $("span.ente:first").parent().each(function() {

        var self = this;
        $(self).html((function() {
            var arr = $(self).html().split("<br>");

            //return "<span class='ente'>" + (!arr[0] != true ? arr[0] : "") + "</span><br/>" + (!arr[1] != true ? arr[1] : "");
        })());

    });
*/

    if ($("u").html() == "Seleziona un ente") {

        $(".legend:first").each(function() {
            var offSet = $(this).offset();
            var width = $(this).width();

            var sub = ".elections_col2 .col_header";

            $(sub).find("p").map(function() {
                return $(this).html().length == 6 ? this: null
            }).remove();

            $(".col_header:eq(1)").hide();
            $(".light_box:eq(0)").hide();

            $(".elections_col2").height(300);

            var html_sub = $(sub).html().replace("Comune di", "Comune di &nbsp;");


            $("body").append((function() {
                //style=\"padding:35px\"
                return $("<div id=\"id_fumetto\"></div>")
                .html(html_sub)
                .append("<br/><br/><br/>")
                .append("<div id='freccia'>" + " <img style=\"display:inline\" src='http://www.corriere.it/Speciali/Politica/2009/elezioni09/SEAS/amministrative/img/freccia.png' /> <span class='freccia'> " + $("u").html() + " </span> </div>")
                .css("background-repeat", "no-repeat")
                .css("top", offSet.top - 20)
                .css("left", offSet.left + width - 20)
                .css("position", "absolute")
                //.css("font-size", "12px")
                //.css("font-weight", "bold")
                //.css("margin", "0 10px 5px")
                .css("width", 327)
                .css("height", 220)
                //.css("background-image", "url(http://www.corriere.it/Speciali/Politica/2009/elezioni09/SEAS/amministrative/img/fumetto.png)")
                .css("left", offSet.left + width - 20);
            })());

            $("#id_fumetto").each(function() {
                //$(this).css("padding-left", "27px");
                }).find("p:eq(1)").css("white-space", "nowrap");

            $("u").parent().parent().remove();
            $(sub).html("");

        });

    }

    $(document).ready(function() {
	
	    //parte da commentare per abilitare i tab dei referendum;
			
	
		  jQuery('ul.elections_tabs_menu').css('width', '937px');
			jQuery('ul.elections_tabs_menu').append('<li class="t0"><a href="/Speciali/Politica/2009/elezioni09/SEAS/referendum/index.shtml">Referendum</a></li>');

			$t2=jQuery(".t2");
			$t1=jQuery(".t1").css("margin-right","5px");
			$t2.after($t1);
	
	    
  
	

        for (el in rgText) {
            if (new RegExp(el, "gi").test(document.location.toString())) rgText[el]()
        };

        //$("p.prev_res").html("RISULTATI PRECEDENTI CONSULTAZIONI - " + $("p.prev_res").html());

    });

})(jQuery);
