// JavaScript Document
$(document).ready(function() {
	initialiserliens();
	initialiserform();	
})
	


function initialiserliens(){
$("a").not("a.music").not("a.externe").not("a.galerie").not('a.lightbox').not('a.cke_off').click(function() {
			var urlcomplete=this.href;
			var urlAenlever="http://www.jeremyhaessig.com/";
			var hash=urlcomplete.substring(urlAenlever.length,urlcomplete.length);
			if (hash==""){unFocus.History.addHistory("accueil");
			}else{
			unFocus.History.addHistory(hash);
			}
			return false;
			})
$("a.lightbox").click(function(){return false;})
$("a.lightbox").lightBox();
$("a.menuchoix").click(function() {
			$("a.menuchoix").parent().removeClass("current");
			$("a.menuchoix").css('background-image', 'url(images/puce.png)');
			$(this).css('background-image', 'url(images/loader.gif)');
			$(this).parent().addClass("current");
			})

}
function clearInfo(){
	$("#oks").html("");
	$("#errors").html("");
	}

function appearInfo(time){
	if (time==undefined){time=2000;}
     $("#oks").slideDown(200);
	 $("#errors").slideDown(200);
	 setTimeout("disappearInfo()", time);
	}
	
function disappearInfo(){
     $("#oks").slideUp(200);
	 $("#errors").slideUp(200);
	}
function scrollToPos(id){
	$('html,body').animate({
scrollTop: $(id).offset().top
}, 500);}

function initialiserform(){
	
  
	$("form").not("#connect_form").not("#contactformgg").submit(function(){
												   var formObject=$(this);
				var datastring="";	
				

$('input,select', this).each(function() {
	datastring+=$(this).attr("id")+"="+$(this).attr("value")+"&";
  });

$('textarea', this).each(function() {
 var instance = CKEDITOR.instances[$(this).attr("id")];
    if(instance)
    {	
		datastring+=$(this).attr("id")+"="+escape(instance.getData())+"&";
        
    }else{
	datastring+=$(this).attr("id")+"="+$(this).attr("value")+"&";
		}
});

//alert(datastring);
	$.ajax({
   type: "POST",
   url: "http://www.jeremyhaessig.com/espacefiles/edit_validation.php",
   data: datastring,
   dataType: "json",
   success: function(msg){
	   clearInfo();
	   //alert(msg);
	   var i=0;
	   //erreurs
	   var errors="<ul>";
	$.each(msg.errors,function(){
		errors+="<li>"+msg.errors[i]+"</li>";
		i++;
		});
	errors+="</ul>";
	$("#errors").html(errors);
	  //oks
	   var oks="<ul>";
	$.each(msg.oks,function(){
		oks+="<li>"+msg.oks[i]+"</li>";
		i++;
		});
	oks+="</ul>";
	$("#oks").html(oks);
	 appearInfo();
	$(this).css("display","none");
	scrollToPos("#header");
	if (formObject.hasClass("oneStepBack")==true){
		history.back();
		}
   }
 });
	
	return false;
});

	}
	
//loader page
function loadPage(urlString,dataString,conteneur){
	if (conteneur ==null){conteneur=$("#contenuJS");
	
	}
	
	$.ajax({
   type: "POST",
   url: urlString,
   data: "goJava=1&thisurl="+urlString,
   dataType: "html",
   success: function(msg){
	   $("#menu2").css("display","none");
	$("li.current a").css('background-image', 'url(images/puce.png)');
	 conteneur.html(msg);
	 if (conteneur.attr("id")=="contenuJS"){
	initialiserliens();
initialiserform();

	 }

   }
 });
	}
	
	
//history

if(window.location.hash==""){
	var urlencours=window.location;
	var urlAenlever="http://www.jeremyhaessig.com/";
	if(window.location=="http://www.jeremyhaessig.com/" || window.location=="http://www.jeremyhaessig.com"){
			window.location="http://www.jeremyhaessig.com/#accueil";}

else{
			var urlin=""+window.location+"";
			var hashaaa=urlin.substring(urlAenlever.length,urlin.length);
			window.location=urlAenlever+"#"+hashaaa;
			}
}

function PromptMe() {
	var stateVar = "nothin'", displayDiv = document.getElementById("HistoryState");
	
	this.historyListener = function(historyHash) {
		stateVar = historyHash;
		loadPage("http://www.jeremyhaessig.com/"+historyHash);
	};
	unFocus.History.addEventListener('historyChange', this.historyListener);
	this.historyListener(unFocus.History.getCurrent());
};
var demoApp;


//setCookies
function SetCookie (name, value) {
	var argv=SetCookie.arguments;
	var argc=SetCookie.arguments.length;
	var expires=(argc > 2) ? argv[2] : null;
	var path=(argc > 3) ? argv[3] : null;
	var domain=(argc > 4) ? argv[4] : null;
	var secure=(argc > 5) ? argv[5] : false;
	document.cookie=name+"="+escape(value)+
		((expires==null) ? "" : ("; expires="+expires.toGMTString()))+
		((path==null) ? "" : ("; path="+path))+
		((domain==null) ? "" : ("; domain="+domain))+
		((secure==true) ? "; secure" : "");
}

function chargerGalerie(galerie){
	$("#slideraccueil_as3").externalInterface({method:'chargerGalerie', args:galerie,error:function(error){ alert('there was an error: ' + error); }});
	return false;
	}
	
function loadEditor(id)
{
	
	var instance = CKEDITOR.instances[id];
    if(instance)
    {
        CKEDITOR.remove(instance);
    }
	
	 CKEDITOR.replace(id,{
        toolbar : [
    [ 'Source', '-', 'Bold', 'Italic','-','TextColor','-','NumberedList','BulletedList','-','JustifyLeft','JustifyCenter','-','Link','Unlink','-','Image','Table','SpecialChar' ]
],
		language : 'fr',
        uiColor : '#eee',
		skin : 'kama',
		width:'67%'
		//contentsCss:'http://www.jeremyhaessig.com/css/style.css'
    });
}

   
	CKEDITOR.on( 'instanceReady', function( e ){
		$("a.cke_off").click = function(){
		return false;
		} 
		
		} )



