/*var _gaq=_gaq||[];_gaq.push(['_setAccount','UA-16858835-7']);_gaq.push(['_trackPageview']);(function(){var ga=document.createElement('script');ga.type='text/javascript';ga.async=true;ga.src=('https:'==document.location.protocol?'https://ssl':'http://www')+'.google-analytics.com/ga.js';var s=document.getElementsByTagName('script')[0];s.parentNode.insertBefore(ga,s);})();*/

$(document).ready(function(){
	$('#diaporama-haut').each(function(){$(this).diaporing({speed:5000,fadespeed:2000,width:948,height:140})});


	$('a.album').fancybox({
		'zoomSpeedIn': 300, 
		'zoomSpeedOut':	300, 
		'overlayShow': true,
		'overlayColor': '#000',
		'overlayOpacity': 0.8,
		'titleShow': true,
		'titlePosition':'inside'
	});

	 $('#form-email').defaultvalue($('#form-email').val());
	 //$('#form-password').defaultvalue($('#form-password').val());
	 
    $('.supprimer').click(function(){
        var lien = $(this).attr("confirm");
        var active = $(this).attr("active");        
        if (active == 1) {
            location.href = lien;
        }else{
            var id = $(this).attr("id");
            var texte = $(this).html();
            $(this).fadeTo(1000,0);
            setTimeout("$('#"+id+"').attr('active','1').attr('html','"+texte+"').html('CONFIRMER').fadeTo(0,1).fadeTo(4000,0)",1000);
            setTimeout("_remove('"+id+"')",5000);
        }
    });
	 
	$('#eventFr').click(function(){var src = $(this).attr('langsrc');Ajax.cl(1,src)});
	$('#eventEn').click(function(){var src = $(this).attr('langsrc');Ajax.cl(2,src)});
	$('#eventDe').click(function(){var src = $(this).attr('langsrc');Ajax.cl(3,src)});
	
	// DEPOT (non ajouté)
    $('.wip').fadeTo(0,0.25).mouseover(function(){$(this).fadeTo(0,1)}).mouseout(function(){$(this).fadeTo(0,.25)});
	$('body').bind('keydown',function(e){if(e.which==87) $('.wip').toggle();});
	// DEPOT
	
	//Debug.Init();
	$('#produit-image').mouseout(function(e){
		var zoomId = '#produit-image-zoom'; 
		$(zoomId).hide();
	});
	$('#produit-image').mousemove(function(e){
		var offset = $(this).offset();
		var x = e.pageX - offset.left;
		var y = e.pageY - offset.top;
		var zoomId = '#produit-image-zoom'; 
		//Debug.Write('this.offsetLeft['+offset.left+ '] this.offsetTop[' + offset.top + ']e.pageX['+e.pageX + '] e.pageY[' + e.pageY + '] / mousemove x['+x+'] y['+y+']');
		
		var swidth = $(this).width();
		var sheight = $(this).height();
		
		var width = $(zoomId + ' img').width();
		var height = $(zoomId + ' img').height();
		var cwidth = $(zoomId).width();
		var cheight = $(zoomId).height();
		
		var perx = (100 / swidth * x)/100;
		var pery = (100 / sheight * y)/100;
		//Debug.Write('width['+width+']');
		$(zoomId).show();
		//$(zoomId + ' img').css({'position':'absolute','top':py,'left':px});
		
		/*
		px% = .75
		600px
		400 = 600 - (400/2)
		*/
		
		
		var px = (perx * width) - (cwidth/2);
		var py = (pery * height) - (cheight/2);
		
		$(zoomId + ' img').css({'position':'absolute','top':-py,'left':-px});
		//$(zoomId).css({'position':'absolute','top':y,'left':x});
		
		Debug.Write(x+'/'+y+' - '+px+'/'+py);
	});

	$('.wip').hide();	
	
	// Change le fond, si nécessaire
	//jQuery.fn.exists = function(){return this.length>0;}
	var full = $('.full').length;

	if(full==1){
		//$('.content').css('background','url(/data/images/fond-site.jpg)');
		//On ne fait rien, le fond est déjà 'fond-site.jpg'
	}else{
		$('.content').css('background','url(/data/images/fond-site2.jpg)');
	}
	
	// Affiche le site
	$('html').fadeTo(1000,1);
	$('.event-video').flowplayer("/data/flash/flowplayer-3.2.7.swf",{clip:{onStart:function(clip){var wrap = jQuery(this.getParent());wrap.css({width:clip.width,height:clip.height});},scaling:'fit',autoplay:true},plugins:{controls:{tooltips:{buttons:true,play:'jouer'},time:true}}});
});

function _remove(id) {
        var html = $('#'+id+'').attr('html');
        $('#'+id+'').removeAttr('active');
        $('#'+id+'').html(html).fadeTo(0,1);
}

var Perte = {
	mdp : function(){
		// On change l'action:
		//$('#form-action').val('chmdp');
		//<input type="hidden" name="action" value="chmdp" />
		var email = $('#form-email').val();
		if (!email) {alert('Merci de préciser une adresse mail');return false}
		$.post(
			'/ajax.php',
			{
				'ajax_action':'0RjaGGITwSCJPRKoMSwNkdeafcdrPV8V',
				'email':email
			}, function(data){
				if(data.code==1){alert(var_sendpass)}
				if(data.code==0){alert('ko')}
			},'json');
	}
}

var Ajax = {
	cl : function(l,r){$.post('/ajax.php',{'ajax_action':'Y5WBmVQcokhVnX1fe3M1cPKcqtTwTq84','lang' : l},function(data){
		if(data.code==1){location.href=r;}
		},'json');},
	pmdp : function(){
		// On change l'action:
		//$('#form-action').val('chmdp');
		//<input type="hidden" name="action" value="chmdp" />
		var email = $('#form-email').val();
		if (!email) {alert('Merci de préciser une adresse mail');return false}
		$.post(
			'/ajax.php',
			{
				'ajax_action':'0RjaGGITwSCJPRKoMSwNkdeafcdrPV8V',
				'email':email
			}, function(data){
				if(data.code==1){alert(var_sendpass)}
				if(data.code==0){alert('ko')}
			},'json');
	},	
	mmdp : function(){
		//var email = $('#form-email').val();
		//if (!email) {alert('Merci de préciser une adresse mail');return false}
		var mdp1 = $('#motdepasse1').val();
		var mdp2 = $('#motdepasse2').val();
		if (mdp1!=mdp2 || (mdp1=='' || mdp2=='')) {
			alert('Les mots de passe ne correspondent pas !');
			return false;
		}
		$.post(
			'/ajax.php',
			{
				'ajax_action':'tC6TXyGj05WS7sapNUzQVEh4buq6kPlx',
				'mdp1':mdp1,
				'mdp2':mdp2
			}, function(data){
				if(data.code==1){alert('1:'+data.message)}
				if(data.code==0){alert('0:'+data.message)}
			},'json');
		return false;
	},
	connexion : function(){
		var email = $('#form-email').val();
		var mdp = $('#form-password').val();
		var url = $('#on-connexion').val();
		//alert(url);
		$.post(
			'/ajax.php',
			{
				'ajax_action':'KTtpJaaQhb4KJtjvvpXcqC27cEKsRSvo',
				'email':email,
				'mdp':mdp				
			}, function(data){
				if(data.code==1){location.href = url}
				if(data.code==0){alert(ErreurConnexion)}
			},'json');
		return false;
	},
	comptemodifier : function(){
		$.post(
			'/ajax.php',
			{
				'ajax_action':'zjFKQUtuYIhNTAg82b4eHcTJlAVfsUew',
				'raison':$('#form-raison').val(),
				'nom':$('#form-nom').val(),
				'prenom':$('#form-prenom').val(),
				'adresse1':$('#form-adresse1').val(),
				'adresse2':$('#form-adresse2').val(),
				'cpostal':$('#form-cpostal').val(),
				'ville':$('#form-ville').val(),
				'pays':$('#form-pays').val(),
				'telfixe':$('#form-telfixe').val(),
				'telport':$('#form-telport').val(),
				'email1':$('#form-email1').val(),
				'email2':$('#form-email2').val()
			}, function(data){
				//alert(data.code+':'+data.message);
				//Debug.Write(data.debug);
				var url = $('#action-modifiercompte').attr('action');
				location.href = url;
			}, 'json');
		return false;
	},
	creerlivraison : function(){
		$.post(
			'/ajax.php',
			{
				'ajax_action':'3YR6c6WIaQBxLtbSTERn9H2UpToQj5p6',
				'post':$('#form-creerlivraison').serialize()
			}, function(data) {
				alert(data.code + ':' + data.message);
			}, 'json');
		return false;
	},
	modifierlivraison : function() {
		$.post(
			'/ajax.php',
			{
				'ajax_action':'QautctGOhTRLBqsbRfVz7eeFDnft0Phk',
				'post':$('#form-modifierlivraison').serialize()
			}, function(data) {
				//alert(data.code + ':' + data.message);
				var url = $('#form-modifierlivraison').attr('action');
				location.href = url;
			}, 'json');
		return false;
	},
	supprimerlivraison : function(value) {
		$.post(
			'/ajax.php',
			{
				'ajax_action':'FUExwgrwM4CntX6lEVxO6ckGjHdnBOA7',
				'id':value
			}, function(data) {
				if(data.code==1){$('#dom-'+value+'').remove()}
			}, 'json');
	}
}

// depot
var overlay = {
	Init : function() {
		$('body').prepend('<div id="overlay-background"></div><div id="overlay-foreground"></div>');
		$('#overlay-background').css({
			'position':'fixed',
			'width':'100%',
			'height':'100%'
		});
	}
}

// depot
var Debug = {
    build : 3,
    id : '#debug',
    timestamp : true,
    ts : new Date(),
    increment : 0,
    active : true,
    Hide : function() {
        $(Debug.id).hide();
    },
	Console : {
		id : '#debugconsole',
		Init : function() {
			$('body').prepend('<div id="' + Debug.Console.id.substr(1) + '">command <input type="text" id="consolecommand" /></div>');
			$(Debug.Console.id).css({
	            'position':'fixed',
	            'width':'600px',
	            'height':'30px',
	            'background':'#000',
	            'color':'#0F0',
	            'font-family':'"Courier New", Courier, monospace',
	            'font-size':'8pt',
	            'border':'#F00 dashed 1px',
	            'margin':'10px',
	            'bottom':'0px',
	            'left':'0px',
	            'z-index':'9999',
	            'display':'none',
	            'line-height':'10px',
	            'overflow':'auto'});
			$(Debug.Console.id).show().fadeTo(0,0.8).draggable();
			$('#consolecommand').focus().val('');
			$('#consolecommand').bind('keydown',function(e){if(e.which==13){
				var command = $('#consolecommand').val();
				switch(command){
					case 'debug':
						Debug.Init();
						break;
					case 'position':
						var position = map.getCenter();
						var lat = position.lat();
						var lng = position.lng();
						var zoom = map.getZoom();
						var type = map.getMapTypeId();
						var func = '[[!map?&lat=`'+lat+'`&lng=`'+lng+'`&zoom=`'+zoom+'`&type=`'+type+'`]]';
						prompt('code de la fonction', func);
						break;
					case 'map':
						initialize();
						$('#maps').resizable({
							ghost:true,
							handles:'se',
							grid: [20,20],
							stop: function() {
								Debug.Write('width:' + $(this).width());
								Debug.Write('height:' + $(this).height());
								}
						});
						break;
					default:
						eval (command);
						break;
				}
				$('#consolecommand').val('');
			}});
		}
	},
    Init : function() {
        $('body').prepend('<div id="' + Debug.id.substr(1) + '"></div>');
        $(Debug.id).css({
            'position':'fixed',
            'width':'600px',
            'height':'200px',
            'background':'#000',
            'color':'#0F0',
            'font-family':'"Courier New", Courier, monospace',
            'font-size':'8pt',
            'border':'#F00 dashed 1px',
            'margin':'10px',
            'bottom':'0px',
            'right':'0px',
            'z-index':'9999',
            'display':'none',
            'line-height':'10px',
            'overflow':'auto'});
        if (Debug.active) $(Debug.id).show().fadeTo(0,0.5).dblclick(function() {Debug.Clear();}).click(function() { /**/ });
        Debug.Write('debug mode (build ' + Debug.build + ') init, ok.');
    },
    Write : function(value) {
        if (!Debug.active) return false;
        Debug.increment++;
        if (Debug.timestamp) ts = Debug.ts.toGMTString() + ': ';
        $(Debug.id).prepend('<p id="value' + Debug.increment + '">[' + Debug.increment + ']' + ts + value + '</p>');
        $('#value' + Debug.increment + '').css({color:'#0F0'});
        $('#value' + (Debug.increment-1) + '').css({color:'#090'});
    },
    Clear : function() {
        $(Debug.id).html('');
    }
};

var Panier = {
	Ajout : function(id, ref, lang) {
		quantite = $('#produit_stock_' + id).val();
		location.href = '/index.php?fond=panier&action=ajouter&ref=' + ref + '&quantite=' + quantite + '&lang=' + lang;
	}
}
