document.observe('dom:loaded', function() {
    var product = $('product');
    var accountLink = $('accountLink');
    var authLink = $('authLink');

    if ($F('basket_key') == 'IN_BASKET') {
        new Ajax.Autocompleter(product, 'autocomplete', 'buy-autocomplete.php', {frequency: 0.1, minChars: 2, paramName: 'card', afterUpdateElement: submitLookFor.curry(true)});
    }
    else {
        new Ajax.Autocompleter(product, 'autocomplete', 'autocomplete.php', {frequency: 0.1, minChars: 2, afterUpdateElement: submitLookFor.curry(true)});
    }

    product.observe('focus', productFocus);

    product.observe('focus', Element.addClassName.curry('lookfor', 'focus'));
    product.observe('blur', Element.removeClassName.curry('lookfor', 'focus'));

    if (accountLink) {
      new Ajax.Request('ajax.php', {method: 'get', parameters: {what: 'accountMenu'}, onComplete: function (response) {
	  new Tip(accountLink, response.responseText, {
	    border: 2,
	    radius: 2,
	    stem: 'topMiddle',
	    hideAfter: 2,
	    hideOn: false,
	    fixed: true,
	    width: 150,
	    hook: {target: 'bottomLeft', tip: 'topMiddle'},
	    delay: 0
	});
      }});
      
      accountLink.observe('click', Event.stop);
    }

    if (authLink) {
    	new Ajax.Request('ajax.php', {method: 'get', parameters: {what: 'authMenu'}, onComplete: function (response) {
        	  new Tip(authLink, response.responseText, {
        	    border: 2,
        	    radius: 2,
        	    stem: 'topRight',
        	    hideAfter: 2,
        	    hideOn: false,
        	    fixed: true,
        	    width: 300,
        	    hook: {target: 'bottomMiddle', tip: 'topRight'},
        	    delay: 0
    	    });
        }});

      authLink.observe('click', Event.stop);

      authLink.observe('prototip:shown', function() {
        $('afMail').focus();
      });
    }

    checkError.delay(0.25);
});

function checkError() {
    new Ajax.Request('ajax.php', {method: 'get', parameters: {what: 'errors'}, onComplete: function (response) {
        if (!response.responseText.blank()) {
          var home = $('home');
          var html = '<ul id="flashMessages">'+response.responseText+'</ul>';

      	  new Tip(home, html, {
          title: 'Jetez un oeil ici',
          border: 2,
          radius: 2,
          closeButton: true,
          hideOn: false,
          hideAfter: 5,
          showOn: '',
          fixed: true,
          width: 'auto',
          stem: 'topLeft',
          hook: {target: 'bottomLeft', tip: 'topLeft'},
          offset: {x: 10, y: 5},
          delay: 0
          });

          home.prototip.show();
        }
    }});
}

function productFocus(e) {
    e.element().clear().stopObserving('focus', productFocus);
}

function linkToLookFor(event) {
	Event.stop(event);

	$('product').value = event.findElement().innerHTML;

	submitLookFor(true);
}

function submitLookFor(strict) {
    $('strict').value = (strict == true) ? 'y' : 'n';
	$('lookfor').submit();
}
