/* DOM READY
--------------------------------------------------------------------------------------------------------------------------------------*/

$(function(){


  // FancyBox pour image
	var select = $('a[href$=".bmp"],a[href$=".gif"],a[href$=".jpg"],a[href$=".jpeg"],a[href$=".png"],a[href$=".BMP"],a[href$=".GIF"],a[href$=".JPG"],a[href$=".JPEG"],a[href$=".PNG"],a.iframe,a.popin');
	select.attr('rel', 'fancybox');
	select.fancybox();
	
	
/*
  // FancyBox pour popin
	var select = $('a.popin');
  	select.attr('rel', 'fancybox');
  	select.fancybox({
  	width: 800,
  	height: 550,
  	autoScale: false,
  	showNavArrows: false
	});
	//on simule le click
	$(".popin").trigger('click');
	*/
	
  // FancyBox pour flipbook
	var select = $('a.iframe');
	select.attr('rel', 'fancybox');
	select.fancybox({
  	width: 960,
  	height: 750,
  	autoScale: false,
  	showNavArrows: false
	});

	
	var $tabs = 0;
	
	$('body').addClass('has-js');
	
	// Reset form focus
	$('.reset-focus').focus(function(){
		if($(this).attr('value') == this.defaultValue) $(this).attr('value', '');
	}).blur(function(){
		if($.trim(this.value) == '') this.value = (this.defaultValue ? this.defaultValue : '');
	});
	
	// Blank links
	$('a[rel=external]').click(function(){
		window.open($(this).attr('href'));
		return false;
	});
	
	// Menu
	
	
	$('#menu li a[title="eco-construction"]').addClass('eco');
	$('#menu li a[title="realisations"]').attr('target','_blank');
	$('#footer li a[title="terreal.com"]').attr('target','_blank');
	$('#menu > li').hover(function(){
		$(this).addClass('open');
		$(this).find('ul').stop(true, true).slideDown(300);
	}, function(){
		$(this).removeClass('open');
		$(this).find('ul').stop(true, true).slideUp(300);
	});
	
		// Jcarousel
	if($('.jcarousel-full')[0]){
		$('.jcarousel-full .carousel').jcarousel();
	}
	
	if($('#home')[0]) initHome();
	if($('.entree-prod')[0]) initEntree();
	if($('#product')[0]) initProduct();
	if($('#sidebar.guide')[0]) initGuide();
	if($('#contacts')[0]) initContact();
	
	/****************************** Page Nuancier TABS Famille Produit**********************/
	$('.tabs .nav_nuancier a').click(function() {
		var url = $(this).attr('href');
		
		$.ajax({
			url:url+"&ajax=1",
			success: function( data ) {//récupérer les données renvoyé pas le serveur (page-nuancier.php dans echo)
				$('.tabs-content').html(data);
				//activer le javascipt sur les nouveaux éléments générés.
				add_nav_listener();
			}
		});
		
		$tabs = $(this).parents('.tabs');
		$tabs.find('.nav_nuancier li').removeClass('current');
		$(this).parent().addClass('current');
		
		$tabs.find('.tab').hide();
		//var tab_id = $(this).attr('href');
		//$tabs.find(tab_id).show();
		$($(this).attr('href')).show();
		return false;
	});
	
	/*********************************TABS page fiche produit***************************************/

	$('.tabs .nav a').click(function(){

	    if ($(this).parents('.nav').hasClass('nav_nuancier')) return false;
		var $tabs = $(this).parents('.tabs');
		$tabs.find('.nav li.current').removeClass('current');
		$(this).parent().addClass('current');
		
		$tabs.find('.tab').hide();
		var tab_id = $(this).attr('href');
		//alert(tab_id);
		$tabs.find(tab_id).show();
		//update url quand clique sur la tab.
		//location.hash = $(this).attr('href');
		//initProduct();
		return false;
	});
	
	//Inner tab listener (PAGE NUANCIER)
	add_nav_listener();
	
	//accéder aux tabs par url
	if($('.inner-tab')[0] || $('.tab')[0]){

		var tab = getUrlVars();
		if(typeof(tab) != "undefined" && tab !== null){
			//alert(tab);
			$('[href$=#'+tab+']').click();
		}
	}



	
});

function initHome(){

  $("#slideshow-home").slides({
			generateNextPrev:true,
			play:5000
	});

}

function initEntree(){
	$(".entree-prod .carousel.ok").slides({generateNextPrev:true, generatePagination:false});
	$(".prev").show();
	$(".next").show();
}

function initProduct(){
	$("#tab-realisation .slideshow").slides();
	$(".slideshow-schema.ok").slides({generateNextPrev:true, generatePagination:false});
	$('#tab-realisation .jcarousel ul').jcarousel();
	$('#tab-realisation .jcarousel a').click(function(){
		$(this).parents('.slideshow').find('.pagination a').eq(parseInt($(this).attr('href').substr(1), 10)-1).trigger('click');
		//console.log($(this).parents('.slideshow').find('.pagination a').eq(parseInt($(this).attr('href').substr(1), 10)-1));
		
		return false;
	});
}

function initGuide(){
	$( "#slider-range" ).slider({
		range: true,
		min: 0,
		max: 100,
		values: [ 9, 100 ],
		slide: function( event, ui ) {
			$("#slider-value span").html( ui.values[ 0 ] + " - " + ui.values[ 1 ] );
		}
	});
	$( "#slider-value span" ).html($("#slider-range").slider("values", 0) +	" - " + $("#slider-range").slider("values", 1));

}

function add_nav_listener() {
	$('.inner-tabs .inner-tabs-nav a').click(function(){
		$tabs = $(this).parents('.inner-tabs');
		$tabs.find('.inner-tabs-nav li').removeClass('current');
		$(this).parent().addClass('current');
		
		$tabs.find('.inner-tab').hide();
		//on récupère ceux qui après le #
		var uuu = $(this).attr('href');
		var uu = uuu.substring(uuu.lastIndexOf("#"))
		$tabs.find(uu).show()
		//update url quand clique sur la tab. (non)
		//location.hash = $(this).attr('href');
		return false;
	});
}


function initContact() {
  $("div.info").hide();
  $(".std-select option:first-child").attr('selected', 'selected');

  $(".std-select option:selected").each(function () {
    $("."+$(this).attr("id")).show("fast");
  });

  $(".std-select").change(function(){
    $("div.info").hide();
    $(".std-select option:selected").each(function () {
      $("."+$(this).attr("id")).show("fast");
    });
  });

}
//récupérer la tab à afficher dans la fiche produit.
function getUrlVars()
{
    var hashes = window.location.href.slice(window.location.href.indexOf('#') + 1);
	//alert(hashes);
    return hashes;
}

