/* lang select */
function init_lang_select() {
  $('#lang').change(function() {
    window.location=$('#lang option:selected').val();
  });
}



function showLook(index) {
    $('.current').removeClass('current');
    $('li.' + index).addClass('current');
    left = $('.current').position().left;
    x = parseInt(left);
    x = x - 345;
    x = -x;
    $('#menu').css('left', x + 'px' );
}



function redimensionnement(){  
    var image_width = $('.home #bg-visu img').width();  
    var image_height = $('.home #bg-visu img').height();      
    
    var over = image_width / image_height;  
    var under = image_height / image_width;  
    
    var body_width = $(window).width();  
    var body_height = $(window).height();  
    
    if (body_width / body_height >= over) {  
    	$('.home #bg-visu img').css({  
    	    'width': body_width + 'px',  
    	    'height': Math.ceil(under * body_width) + 'px',  
    	    'left': '0px'
    	    //'top': Math.abs((under * body_width) - body_height) / -2 + 'px'  
    	});  
    }   
    
    else {  
    	$('.home #bg-visu img').css({  
    	    'width': Math.ceil(over * body_height) + 'px',  
    	    'height': body_height + 'px',  
    	    'top': '0px',  
    	    'left': Math.abs((over * body_height) - body_width) / -2 + 'px'  
    	});  
    }  
}


$(window).load(function () {
  redimensionnement();
});


function showHideSubNav(posBas, posHaut){
  
    var subNavBtn = $("#sub-nav-button");
    var bloc = $("#bloc-bottom");
    
    if(subNavBtn.hasClass("closed")){
    	subNavBtn.addClass("opened").removeClass("closed");
    	bloc.animate({bottom: posHaut}, 1200, "easeInOutBack");
    }
    else{
    	subNavBtn.addClass("closed").removeClass("opened");
    	bloc.animate({bottom: posBas}, 1200, "easeInOutBack");    
    }
}

$(document).ready(function() {

    init_lang_select();
    
    
    $(".home #bloc-bottom").show();
    
    
    /* background resizable */     
    $(window).resize(function(){  
      redimensionnement();  
    });
    
    $(".map-desc").parent().css("overflow", "visible");

    /* Formulaire */

    /* labels */
    $(".home label").hide();
  
    /* Inputs */
    var $Input = $("input[type=text]");
    $Input.addClass("unfocused");
    
    $(".home input[type=text]").each(function(){
    	$this = $(this);
    	title = $this.parent().children("label").text();
    	if($this.val() == ""){
    	    $this.val(title);
    	}
    	else if($this.val() != title){
    	    $this.addClass("focused").removeClass("unfocused");
    	}
    });
    
    $(".home input[type=text]").focus(function(){
    	$this = $(this);
    	title = $this.parent().children("label").text();
    	if($this.val()==title){
    	    $this.val("");
    	}
    	else if($this.val()==""){
    	    $this.val(title);
    	}
    });
    
    $(".home input[type=text]").blur(function(){
    	$this = $(this);
    	title = $this.parent().children("label").text();
    	if($this.val()==""){
    	    $this.val(title).removeClass("focused").addClass("unfocused");
    	}
    });
    
    
    
    $Input.focus(function(){
    	$this = $(this);
    	$this.addClass("focused").removeClass("unfocused");
    });
    
    $Input.blur(function(){
    	$this = $(this);
    	if($this.val() == ""){
    	    $this.addClass("unfocused").removeClass("focused");
    	}
    });
    
    
    /* Textareas */
    
    var $textarea = $("textarea");
    
    $textarea.addClass("unfocused");
    
    $textarea.focus(function(){
    	var $this = $(this);
    	$(this).removeClass("unfocused").addClass("focused");
        });
        
        $textarea.blur(function() {
    	if($.trim($('textarea').val()) == ""){
    	    $(this).addClass("unfocused").removeClass("focused");
    	} 
    	else{
    	    $(this).addClass("focused").removeClass("unfocused");
    	}
    });
    
    /* Selects */
    
    $("select").addClass("select");
    
    if (!$.browser.opera) {
	
    	$('select.select').each(function(){
    	    var title = $(this).parent().find("label").text();
          title = $('option:selected',this).text();
    	    $(this)
    		.css({'z-index':10,'opacity':0,'-khtml-appearance':'none'})
    		.after('<span class="select">' + title + '</span>')
    		.change(function(){
    		    val = $('option:selected',this).text();
    		    $(this).next().addClass("optioned").text(val);
    		})
    	});
	
    };
    

    /* SubNav home */

    var subNavBtn = $(".home #sub-nav-button");
    var blocHome = $("#bloc-home");
    
    subNavBtn.addClass("opened");
    
    setTimeout(
      '$(".home #bloc-bottom").animate({bottom: "-186"}, 1200, "easeInOutBack",function(){$("#sub-nav-button").addClass("closed").removeClass("opened");})',
	     7000);
  
    subNavBtn.click(function(){
      showHideSubNav(-186, 20);
    });
    
    
    /* Navigation */
    
    var $PrimaryNav = $(".int #primary-nav");
    var $primaryNavUl = $(".int #primary-nav ul");
    
    $PrimaryNav.after('<a href="#" id="show-nav" class="opened">Montrer la barre de navigation</a>');
    var $ShowNav = $("#show-nav");
    
    $ShowNav.click(function(){
      if($ShowNav.hasClass("opened")){
        $PrimaryNav.animate({left: '700'}, 800 ,"easeInOutBack");
        $ShowNav.animate({backgroundPosition: 6},800).removeClass("opened").addClass("closed");
      }
      else{
        $PrimaryNav.animate({left: '0'}, 800 ,"easeInOutBack");
        $ShowNav.animate({backgroundPosition: -27},800).removeClass("closed").addClass("opened");
      }
    });
    
    
    /* Les collections */
    
    $(".collections h1").hide();
    $(".collections #bloc-bottom").show();
    
    // Fermeture de la nav après 3s, uniquement sur 'Collections'
    setTimeout('$(".collections #primary-nav").animate({left: 700},function(){$("#show-nav").animate({backgroundPosition: 6},800).addClass("closed").removeClass("opened");})',3000);
    
    $("#nav-collections a:last-child").addClass("last");
    
    setTimeout('$(".collections #bloc-bottom").animate({bottom: 20},800 ,"easeInOutBack");$(".collections #sub-nav-button").removeClass("closed").addClass("opened")',3000);
    
    
    $(".collections #sub-nav-button").click(function(){
      showHideSubNav(-52, 20);
    });

    
    /* Ou nous trouver */
    
    $("#result-villes li:odd").addClass("imp");
    
    $("#hidden-link").hide();
    
    $("#result-villes li").hover(
      function(){
        $(this).addClass("hover");
      },
      function(){
	    $(this).removeClass("hover");
    });
    
    $("#online").click(function(){
      var link = $("#hidden-link").attr("href");
      window.open(link,'_blank');
    });
    
    
    /* Boutique */
    
    $(".boutique .section .article:last-child").addClass("last");
    
    
    $('#bloc-plus').hide().before('<button id="button-plus">En savoir plus</button>');
    $('#button-plus').toggle(function(){
      $('#bloc-plus').slideDown('slow');
	    $('#button-plus').text('Fermer');
    },
    function(){
	    $('#bloc-plus').slideUp('slow');
	    $('#button-plus').text('En savoir plus');
    });
    
    /* Recrutement */  
    
    $("#list-offre .sub:not('.active')").hide();

    $("#list-offre .toggle-sub span").each(function()
    {
      var texte = $(this).text();
      $(this).replaceWith('<a href="" class="title">'+ texte +'<\/a>');
    });

    $("#list-offre .toggle-sub > a").click(function()
    {
      if ($(this).next(".sub:visible").length != 0)
        $(this).next(".sub").slideUp("normal");
      else
      {
        $("#list-offre .sub").slideUp("normal");
        $(this).next(".sub").slideDown("normal");
      }
    return (false);
    });

    $("#list-offre .sub.active .active").removeAttr("href");

    $('#list-offre li').click(function(){
        var link = $(this).find('.big').attr('href');
        location.href = link;
    });

    $('.offre-spontanee').click(function(){
        var link = $(this).find('.big').attr('href');
        location.href = link;
    });
    
    /* Facebox */
    $("#facebox .close").html("");
    
    
    /* Footer */      
    $("#footer li:last-child").addClass("last"); 
  

});

