/**
 * $Id: javascript.js 227 2010-11-12 07:46:12Z matthieu $
 */
 

var sections=new Array('news','releases','photos', 'links', 'faq');

var section=null;
var action=null;
var id=null;

$(document).ready(function(){


    //ouverture section par défaut
    if (section) setSection(section,true, function(){
        //chargement sous page
        if (section && action) {
            $.get(section+'/'+action+'/'+id , {mode: 'ajax'}, function(data, stat) {
                $('div.'+section).replaceWith(data);
            });
        }
    });
    
    //affichage progression requetes ajax
    $('#ajaxWait').ajaxStart(function(){
        $(this).show();
        $('body, a').css('cursor', 'wait');
    })
    .ajaxStop(function(){
        $(this).hide();
        $('body, a').css('cursor', 'auto');
    });

    //initialisation scrollBar
    $("#scrollbar div").draggable({ 
        containment: '#scrollbar', 
        drag: function(evt,ui) {
        
            var pos=ui.position.left*4000/640;
            
            $('#sectionFrame').scrollLeft(pos);
            
            //chargement des pages a la volée pendant le scroll
            if (pos > 3200) loadSection('faq');
            if (pos > 2200 && pos < 3800) loadSection('links');
            if (pos > 1200 && pos < 2800) loadSection('photos');
            if (pos > 200 && pos < 1800) loadSection('releases');
            if (pos < 800) loadSection('news');

        },
        stop: function(evt,ui) {
            
            var pos=ui.position.left*4000/640;
            
            //ouvre la section la plus proche
            if (pos < 500) setSection('news');
            else if (pos < 1500) setSection('releases');
            else if (pos < 2500) setSection('photos');
            else if (pos < 3500) setSection('links');
            else setSection('faq');
        }
    });
    
    //click dans la barre scroll
    $('#scrollbar').click(function(evt){
        var pos=(evt.pageX - $(this).position().left) ;
        
        //ouvre la section la plus proche
        if (pos < 160) setSection('news');
        else if (pos < 320) setSection('releases');
        else if (pos < 480) setSection('photos');
        else if (pos < 640) setSection('links');
        else setSection('faq');
    });

    //click menu, changement de section
    $('#menu a:not(#contactmenu)').click(function(){
        setSection($(this).attr('href'));
        return false;
    });
    
    
     //scroll horizontal releases
     $('#sectionContainer').on("mousemove", ".horizScroll", function(evt){
         
         var ulElmt=$(this).find('ul');
         //largeur du UL depend du nombre de releases
         if (!ulElmt.data('computed')) {
             ulElmt.width(275 * ulElmt.find('li').length)
                 .data('computed', true);
         }
         
         var posX=evt.pageX -$(this).offset().left;
         var newpos = (posX -100)  *  (ulElmt.width() -300)  / $(this).width()   ;
         $(this).scrollLeft(newpos);
     });
     
    
    
    //clics sur liens dans les pages
    $('#sectionContainer').on("click", ".ajaxLoad", function(){

        var href=$(this).attr('href');
        if (!href) var href=$(this).find('a').attr('href');
        
        var elmt=this;
        $.get(href, {mode: 'ajax'}, function(data, stat) {
            $(elmt).closest('div.section').replaceWith(data);
        });
        return false;
    });

    
    //boutons scroll haut/bas
    $('#page').on( "click", ".navdown", function(){
        var scrollable=$(this).closest('.section').find('.scrollable');
        var newpos=scrollable.scrollTop() + scrollable.height();
        scrollable.animate({scrollTop:newpos}, 'slow');
        return false;
    });
    
    $('#page').on("click", ".navup", function(){
        var scrollable=$(this).closest('.section').find('.scrollable');
        var newpos=scrollable.scrollTop() - scrollable.height();
        scrollable.animate({scrollTop:newpos}, 'slow');
        return false;
    });
    
    
    //Accordéon FAQ
    $('#page').on('click', "#faq-liste dd", function(){
        $(this).next().toggle();
        if ($(this).next().is(':visible')) {
             var pos=$('#faqframe').scrollTop() + $(this).position().top - 136;
             $('#faqframe').animate({scrollTop:pos}, 'fast');
         }
    });


    //Config flash player audio
    AudioPlayer.setup("view/flash/player.swf", {  
        width: 200 ,
        autostart: "yes",
        animation: 'no',
        leftbg: 'dddddd',
        rightbg: 'dddddd',
        loader: '000000',
        initialvolume: 80
    }); 
    
    
    
    
});





function loadSection(section,funct)
{
    //chargement page si pas encore chargée
    if ($('div.'+section).children().length == 0) {
        $.get(section, {mode: 'ajax'}, function(data) {
            $('div.'+section).replaceWith(data);
            if ($.isFunction(funct)) funct();
        });
    }
    else if ($.isFunction(funct)) funct();
}



function setSection(section,noanim,funct)
{
    loadSection(section,funct);
    
    idx=null;
    for (i in sections) if (sections[i] == section) idx=i;
    if (idx == null) return false;
    
    if (noanim) {
        $("#scrollbar div").css('left', idx*640/4);
        $('#sectionFrame').scrollLeft( idx*1000);
    }
    else {
        $("#scrollbar div").animate({'left': idx*640/4}, 'fast');
        $('#sectionFrame').animate({scrollLeft: idx*1000},'fast');
    }

    $('#menu a.selected').removeClass('selected');
    $('#menu a#'+section+'menu').addClass('selected');
    return true;
    
}




// ============= FLASH PLAYER ===========


function playTrack(url, titre)
{
    AudioPlayer.embed("player", {
        soundFile: url,
        titles: titre
    });  
    return false;
}



function popup(link,w,h)
{
    return !window.open(link.href, "popup", "menubar=no, status=no, scrollbars=no, menubar=no, width="+w+", height="+h )
}


