(function(){
	/*
	 * jQuery PlaceHolder + Search Submit for SiteCore
	 * it submits the search query on enter button and on search button.
	 * place holder functionality for browsers which don't support placeholder
	 * needs: (
	 *<fieldset>
	 * 		<div>
	 *			<input type="hidden" name="url" value="search.aspx" />
	 *			<input class="search" type="search" placeholder="Zoek..." name="search"/>
	 *		</div>
	 *		<button class="submit_search">search</button> <!-- can be a.submit or input.submit as well -->
	 * </fieldset>
	 * @author Lee Boonstra, <lee.boonstra@efocus.nl>
	 * @since 21 jan 2011
	 * @version 1.0
	 * @copyright eFocus
	 *
	 * @uses jQuery 1.2.6, <http://www.jquery.com> or higher
	 */
	jQuery.fn.SearchInput = function(){
		var strSavedString = "";
		
		/*
		 * Does the browser support placeholder attribute?
		 * @return boolean
		 */
		function isBrowserSupport(){
			var i = document.createElement("input");
			return "placeholder" in i;
		}
			
		return this.each(function(){	
			var button = jQuery(this).parent().parent().find('.submit_search');
			var url = jQuery('fieldset.search').find('input[name|=url]').val();
			var emptySearch = jQuery('fieldset.search').find('input[name|=emptysearch]').val();
			var urlDealer = jQuery('div.find_a_dealer').find('input[name|=urlDealer]').val();		
		
			var e = jQuery(this);
			
			var strPlaceHolderText = e.attr("placeholder");
			
			if(e.val().length == 0){
				e.val(strPlaceHolderText);	
			}
			
			/*
			 * On field focus remove placeholder if there is no written text in it.
			 * @return void
			 */
			e.focus(function(){
				strSavedString = e.val();
				if (strSavedString === strPlaceHolderText) {
					e.val("");	
				}
			});
			
			/*
			 * Save the written text in a variable
			 * @return void
			 */
			e.change(function(){
				strSavedString = e.val();
			});
			
			/*
			 * While leaving the focus of the field, put the placeholder back
			 * or the written text.
			 * @return void
			 */
			e.blur(function(){
				if(isBrowserSupport()===false){
					if(strSavedString !== strPlaceHolderText && strSavedString !== ""){
						e.val(strSavedString);		
					} else {
						e.val(strPlaceHolderText);
					}	
				}
			});
			
			/*
			 * Search if the key is enter or button has been clicked, 
			 * if there's a input hidden url and the input is not empty and does not contain the placeholder
			 */
			function search(ev){
				if (ev.keyCode === 13 && strSavedString !== strPlaceHolderText && strSavedString !== "" && url !== null) {
					window.location.href = url + "?q=" + strSavedString;
					return true;
				}
				if(ev==="click" && strSavedString !== strPlaceHolderText && strSavedString !== "" && url !== null){
					window.location.href = url + "?q=" + strSavedString;
					return true;
				}
				if(ev.keyCode === 13 || ev==="click" && jQuery('input.search').val() == strPlaceHolderText && emptySearch !== null || jQuery('input.search').val() == "" && emptySearch !== null){
					window.location.href = emptySearch;
					return true;
				}
			}
			
			/*
			 * Submit the query 
			 */			
			e.keyup(function(ev){
				search(ev);	
				return false;
			})
			button.click(function(){
				search("click");
				return false;
			});
			
			function searchDealer(ev){
				jQuery("#dealer_countries option:selected").each(function(i, selected){ 
					this[i] = jQuery(selected).val();
					strCountry = this[i];
				
					if (ev.keyCode === 13 && strSavedString !== strPlaceHolderText && strSavedString !== "" && urlDealer !== null) {
						window.location.href = urlDealer + "?c=" + strCountry + "&q=" + strSavedString;
						return true;
					}
					if(ev==="click" && strSavedString !== strPlaceHolderText && strSavedString !== "" && urlDealer !== null){
						window.location.href = urlDealer + "?c=" + strCountry + "&q=" + strSavedString;
						return true;
					}
				});
			}
			
			var eDealer = jQuery('div.find_a_dealer input.search');
			var buttonDealer = jQuery('div.find_a_dealer input.submit_search');
			
			eDealer.keyup(function(ev){
				searchDealer(ev);
				return false;	
			})
			buttonDealer.click(function(){
				searchDealer("click");
				return false;	
			});
						
		});
			
	};
})(jQuery);
