jQuery(function($){
   $("#username").Watermark("identifiant","#adadad");
   $("#password").Watermark("aaaaaaaa","#adadad");
   $("#email_address").Watermark("votre adresse email","#adadad");
   $("#game-input").Watermark("tapez le début du nom du jeu","#adadad");
});


var obj = null;
var obj2 = null;
function checkHover() {
   if (obj) {
      obj.find('ul').fadeOut('fast');
   } 
} 

function menuRemember(obj)
{
	switch(obj.attr('id'))
	{
		case 'pnstore':
			$.cookie("JM_Menu", "Goo", { path: '/',expires: 30 });
			break;
		case 'accessoire':
			$.cookie("JM_Menu", "Acc", { path: '/',expires: 30 });
			break;
		case 'game':
			$.cookie("JM_Menu", "Gam", { path: '/',expires: 30 });
			break;
		case 'occasion':
			$.cookie("JM_Menu", "Occ", { path: '/',expires: 30 });
			break;
		case 'home':
			$.cookie("JM_Menu", "Hom", { path: '/',expires: 30 });
			break;
		
	}
} 
 
function checkHover2() {
	   if (obj2) {
	      obj2.find('div').fadeOut('fast');
	   } 
	} 

$(document).ready(function(){
		
		var instanceOne = new ImageFlow();
		instanceOne.init({ 
			ImageFlowID: 'recemment',
			reflectionGET: '&bgc=F5F5F5&fade_start=20%&height=10%',
			xStep: 100,
			buttons:true,
			opacity: true,
			startID: 1,
			imageFocusM: 1,
			slideshow: true,
            slideshowAutoplay: true,
            slideshowSpeed: 4000,
            circular: true,
            reflectPath: "/",
            reflectionP: 0.1,
            scrollbarP: 0.85,
			startAnimation: true
		});
	
	 	 // LOGIN
	 	 
	 	 $('#login').click(function()
		{
	
			$.post(
					"/login/login",
					{"password" : $('#password').val(), "username" : $('#username').val()},
					function(data){
						if(data != "")
						{
							$('#utilisateur').html(data); // Fill the suggestions box
						}
					}); 
		});
	
		$('#username').keypress(function(e)
				{
			 		if (e.which == 13)
			 		{
						$.post(
								"/login/login",
								{"password" : $('#password').val(), "username" : $('#username').val()},
								function(data){
									if(data != "")
									{
										$('#utilisateur').html(data); // Fill the suggestions box
									}
								}); 
			 		}
				});
	
		$('#password').keypress(function(e)
		{
	 				if (e.which == 13)
	 				{
						$.post(
								"/login/login",
								{"password" : $('#password').val(), "username" : $('#username').val()},
								function(data){
									if(data != "")
									{
										$('#utilisateur').html(data); // Fill the suggestions box
									}
								}); 
	 				}
		});
		
		// SOMMAIRE

		 
		 // une partie dans layout
		 
		 
		 if($.cookie("JM_Menu"))
		 {
			var obj;
			switch($.cookie("JM_Menu"))
			{
			 	case 'Goo':
			 		obj = $('#menu div ul.pnstore li a#pnstore');
				 	break;
			 	case 'Gam':
			 		obj = $('#menu div ul.game li a#game');
				 	break;
				case 'Occ':
			 		obj = $('#menu div ul.occasion li a#occasion');
				 	break;
			 	case 'Acc':
			 		obj = $('#menu div ul.accessoire li a#accessoire');
				 	break;
			 	case 'Hom':
			 		obj = $('#menu div ul.home li a#home');
				 	break;
			}
	
			obj.parent('li').addClass('selected');
			obj.addClass('hover');
			var id = obj.attr("id");
		
			$("#menu_"+id).addClass('menuSelected');
			$("#menu_"+id).parent('li').addClass('selected');
			$("#menu_"+id).show();
		}
		else
		{
			var obj = $('#menu div ul.home li a#home');
			obj.parent('li').addClass('selected');
			obj.addClass('hover');
			var id = obj.attr("id");
	
			$("#menu_"+id).addClass('menuSelected');
			$("#menu_"+id).parent('li').addClass('selected');
			$("#menu_"+id).show();
		}
		 

		 if($.cookie("JM_help"))
		 {
			 if($.cookie("JM_help") == 1)
			 {
				 $('.bouton_ouvrir').hide();
			 }
			 else
			 {
				 $('.bouton_fermer').hide();
				 $('#help').addClass("help_fermer");
			 }
		 }
		 else
		 {
			 $('.bouton_ouvrir').hide();
		 }
		 
			$('.bouton_fermer').click(
				function() {
				   $('#help').addClass("help_fermer");
				   $('.bouton_fermer').hide();
				   $('.bouton_ouvrir').show();
				   $.cookie("JM_help", "0", { path: '/',expires: 30 });
				}
			);
			$('.bouton_ouvrir').click(
				function() {
				   $('#help').removeClass("help_fermer");
				   $('.bouton_fermer').show();
				   $('.bouton_ouvrir').hide();
				   $.cookie("JM_help", "1", { path: '/',expires: 30 });
				}
			);
			
				
	// RECHERCHE
	
	var currentSelection = 0;
		var currentUrl = '';
		var clic = 0;

	var autocompleteoptions = {
		    callback:function(){
				lookup($('input#q').val());
			},
		    wait:400,
		    highlight:true,
		    captureLength:1
	}
		
	$('input#q').typeWatch( autocompleteoptions );
	
		$('#submitsearch').click(function()
		{
			if($('input#q').val() != "" && $('input#q').val() != "ex : New Super Mario Bros, RPG, pour enfants...")
			{	
				window.location= "/recherche/name-"+$('input#q').val();
			}
			else
			{
				window.location= "/recherche";
			}
		});
		

		$('input#q').live('keyup',function (e) {
			Defer.schedule('defer', function () {
			
					switch(e.keyCode) 
					{ 
			         	case 13:
				         	
							if($("#searchresults a.hover").length > 0)
							{
								window.location= "/search/search/search/"+$("#searchresults a.hover span.searchheading").text();
							}
							else
							{
								if($('input#q').val() != "")
								{
									window.location= "/search/search/search/"+$('input#q').val();
								}
								else
								{
									window.location= "/Recherche";
								}
							}
			         		
			         		break;
			      	}
			}, 30);
		});
		
		$("input#q").mouseenter(function(){
		 	$('.submenu').fadeOut();
		});
	

});


var Defer = {
		config: {
	defaultTimeout: 1000
},
stack: {},
schedule: function(id, fn, timeout) {
	if (typeof(timeout) == 'undefined') {
		timeout = Defer.config.defaultTimeout;
	}
	if (typeof(Defer.stack[id]) == 'undefined') {
		Defer.stack[id] = {defer: false, fn: fn, timeout: timeout };
		window.setTimeout('Defer.execute(\'' + id + '\')', timeout);
	} else {
		Defer.stack[id].defer = true;
	}
},
execute: function(id) {
	if (typeof(Defer.stack[id]) != 'undefined' && !Defer.stack[id].defer) {
		Defer.stack[id].fn();
		Defer.stack[id] = undefined;
	} else {
		Defer.stack[id].defer = false;
		window.setTimeout('Defer.execute(\'' + id + '\')', Defer.stack[id].timeout);
	}
}
};
function navigate(direction) {

	   // Check if any of the menu items is selected
	   if($("#suggestions ul li a.hover").size() == 0) {
	      currentSelection = -1;
	   }
	   
	   if(direction == 'up' && currentSelection != -1) {
	      if(currentSelection != 0) {
	         currentSelection--;
	      }
	   } else if (direction == 'down') {
	      if(currentSelection != $("#suggestions ul li a").size() -1) {
	         currentSelection++;
	      }
	   }
		
	   setSelected(currentSelection);
	}


function setSelected(menuitem) {

	   $("#suggestions ul li a").removeClass("hover");
	   $("#suggestions ul li a").eq(menuitem).addClass("hover");
	   currentUrl = $("#suggestions ul li a").eq(menuitem).attr("href");
	}


function lookup(inputString) {

	if(inputString.length < 3) {
		$('#suggestions').fadeOut(); // Hide the suggestions box
	}  else {
		$('#loadSearch').fadeIn('fast');
		
		$.post("/search/search", {queryString: ""+inputString+""}, function(data) { // Do an AJAX call
			$('#loadSearch').fadeOut('fast');
			$('#suggestions').fadeIn(); // Show the suggestions box
			$('#suggestions').html(data); // Fill the suggestions box
		});
	}
}