var base = '';
var module = '';
var hasNext = true;
var hasPrec = true;
$(function(){
	
	base = $('#base').val();
	module = $('#module').val();
	$('#logo').bind('click', function(){
		window.location.replace('index.php?module=accueil');
	});
	showActualitiesAjax(0, 'next');
	
	$('#btn_search_google').bind('click', function(){
		var text = $('input:text[name=text_search]').val();
		window.location.replace('index.php?module=rechercher&context='+text);
	});
	$('input:text[name=text_search]').bind('keypress', function(event){
		if (event.keyCode == "13") { 
			var text = $(this).val();
			window.location.replace('index.php?module=rechercher&context='+text);
		} 
	});
	$('#menu_quick_right_favorite').click(function(event){
			event.preventDefault(); // prevent the anchor tag from sending the user off to the link
		    var url = 'http://www.axiomeassocies.fr/';
		    var title = 'Axiome Associés';
		       
	        if (window.sidebar) { // Mozilla Firefox Bookmark
	        	window.sidebar.addPanel(title, url, '');
	        } else if( window.external ) { // IE Favorite
	        	window.external.AddFavorite( url, title);
	        } else if(window.opera) { // Opera 7+
	        	return false; // do nothing - the rel='sidebar' should do the trick
	        } else { // for Safari, Konq etc - browsers who do not support bookmarking scripts (that i could find anyway)
	        	alert('Malheusement, votre navigateur ne supporte pas cette action, veuillez ajouter manuellement ce favori.');
	        }
			return false;
			
	});	
    $(".communication_carousel").jCarouselLite({
        btnPrev: ".carousel-prev",
        btnNext: ".carousel-next",
        auto: 1500,
        speed: 2000,
        visible: 1,
        vertical: true
    });
	$('#communication li a').flyout({
			outEase:'easeOutQuad',
			inEase:'easeInBack',
			loader:'loader2',
			widthMargin:300,
			heightMargin:300
	});
    $("#btn_prev_actuality").bind('click',function(){
    	if(hasPrec){
        	var offset = $('input[name=offset_actuality]').val();
        	showActualitiesAjax(offset,'prec');    		
    	}
    });
    $("#btn_next_actuality").bind('click',function(){
    	if(hasNext){
	    	var offset = $('input[name=offset_actuality]').val();
	    	showActualitiesAjax(offset,'next');    
    	}
    });
	$('#content_central').css('width','590px');
	

});
function showActualitiesAjax(offset, sens){
	$.ajax({
		type: 'POST',
		url: base+'modify/show.php',
		data: ({'offset': offset, 'object': 'actuality', 'sens': sens}),
		dataType: 'xml',
		success: showActualities,
		error: function(){alert('Erreur serveur');}
	});
}
function getUrlActuality(id, date, title){
	var html = ""+
		"			<a class='row_actuality' id='actuality_"+id+"' href=\"/actualite/"+date+"/"+title+"\">" +
		"				<span style='font-style: italic;'>"+date+"</span>" +
		"				"+title +
		"			</a>";
	return html;
}
function showActualities(xml){
	//alert(xml);
	$(xml).find('offset').each(function(){
		var value = $(this).find('value').text();
		if($(this).find('hasNext').text() == '1'){
			hasNext = true;
			$("#btn_next_actuality").css('visibility', 'visible');
		}else{
			hasNext = false;
			$("#btn_next_actuality").css('visibility', 'hidden');
		}
		if($(this).find('hasPrec').text() == '1'){
			hasPrec = true;
			$("#btn_prev_actuality").css('visibility', 'visible');
		}else{
			hasPrec = false;
			$("#btn_prev_actuality").css('visibility', 'hidden');
		}
		$("input[name=offset_actuality]").val(value);
	});
	$('#box_actuality_content').empty();
	var html = "<ul>";
	$(xml).find('row').each(function(){
		var id = $(this).find('id').text();
		var title = $(this).find('title').text();
		var date = $(this).find('date').text();
		html += "<li>"+getUrlActuality(id, date, title)+"</li>";
	});

	html += "</ul>";
	
	$('#box_actuality_content').append(html);
}
function getUrlProperty(property){
	var vars = [], hash;
	var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	for(var i = 0; i < hashes.length; i++){
		hash = hashes[i].split('=');
		if(hash[0].toLowerCase() == property.toLowerCase()){
			return (hash.length == 2) ? hash[1] : "";
			 
		}
	}
	return false;
}
var $_GET = {};

document.location.search.replace(/\??(?:([^=]+)=([^&]*)&?)/g, function () {
    function decode(s) {
        return decodeURIComponent(s.split("+").join(" "));
    }

    $_GET[decode(arguments[1])] = decode(arguments[2]);
});
function onErrorQueryAjax(xhr, ajaxOptions, thrownError){
    alert("["+xhr.status+"] "+ thrownError);
}
