$(document).ready(function() {

    if(ie6){
        return;
    }

    $('#menu').find('a').each(function(){
        if($(this).attr('name')!=""){
            var tmp = $(this).attr('href');
            $(this).attr('href','#' + $(this).attr('name'));
            $(this).attr('name','tmp_' + tmp);
        }
    });

    function filterPath(string) {
        return string.replace(/^\//,'')
                    .replace(/(index|default).[a-zA-Z]{3,4}$/,'')
                    .replace(/\/$/,'');
    }

    $('#menu a[href*=#]').each(function() {
        if ( this.hash != "#contact"
                && filterPath(location.pathname) == filterPath(this.pathname)
                && location.hostname == this.hostname
                && this.hash.replace(/#/,'') ) {
            var $targetId = $(this.hash), $targetAnchor = $('[name=' + this.hash.slice(1) +']');
            var $target = $targetId.length ? $targetId : $targetAnchor.length ? $targetAnchor : false;
            if ($target) {
                //alert('bind click');
                var targetOffset = $target.offset().top;
                //alert(targetOffset);
                $(this).click(function() {
                    //alert('passe ici 2');
                    $('html, body').animate({scrollTop: targetOffset}, 600);
                    //return false;
                });
            }
        }
    });

    var lightBoxPage = typeof(pagePourLightBox) != 'undefined';

    if(!lightBoxPage){
        $('#menu a[href*=#]').each(function() {
            if ($(this).attr('href')!="#contact") {
                //alert('bind click');
                var targetOffset = $($(this).attr('href')).offset().top;
                //alert(targetOffset);
                $(this).click(function() {
                    //alert('passe ici 2');
                    $('html, body').animate({scrollTop: targetOffset}, 600);
                    //return false;
                });
            }
        });
    }

    $('#menu a[href*=#]').each(function() {
        if ( this.hash != "#contact"
                && filterPath(location.pathname) == filterPath(this.pathname)
                && location.hostname == this.hostname
                && this.hash.replace(/#/,'') ) {
            var $targetId = $(this.hash), $targetAnchor = $('[name=' + this.hash.slice(1) +']');
            var $target = $targetId.length ? $targetId : $targetAnchor.length ? $targetAnchor : false;
            if ($target) {
                var targetOffset = $target.offset().top;
                $('html, body').animate({scrollTop: targetOffset}, 600);
                return false;
            }
        }
    });


    $('div').find('.page').each(function(){
        loadBlock($(this).attr('id'));
    });

    if(!lightBoxPage){
        var offsetTo = $('.pageToOffset').attr('id');
        var offsetTop = $('#'+offsetTo).offset().top;
        $('html, body').animate({scrollTop: offsetTop}, 0);

        // Initiating menu
        $('#menu ul').lavaLamp({
            fx: 'easeOutExpo',
            speed: 800
        });
    }

    $('#contact_link').click(function() {$("#fancybox-outer").css({'backgroundColor': '#FFF'});});
    $('#contact_link').fancybox({
                                    onCleanup	:	function() {
                                            $.validationEngine.closePrompt(".formError",true);
                                    }
                            });
	$('#lien_contact').click(function() {$("#fancybox-outer").css({'backgroundColor': '#FFF'});});
    $('#lien_contact').fancybox({
                                    onCleanup	:	function() {
                                            $.validationEngine.closePrompt(".formError",true);
                                    }
                            });
							
    $('.mentions').click(function() {$("#fancybox-outer").css({'backgroundColor': '#CCC'});});
    $('.mentions').fancybox();
	
    $('.projets').click(function() {$("#fancybox-outer").css({'backgroundColor': '#CCC'});});
    $('.projets').fancybox();

    setTimeout(loadGeolocationBox, 2000);


    $("#contact_form").validationEngine({
                                    scroll: false,
                                    promptPosition: "topLeft",
                                    ajaxSubmit: true,
                                    ajaxSubmitFile: "/page/contact",
                                    ajaxSubmitMessage: "Votre message a été envoyé",
                                    validationEventTriggers: "keyup blur",
                                    success : false,
                                    failure : false
                            });
    $("#newsletter_form").validationEngine({
                                    scroll: false,
                                    promptPosition: "topLeft",
                                    ajaxSubmit: true,
                                    ajaxSubmitFile: "/page/newsletter",
                                    ajaxSubmitMessage: "Inscription enregistrée !",
                                    success : function() {setTimeout(function () {$('#newsletter_div').fadeTo('slow', 0);}, 5000);},
                                    failure : false
                            });
    $("#newsletter").blur(function() {
                    $.validationEngine.closePrompt("#newsletter");
            });

    if(!lightBoxPage){
        var doublon = $('#wall').html();
        //alert(doublon);
        swfobject.embedSWF("/wall/wall.swf", "wall", "1001", "600", "9.0.0","images/expressInstall.swf", {config: '/wall/wall.xml'}, {wmode: 'transparent'});
        $('.mainRefs').append("<div style='display:none;'>" + doublon + '</div>');
        swfobject.embedSWF("/flash/ange.swf", "ange", "225", "85", "9.0.0","images/expressInstall.swf", {}, {wmode: 'transparent'});
        swfobject.embedSWF("/flash/cascade.swf", "cascade", "400", "925", "9.0.0","images/expressInstall.swf", {}, {wmode: 'transparent'});
        swfobject.embedSWF("/flash/soucoupe.swf", "soucoupe", "550", "450", "9.0.0","images/expressInstall.swf", {}, {wmode: 'transparent'});
        swfobject.embedSWF("/flash/etoile.swf", "etoile", "370", "110", "9.0.0","images/expressInstall.swf", {}, {wmode: 'transparent'});
        swfobject.embedSWF("/flash/avion.swf", "avion", "150", "70", "9.0.0","images/expressInstall.swf", {}, {wmode: 'transparent'});
        swfobject.embedSWF("/flash/geoloc.swf", "geolocFlash", "100", "100", "9.0.0","images/expressInstall.swf", {}, {wmode: 'transparent'});
    }

    ajoutTrackingAnalytics();
});

function rendreInvisible(){
    $('div[id=disparaitre]').hide();
}

function openBox(displayQr){
    if(displayQr)
        $('#reference').css('width','700px');
    $.fancybox({
        'href'      : '#reference',
        onClosed    : function() { document.location.href = "/"; }
    });
    $('div[id=disparaitre]').html('');
    $("#fancybox-outer").css({'backgroundColor': '#666'});
}

function changerReferenceTo(id){
    $.fancybox({
        'href'	: '/reference/' + id + '/flash',
        onClosed    : function() { document.location.href = "/"; }
    });
    $("#fancybox-outer").css({'backgroundColor': '#666'});
    return false;
}

function loadGeolocationBox(){
    $('#geolocalisationDiv').click(function() {$("#fancybox-outer").css({'backgroundColor': '#FFF'});});
    $('#geolocalisationDiv').fancybox();
}

function submitFormTo(frm, id)
{
    var dataString = '?fromAddress=' + $('#fromAddress').val();
    $.get(frm.action + dataString,
      function(data) {
        $('#' + id).html(data);
      }
    );
    return false;
}


function clickRef(id)
{
    $.fancybox({
        'href'	: '/reference/' + id + '/flash'
    });
    $("#fancybox-outer").css({'backgroundColor': '#666'});
    pageTracker._trackPageview($('#ref' + id).attr('href'));
}

function loadBlock(id){
    var htmlFound;

    jQuery.get('/' + id + '/true/', function(data) {
        if(!jQuery(data)){
                //alert('non jquery');
                return;
        }
        htmlFound = $(data).find('#'+id).html();

        if($('#'+id).html()==""){
            $('#'+id).html(htmlFound);
        }

});

}

function ajoutTrackingAnalytics(){
    $('.tracking').click(function() { 
        var cible = $(this).attr('href');
        cible = cible.replace('#','/');
        //alert('cible : ' + cible);
        pageTracker._trackPageview(cible);
    });
    $('.trackingBase').click(function() {
        var cible = $(this).attr('name');
        cible = cible.substr(4)
        //alert('cible : ' + cible);
        pageTracker._trackPageview(cible);
    });
}
