
/* - ++resource++snowjamboree.theme.javascript/snow.js - */
var BrowserDetect = {
    init: function () {
        this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
        this.version = this.searchVersion(navigator.userAgent)
            || this.searchVersion(navigator.appVersion)
            || "an unknown version";
        this.OS = this.searchString(this.dataOS) || "an unknown OS";
    },
    searchString: function (data) {
        for (var i=0;i<data.length;i++) {
            var dataString = data[i].string;
            var dataProp = data[i].prop;
            this.versionSearchString = data[i].versionSearch || data[i].identity;
            if (dataString) {
                if (dataString.indexOf(data[i].subString) != -1)
                    return data[i].identity;
            }
            else if (dataProp)
                return data[i].identity;
        }
    },
    searchVersion: function (dataString) {
        var index = dataString.indexOf(this.versionSearchString);
        if (index == -1) return;
        return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
    },
    dataBrowser: [
        {
            string: navigator.userAgent,
            subString: "Chrome",
            identity: "Chrome"
        },
        {   string: navigator.userAgent,
            subString: "OmniWeb",
            versionSearch: "OmniWeb/",
            identity: "OmniWeb"
        },
        {
            string: navigator.vendor,
            subString: "Apple",
            identity: "Safari",
            versionSearch: "Version"
        },
        {
            prop: window.opera,
            identity: "Opera"
        },
        {
            string: navigator.vendor,
            subString: "iCab",
            identity: "iCab"
        },
        {
            string: navigator.vendor,
            subString: "KDE",
            identity: "Konqueror"
        },
        {
            string: navigator.userAgent,
            subString: "Firefox",
            identity: "Firefox"
        },
        {
            string: navigator.vendor,
            subString: "Camino",
            identity: "Camino"
        },
        {       // for newer Netscapes (6+)
            string: navigator.userAgent,
            subString: "Netscape",
            identity: "Netscape"
        },
        {
            string: navigator.userAgent,
            subString: "MSIE",
            identity: "Explorer",
            versionSearch: "MSIE"
        },
        {
            string: navigator.userAgent,
            subString: "Gecko",
            identity: "Mozilla",
            versionSearch: "rv"
        },
        {       // for older Netscapes (4-)
            string: navigator.userAgent,
            subString: "Mozilla",
            identity: "Netscape",
            versionSearch: "Mozilla"
        }
    ],
    dataOS : [
        {
            string: navigator.platform,
            subString: "Win",
            identity: "Windows"
        },
        {
            string: navigator.platform,
            subString: "Mac",
            identity: "Mac"
        },
        {
               string: navigator.userAgent,
               subString: "iPhone",
               identity: "iPhone/iPod"
        },
        {
            string: navigator.platform,
            subString: "Linux",
            identity: "Linux"
        }
    ]

};
BrowserDetect.init();

jQuery(function($) {
    
    var os = BrowserDetect.OS.toLowerCase(), 
        browser = BrowserDetect.browser.toLowerCase(), 
        version = BrowserDetect.version;

    $("body").addClass(os + " " + browser + " " + browser + version);

    /* hover de menu si IE7 */

    if (browser == "explorer" && version == 7) {

        $("#snow-globalnav li.niv1").hover(
            function(){
                $("#snow-globalnav .sub:visible").hide();
                $(this).children(".sub").show();
            }, function(){
                return false;
            }
        );
        $("#snow-globalnav li.niv1 .sub ul li a").hover(
            function(e){
                e.stopPropagation();
                return false;
            }
            , function(e){
                e.stopPropagation();
                return false;
            }
        );
        $("#snow-globalnav li.niv1 .sub").hover(
            function(){
                return false;
            }
            ,function(){
                $(this).hide();
            }   
        )
    }

    /* clique sur une nouvelle */
    $('#viewlet-nouvelles td.nav .uls a').click(function(e) {
        e.preventDefault();
        var url = $(this).attr('href') + '/nouvelle_ajax';
        var $link = $(this);
        
        $('#viewlet-nouvelles td.nav .uls a').removeClass('active');
        $link.addClass('active');
        
        $('#viewlet-nouvelles .viewer').fadeOut(function(){
            var success = function(data) {
                $('#viewlet-nouvelles .viewer').html(data).fadeIn(function() {$(this).show()});
                // le .show() est nécessaire pour IE8 qui n'affiche pas le TD sinon

                // reload facebook FBML
                var div = $('#viewlet-nouvelles .viewer');
                FB.XFBML.parse(div[0]);
            };
            $.ajax({'url' : url,'type' : 'get','success' : success});
        });

    });

    /* pagination des nouvelles */
    $('#viewlet-nouvelles').serialScroll({
        target: '.nav-shell'
        , items: '.ul'
        , prev: 'a#prev'
        , next: 'a#next'
        , duration: 400
        , cycle: false
        , onBefore:function( e, elem, $pane, $items, pos ){

            if( $items.length <= 1 ){
                return;
            }
            
            if($(elem).is(":first-child")){
                $("#viewlet-nouvelles table a#prev").addClass("hover");
            }else{
                $("#viewlet-nouvelles table a#prev").removeClass("hover");
            }
            
            if($(elem).is(":last-child")){
                $("#viewlet-nouvelles table a#next").addClass("hover");
                $("#viewlet-nouvelles table a#prev").removeClass("hover");
            }else{
                $("#viewlet-nouvelles table a#next").removeClass("hover");
            }
        }
    });

    /* starter le video vimeo sur le click du lien */
    $("#viewlet-nouvelles p.play-video a").live("click", function(e){

        $("#viewlet-nouvelles .text-wrapper").slideUp(500, function(){
            var player = $("#vimeo_player").get(0);

            if (player) {
                if (typeof(player.api_play) == 'function') {
                    player.api_play();
                } else {
                    var player = $(player).children("embed").get(0);
                    if (typeof(player.api_play) == 'function') {
                        player.api_play();
                    }
                }    
            }else{
                // ie7   
                if (BrowserDetect.browser.toLowerCase() == "explorer" && BrowserDetect.version == 7) {
                    var player = $("#viewlet-nouvelles .viewer .main embed").get(0);
                    player.api_play();
                }
            }
        });

        return false;
    });

    // var serial_scroll_step = 9;
    // var news_length = $("#viewlet-nouvelles .nav ul li a").length;
    // if(news_length <= serial_scroll_step){
    //     $("#viewlet-nouvelles table .nav a.next").addClass("hover");
    // }
    
    
    var $socializer_lis = $("#socializer ul.nav li");
    $("#socializer ul.nav li a").click(
        function(e){
            if ( $(this).parent().attr("id") == "rss" ) {
                return;
            }
            e.preventDefault();

            $socializer_lis.removeClass("active");
            $("#socializer .container .social").hide();
            
            var $this = $(this);
            var $parent = $($this.parent());
            $parent.addClass("active");
            $("#socializer .container ." + $parent.attr("id")).show();
            
            //var url = "/@@socializer?which=" + $parent.attr("id");
            //$.ajax({"url" : url, "type": "get", "success": success});
        }
    );

    /* colorbox */
    $('.communiques ul li a').each(function(i) {
        var $e = $(this);
        var href = $e.attr('href') + '/@@communique_ajax';
        $e.colorbox({'width' : '800px', 'height' : '600px', 'href' : href});
    });

    $("body.template-fg_base_view_p3 .pfg-form .field .fieldErrorBox").each(
        function(){
            if($(this).html().trim().length == 0){
                $(this).hide();
            }  
        }
    );
    
    /* module de compétitions à l'accueil */
    $all_lis = $("#module-competitions ul li");
    $all_lis.filter(".first-a").addClass("active");

    $("#module-competitions ul li").click(function(e){
        var $li = $(this);
        

        if ($li.hasClass("active")) {
            href = $li.children("a.lien-flottant").attr("href");
            window.location.href = href;
        }else{
            $active = $all_lis.filter(".active");
            $active.animate(
                {width: '-=214'}
                , 200
                , function(){
                    $active.removeClass("active");
                    $active.removeClass("first-a");
                    $li.animate(
                        {width: '+=214'}
                        , 200
                        , function(){
                            $li.addClass("active");
                        }
                    );
                }
            );

            e.stopPropagation();
            e.preventDefault();
            return false;
        }
    });

    /* Horaires et résultats. copier de accueil.pt */
    $("#horaire-resultats ul.nav a.menu").click(
        function(e){
            e.preventDefault();
            $("#horaire-resultats ul li").removeClass("active");
            $parent = $($(this).parent());
            $parent.addClass("active");

            $parent = $(this).parent();

            if($parent.attr("id") == "hor"){
                $("#horaire-resultats .container.horaire").show();
                $("#horaire-resultats .container.resultats").hide();
            }else{
                $("#horaire-resultats .container.horaire").hide();
                $("#horaire-resultats .container.resultats").show();
            }
        }
    );


    $("#horaire-resultats ul li select").change(
        function(e){
            var val = $(this).val();

            if ( !$("#horaire-resultats .container.horaire").is("visible") ) {
                $("#horaire-resultats ul.nav li#hor a.menu").trigger("click");
            };

            $("#horaire-resultats .container.horaire ul:visible").fadeOut(
                function(){
                    $("#horaire-resultats .container.horaire ul." + val).fadeIn();    
                }
            );
        }
    );
    
    var first = $("#horaire-resultats ul li select option:selected");    
    $("#horaire-resultats .container.horaire ul." + first.attr("value")).show();    
});


