$.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});
	});
}



$(document).ready(function(){
	
	//fix png for IE...
	$.ifixpng('/site/images/pixel.gif')
	$('img[@src$=.png]').ifixpng();
	
	$('.menubuttonface').each(function(){
		$(this).css({cursor: "pointer"});
		$(this).buttonDeSelected();
	});
	
	/*
		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();
			}
		});
		
		var page = $(this).attr('rel');
		var offset = 600*($(this).attr('id')-1)*(-1);
		var content = $('div.content');
	//	content.hide();
		$(this).parent().parent().find('ul').animate({ top: offset}, 650,'swing',function(){
			//SET THE TITLE TO THE PAGE ACCORDINGLY!!!
			$(document).attr("title","Vertical Aventure | " + page);
			//LOAD THE CONTENT
			content.load("/getContent.php?page="+page,function(responseText, textStatus, XMLHttpRequest){
				// Need to re-init LyteBox...
				initLytebox();
				if(page == 'galerie'){
					//need to take care of the album pagination in case of...
					initGalerie();
				}
			});
			//SHow content
			content.fadeIn("fast",'swing');
			//Notify Google
			pageTracker._trackPageview("/"+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();
}