
function printArea(el){
    new_window = window.open();
    doc = new_window.document;
    var links = window.document.getElementsByTagName('link');
    for (var i = 0; i < links.length; i++) {
        if (links[i].rel.toLowerCase() == 'stylesheet') {
            doc.write('<link type="text/css" rel="stylesheet" href="' + links[i].href + '"></link>');
        }
    }
    doc.write('<div class="' + $(el).attr("class") + '">' + $(el).html() + '</div>');
    doc.close();
    new_window.print();
}


function confirmDelete(){
    var agree = confirm("Veuillez confirmer la suppression");
    if (agree) 
        return true;
    else 
        return false;
}



$.fn.hoverClass = function(c){
    return this.each(function(){
        $(this).hover(function(){
            $(this).addClass(c);
        }, function(){
            $(this).removeClass(c);
        });
    });
};

$(document).ready(function(){
    /* recherche du menu activé */
    var reg = new RegExp("(/)", "g");
    var url_flat = window.location.pathname.replace(reg, "");
    var id = '#' + url_flat;
    var menu_number = 0;
    
    if ($(id).length == 0) {
        // on n'a pas trouvé, on split l'url pour chercher le début au pire
        var newReg = new RegExp("/", "g");
        var tableau = window.location.pathname.split(newReg);
        if (tableau.length >= 1) {
            var premier_element = tableau[1];
            id = '#' + premier_element;
        }
    }
    $(id).addClass("navcontainer-selected");
    $(id).parent().addClass("on");
    /* récupération du parent */
    var fragment = $(id).parent().parent().parent().attr("id");
    if (fragment) {
        var newReg = new RegExp("(-)", "g");
        var tableau = fragment.split(newReg);
        if (tableau.length >= 3) {
            menu_number = tableau[2] - 1;
        }
        else 
            if (tableau.length >= 2) {
                // bug ie, le tableau est fragment, 3 par exemple alors que sous FF c'est fragment, -, 3
                menu_number = tableau[1] - 1;
            }
    }
    
    var $tabs = $("#container-1 > ul").tabs({
        selected: menu_number,
		deselectable: true,
        select: function(event, ui){
            var link = $('#' + ui.panel.id + ' ul > :first-child > :first-child').attr("href");
            window.location = link;
        }
        
    });
    $('#container-1 > ul').bind('tabsselect', function(event, ui){
        //alert('hi');
    });
    
    $("#container-1").parent().hover(function(){
        /* rien a faire ici */
    }, function(){
        /* affichage du menu */
        //$("#container-1 > ul").tabs('select', menu_number);
    });
    
    if (fragment != "fragment-2" && window.location.pathname.indexOf("tribes/tribe", 0) == -1) {
        /* pas dans un service */
        $('#nav-unservice').parent().remove();
        $('#fragment-2').remove();
    }
    else {
        /* dans un service */
        $('#nav-vide').parent().remove();
        $('#fragment-7').remove();
    }
    
    
    $("#dropdownmenu-one li").hover(function(){
        $("ul", this).fadeIn("fast");
    }, function(){
    });
    if (document.all) {
        $("#dropdownmenu-one li").hoverClass("sfHover");
    }
    
    $("#login-dialog").dialog({
        autoOpen: false,
        draggable: false,
        modal: true,
        resizable: false,
        width: 500,
        height: 400,
        position: 'center'
    });
    
    
    $("#imprimer").click(function(){
        printArea($("#center-content"));
    });
    
    
    
    
    $('#services-carousel').jcarousel({
        auto: 4,
        wrap: 'last',
        visible: 3,
        scroll: 1
    });
    
    $("#accordeon").accordion({
        event: "click",
        autoHeight: false,
        alwaysOpen: false,
        header: "a.accordion-label"
    });
    
    $("#action-show-tchat").click(function(){
        window.open("http://www.portailthd.fr/cgi-bin/cgiirc/irc.cgi", 'tchat', '');
    });
    
    // sélection du texte de la recherche
    $("#id_q").focus(function(){
        // only select if the text has not changed
        if (this.value == this.defaultValue) {
            this.value = '';
        }
    })
    
    $('#inscription_form').pager('fieldset', {
        prevText: '<< Précédent',
        nextText: 'Suivant >>',
        navId: 'navinscriptionform',
        linkText: ['', '', '', "", '']
    });
    
    
    
    $('#loading').removeClass('loading');
    
    
});
