// JavaScript Document 
jQuery(document).ready(function(){	

  storeJSONObject = { 
	  "stores" : magasinsCultura,
	  "labs" : []		
	};			
	// binding events
	triggerClick(jQuery(".storeSearchInput"),jQuery("#validStoreSearchButton"));
	
	jQuery('.storeSearchInput').bind('focus',function(event){	jQuery(this).val('');	});	
  jQuery('.storeSearchButton').bind("click", storeSearch);
});

function storeSearch(){
  jQuery(this).unbind("click", storeSearch).attr('disabled','disabled');
  var storeSearchInput = jQuery(".storeSearchInput");
  jQuery("#dialog").dialog({ 
	  modal: true,
	  resizable: false,
	  draggable: true,
	  autoOpen: false,
	  height:"auto",
	  width:"486px",
	  overlay: {
	  opacity: 0.7,
	  background: "white"
	  },
	  title: "Nous recherchons votre magasin",
	  dialogClass: "style1",
	  close:function(){
	    showhideSelect();
	    jQuery(".storeSearchButton").bind("click", storeSearch).removeAttr('disabled');
	    storeSearchInput.focus();
    },
	  open: function(event, ui) {
			  storeInput(storeSearchInput, storeJSONObject);
			  tracker55.fb("recherche","atelier",storeSearchInput.val());
	  }
  });
	
  jQuery("#dialog").load("dialogBoxes/dialogStores.html", function(){
	  // hiding search selectbox for IE6
	  showhideSelect();
	  jQuery("#dialog").dialog("open");
  });
  return false;
}

function storeInput (event, data) {
	// ! catch input and JSON datas
	var inputValue = jQuery(event).val();
	inputValue = replaceAccent(inputValue);  
	var storeResults = data.stores;
	 var matchesCount = 0;
	  
	// ! define digits range
	var regDigits = new RegExp(/^[0-9]{2,5}$/);
	var matchDigits = inputValue != "ville ou code postal" ? inputValue.match(regDigits) : null;

	// ! define words range
	var regWords = new RegExp(/^[a-zA-Z\s.\S.\-]{2,26}$/);
	var matchWords = inputValue != "ville ou code postal" ? inputValue.toLowerCase().match(regWords) : null;
	//jQuery(".promoArea").empty();
	
	// ! start with digits, the smallest range {2}
	switch(matchDigits){
	case null:
		// ! not a valid number so start with words,
		switch(matchWords){
			case null:
				// ! Numbers or Words are invalids,

				jQuery(".introStorePop").remove();
				 jQuery(".ui-dialog-title").html("Veuillez reformuler votre recherche");
				jQuery(".storePop").html("<p id='storeSearchError'>Vos critères de recherche ne sont pas valides ou insuffisants (Code Postal ou Ville)</p><ul class='listStoreError'><li>- Code Postal : 2 à 5 caractères maximum</li><li>- Ville : 2 caractères minimum</li></ul>");
			break;
			
			default:
				// ! Valid Word Search, => ville_search
				jQuery(storeResults).each(function(i){	
			    var ville = replaceAccent(this.ville_search.toLowerCase()); 			
			 		var cityResult = (ville.indexOf(matchWords.toString()));
		  		
		  		// ! Search results in stores.ville_search
		  		switch(cityResult){
		  			case -1:break;
		  			default:
		  				// ! Words Found
		  				matchesCount++;
		  				jQuery(".introStorePop").remove();
		  				jQuery(".storePop").append("<p class='itemStorePop'><a href=\"MagAteliers.aspx?MagId="+this.MagId+"\" title=\"Voir les ateliers de "+this.ville+"\">"+this.ville+"</a> : "+ this.adresse + " : "+ this.code_postal+"</p>");
		  		}
			 	});
			}
	break;
	default:
		// ! Valid Numbers Search, => code_postal
		jQuery(storeResults).each(function(i){
  		var postalResult =  (this.code_postal.substring(0, matchDigits.toString().length).indexOf(matchDigits.toString()));
  		// ! Search result in stores.code_postal
  		switch(postalResult){
  			case -1:break;
  			default:
  				// ! Numbers found
  				matchesCount++;
		  		jQuery(".introStorePop").remove();
		  		jQuery(".storePop").append("<p class='itemStorePop'><a href=\"MagAteliers.aspx?MagId="+this.MagId+"\" title=\"Voir les ateliers de "+this.ville+"\">"+this.ville+"</a> : "+ this.adresse + " : "+ this.code_postal+"</p>");
  		}
	 	});
	}
	
	// Report 
	if(matchDigits || matchWords){
 	  if(matchesCount==0 )
 	  {
 	   jQuery(".ui-dialog-title").html("Aucun résultat trouvé");
	   jQuery(".storePop").html("<p id='storeSearchError'>Désolé, nous ne trouvons aucun résultat correspondant "+(matchDigits ? " au code postal <u>"+inputValue : " à la ville <u>"+inputValue)+"</u>.</p>");
    }
    else
       jQuery(".ui-dialog-title").html(matchesCount +" résultat"+(matchesCount>1 ? "s":"")+" trouvé"+(matchesCount>1 ? "s":""));
  }
}
