jQuery.fn.onImagesLoaded =function(_cb) { 
	return this.each(function() {
		var $imgs = (this.tagName.toLowerCase()==='img')?$(this):$('img',this),
		_cont = this,
		i = 0,
		_done=function() {
			if( typeof _cb === 'function' ) _cb(_cont);
		};
		if( $imgs.length ) {
			$imgs.each(function() {
				var _img = this,
				_checki=function(e) {
					if((_img.complete) || (_img.readyState=='complete'&&e.type=='readystatechange') ){
						if( ++i===$imgs.length ) _done();
					}
					else if( _img.readyState === undefined ) // dont for IE
					{
						$(_img).attr('src',$(_img).attr('src')); // re-fire load event
					}
				}; // _checki \\
				$(_img).bind('load readystatechange', function(e){_checki(e);});
				_checki({type:'readystatechange'}); // bind to 'load' event...
			});
		} else _done();
	});
};
function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}
var http = createRequestObject();
var adress;
function sndReq(page) {
    http.open('get', 'rpcajax.php?page='+page);
    http.onreadystatechange = handleResponse;
    http.send(null);
}
function handleResponse() {
	if(http.readyState == 4){
		var response = http.responseText;
		if (response!='404'){
			var update = new Array();
			if(response.indexOf(',next!,' != -1)) {
				update = response.split(',next!,');
				$("#preload").html(update[1]);
				$("#preload").onImagesLoaded(function(_this){
					$('#loading').animate({ opacity:0 }, 'slow', function(){$('#loading').remove();});
					$.address.title($.address.title().split(' | ')[0] + ' | ' + update[0]);
					$("#preload").remove();
					$('#mainContent').html(update[1]);
					$("#taille").animate({ height: document.getElementById('mainContent').offsetHeight + document.getElementById('social').height, opacity:1 }, "slow");
					piwikTracker.setDocumentTitle($.address.title());
					piwikTracker.setCustomUrl( 'http://'+window.location.host+adress );
					piwikTracker.trackPageView();
				});
			}
		}else{
			window.location = "/#";
		}
	}
}
var idact = new String;
$(document).ready(function(){
	$("#menu a")
	.click(function(){
		return false;
	})
})
$.address
.init(function(event) {
})
.change(function(event) {
	adress = event.value;
	if (adress!='/'){
		var teste = adress.replace(RegExp('/', 'g'),'');
		teste = teste.replace(RegExp('.html', 'g'),'');
		idact= teste;
		$('#menu #tablemenu td').each(function(){
			if ($(this).hasClass('open')){
				$(this).removeClass();
			}
		});
		$('#'+idact).addClass('open');
		if($('body').children().is('#loading')==false){
			$("<div id='loading'></div>").prependTo('body');
			$("<div id='preload'></div>").appendTo("body").hide();
		} else {
			$("#loading").stop();
			$("#taille").stop();
			http.abort();
		}
		$("#loading").animate({ opacity:0 }, 0, "linear", function(){$('#loading').animate({ opacity:0.7 }, 1000, "linear", function(){ sndReq(teste)})});
		$("#taille").animate({ opacity:0 },"fast", "linear", function(){$('#mainContent').empty()});
	}else{
		adress = window.location.pathname;
		if (adress=='/'){
			adress='/accueil.html';
		}
       	window.location = "/#"+ adress;
	}
});
function parseInfo(data){
}
$(document).everyTime(1000*60*15, function() {
	var ok = "ok";
	$.getJSON("../session_must_go_on.php", {"ok" : ok}, parseInfo); 
});

