// User specific scripts

var User = {
	searchHistory:{
		init: function(){
			this.oTxtStreet = $("#street");
			this.oSelEnv = $("#area_sel");
		},
		setValues: function(oInputData){
			var sWhat = oInputData.txtWhat ? unescape(oInputData.txtWhat) : "";
			var sWhere = oInputData.txtWhere ? unescape(oInputData.txtWhere) : "";
			var sStreet = oInputData.txtStreet ? unescape(oInputData.txtStreet) : "";
			var sEnvDist = oInputData.selDistance ? unescape(oInputData.selDistance) : "";
			
			
			// if one of envFields is not empty open envSearch, 
			// if nothing selected from where-history, hide it.
			// But only hide it if not already hidden
			(sStreet != "" || sEnvDist != "") ? goNav.envSearch.show() : ((goNav.envSearch.isVisible() && sWhat == "") ? goNav.envSearch.hide() : 0);
			
			// Fill textfields
			if(sWhat != "") {
				// Change Textfield. Set to standardMode with black letters, value shouldnt be selected
				goNav.examples.inputWhat.setTxtToStandardMode().val(sWhat).addClass("nobluemark").focus();
			} else if(sWhere != "" || sStreet != "" || sEnvDist != "") {
				// Change Textfield to standard mode if one of the loc-fields is not empty
				goNav.examples.inputWhere.setTxtToStandardMode().addClass("nobluemark").focus();
			}			
			
			if(sWhere != "") goNav.examples.inputWhere.val(sWhere);
			if(sStreet != "") this.oTxtStreet.val(sStreet);
			// select value of selectbox, ych: wird dieser Wert noch gespeichert ? 
			if(sEnvDist != "") this.oSelEnv.val(sEnvDist); 

			// Hide Active HistoryBox
			if (this.getActiveBox()) this.getActiveBox().hide();
			
			// Check Historytype and proof for submitting or setting focus
			if(sWhat != ""){
				// What-History selected
				// focus where-field if empty or with examples value. no submit.
				(goNav.examples.inputWhere.isEmpty()) ? goNav.examples.inputWhere.focus() : 0;
			}
		},
		resetList: function(sType){
			// Clear Cookie
			$.cookie('GY_1HIST','', {expires:1,path: '/'});
			// Return if Box-Class was not completely build (e.g. hide-function is not there)
			if(!isFunction(this.getHistoryClassFromType(sType).hide)) return;
			// Hide Layer
			this.getHistoryClassFromType(sType).hide();
			// Disable History
			this.getHistoryClassFromType(sType).disable();
		},
		getHistoryClassFromType: function(sType){
			switch(sType){
				case "what":
					return this.HistBoxClassWhat;
					break;
				case "where":
					return this.HistBoxClassWhere;
					break;
			}
		},
		setActiveBox: function(oBoxClass){
			this.oActiveBox = oBoxClass;
		},
		getActiveBox: function(){
			return this.oActiveBox;
		},
		isDisabled:  function(){
			var booHistWhatOff = $(".histWhatOff").length > 0 ? true : 0;
			var booHistWhereOff = $(".histWhereOff").length > 0 ? true : 0;
			return {histWhat:booHistWhatOff, histWhere: booHistWhereOff};
		},
		hide: function(sType){
			this.getHistoryClassFromType(sType).hide();
		},
		BoxClass: function(sHolder, sSwitchBtn, sInput){
			this.sHolder = sHolder;			
			this.sSwitchBtn = sSwitchBtn;
			
			// Return if HistoryHolder or Switch-Btn are not available
			if(!isObject(document.getElementById(this.sHolder)) || !isObject(document.getElementById(this.sSwitchBtn))) return;
			
			this.oSwitchBtn = $("#"+this.sSwitchBtn);
									
			// Set Textfields and Selectfield
			this.sInput = sInput;			
			
			this.sTxtTitleHistHide = "Kürzliche Suchen ausblenden";			
			this.sTxtTitleHistShow = "Kürzliche Suchen anzeigen";
			
			this.setHandlers = function(oBoxClass){
				$(document.body).bind("mouseup", {oBoxClass: oBoxClass}, this.handleMouseClick);
			};
			
			this.handleMouseClick = function(event){
				var oBoxClass = event.data.oBoxClass;

				var obj = event.target;
				while(obj.id != oBoxClass.sHolder && obj.id != oBoxClass.sSwitchBtn && obj.nodeName.toLowerCase() != "body" && obj.nodeName.toLowerCase() != "html") obj = obj.parentNode;

				// Hide History if outside of histLayer
				if(obj.id == oBoxClass.sHolder || obj.id == oBoxClass.sSwitchBtn || oBoxClass.isDisabled()) return;
				else oBoxClass.hide();
			};
			
			this.toggle = function(){
				var displayOpt = this.display;
				(!displayOpt || displayOpt == "none" || displayOpt == "") ? this.show() : this.hide();
				return false;
			};
			
			this.show = function(){
				User.searchHistory.setActiveBox(this);
				this.oSwitchBtn.parent().attr("class", "S_btn_dropdownStart");
				this.setSwitchTitle(this.sTxtTitleHistHide);
				goUtils.setLyr(this.sInput, this.sHolder, -35, 30);
				$("#"+this.sHolder).css(goUtils.css.visible);
				return false;
			};
			
			this.hide = function(){
				$("#"+this.sHolder).css(goUtils.css.invisible);
				this.oSwitchBtn.parent().attr("class", "S_btn_dropdownStart_off");
				this.setSwitchTitle(this.sTxtTitleHistShow);
				return false;
			};
			
			this.disable =  function(){
				$(this.oSwitchBtn).remove();
				$(".searchforms").addClass("histWhatOff");
				$(".searchforms").addClass("histWhereOff");
				this.setIsDisabled();
			};
			this.setIsDisabled = function(){
				this.booDisabled = true;
			};
			this.isDisabled = function(){
				return (this.booDisabled) ? this.booDisabled : false;
			};
			
			this.setSwitchTitle = function(sTitle){
				this.oSwitchBtn.parent().get(0).title = sTitle;
				// ALT-Tag for IE
				this.oSwitchBtn.attr("alt", sTitle);
			};
			
			this.setHandlers(this);
			
			
		}		
	}	
}

// create HistoryBox Classes
$(document).ready(function(){
	User.searchHistory.HistBoxClassWhat = new User.searchHistory.BoxClass("histBoxWhat", "btnDropdownWhat", "what");
	User.searchHistory.HistBoxClassWhere = new User.searchHistory.BoxClass("histBoxWhere", "btnDropdownWhere", "where");
});
 

