/*
 * Accessible Searchform Utilities
 * Author: Tim Isenheim
 * Author URI: http://www.freshlabs.de
 * Version: 0.9
 */

/*
 * This sourcecode is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License
 * http://creativecommons.org/licenses/by-nc-sa/2.5/
 * Contact the Author for more information
 */

/*
 * ---------------------
 * Searchform Utilities
 * ---------------------
 */
var Searchform = {

	searchform : null,
	searchInput : null,
	searchSubmit : null,
	searchInput_defaultText : "Website durchsuchen...",
	resetButton : null,
	searchResetID : "searchreset",
	typedIn : false,      // is a search-term typed in?
	isSafari : false,

	/*
	 * init (Form-ID, Input-ID, Submit-ID)
	 * initializes the Searchform
	 */
	init : function(fid, iid, sid){
		this.searchform   = $(fid);
		this.searchInput  = $(iid);
		this.searchSubmit = $(sid);
		
		if (navigator.userAgent.indexOf("Safari") == -1){
			this.isSafari = false;
			this.initResetButton();
			this.appendReset();
			this.devisualize();
		}
		else{
			this.isSafari = true;
			this.searchInput.setAttribute("autosave", "alpha");
			this.searchInput.setAttribute("results", 5);
			this.searchInput.setAttribute("type", "search");
		}

		//this.searchSubmit.style.display = "none";
		//this.searchInput.setAttribute("autocomplete", "off");
		//this.searchInput.value = this.searchInput_defaultText;

		this.searchInput.onfocus = function(){ Searchform.visualize(); }
		this.searchInput.onblur = function(){ Searchform.devisualize(); }

		
	},

	initResetButton : function(){
		// create and append as long as the button doesn't exist
		if(!document.getElementById(this.searchResetID)){
			this.resetButton = document.createElement('input');
			this.resetButton.setAttribute("id", this.searchResetID);
			this.resetButton.setAttribute("type","button");
			this.resetButton.value = "X";
			this.resetButton.title = "clear the searchform";
			this.resetButton.onclick = this.resetButton.onmousedown = this.resetButton.onkeypress = function(){
				Searchform.resetForm();
			}
			this.hideReset();
		}
	},

	appendReset : function(){
		if(!this.isSafari){
			this.searchform.appendChild(this.resetButton);
		}
	},

	showReset : function(){
		if(!this.isSafari)
				this.resetButton.style.display = "block";
	},

	hideReset : function(){
		if(!this.isSafari)
				this.resetButton.style.display = "none";
	},

	
	resetForm : function(){
		this.searchInput.focus();
		this.searchInput.value = "";
		  
		this.hideReset();
		this.typedIn = false;	 
		closeResults();
	},
	
	visualize : function(){	
		if (!this.isSafari){
			this.searchform.className = "focused";
		}
		if(this.searchInput.value == this.searchInput_defaultText){
			this.searchInput.value = "";
			this.hideReset();
		}
	},
	
	devisualize : function(){
		if(this.searchInput.value == "") 
	  		this.searchInput.value = this.searchInput_defaultText;
		if (!this.isSafari){
	  		this.searchform.className = "applestyle";
	  		this.showReset();
	  	}
	}	
}
