////////////////////////////////////////////////////////////////////////////////////////////////	
// $RCSfile: suche.js,v $
// $Source: /home/cvs/ihk4/scandio-cms/website-sisby/src/js/suche.js,v $
// $Date: 2010-01-28 13:08:38 $
// $Revision: 1.7 $
// $Author: ischwarzer $
// remarks: 
////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////
//	Function: slide(elem)
//  toggles hover-slider and click slider
//  Parameter1: elem (optional)
//  notice: needs special DOM structure
		function slide(elem) {
				if(elem) { 	// Navigatoren
						var id = "#"+elem.parent().get(0).id;
						//alert(id);
						$("div.effect:visible").hide();
						$(id).find(".effect").toggle('slide', '', 500);
		
				} else { 		// erweiterte Suche		 
						$("#effect").toggle('slide','',1000);
				}	
		}

//////////////////////////////////////////////
//	Function: removeLastParam(strHref, toRemove)
//	removes last Parameter from given url, if it is there
//  Parameter1: strHref -- an url
//  Parameter2: toRemove -- Parameter to be removed 'name=value'
//  notice: Parameter needs to be last.. if Parameter check in between is needed 
//	function has to be adjusted, value has to be the same as in toRemove

		function removeLastParam(strHref, toRemove) {
				var arrParam = strHref.split('&');
				var lastParam= arrParam[arrParam.length-1];
				var lastParamName = lastParam.split('=')[0];
				var toRemoveName = toRemove.split('=')[0];						
				if( lastParamName == toRemoveName ) { // letzter Parameter ist toRemove, also entfernen
						return strHref.substring(0, strHref.length-lastParam.length-1);
				} else { // nothing to remove
						return strHref ;
				}
		}

//////////////////////////////////////////////
//	Function: addParam(strHref, newParam) 
//	appends newParam to the given Url
//  Parameter1: strHref -- an url
//  Parameter2: newParam -- Parameter to be added 'name=value'
//  notice: Parameter needs to be last.. if Parameter check inbetween is needed 
//	function has to be adjusted
		function addParam(strHref, newParam) {
				var arrParam = strHref.split('&');
				var lastParam= arrParam[arrParam.length-1];
				var lastParamName = lastParam.split('=')[0];
				var newParamName = newParam.split('=')[0];						
				if( lastParamName == newParamName ) { //Parameter ist bereits da, also erst weg, dann hin....
//alert(strHref.substring(0, strHref.length-lastParam.length+1) +newParam);
						return strHref.substring(0, strHref.length-lastParam.length) + "&" + newParam;
				} else {
				// neuer Parameter ist noch nicht da
					return strHref +  "&" + newParam;
				}
		}

//////////////////////////////////////////////
//	Plugin: clearForm ()
//	resets Form or Form elements
//  can be called on forms or form elements
		$.fn.clearForm = function() {
				return this.each(function() {
						var type = this.type, tag = this.tagName.toLowerCase();
						if (tag == 'form') {
							return $(':input',this).clearForm();
						}
						if (type == 'text' || type == 'password' || tag == 'textarea') {
								this.value = '';
						} else if (type == 'checkbox' || type == 'radio') {
								this.checked = false;
						} else if (tag == 'select') {
								this.selectedIndex = -1;
						}
				});
		};
		
//////////////////////////////////////////////
//	Plugin: isAnyFieldsChanged()
//	stores in hidden field #isAnyFieldsChanged, whether fields has changed or not 
//	needs field #isAnyFieldsChanged

		$.fn.isAnyFieldsChanged = function() {
			$("#isAnyFieldsChanged").attr("value", "false"); 
//alert("call to isAnyFieldsChanged");			
			return this.each(function() {
				var type = this.type, tag = this.tagName.toLowerCase();
				var initVal = $(this).attr("initValue");
//alert("name:"+this.name+"==>initval ="+initVal+" , value ="+this.value);				
				if (type == 'text' && (this.value.length > 0 || initVal ) &&  this.value != initVal) {
//alert("name:"+this.name+"==>initval ="+initVal+" , value ="+this.value);
					$("#isAnyFieldsChanged").attr("value", "true");
				} else if ((type == 'checkbox' || type == 'radio')) {
//alert("name:"+this.name+"==>initval ="+initVal+" , value ="+this.value);					
					// initval = "true" and checkbox: false or initVal undefined and checked
					if( ( initVal == "true"  && !this.checked ) 
					  ||( !initVal && this.checked )) {
						$("#isAnyFieldsChanged").attr("value", "true");
					}
				}
				//alert($("#isAnyFieldsChanged").attr("value"));				
			});
		};

//////////////////////////////////////////////
//		Plugin: isAnyFieldsSet()
//		stores in hidden field #isAnyFieldsChanged, whether fields are set or not 
//		needs field #isAnyFieldsChanged

		$.fn.isAnyFieldsSet = function() {
			$("#isAnyFieldsChanged").attr("value", "false"); 
			return this.each(function() {
				var type = this.type, tag = this.tagName.toLowerCase();
				if (type == 'text' && this.value.length > 0 ) {
					$("#isAnyFieldsChanged").attr("value", "true");
				} else if ((type == 'checkbox' || type == 'radio')) {
					if( this.checked ) {
						$("#isAnyFieldsChanged").attr("value", "true");
					}
				}
			});
		};	
			
//////////////////////////////////////////////
//	Plugin: handleMessage(mode , type)
//	shows or hides Messages
//	Parameter1: msgId -- msg-Container on which action will be done
//	Parameter2: action -- show / hide
//	shows or hides Messages
//	needs field #isOpenErwSuche
		$.fn.handleMessage = function(msgId , action) {
				var status = $(this).attr("value");
				var isOpenErwSuche = $("#isOpenErwSuche").attr("value");
//alert("in handleMessage("+msgId+" , "+ action+ " )");
				return this.each(function() {
						if(action == "show") {											
								if(status == "true" && isOpenErwSuche == "false") {
									$(msgId).show();
								}	
						}
						if(action == "hide") {											
								if(status == "true" && isOpenErwSuche == "true") {
										$(msgId).hide();
								}	
						}
				});
		};	

		
//////////////////////////////////////////////
//	Plugin: handleLinks(mode , type)
//	adds or removes Parameter "isOpenErwSuche=true" to links and options
//	Parameter1: mode -- expand / reduce : add / remove Parameter
//	Parameter2: type -- select / link : Field that has to be changed
//	on open: all Search Links need additional parameter  &isOpenErwSuche=true
		$.fn.handleLinks = function(mode , type) {
				return this.each(function() {
						var cmpString , attrib;
						if(type == "link") {
								cmpString = "#";
								attrib = "href";
						}
						if(type == "select") {
							cmpString = "";
							attrib = "value";
//alert("expand : attrib="+attrib+" , cmpString="+cmpString);
						}
						if(mode == "expand") {
								if($(this).attr(attrib) != cmpString) {
									 $(this).attr(attrib ,addParam($(this).attr(attrib), "isOpenErwSuche=true"));
								}
						}
						if(mode == "reduce") {
								 if($(this).attr(attrib) != type) {
									 $(this).attr(attrib ,removeLastParam($(this).attr(attrib), "isOpenErwSuche=true"));
								 }	 
						}
				});
		};
		
//////////////////////////////////////////////
//	Plugin: initErwSuche()
//	checks onload, if Expanded Expert Search (EES) has to be openend
//	on open: all Search Links need additional parameter  &isOpenErwSuche=true
//  prepares values for EES, and marks fields
//	calls: handleLinks() , handleMessage(), saveErwSuche()
		$.fn.initErwSuche = function() {
			return this.each(function() {
				//init Zustand der erweiterten Expertensuche merken
	 			$("#erweiterteexpertensuche :input").saveErwSuche();
				var eesIsInitValues = $("#isInitValues").attr("value");
				//init Werte der gesamten Expertensuche merken
	 			$("#expertensuche :input").saveErwSuche();
				//mark expertensuche -Fields in contrast to EES Fields
				$("#expertensuche :input").addClass("esuche1");
				$("#erweiterteexpertensuche :input").removeClass("esuche1");

				// Suche soll immer geschlossen bleiben: CR vom 27.1.2010
				$(this).attr("value", "false");
	 			var isOpen = $(this).attr("value");

//alert("initErwSuche isOpen="+isOpen);																
				if(isOpen == "true" ) { // erweiterte Suche offen lassen und Parameter setzen
						// an alle Navigatorenlinks Parameter &isOpenErwSuche=true anh�ngen
						$("#navigatoren a").handleLinks("expand" , "link");
						// an die Links der filterSection Parameter &isOpenErwSuche=true anh�ngen
						$("#filterSection a").handleLinks("expand" , "link");
						// an die Links in den Selectboxen Parameter &isOpenErwSuche=true anh�ngen
						$("select option").handleLinks("expand" , "select");
						// Link ausblenden
						$("#erweiterteSucheLink a").hide(); 
//alert("handleMessage init:hide");			
						// erweiterte Suche - versteckte Suchkriterien ausblenden
						$("#isInitValues").handleMessage("#erwsucheStatus" , "hide" );

				}	else { // erweiterte Suche ausblenden
					$("#effect").hide();

					var isInitValues = $("#isInitValues").attr("value");
//alert("handleMessage init: show - isInitValues="+isInitValues + " eesIsInitValues = "+eesIsInitValues);		
					if(eesIsInitValues == "true") {
						// erweiterte Suche - versteckte Suchkriterien einblenden
//alert("handleMessage: show");						
						$("#isInitValues").handleMessage( "#erwsucheStatus" , "show" );
					}	
				}	
				
			});
		};	


//////////////////////////////////////////////
//	Plugin: restoreErwSuche()
//	restores values in Expanded Expert Search (EES)
//	needs attribute initValue to be set in tags
		$.fn.restoreErwSuche = function() {
			return this.each(function() {
					var type = this.type; 
					var initVal = $(this).attr("initValue");
					if(type == "text" ) {
							 $(this).attr("value", initVal);
					}
					if(type == "checkbox") {
							 this.checked = initVal;
					}
			});
		};

//////////////////////////////////////////////
//	Plugin: saveErwSuche()
//	saves the initial values of Expanded Expert Search (EES) to attribute initValue in Tags
//	sets attribute initValue into all tags which has intial value other then "" or false

		$.fn.saveErwSuche = function() {
				$("#isInitValues").attr("value", "false");
				return this.each(function() {
						var type = this.type; 
						if(type == "text" && this.value.length > 0) {
//alert("Field ["+this.name+"],["+type+"], ["+this.value+"]");							
								$(this).attr("initValue", this.value);
								$("#isInitValues").attr("value", "true");
						}
						if(type == "checkbox" && this.checked == true) {
//alert("Field ["+this.name+"],["+type+"], ["+this.checked+"]");									
					 			$(this).attr("initValue", this.checked);
 					 			$("#isInitValues").attr("value", "true");
						}
				});
		};
		
//////////////////////////////////////////////
//		Plugin: setHeight()
//		saves the initial values of Expanded Expert Search (EES) to attribute initValue in Tags
//		sets attribute initValue into all tags which has intial value other then "" or false

		$.fn.setHeight = function() {
			return this.each(function() {
				// EES always must hide map
				var eesHeight = $("#effect").height();
				var mapHeight = $(".map").height() + 95;
				if(mapHeight > eesHeight) {
					$("#effect").attr("height" , mapHeight );
					$("#effect").css("height" , mapHeight );
				}
			});
		};	
		


//////////////////////////////////////////////
//	Prototype-Methode zu Stringobjekt: toLocaleFormat()
//	formats numbers to 1.000.000 

		String.prototype.toLocaleFormat = function() {
		    var n = this;
		    ns = String(n).replace('.', ',');
		    var w = [];
		    while (ns.length > 0) {
		        var a = ns.length;
		        if (a >= 3) {
		            s = ns.substr(a - 3);
		            ns = ns.substr(0, a - 3);
		        } else {
		            s = ns;
		            ns = "";
		        }
		        w.push(s);
		    }
		    for (i = w.length - 1; i >= 0; i--) {
		        ns += w[i] + ".";
		    }
		    ns = ns.substr(0, ns.length - 1);
		    return ns.replace(/\.,/, ',');
		}

		