
// Redirect to home
//if (location.pathname == "/" || location.pathname == ""  ) {
//   location.href = "/index.php/eng/Home";
//}

$(document).ready(function(){
	var desc_menu = $('.news-blok h4 a');
 
	 for(var i=0; i< desc_menu.length ; i++){
		if(desc_menu[i].href.match('Agenzia') != null){
			$(desc_menu[i]).attr('href','/index.php/agenzia');
		}
		if(desc_menu[i].href.match('Giustizia-Sportiva') != null){
			$(desc_menu[i]).attr('href','/index.php/giustizia');
		}
		if(desc_menu[i].href.match('Giustizia-Sportiva') != null){
			$(desc_menu[i]).attr('href','/index.php/giustizia');
		}
	 }
	 
	 var sdesc_menu = $('.orange-links');
	 
	 for(var i=0; i< sdesc_menu.length ; i++){
		
		if(sdesc_menu[i].href.match('/Giochi/Tris') != null){
			$(sdesc_menu[i]).attr('href','/index.php/tris');
		}
		
	 }


    // Video page
    if (location.href.indexOf("Video") != -1 ) {
        $("ul#submenu > li").hide();

        // attach links to videos
        $.each( $("div.class-image"), function() {
            var header = $(this).children("h3").text();
            var current_href = location.href;
            if (current_href.substring(-1) != "/" ) {
                current_href = current_href + "/";
            }
            var image_number = header.substring(5, header.length);
            $(this).children("div").children("p").children("a").attr(
                "href", current_href + "video" + image_number);

        });


    }

    // L'ente page
    if (location.href.indexOf("L-Agenzia") != -1 ) {
		
    } 

    // I-collaboratori page
    if (location.href.indexOf("I-collaboratori") != -1 ) {

        // fix first column width
        $("tr>td:nth-child(1)").attr("width", 450);
        // hide the news details
        $("tr.bglight").hide();
        // attach link to a bold header  
        var id = 0;
        $.each( $("tr.bgdark") , function() {
            var header = $( $(this).children("td").get(0) ).children("b");
            var link_text = header.text();
            var news = $(this).next();
            // attach link to a bold header  
            header.html("<a href='#' id='news_"+id+"' >"+link_text+"</a> \n");
            // tag the content
            news.attr("id", "content_"+id);
            id++;
        });

        $("a[id^=news_]").click(function() {
            var news_id = $(this).attr("id");
                news_id = news_id.substring(5, news_id.length);

            $("tr#content_"+news_id).toggle();
            return false;
        });

    } 


    // Controllo-e-disciplina-corse page
    if (location.href.indexOf("Controllo-e-disciplina-corse") != -1 ) {
        // get content
        var content = $("div#midcolumn > p");

        var id = 0;
        $.each( content.children("b"), function() {
            var text = $(this).html();
            var link = "<a href='#' id='news_"+id+"' >"+text+"</a> <br/> \n";
            var news = "<div  id='content_"+id+"'  >";
            content.html( content.html().replace("<b>"+text+"</b>","</div>"+link+news) );
            id++;
        });

        $("div[id^=content_]").hide();
        
        $("a[id^=news_]").click(function() {
            var news_id = $(this).attr("id");
                news_id = news_id.substring(5, news_id.length);

            $("div#content_"+news_id).toggle();
            return false;
        });

    }

    // Normativa
    if (location.href.indexOf("Normativa") != -1 ) {
        // cut document list
        $("div#midcolumn > ul > li").hide();
        for (var i = 1; i<16; i++) {
            $("div#midcolumn > ul >li:nth-child("+i+")").show();
        }
    
        //attach 'more' button
        $("div#midcolumn > ul").after("<a href='#' id='more_docs' >More</a> \n");
        
        $("a#more_docs").click(function() {
            $("div#midcolumn > ul > li").show();
            $(this).hide();

            return false;
        });

    }



    // Calendario
    if (location.href.indexOf("Calendario") != -1 ) {


    }

});
