google.load("search", "1");
/*** INITIALISATION ***/
var tabs = new Array();
tabs[1] = '<b>La recherche guidée</b> vous permet, par étapes successives très intuitives, de sélectionner vos bijoux de piercing en fonction de leur forme, matière, taille, et même par couleur...';
tabs[2] = '<b>Vous connaissez déjà la référence d\'un bijou ?</b><br />Tapez simplement le début de cette référence et nous vous ferons toutes les propositions correspondantes...';
tabs[3] = '<b>Vous êtes fan de salamandre ou de papillon ?</b><br />Retrouvez très facilement tous les bijoux correspondants aux mots-clés saisis. Idéal pour aller à l\'essentiel !';
tabs[4] = '<b>La recherche sur tout le site</b> vous permet de retrouver grâce à des mots-clés des informations sur toutes les pages de notre site (produits, conseils...) - Motorisé par Google';
init = function() {
	// masquage
	$('indicator1').hide();
	$('tr3').hide();
	$('tr4').hide();
	$('resultDiv').hide();
	$('GODiv').hide();
	for (i=1; i<5; i++) { $('searchDiv'+i).hide(); }
	// par référence
	new Ajax.Autocompleter('input2','autosearch2','ajax/ajax_catalogue_search_2.php',{
		parameters: 'type=2',
		minChars: 3,
		method: 'get',
		callback: function() { return '&input='+$F('input2').gsub('%','') },
		indicator: 'indicator1',
		afterUpdateElement: findRef,
		select: 'totake'
	});
	google.setOnLoadCallback(initGO);
}
initGO = function() {
	var searchControl = new google.search.SearchControl();
	var options = new google.search.SearcherOptions();
	options.setExpandMode(google.search.SearchControl.EXPAND_MODE_OPEN);
	var siteSearch = new google.search.WebSearch();
	siteSearch.setUserDefinedLabel("Résultats sur Inoki-piercing.fr");
	siteSearch.setUserDefinedClassSuffix("siteSearch");
	siteSearch.setSiteRestriction("inoki-piercing.fr");
	searchControl.addSearcher(siteSearch, options);
	searchControl.draw($('searchresultGO'));
}
reinit = function() {
	// rech 1
	$('crit1').selectedIndex=0;
	checkSearch(1);
	// rech 2
	$('input2').value = '';
	// rech 3
	$('input3').value = '';
	// for all
	for (i=1; i<5; i++) {
		$('searchDiv'+i).hide();
	}
	$('resultDiv').hide();
	$('tempdiv').show();
}
/* click sur bouton de sélection de type de recherche */
selSearchType = function(num) {
	nd();
	if ($('tempdiv').visible()) {
		$('tempdiv').hide();
	}
	for (i=1; i<5; i++) {
		if (i == num) {
			//$('btn'+i).disable();
			$('searchDiv'+i).show();
		}
		else {
			//$('btn'+i).enable();
			$('searchDiv'+i).hide();
		}
	}
	$('resultDiv').show();
	var mess;
	switch(num){
		case 1:
			mess = 'Sélectionnez un emplacement...';
			$('resultDiv').show();
			$('GODiv').hide();
			break;
		case 2:
			mess = 'Tapez un début de référence...';
			$('resultDiv').show();
			$('input2').focus();
			$('GODiv').hide();
			break;
		case 3:
			mess = 'Tapez un ou plusieurs mots-clés...';
			$('resultDiv').show();
			$('input3').focus();
			$('GODiv').hide();
			break;
		case 4:
			mess = '';
			$('resultDiv').hide();
			$('GODiv').show();
			break;
	} // switch
	$('searchTxt').update(mess);
}
/* recherche par type */
checkSearch = function(crit){
	// secu
	if ((crit < 1) || (crit > 6)) { return; }
	// remise à zéro
	if ((crit == 1) && ($F('crit1') == '')) {
		$('tr3').hide();
		$('tr4').hide();
		for (i=2; i<7; i++) {
			$('th'+i).update();
			$('td'+i).update();
			$('searchTxt').update('Sélectionnez un emplacement...');
		}
		$('searchresult').update();
	}
	// sinon
	else {
		// on remet à zéro les critères suivants (sauf si > 3)
		for (i=(crit+1); i<7; i++) {
			if (crit < 4) {
				//$('th'+i).update('&nbsp;');
				$('td'+i).update('&nbsp;');
			}
		}
		$('searchresult').update();
		// requête ajax
		$('searchTxt').update('Recherche en cours...');
		new Ajax.Request('ajax/ajax_catalogue_search.php', {
			method: 'post',
			encoding: 'iso-8859-15',
			parameters: $H({
				lang: $F('lang'),
				crits: composeCrits(crit),
				clientid: Cookie.get('I_id'),
				clienttype: Cookie.get('I_type')
			}).toQueryString(),
			onFailure: function() {
				$('searchTxt').update('Erreur Ajax...');
			},
			onSuccess: function(requester) {
				var rep = [];
				var reponse = requester.responseText;
				eval(reponse);
				if (rep['count'] > 20) {
					$('searchTxt').update(rep['count'] + ' résultat(s)... Affinez votre recherche');
					$('searchresult').update();
				}
				else {
					$('searchTxt').update(rep['count'] + ' résultat(s)');
				}
				for (i = 2; i < 7; i++) {
					if ((rep['th'+i]) && (rep['th'+i] != '')) {
						$('th'+i).update(rep['th'+i]);
						$('td'+i).update(rep['td'+i]);
						if (i > 3) {
							$('tr3').show();
							$('tr4').show();
						}
						else {
							$('tr3').hide();
							$('tr4').hide();
						}
					}
				}
				$('searchresult').update(rep['aff'].gsub('§',"'"));
			}
		});
	}
}
composeCrits = function(crit) {
	var crits = '';
	for (i=1; i<7; i++) {
		if ($('crit'+i)) {
			crits += 'crit'+i + '=' + $F('crit'+i)+';';
		}
	}
	return crits;
}
/*** FONCTION DE RECHERCHE SI REFERENCE ENTREE ***/
findRef = function(text,li) {
	var ref = $F('input2').split(/ - /);
	new Ajax.Request('ajax/ajax_catalogue_search_2.php', {
		method: 'get',
		encoding: 'iso-8859-15',
		parameters: $H({
			lang: $F('lang'),
			clientid: Cookie.get('I_id'),
			clienttype: Cookie.get('I_type'),
			ref: ref[0].gsub('%',''),
			type: 3
		}).toQueryString(),
		onFailure: function() {
			$('searchTxt').update('Erreur Ajax...');
		},
		onSuccess: function(requester) {
			var rep = [];
			var reponse = requester.responseText;
			eval(reponse);
			$('searchTxt').update(rep['count'] + ' résultat(s)');
			$('searchresult').update(rep['aff'].gsub('§',"'"));
		}
	});
}
/*** FONCTION DE RECHERCHE PAR MOTS-CLES ***/
searchByKeywords = function() {
	var keywords = $F('input3').strip();
	if (keywords.length < 3) {
		return false;
	}
	new Ajax.Request('ajax/ajax_catalogue_search_2.php', {
		method: 'get',
		encoding: 'iso-8859-15',
		parameters: $H({
			lang: $F('lang'),
			clientid: Cookie.get('I_id'),
			clienttype: Cookie.get('I_type'),
			keywords: keywords,
			type: 4
		}).toQueryString(),
		onFailure: function() {
			$('searchTxt').update('Erreur Ajax...');
		},
		onSuccess: function(requester) {
			var rep = [];
			var reponse = requester.responseText;
			eval(reponse);
			// plus de 20 réponses
			if (rep['count'] > 20) {
				$('searchTxt').update(rep['count'] + ' résultat(s)... Affinez votre recherche...');
				$('searchresult').update();
			}
			// 20 réponses maxis
			else {
				$('searchTxt').update(rep['count'] + ' résultat(s)');
				$('searchresult').update(rep['aff'].gsub('§',"'"));
			}
		}
	});
}
chkKWsearch = function(event) {
	if (event.keyCode == 13) {
		if (!$('input3').present()) {
			$('input3').focus();
		}
		else {
			searchByKeywords();
		}
	};
}
/**********
***   ***
**********/
explain = function(num) {
	var content = '<div id="expfs-top"></div>';
	content += '<div id="expfs-mid">' + tabs[num] + '</div>';
	content += '<div id="expfs-bot"></div>';
	overlib(tabs[num], CENTER, WIDTH, 300, HEIGHT, 76, BORDER, 0, CELLPAD, 0, FOLLOWMOUSE, ANCHOR, 'btn'+num, ANCHORX,-90,ANCHORY,36,FGCLASS,'fgfs',BGCLASS,'bgfs');
	// correction bug Safari/Chrome
	if (Prototype.Browser.WebKit) {
		var pos = getOffsets($('overDiv'));
		var newx = pos[0]-90;
		var newy = pos[1]+36;
		$('overDiv').style.top = newy+'px';
		$('overDiv').style.left = newx+'px';
	}
}
/*** lancement initialisation ***/
document.observe('dom:loaded', init);