function createXhrObject(){
    if (window.XMLHttpRequest) 
        return new XMLHttpRequest();
    
    if (window.ActiveXObject) {
        var names = ["Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.3.0", "Msxml2.XMLHTTP", "Microsoft.XMLHTTP"];
        for (var i in names) {
            try {
                return new ActiveXObject(names[i]);
            } 
            catch (e) {
            }
        }
    }
    window.alert("Votre navigateur ne prend pas en charge l'objet XMLHTTPRequest.");
    return null; // non supporté
}

var new_search = document.getElementById('new_search');
var search_results = document.getElementById('new_search_results');
var ajaxSearch = new createXhrObject();
var ajaxPreviewSearch = new createXhrObject();
var search_preloader = document.getElementById('search_preloader');
var new_search_zone = document.getElementById('new_search_zone');
var current_search_filter = document.getElementById('current_search_filter');

function sEngine() {
	
	this.input_value = "";
	this.input_length = 0;
	this.status = 0;
	this.started = 0;
	this.words;
	this.decalage = 0;
	this.searched_words = "";
	this.first = 0;
	this.latestPreview;
	this.previewDecalage;
	
	this.hover = function(el) {
		
		el.style.backgroundImage = "url('gfx/lightbox_selected.gif')";
		el.style.color = "#FFF";		
				
	}
	
	this.hoverOut = function(el) {
		
		el.style.backgroundImage = "none";
		el.style.color = "#000";
		
	}
	
	this.submit = function() {
		
		this.input_value = new_search.value;
		
		if(this.filtreTxt == "all") {
			window.location = "index.php?search=" + this.input_value + '&lang=' + document.getElementById('current_language').value;
		}
		else {		
			window.location = "index.php?search=" + this.input_value + '&search_filter=' + this.filtreTxt + '&lang=' + document.getElementById('current_language').value;
		}
	}
	
	this.checkFilters = function() {
	
		this.filters = new Array();
		this.filtersCompte = 0;
		this.filtreTxt = new Array();
		
		if(document.getElementById('filter_xs').checked == true) {
			this.filters['xs'] = 1;
			this.filtersCompte++;
			this.filtreTxt.push('xs');
		}
		else {
			this.filters['xs'] = 0;
		}
		
		if(document.getElementById('filter_s').checked == true) {
			this.filters['s'] = 1;
			this.filtersCompte++;
			this.filtreTxt.push('s');
		}
		else {
			this.filters['s'] = 0;
		}
		
		if(document.getElementById('filter_m').checked == true) {
			this.filters['m'] = 1;
			this.filtersCompte++;
			this.filtreTxt.push('m');
		}
		else {
			this.filters['m'] = 0;
		}
		
		if(document.getElementById('filter_l').checked == true) {
			this.filters['l'] = 1;
			this.filtersCompte++;
			this.filtreTxt.push('l');
		}
		else {
			this.filters['l'] = 0;
		}
		
		if(document.getElementById('filter_xl').checked == true) {
			this.filters['xl'] = 1;
			this.filtersCompte++;
			this.filtreTxt.push('xl');
		}
		else {
			this.filters['xl'] = 0;
		}
		
		if(document.getElementById('filter_xxl').checked == true) {
			this.filters['xxl'] = 1;
			this.filtersCompte++;
			this.filtreTxt.push('xxl');
		}
		else {
			this.filters['xxl'] = 0;
		}
		
		if(document.getElementById('filter_xxxl').checked == true) {
			this.filters['xxxl'] = 1;
			this.filtersCompte++;
			this.filtreTxt.push('xxxl');
		}
		else {
			this.filters['xxxl'] = 0;
		}
		
		if(document.getElementById('filter_highres').checked == true) {
			this.filters['highres'] = 1;
			this.filtersCompte++;
			this.filtreTxt.push('highres');
		}
		else {
			this.filters['highres'] = 0;
		}
		if(document.getElementById('filter_lowres').checked == true) {
			this.filters['lowres'] = 1;
			this.filtersCompte++;
			this.filtreTxt.push('lowres');
		}
		else {
			this.filters['lowres'] = 0;
		}
		
		if(document.getElementById('filter_fotolia').checked == true) {
			this.filters['fotolia'] = 1;
			this.filtersCompte++;
			this.filtreTxt.push('fotolia');
		}
		else {
			this.filters['fotolia'] = 0;
		}
		if(document.getElementById('filter_istock').checked == true) {
			this.filters['istock'] = 1;
			this.filtersCompte++;
			this.filtreTxt.push('istock');
		}
		else {
			this.filters['istock'] = 0;
		}
		if(document.getElementById('filter_none').checked == true) {
			this.filters['none'] = 1;
			this.filtersCompte++;
			this.filtreTxt.push('none');
		}
		else {
			this.filters['none'] = 0;
		}
		
		
		if(document.getElementById('filter_rf').checked == true) {
			this.filters['rf'] = 1;
			this.filtersCompte++;
			this.filtreTxt.push('rf');
		}
		else {
			this.filters['rf'] = 0;
		}
		if(document.getElementById('filter_rm').checked == true) {
			this.filters['rm'] = 1;
			this.filtersCompte++;
			this.filtreTxt.push('rm');
		}
		else {
			this.filters['rm'] = 0;
		}
		
		
		if(document.getElementById('filter_square').checked == true) {
			this.filters['square'] = 1;
			this.filtersCompte++;
			this.filtreTxt.push('square');
		}
		else {
			this.filters['square'] = 0;
		}
		if(document.getElementById('filter_portrait').checked == true) {
			this.filters['portrait'] = 1;
			this.filtersCompte++;
			this.filtreTxt.push('portrait');
		}
		else {
			this.filters['portrait'] = 0;
		}
		if(document.getElementById('filter_landscape').checked == true) {
			this.filters['landscape'] = 1;
			this.filtersCompte++;
			this.filtreTxt.push('landscape');
		}
		else {
			this.filters['landscape'] = 0;
		}
		
		if(this.filtersCompte == 17) {
			this.filtreTxt = "all";
		}
		else {
			this.filtreTxt = this.filtreTxt.join(';');
		}
	
	}
	
	this.setFilter = function(filtre) {
	
		if(this.filters[filtre] == 1) {
			this.filters[filtre] = 0;
		}
		else {
			this.filters[filtre] = 1;
		}
		
		searchEngine.checkFilters();
				
	}
	
	var previewTyping = 0;
		
	this.typing = function() {
		
		this.input_value = new_search.value;
		this.input_length = new_search.value.length;
		
		if(this.first == 0) {
			this.first = 1;
		}
		
		if(this.searched_words.length > 0) {
			
			this.decalage = this.searched_words.length;
			this.input_length = new_search.value.length - this.decalage;
			this.input_value = new_search.value.substr(this.decalage, new_search.value.length);
			
		}
		
		if(this.searched_words.length > new_search.value) {
			this.searched_words = new_search.value;
		}
		
		if(this.input_length == 3 && this.status == 0) {
			searchEngine.ajax();
			this.status = 1;
		}
		else if(this.input_length < 3) {
			this.status = 0;
		}
		else if(this.status == 2) {
			searchEngine.arraySearch();
		}
		
		if(previewTyping == 0) {
			previewTyping = 1;
			setTimeout(function() {
				searchEngine.ajaxPreview();
				previewTyping = 0;
			}, 1000);
		}
		
				
	}
	
	this.ajax = function() {
		
		ajaxSearch.open('GET', 'search/ajax.search.php?search=' + this.input_value, true);
		ajaxSearch.onreadystatechange = function() {
			if(ajaxSearch.readyState == 4) {
				searchEngine.postAjax();
			}
		}
		ajaxSearch.send(null);
		
	}
	
	this.ajaxPreview = function() {
		
		ajaxPreviewSearch.open('GET', 'search/search.preview.php?search=' + this.input_value, true);
		ajaxPreviewSearch.onreadystatechange = function() {
			if(ajaxPreviewSearch.readyState == 4) {
				searchEngine.showPreview();
			}
		}
		ajaxPreviewSearch.send(null);
		
	}
	
	this.showPreview = function() {
		document.getElementById('search_preview').style.position = "absolute";
		document.getElementById('search_preview').style.width = '200px';
		document.getElementById('search_preview').style.top = new_search.offsetTop + 6 + 'px';
		document.getElementById('search_preview').style.left = new_search.offsetLeft + new_search.offsetWidth - 210 + 'px';
		document.getElementById('search_preview').style.display = 'block';
		document.getElementById('search_preview').innerHTML = ajaxPreviewSearch.responseText;
	}
	
	this.postAjax = function() {
		
		this.words = ajaxSearch.responseText.split(';');
		
		if (this.words != "") {
		
			search_results.innerHTML = '<ul>';
			
			for (x = 0; x < this.words.length; x++) {
				search_results.innerHTML += '<li onmouseover="searchEngine.hover(this);" onmouseout="searchEngine.hoverOut(this);" onclick="searchEngine.addKeyword(this.innerHTML);">' + this.words[x] + '</li>';
			}
			
			search_results.innerHTML += '</ul>';
			search_results.style.position = "absolute";
			search_results.style.top = new_search.offsetTop + new_search.offsetHeight - 1 + "px";
			search_results.style.left = new_search.offsetLeft + "px";
			search_results.style.zIndex = 100;
			search_results.style.backgroundColor = "#FFF";
			search_results.style.border = "1px solid grey";
			search_results.style.borderTop = "0px";
			search_results.style.display = "block";
			this.status = 2;
			
		}
		
	}
	
	this.arraySearch = function() {
		
			if (this.words.join(',').search(this.input_value) != -1) {
			
				search_results.innerHTML = '<ul>';
				
				for (x = 0; x < this.words.length; x++) {
				
					if (this.words[x].search(this.input_value) != -1) {
						search_results.innerHTML += '<li onmouseover="searchEngine.hover(this);" onmouseout="searchEngine.hoverOut(this);" onclick="searchEngine.addKeyword(this.innerHTML);">' + this.words[x] + '</li>';
					}
					
				}
				
				search_results.innerHTML += '</ul>';
				search_results.style.display = "block";
				
			}
			else {
				search_results.style.display = "none";
			}
			
		
				
	}
	
	this.addKeyword = function(word) {
		new_search.value = this.searched_words + word;
		this.searched_words += word + ' ';
		search_results.style.display = "none";
		new_search.focus();
	}
		
	this.toggle = function() {
		
		var search_filters = document.getElementById('new_search_filters');
		
		if(search_filters.style.display == "none") {
			search_filters.style.display = "block";
			search_results.style.display = "none";
		}
		else {
			search_filters.style.display = "none";
		}
		
	}
	
	
}

var searchEngine = new sEngine();

searchEngine.checkFilters();

new_search.onkeyup = function(e) {
	if(!isIE()) {
		searchEngine.typing();
	}
}

new_search.onclick = function() {
	
	if(searchEngine.first == 0) {
		new_search.value = "";
		new_search.style.color = "#000";
		new_search.style.fontSize = "16px";
		searchEngine.started = 1;
	}
	
	
}

new_search.onblur = function() {
	
	setTimeout('search_results.style.display = "none"', 500);
	
}
