var debugMode= 0xff;

function log(txt) {
	var i = 0,z;
	if (!(debugMode&1))
		return;
	do {
		if (window.console && console.log && !console.emul){
			return console.log.apply(console, arguments);
		}else {
			var dbg = document.getElementById('debug');
			if (!dbg)
				return;
			var div = document.createElement('div');
			if (typeof txt != "string") {
				/*if (typeof txt == "object")
				txt = Object.keys(txt);
				else*/
				//txt = Object.inspect(txt);
			}
			div.appendChild(document.createTextNode(txt));
			z = String((this.___dbg_cnt++) & 1 << 2);
			div.style.backgroundColor = "#" + z + z + z;
			dbg.insertBefore(div, dbg.firstChild);
		}
		txt = arguments[++i];
	}while (txt!==undefined);
}

if(!window.console){
	if (!("console" in window) || !("firebug" in console)){
	var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
		"group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
	window.console = {emul:1};
	for (var i = 0; i < names.length; ++i)
		window.console[names[i]] = function() {};
	window.console.log = log;
	}
}

function load(page, param, target_id, fct) {
  if (window.all && !window.opera){
      var XhrObj = new ActiveXObject("Microsoft.XMLHTTP");
  }
  else{
      var XhrObj = new XMLHttpRequest();
  }
  var target = document.getElementById(target_id);
  if(target!=undefined){
  	target.innerHTML="<div id='waitDiv' style='position:absolute;padding:15px;text-align:center;'><img src='/graphics/loading.gif' /><br/>Veuillez patienter!</div>";
  }
  log("POST", page, true);
  XhrObj.open("POST", page, true);
  XhrObj.onreadystatechange = function() {
    if (XhrObj.readyState == 4 && XhrObj.status == 200){
        if(target!=undefined){
          target.innerHTML = XhrObj.responseText;
        }
        if(fct!=undefined){
          fct.call(XhrObj.responseText);
        }
    }
    if(target!=undefined){
        var scripts = target.getElementsByTagName("script")
        for (var i = 0; i < scripts.length; ++i) {
           var s = scripts[i];
           if (!s.text) {
               dump(s);
               dump("len =" + scripts.length + "   i=" + i);
           }
           if (s.text) {
               eval(s.text);
           }

        }
    }
  }
  XhrObj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  XhrObj.send("ajax=1&"+param);
  return false;
}

function trackClick(that){
	var x = that.id.split("-");

	if(x[1]>0)
		load("/track.php","object="+x[0]+"&position="+x[1]+"&id="+x[2],{});
	return true;
}

//SETTING UP OUR POPUP  
//0 means disabled; 1 means enabled;  
var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#popup-bg").css({
			"opacity": "0.7"
		});
		$("#popup-bg").fadeIn("slow");
		$("#popupform2").fadeIn("slow");
		popupStatus = 1;
	}
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupform2").height();
	var popupWidth = $("#popupform2").width();
	//centering
	$("#popupform2").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	$("#popup-bg").css({
		"height": windowHeight
	});
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#popup-bg").fadeOut("slow");
		$("#popupform2").fadeOut("slow");
		popupStatus = 0;
	}
}

function showPopUp(that){
	var x = that.id.split("-");
	console.log(that.id);
	console.log("pop");
	$("#thumbPopup").attr('src', dataUrl+'/data/thumbs/'+x[2]+'-217x342.jpg');
	centerPopup();
	loadPopup();

	if(x[1]>0)
		load("/track.php","object="+x[0]+"&position="+x[1]+"&id="+x[2],{});
	return true;
}

function verifVille(that){
	if(that.value.length==0){
		$('#erreur_ville').hide();
		$('#erreur_code').hide();
		$(that.id).removeClass="erreur";
	}
	else{
		if(isNaN(that.value)){
			$('#erreur_code').hide();
			$(that.id).removeClass="erreur";
			if(that.value.length<3){
    			$('#erreur_ville').show();
				$(that.id).addClass="erreur";
			}
			else{
    			$('#erreur_ville').hide();
				$(that.id).removeClass="erreur";
			}
	    }
    	else{
    		$('erreur_ville').hide();
			if(that.value.length<5){
				$('#erreur_code').show();
				$(that.id).addClass="erreur";
			}
			else{
				$('#erreur_code').hide();
				$(that.id).removeClass="erreur";
			}
		}
	}
}

function verifMail(that){
	if(validMail(that.value)){
		$(that.id).removeClass="erreur";
		$('#erreur_mail').hide();
	}
	else{
		$(that.id).addClass="erreur";
		$('#erreur_mail').show();
	}
}

function validMail(mail){
  	var place = mail.indexOf("@",1);
  	var point = mail.indexOf(".",place+1);
  	if ((place > -1)&&(mail.length >2)&&(point > 1)){
  		return true;
  	}
  	else{
  		return false;
  	}
}

$(document).ready(function(){
	//Click the x event!
	$("#popupContactClose").click(function(){
		disablePopup();
	});
	//Click out event!
	$("#popup-bg").click(function(){
		disablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});
	$(".cadeau-bouton").click(function() {
		$("#message").fadeOut("fast");	
		$("#message").fadeIn("fast");	 
		$("#message").html("Cadeau envoy&eacute; !");		
		return false;	
	});
});
