

$.fn.buttonOn = function(){
	var mask = $(this).parent().find('.buttonmask');
	var buttontxt = $(this).parent().find('.menubuttontxt');
	var buttonimg = $(this).parent().find('.menubuttonimage').find('#pictoimage');
	$(this).queue(function (){
		buttonimg.fadeTo(200,0);	
		$(this).dequeue();
	});
	$(this).queue(function (){
		mask.fadeTo(300, 0.4);
		$(this).dequeue();
	});
	$(this).queue(function (){
		buttontxt.fadeTo(300,1);
		$(this).dequeue();
	});
	return false;
}

$.fn.buttonOff = function(){
	var mask = $(this).parent().find('.buttonmask');
	var buttontxt = $(this).parent().find('.menubuttontxt');
	var buttonimg = $(this).parent().find('.menubuttonimage').find('#pictoimage');
	$(this).queue(function (){
		buttonimg.fadeTo(200,1);
		$(this).dequeue();
	});
	$(this).queue(function (){
		mask.fadeTo(100, 0.0);
		$(this).dequeue();
	});
	$(this).queue(function (){
		buttontxt.fadeTo(100,0.0);
		$(this).dequeue();
	});
	return false;
}

$.fn.buttonDeSelected = function(){
	//unbind all button face actions...
	$(this).unbind();
	$(this).bind("mouseenter",function () {
		return $(this).buttonOn();
	});
	$(this).bind("mouseleave",function () {
		return $(this).buttonOff();
	});
	var mask = $(this).parent().find('.buttonmask');
	var buttontxt = $(this).parent().find('.menubuttontxt');
	var buttonimg = $(this).parent().find('.menubuttonimage').find('#pictoimage');
	mask.css({opacity: 0});
	buttonimg.css({opacity: 1});
	buttontxt.css({opacity: 0});
}


$.fn.buttonSelected = function(){
	//unbind all button face actions...
	$(this).unbind();
	//display selected button
	var mask = $(this).parent().find('.buttonmask');
	var buttontxt = $(this).parent().find('.menubuttontxt');
	var buttonimg = $(this).parent().find('.menubuttonimage').find('#pictoimage');
	mask.fadeTo("fast",0, function(){
		buttonimg.css({opacity: 0});
		buttontxt.css({opacity: 1});
	});
}


/* Right tabs deselection*/	
function deselectRightTabs(){
	$('.rightlink_selected').each(function(){
		$(this).css({opacity:0.7});
		$(this).hover(function() {
				$(this).animate({
					opacity:1
				},50);
			}, function() {
				$(this).animate({
					opacity:0.7
				},50);
		});
		$(this).removeClass('rightlink_selected').addClass('rightlink');
	});
}

/* change content page */
function displayPage(page, pageLink, pageTxt, lang){
	
	var content = $('div.content');	
	
	//SET THE TITLE TO THE PAGE ACCORDINGLY!!!
	$(document).attr("title", pageTxt + " | Vertical Aventure");
	//LOAD THE CONTENT
	content.load("/getContent.php?page="+page+"&lang="+lang,function(responseText, textStatus, XMLHttpRequest){
		// Need to re-init LyteBox...
		initLytebox();
		if(pageLink == 'gallery'){
			//need to take care of the album pagination in case of...
			initGalerie();
		}
	});
	
	// set new parameters
	jQuery.data(document.body, 'currentpage', page);
	jQuery.data(document.body, 'currentpagelink', pageLink);
	jQuery.data(document.body, 'lang', lang);
}

/* reload language selection part*/
function reloadLangMenu(){
	var langdiv = $('div.langbox');
	langdiv.load("/changeLang.php?page="+jQuery.data(document.body, 'currentpagelink'));
}

$(document).ready(function(){
	
	$('.menubuttonface').each(function(){
		$(this).css({cursor: "pointer"});
		$(this).buttonDeSelected();
	});
	
	// Set language and current page
	jQuery.data(document.body, 'currentpage', CURRENT_PAGE);
	jQuery.data(document.body, 'currentpagelink', CURRENT_PAGE_LINK);
	jQuery.data(document.body, 'lang', MYLOCALE);
	
	// Language selection 
	$('body').delegate(".localize", "click", function() {
		var lang = $(this).attr('lang');
		var page = $(this).attr('alt');
		
		jQuery.data(document.body, 'currentpage', page);
		window.location = "/"+lang+"/"+page;
	});
	
	/*
		THIS NAVIGATION PAGE LOADING STUFF NEEDS TO BE BETTER DONE!!!
	*/
	$('.menubutton').click(function(){
		//reset all buttonfaces...
		var buttonface = $(this).find('.menubuttonface');
		var id = $(this).attr('id');
		$('.menubuttonface').each(function(){
			if(id != $(this).parent().attr('id')){
				$(this).buttonDeSelected();
			} else {
				//leave the button in OnselectionMode...
				buttonface.buttonSelected();
			}
		});
	
		deselectRightTabs();
		
		var buttonimage = $(this).find('.menubuttonimage').find('#pictoimage');
		var pageLink = buttonimage.attr('alt');
		
		var buttontxt = $(this).find('.menubuttontxt');
		var pageTxt = buttontxt.text();
		
		var page = $(this).attr('rel');
		var offset = 600*($(this).attr('id')-1)*(-1);
		
		//	change background image 
		$(this).parent().parent().find('ul').animate({top: offset}, 650,'swing',displayPage(page, pageLink, pageTxt, MYLOCALE));
		
		// reload lang's href
		reloadLangMenu();
	});
	
	$(".rightlink").hover(function() {
			$(this).animate({
				opacity:1
			},50);
		}, function() {
			$(this).animate({
				opacity:0.7
			},50);
	});
	
	setMenuBg(CURRENT_PAGE);
});


function setMenuBg(page){
	var bg = $('.menubutton[rel='+page+']');
	var offset = 600*(bg.attr('id')-1)*(-1);
	bg.parent().parent().find('ul').css({ top: offset});
	$('.menubutton[rel='+page+']').find('.menubuttonface').buttonSelected();

	//rightmenu
	if(page=="faq"|| page=="liens"){
		$('.rightlink[rel='+page+']').css({opacity:1});
	}
}

