$(function(){

	var url_site = 'http://www.rezoactif.com';
	
	$('body').addClass('js');

	// Slideshow de la page d'accueil
	$('#slideshow').loopedSlider({
		autoStart: 4000,
		restart: 4000,
		slidespeed: 500,
		fadespeed: 500
	});
	
	// Slideshow de la partie Expertises
	$('.slideshow, #slideshow_app, #slideshow_reference').loopedSlider({
		slidespeed: 500,
		fadespeed: 500
	});
	
	// Slideshow de la partie Application
	$('').loopedSlider({
		slidespeed: 500,
		fadespeed: 500
	});
	
	$('#conteneur_tweet, #liste_clients').cycle({
		fx:      'fade',
		speed:    800,
		timeout:  6000
	});
	
	
	// Validation du formulaire avant l'envoi
	$('#formulaire_contact form').validationEngine();
	$('#formulaire_contact_partenariat form').validationEngine();
	
	// Suppression du texte présent dans le bouton OK du footer
	$('#footer form .submit').val('');
	
	// Activation de l'effet lightbox sur les images de la page technologie
	$('.activranking a').colorbox({height: "95%", opacity: 0.9});
	
	
	// ---------------------------------------------------------
	// Gestion du slider de la page d'accueil
	// ---------------------------------------------------------

	var nombre_projet = $('#slider_realisations li').length;
	var largeur_projet = 203;
	
	var largeur_conteneur_projet = (largeur_projet * nombre_projet) - 27;
	
	var barre_defilement = 582;
	var coef = (largeur_conteneur_projet - barre_defilement) / 400;
	
	var position_slider = $('#page').findPos();
		
	$("#slide").draggable({ 
		axis: 'x',
		containment: '#slider',
		drag: function(event, ui) {
			
			var offset = ui.offset.left - position_slider.x;

			offset = coef * offset;
			
			$('ul#slider_realisations').animate({left:	- offset+'px'}, { duration: 1000, queue: false });
		}
	});
	
	var slider_max = $('#slider').width() - $('#slide').width();
    $('#slider').click(function(ev){
        var x = ev.pageX - $(this).offset().left - $("#slide").width() / 2;
        if ( x < 0 ){ x = 0 }
        if ( x > slider_max ){ x = slider_max }

        $("#slide").stop().animate({left: x + 'px'}, {duration: 50, queue: false});
        $('ul#slider_realisations').animate({left: -(x * coef)+'px'}, { duration: 1000, queue: false });
        
    });
	
	
	// ---------------------------------------------------------
	// Gestion de la partie CMS
	// ---------------------------------------------------------
	
	$('.wordpress div, .prestashop div').hide();
	$('#solutions_cms h3 a').click(function() {
		var cms = $(this).parent().parent().attr('class');
		
		$('#solutions_cms ul div:visible').hide();
		
		clean_images();
		
		$('#solutions_cms ul li.'+ cms +' div').show().parent().find('img').attr('src', url_site+'/public/images/logo_'+ cms +'_active.png');

		return false;
	});
	
	function clean_images() {
		$('#solutions_cms .rezoactif_cms img').attr('src', url_site+'/public/images/logo_rezoactif_cms.png');
		$('#solutions_cms .wordpress img').attr('src', url_site+'/public/images/logo_wordpress.png');
		$('#solutions_cms .prestashop img').attr('src', url_site+'/public/images/logo_prestashop.png');
	}

	
	// ---------------------------------------------------------
	// Gestion de la partie expertise
	// ---------------------------------------------------------
	
	$('#selection_rubrique a').click(function() {
		$.get( $(this).attr('href'), function(data) {
			$('#page').find('#selection_rubrique').next('div').remove();
			
			var content = $(data).find('#selection_rubrique').next('div');
			$('#page').find('#selection_rubrique').after(content);
			
			// Slideshow de la partie Expertises
			$('.slideshow').loopedSlider({
				slidespeed: 500,
				fadespeed: 500
			});
		});
		
		$('#selection_rubrique a.active').removeClass('active');
		$(this).addClass('active');
		
		return false;
	});
	
	
	// ---------------------------------------------------------
	// Gestion de la partie clients
	// ---------------------------------------------------------
	
	$('.liste_clients div').hide();
	$('.liste_clients li > a').live('click', function() {
		$('.liste_clients div:visible').toggle(300);
		
		// Pour que IE6 place le cadre au dessus des autres (mauvaise gestion des z-index)
		var ul_parent = $(this).parent().parent();
		$(this).parent().clone().appendTo(ul_parent).find('div').toggle(300);

		$(this).parent().remove();
		
		return false;
	});
	
	$('.liste_clients li div').live('click', function() {
		$('.liste_clients > li').css('visibility', 'visible');
		$(this).toggle(300);
	});

	if((window.location.hash) && ($(".liste_clients").children(window.location.hash).length)) 
    { 
        $(window.location.hash).show();
		$('.liste_clients li#' + window.location.hash + ' > div').show().css('z-index', '10');
    }

	$('#classement_clients a:first').addClass('active');
	$('#classement_clients a').click(function(e) {
		afficher_clients($(this).attr('href'));
		$('#classement_clients a').removeClass('active');
		$(this).addClass('active');
		
		e.preventDefault();
	});
	
	$('.clients .pagination a').live('click', function(e) {
		afficher_clients($(this).attr('href'));
		
		$('.pagination li').removeAttr('disabled').removeClass('active');
		$(this).parent().addClass('active').attr('disabled','disabled');
		
		e.preventDefault();
	});
	
	$('.clients .pagination li:first').addClass('active').attr('disabled','disabled');
	// Positionnement de la pagination au centre
	var largeur_pagination = $('.clients .pagination').width();
	$('.clients .pagination').css({'position' : 'relative', 'left' : (957 / 2) - (largeur_pagination / 2)});
	
	
	function afficher_clients(href) {
		// Attribution de la classe active à l'élément cliqué
	
		var blocClients = $(".liste_clients")[0];
		
		$.get(href, function(clients) {

			// Update pager
			var contenu_pagination = $(clients).find('.pagination');
			$('.pagination').remove();
			$('.liste_clients').after(contenu_pagination);
			
			var largeur_pagination = $('.clients .pagination').width();
			$('.clients .pagination').css({'position' : 'relative', 'left' : (957 / 2) - (largeur_pagination / 2)});
			
			$(blocClients).children().each(function() {
				var produitTrouve;

				if ($("[id="+this.id+"]", clients).length == 0) {
					// Le produit affiché n'est pas dans les résultats de la requête, il faut le supprimer.

					$(this).fadeOut(750, function() {
						$(this).remove();
					});
				}
			});


			$(clients).find('.liste_clients > li').each(function() {

				var id = $(this).attr("id");
			  
				var rang = $(this).children('a').attr('class');
			  
				var top = (Math.floor(rang / 4) * 168);
				var left = ((rang % 4) * 242);
				
				var sens_panneau = $(this).children('div').attr('class');
				
				//regex = new RegExp($(this).attr('id'),"gi");
				var clientDejaAffiche ='';
			  
				if ((clientDejaAffiche = $("#"+id, blocClients)).length == 0) {
					// Le produit n'est pas déjà affiché, il faut l'ajouter.
				
					$(blocClients).append($(this));
					setTimeout('$("#'+id+'").fadeIn(375);', 375);
					
					$('.liste_clients div').hide();
				} else {
					// Le produit est déjà affiché, il faut le déplacer.
					$(clientDejaAffiche).animate({ top: top+"px", left: left+"px"}, 750);
					$(clientDejaAffiche).children('div').removeClass().addClass(sens_panneau);
					$('.liste_clients div').hide();
				}

			});

		});
		
		$('.liste_clients div').hide();
	}
	
	function intval(mixed_var, base) {
		var type = typeof( mixed_var );
	 
		if (type === 'boolean') {
			return (mixed_var) ? 1 : 0;
		} else if (type === 'string') {
			tmp = parseFloat(mixed_var, base || 10);
			return (isNaN(tmp) || !isFinite(tmp)) ? 0 : tmp;
		} else if (type === 'number' && isFinite(mixed_var) ) {
			return Math.floor(mixed_var);
		} else {
			return 0;
		}
	}
	
});

// Google Maps
function initialize() {
	// var latlng = new google.maps.LatLng(48.871077, 2.346812);
	var latlng = new google.maps.LatLng(48.908074, 2.29213);    
	var myOptions = {
		zoom: 14,
		center: latlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	
	// var myLatlng = new google.maps.LatLng(48.871077, 2.346812);
	var myLatlng = new google.maps.LatLng(48.908074, 2.29213);
	var myOptions = {
	  zoom: 14,
	  center: myLatlng,
	  mapTypeId: google.maps.MapTypeId.ROADMAP
	}

	var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);


	var marker = new google.maps.Marker({
		position: myLatlng,
		map: map,
		title:"Rezoactif"
	});
}

jQuery.fn.extend({
   findPos : function() {
       obj = jQuery(this).get(0);
       var curleft = obj.offsetLeft || 0;
       var curtop = obj.offsetTop || 0;
       while (obj = obj.offsetParent) {
                curleft += obj.offsetLeft
                curtop += obj.offsetTop
       }
       return {x:curleft,y:curtop};
   }
});
