/*
 * JavaScript Intersnack
 * Author: SOOYOOS
 * 
 */


//Gets the base URL

function getBaseURL() {
    var url = location.href;  // entire url including querystring - also: window.location.href;
    if(baseURL == '')
    {
        baseURL = url.substring(0, url.indexOf('/', 14));
    }

    if (baseURL.indexOf('http://localhost') != -1) {
        // Base Url for localhost
        var url = location.href;  // window.location.href;
        var pathname = location.pathname;  // window.location.pathname;
        var index1 = url.indexOf(pathname);
        var index2 = url.indexOf("/", index1 + 1);
        var index3 = url.indexOf("/", index2 + 1);
        var baseLocalUrl = url.substr(0, index3);

        return baseLocalUrl + "/";
    }
    else {
        // Root Url for domain name
        return baseURL + "/";
    }

}

//functions for the home page carousel
function hprecedent(){
	carousel = $('#slider').data('jcarousel');
	carousel.prev();
}


function hsuivant(){	
	carousel = $('#slider').data('jcarousel');
	carousel.next();
}


//API GOOGLE MAPS (implantations)

function initialize(numPlace) {

	Roissy = new google.maps.LatLng(48.98968,2.516084);
	VicSurAisne = new google.maps.LatLng(49.395879,3.102436);
	Charvieu = new google.maps.LatLng(45.734688,5.154798);
	
	var Places = [Roissy, VicSurAisne, Charvieu];
	
	
    var myOptions = {
      zoom: 13,
      center: Places[numPlace],
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    
    var map = new google.maps.Map(document.getElementById("mapcanvas"),
        myOptions);
    
    
    var marker_0 = new google.maps.Marker({
	      position: Places[0], 
	      map: map, 
	      title:"Site Commercial Roissy"
	});
    
    var marker_1 = new google.maps.Marker({
	      position: Places[1], 
	      map: map, 
	      title:"Vic sur Aisne"
	});
    
    var marker_2 = new google.maps.Marker({
	      position: Places[2], 
	      map: map, 
	      title:"Charvieu - Chavagnieu"
	});
    

	var infowindow = new google.maps.InfoWindow({
	    content: '<p>Le site commercial de Roissy <br/> Continal Square - Roissy Pole Bâtiment Neptune - BP 1172<br/> 95727 ROISSY CDG Cedex</p>'
	});
	

	google.maps.event.addListener(marker_0, 'click', function() {
	  infowindow.open(map,marker_0);
	});
	
	
	var infowindow2 = new google.maps.InfoWindow({
	    content: '<p>Le siège social et le site de Production<br/> situé à Vic sur Aisne<br/>BP1 - Montigny Lengrain<br/>02290 VIC SUR AISNE</p>'
	});
	

	google.maps.event.addListener(marker_1, 'click', function() {
	  infowindow2.open(map,marker_1);
	});
	
	var infowindow3 = new google.maps.InfoWindow({
	    content: 'Le site de production Benoit SNC<br/> situé à Charvieu Chavagnieux<br/>ZI de Montbertrand 15 rue du Claret<br/>38230 CHARVIEU - CHAVAGNEUX</p>'
	});
	

	google.maps.event.addListener(marker_2, 'click', function() {
	  infowindow3.open(map,marker_2);
	});
    
}


$(document).ready(function(){
	
	//Greyboxes links
	$('#greybox, .greybox').click(function(){
		lien=$(this).find('a').first();
		window.location = lien.attr("href");
	});
	
	
	//Print page function
	$('#print').click(function(){
		$("html").jqprint();
	});
	
	//Page contact popup
        $("#menu-item-38 a").click(function()
        {
            $.ajax({url:$(this).attr('href'), type:"GET", success:function(html){$.fancybox(html);}});
            return false;
        });
		
		$("#menu-item-36 a").fancybox({'autoDimensions'	: false,			'width'         		: 550,			'height'        		: 450,'centerOnScroll':true});
		$("#menu-item-35 a").fancybox({'autoDimensions'	: false,			'width'         		: 550,			'height'        		: 350,'centerOnScroll':true});
	//Font resizer

	// Increase Font Size - Max value : 20px;
	$("#tplus").click(function(){
	    var currentFontSizeGrip = $('.grip').css('font-size');
	    var currentFontSizeText = $('.text').css('font-size');
	    var currentLineHeightText = $('.text').css('line-height');
	    var currentFontSizeNumGrip = parseFloat(currentFontSizeGrip, 10);
	    var currentFontSizeNumText = parseFloat(currentFontSizeText, 10);
	    var currentLineHeightNumText = parseFloat(currentLineHeightText,10);
	    var newFontSizeGrip = currentFontSizeNumGrip+1.0;
	    var newFontSizeText = currentFontSizeNumText+1.0;
	    var newLineHeightText = currentLineHeightNumText+1.0;
	    if(newFontSizeText<=20){
		    $('.grip').css('font-size', newFontSizeGrip);
		    $('.text').css('font-size', newFontSizeText);
		    $('.text').css('line-height', newLineHeightText+'px');
	    }
	    return false;
	  });
	  // Decrease Font Size -  Min value : 10px;
	$("#tmoins").click(function(){
	  	var currentFontSizeGrip = $('.grip').css('font-size');
	    var currentFontSizeText = $('.text').css('font-size');
	    var currentLineHeightText = $('.text').css('line-height');
	    var currentFontSizeNumGrip = parseFloat(currentFontSizeGrip, 10);
	    var currentFontSizeNumText = parseFloat(currentFontSizeText, 10);
	    var currentLineHeightNumText = parseFloat(currentLineHeightText,10);
	    var newFontSizeGrip = currentFontSizeNumGrip-1.0;
	    var newFontSizeText = currentFontSizeNumText-1.0;
	    var newLineHeightText = currentLineHeightNumText-1.0;
	    if(newFontSizeText>10){
		    $('.grip').css('font-size', newFontSizeGrip);
		    $('.text').css('font-size', newFontSizeText);
		    $('.text').css('line-height', newLineHeightText+'px');
	    }
	    return false;
	});
	
	
	
	//Bloc Actualités
	
	//Toggle Effects 
	
	$('.toggler').click(function(){
		$('#contentbox .data_bloc div.content').toggle("speed");
	});

		//Filters
	$('ul#submenu li').click(function(){
		$('ul#submenu li').removeClass('active');
		$(this).addClass('active');
		//On se replace par défaut sur la 1ere page
		$('#news_page').attr("class","0");
		filter();
	});
	
	
	$('#prev').click(function(){
		curr_page=$('#news_page').attr("class");
		$('#news_page').attr("class",eval(curr_page)+1);
		filter();
		return false;
	});
	
	
	$('#next').click(function(){
		curr_page=$('#news_page').attr("class");
		$('#news_page').attr("class",eval(curr_page-1));
		filter();
		return false;
		
	});
	
	
	function filter(){
		cat_id =$('ul#submenu .active').first().attr('id');
		page=$('#news_page').attr("class");
		
		$.post(getBaseURL() + "wp-admin/admin-ajax.php",
			{action : 'filter_cat',	main_catid : cat_id , news_page : page},
			function(data){
				
				posts=eval(data);
				//Retirer les posts
				$('.news').remove();
				//Afficher les posts
				$(posts[0]).insertAfter('.data_news #submenu');
				//Cacher le lien "Plus r�cents" si page = 0 sinon l'afficher
				if(page==0){
					$('#next').hide();
				}else{
					$('#next').show();
				}
				//Cacher le lien Plus anciens si pas d'autres articles, sinon l'afficher
				if(posts[1]){
					$('#prev').show();
				}else{
					$('#prev').hide();
				}
				
			});
	}
	
	
	
	
	//Carousel - Home Page
	
	var timer;
	


	$('#slider').jcarousel({ buttonNextEvent : 'click', buttonPrevEvent : 'click' , wrap: 'both', visible : 7, auto : 1 , scroll : 1, initCallback : init_carousel});
	
	function init_carousel(carousel){

		$('#slider').click(function(){
			carousel.stopAuto();
		});
		
		$('.jcarousel-prev').click(function(){
			if($('.sliding').size()==0){
				timer = window.setInterval("hprecedent();",1000);
                                window.clearInterval(timer);
			}
		});
		
		
		$('.jcarousel-next').click(function(){
			if($('.sliding').size()==0){
				timer = window.setInterval("hsuivant();",1000);
                                window.clearInterval(timer);
			}
		});
		
	}
	
	
	//Patch JS IE6
	
	if($.browser.msie && ( $.browser.version=="6.0" || $.browser.version=="7.0")){ 
		
		//Pour afficher le carousel de la home
		if($('#slider').size()>0){
			carousel = $('#slider').data('jcarousel');
			carousel.next();
		}
		
		//Menu de la home page
		
		$('#menu-header li.menu-item').mouseenter(function(){
			Parent = $(this);
			
			if (Parent.attr('id')=='menu-item-170' || Parent.attr('id')=='menu-item-19' || Parent.attr('id')=='menu-item-18' || Parent.attr('id')=='menu-item-17'){
				$(document).find('.sub-menu').each(function(){
					if($(this).parent().attr('id')!=Parent.attr('id')){
						$(this).hide();
						
					}
				});
			}
			
			Parent.find('.sub-menu').first().show();

			
		});
		
		$('#menu-header li.menu-item .sub-menu').mouseleave(function(){
			$(this).hide();
		});
		
		
	}
	
	
	//Marques
	
	$('.marque_area').mouseover(function(){
		$(this).find('.description').show();
	});
	
	$('.marque_area').mouseleave(function(){
		$(this).find('.description').hide();
	});
	
	
	
	//Carousel histoire
	
	var nbreItem = $(".carousel-top-item").length;
    var pasTop = $(".carousel-top-item").width();
    var pasBot = $(".carousel-bot-item").width();
    var positionTop = 0;
    var positionBot = 0;
    var nbreItemVisibleBot = 4;
    var itemVisibleBot = 1;
    
    $(".carousel-top-content").width(nbreItem*pasTop);
    $(".carousel-bot-content").width(nbreItem*pasBot);
    $(".carousel-top-right").click(function(){
        suivant();
    });
    $(".carousel-top-left").click(function(){
        precedent();
    });
    $(".carousel-bot-right").click(function(){
        suivant();
    });
    $(".carousel-bot-left").click(function(){
        precedent();
    });
    
    $(".carousel-bot-item").click(function(){
        var itemClickPosition = $(this).prevUntil().length;
        var itemActivePosition = $(".active").prevUntil().length;
        
        if(itemClickPosition > itemActivePosition)
        {
            action = suivant;
        }
        else
        {
            action = precedent;
        }
        for(i = Math.abs(itemClickPosition - itemActivePosition); i > 0; i--)
        {
            action();
        }
        
        
   })
    
    suivant = function(){
        if( positionTop > (-(nbreItem-1) * pasTop) )
        {
            positionTop -= pasTop;
            $(".carousel-top-content").animate({left:'-='+pasTop+'px', y:0, queue:false}, 500, 'linear');
            $(".carousel-bot-item.active").removeClass("active").next().addClass('active');
            if(itemVisibleBot < (nbreItem - nbreItemVisibleBot))
            {
                $(".carousel-bot-content").animate({left:'-='+pasBot+'px', y:0, queue:false}, 500, 'linear');
                itemVisibleBot += 1;
            }
        }
    }
    precedent = function(){
        if( positionTop < 0 )
        {
            positionTop += pasTop;
            $(".carousel-top-content").animate({left:'+='+pasTop+'px', y:0, queue:false}, 500, 'linear');
            $(".carousel-bot-item.active").removeClass("active").prev().addClass('active');
            if(itemVisibleBot > 1)
            {
                $(".carousel-bot-content").animate({left:'+='+pasBot+'px', y:0, queue:false}, 500, 'linear');
                itemVisibleBot -= 1;
            }
        }        
    }
	
    //Communiqué de presse
    $("#select-annee").change(function(){
        $(this).parents("form").submit();
    });
    
    //Revue de presse
     $("#select-annee").change(function(){
        $(this).parents("form").submit();
    });
    $(".select-cat_slug").change(function(){
        $(this).parents("form").submit();
    });
    
    
    
    //Carousel Nouveautés
    
    $('#products_gallery').spacegallery({loadingClass: 'loading'});
    
    
    $('.move_carousel').click(function(){
    	$('#products_gallery a').click();
    });
    $('#products_gallery a').bind("click", function(){
        if($(".details.active").next().length > 0)
        {
            next = $(".details.active").next(); 
        }
        else
        {
            next = $(".details:first");
        }
        $(".details.active").addClass("hidden").removeClass("active");
        $(next).addClass("active").removeClass("hidden");
    });
    

    //Offres d'emploi
    
	$(".btn-details-offre").toggle(
			function(){$(this).parent().parent().next().show(); $(this).html('Replier cette offre'); $(this).parent().css('border', '1px solid #fff');},
			function(){$(this).parent().parent().next().hide(); $(this).html('Voir le détail');  $(this).parent().css('border', '1px solid #d8e2c9');}
	);
	$(".jobman-message").parents("tr").prev().find(".btn-details-offre").click();
	$(".jobman-message").parent().prev().remove();
	$(".jobman-message").parent().show();
	$(".details-offre a").click(function(){
	$(this).parent().find("div.form-hidden").show();
	    $(this).remove();
	    return false;
	});
    
	//Carousel Header Page
    
    /*$('#slideshow').jcarousel({
        auto: 4, 
        animation: 1000, 
        scroll:1,
        wrap: 'circular',
        easing: 'fade',
        itemFallbackDimension:896
    });*/
    	$('.slideshow-content').cycle({fx:'fade', timeout: 5000 });

	
    
    
    
    
	
});
