var evFn = function() {

  this.currentSlideShowId = 0;
  
  this.currentFloorplanId = 0;
  
  this.currentPreviewId = 0;
  
  //this.slideShow = new Array();
  
  this.dePropagate = function(event) {
  	if (window.event) {
  		window.event.cancelBubble = true;
  	}
  	if (event.stopPropagation) {
  		event.stopPropagation();
  	} 
  }
  
  this.showSms = function() {
      YAHOO.util.Dom.setStyle("sms", "display", "block");	
		var docHeight = YAHOO.util.Dom.getY("footer") + YAHOO.util.Dom.get("footer").offsetHeight + 10;
		YAHOO.util.Dom.setStyle("smsbg", "height", docHeight + "px");
  }
  
  this.closeSms = function() {
    YAHOO.util.Dom.setStyle("sms", "display", "none");
  }
  
  this.showVirtualTour = function() {
      YAHOO.util.Dom.setStyle("virtualtour", "display", "block");	
		var docHeight = YAHOO.util.Dom.getY("footer") + YAHOO.util.Dom.get("footer").offsetHeight + 10;
		YAHOO.util.Dom.setStyle("virtualtourbg", "height", docHeight + "px");
  }
  
  this.closeVirtualTour = function() {
    YAHOO.util.Dom.setStyle("virtualtour", "display", "none");
	}
  
  this.showSlideShow = function(id) { 
    YAHOO.util.Dom.setStyle("slideshowbg", "display", "block");	
		var docHeight = YAHOO.util.Dom.getY("footer") + YAHOO.util.Dom.get("footer").offsetHeight + 10;
		YAHOO.util.Dom.setStyle("slideshowbg", "height", docHeight + "px");
		YAHOO.util.Dom.setStyle("gallery", "display", "block");	
		YAHOO.util.Dom.setStyle("gallery", "height", docHeight + "px");
		this.currentSlideShowId = id;
		this.setActiveBigForGallery("big_" + (id + 1));
  }
  
  this.showFloorplan = function(id) { 
    YAHOO.util.Dom.setStyle("slideshowbg", "display", "block");	
		var docHeight = YAHOO.util.Dom.getY("footer") + YAHOO.util.Dom.get("footer").offsetHeight + 10;
		YAHOO.util.Dom.setStyle("slideshowbg", "height", docHeight + "px");
		YAHOO.util.Dom.setStyle("floorplan", "display", "block");	
		YAHOO.util.Dom.setStyle("floorplan", "height", docHeight + "px");
		this.currentFloorplanId = id;
		this.setActiveForFloorplan("floor_" + (id + 1));
  }
  
  this.showSiteplan = function(id) { 
    YAHOO.util.Dom.setStyle("slideshowbg", "display", "block");	
		var docHeight = YAHOO.util.Dom.getY("footer") + YAHOO.util.Dom.get("footer").offsetHeight + 10;
		YAHOO.util.Dom.setStyle("slideshowbg", "height", docHeight + "px");
		YAHOO.util.Dom.setStyle("siteplan", "display", "block");	
		YAHOO.util.Dom.setStyle("siteplan", "height", docHeight + "px");
		this.currentSiteplanId = id;
		this.setActiveForSiteplan("siteplan_" + (id + 1));
  }
  
  this.nextSiteplan = function() {
    this.showSiteplan((this.currentSiteplanId + 1) % totalSiteplanImages);
  }

  this.previousSiteplan = function() {
    this.showSiteplan((totalSiteplanImages + this.currentSiteplanId - 1) % totalSiteplanImages);
  }
  
  this.nextFloorplan = function() {
    this.showFloorplan((this.currentFloorplanId + 1) % totalFloorplanImages);
  }

  this.previousFloorplan = function() {
    this.showFloorplan((totalFloorplanImages + this.currentFloorplanId - 1) % totalFloorplanImages);
  }
  
  this.nextSlideShow = function() {
    this.showSlideShow((this.currentSlideShowId + 1) % totalSlideShowImages);
  }

  this.previousSlideShow = function() {
    this.showSlideShow((totalSlideShowImages + this.currentSlideShowId - 1) % totalSlideShowImages);
  }
  
  this.nextPreview = function() {
    this.showPreview((this.currentPreviewId + 1) % totalSlideShowImages);
  }

  this.previousPreview = function() {
    this.showPreview((totalSlideShowImages + this.currentPreviewId - 1) % totalSlideShowImages);
  }
  
  this.showPreview = function(id) {
    this.currentPreviewId = id;
    this.showImage(this.currentPreviewId);
    var mobile_current = YAHOO.util.Dom.get("mobile_current")
    if ( mobile_current ) {
      mobile_current.innerHTML = id + 1;
    }
  }
  
	this.setActiveBigForGallery = function(active) {
		var elements = YAHOO.util.Dom.getChildren("galleryImages");
		var imgcount = 0;
		var actImg = active.split("_")[1];
		for(var i=0;i<elements.length;i++) {
			if(elements[i].nodeName == "DIV") {
				imgcount++;
				YAHOO.util.Dom.removeClass(elements[i], "active");
			}
		}
		YAHOO.util.Dom.addClass(active, "active");
		var imageWidth = YAHOO.util.Dom.get(active).offsetWidth;
		YAHOO.util.Dom.setStyle("gallerynavi", "width", imageWidth + "px");
		document.getElementById("galleryAct").innerHTML = actImg;
		document.getElementById("galleryCount").innerHTML = imgcount;
		var imagePosRight = YAHOO.util.Dom.getX(active) + imageWidth;
	}
	
	
	this.setActiveForFloorplan = function(active) {
		var elements = YAHOO.util.Dom.getChildren("floorplanImages");
		var imgcount = 0;
		var actImg = active.split("_")[1];
		for(var i=0;i<elements.length;i++) {
			if(elements[i].nodeName == "DIV") {
				imgcount++;
				YAHOO.util.Dom.removeClass(elements[i], "active");
			}
		}
		YAHOO.util.Dom.addClass(active, "active");
		var imageWidth = YAHOO.util.Dom.get(active).offsetWidth;
		YAHOO.util.Dom.setStyle("floorplannavi", "width", imageWidth + "px");
		document.getElementById("floorplanAct").innerHTML = actImg;
		document.getElementById("floorplanCount").innerHTML = imgcount;
	}
	
	this.setActiveForSiteplan = function(active) {
		var elements = YAHOO.util.Dom.getChildren("siteplanImages");
		var imgcount = 0;
		var actImg = active.split("_")[1];
		for(var i=0;i<elements.length;i++) {
			if(elements[i].nodeName == "DIV") {
				imgcount++;
				YAHOO.util.Dom.removeClass(elements[i], "active");
			}
		}
		YAHOO.util.Dom.addClass(active, "active");
		var imageWidth = YAHOO.util.Dom.get(active).offsetWidth;
		YAHOO.util.Dom.setStyle("siteplannavi", "width", imageWidth + "px");
		document.getElementById("siteplanAct").innerHTML = actImg;
		document.getElementById("siteplanCount").innerHTML = imgcount;
	}
	
	this.showImage = function(id) {

		var bildID = "pic_" + (id+1);
		var images = YAHOO.util.Dom.getChildren("imagecontainer");
		for (var i=0; i<images.length;i++) {
			if(YAHOO.util.Dom.hasClass(images[i], "active")) {
				YAHOO.util.Dom.removeClass(images[i].id, "active");
			}
		}

		YAHOO.util.Dom.addClass(bildID, "active");

	}
	
	this.closeSlideShow = function() {
    YAHOO.util.Dom.setStyle("gallery", "display", "none");
    YAHOO.util.Dom.setStyle("slideshowbg", "display", "none");	
	}
	
		
	this.closeFloorplan = function() {
		YAHOO.util.Dom.setStyle("floorplan", "display", "none");
		YAHOO.util.Dom.setStyle("slideshowbg", "display", "none");	
	}
	
	this.closeSiteplan = function() {
		YAHOO.util.Dom.setStyle("siteplan", "display", "none");
		YAHOO.util.Dom.setStyle("slideshowbg", "display", "none");	
	}
	
  this.openGalleryPrint = function(id) {
    /*var printImageSrc = YAHOO.util.Dom.get("img_" + (id + 1)).src;*/
    var url = gallerylink + "&imageDisplayPath=" + imagePathes[id];
    openFlexWindow(url,'zoom','resizable=yes,scrollbars=yes',800, 600,'true');
  }
   
  this.setSuggestMode = function(mode) {
    var search = document.getElementById('search');
    YAHOO.util.Dom.removeClass(search, "userdefined");
    YAHOO.util.Dom.removeClass(search, "suggested");
    if (suggestMode && !mode) {
      search.value = '';
      suggestMode = false;
    }
    if (mode && (suggestMode || search.value == '')) {
      search.value = suggestions[suggestArea];
      suggestMode = true;
    }
    YAHOO.util.Dom.addClass(search, suggestMode ? "suggested" : "userdefined");
  }
  
};

function evSlider (sliderId, facetMin, facetMax, selectedMin, selectedMax, url, locale, factor)  {

	this.MAX_STEPS = 200;
	
    // slider id
	this.sid='#'+sliderId;
	// id of input field min
	this.sid_min = this.sid+'_min';
	// id of input field max
	this.sid_max = this.sid+'_max';
	// id of label field min
	this.sid_min_label = this.sid+'_min_label';
	// id of label field max
	this.sid_max_label = this.sid+'_max_label';
	
	// id of selected info field min
	this.sid_selected_min_info = this.sid+'_selected_min_info';
	// id of seleced info field max
	this.sid_selected_max_info = this.sid+'_selected_max_info';
	
	this.url=url;
	this.locale=locale;
	
	var self = this;
	
	this.calculateStepSize = function(min,max){
	
		var stepSizes = new Array(1,2,5);
        var diff = max-min;
        var base =1;

        if (diff<=this.MAX_STEPS){
            return 1;
        }

        // quite sure it will step at some point...
        while (true){

            for (i=0;i<stepSizes.length; ++i){
				s = stepSizes[i];
            	currentStepSize = base * s;
                if (currentStepSize*this.MAX_STEPS>=diff){
                    return currentStepSize;
                }
            }
            base=base*10;
        }
	}
	
	this.factor=factor;
	
	this.stepSize= this.calculateStepSize(Math.floor(facetMin*factor),Math.ceil(facetMax*factor));
	
	this.calculatedMin = Math.floor(facetMin*factor/this.stepSize)*this.stepSize;
	this.calculatedMax = Math.ceil(facetMax*factor/this.stepSize)*this.stepSize;
	
	this.displayedMin = selectedMin ==-1 ? self.calculatedMin : Math.round(selectedMin*self.factor);
	this.displayedMax = selectedMax ==-1 ? self.calculatedMax : Math.round(selectedMax*self.factor);
	
	this.init = function(){
	  
	  $( this.sid ).slider({
			range: true,
			min: self.calculatedMin,
			max: self.calculatedMax,
			step: self.stepSize,
			values: [ self.displayedMin , self.displayedMax ],
			
			slide: function( event, ui ) {
				self.setInputMin( ui.values[ 0 ] );
				self.setInputMax( ui.values[ 1 ] );
			},
			stop: function (event, ui){
				self.goTo(ui.values[0],ui.values[1]);
			}
		});
		$(this.sid_min_label).text(this.formatNumber(this.calculatedMin));
		$(this.sid_max_label).text(this.formatNumber(this.calculatedMax));
		$( this.sid ).addTouch();

		this.setInputMin(this.getSliderMin());
        this.setInputMax(this.getSliderMax());
        

	  	$(this.sid_min).keypress(function(e){
	  		if(e.which == 13){
	  			self.setMinFromInput();
	  			self.gotoWithSliderValues();
	  		}
	  	});
	  	
	  	$(this.sid_min).focusout(function(e){
	    	self.setMinFromInput();
	  	});

	  
	  	$(this.sid_max).keypress(function(e){
	  		if(e.which == 13){
	  			self.setMaxFromInput();
	  			self.gotoWithSliderValues();
	  		}
	  	});
	  $(this.sid_max).focusout(function(e){
	  		self.setMaxFromInput();
	  		self.gotoWithSliderValues();
	  });
	}
	
	this.setMinFromInput = function (){
		var inputMin = this.getInputMin()
		if (inputMin > this.getSliderMax() || inputMin < this.getSliderMin()) {
		  inputMin = this.getSliderMin();
		}
  	this.setSliderMin(inputMin);
  	this.setInputMin(inputMin);
	}
	
	this.setMaxFromInput = function (){
    var inputMax = this.getInputMax()
    if (inputMax < this.getSliderMin() || inputMax > this.getSliderMax()) {
      inputMax = this.getSliderMax();
    }
    this.setSliderMax(inputMax);
    this.setInputMax(inputMax);
	}
	
	this.getSliderMin = function (){
		return this.getSliderVal(0);
	}

	this.getSliderMax = function (){
		return this.getSliderVal(1);
	}

	this.getSliderVal = function(index){
		return $( this.sid ).slider( "values", index);
	}
	
	this.setSliderMin = function (value){
		this.setSliderVal(0,value);
	}

	this.setSliderMax = function (value){
		this.setSliderVal(1,value);
	}
	
	this.setSliderVal = function (index,value){
		$( this.sid ).slider( "values", index , value);
	}
	 	
	this.getInputMin = function (){
		return $( this.sid_min ).parseNumber({format:"#,###",locale:this.locale});
	}

	this.getInputMax = function (){
		return $( this.sid_max ).parseNumber({format:"#,###",locale:this.locale});
	}

	this.setInputMin = function (value){
		$( this.sid_min ).val(this.formatNumber(value));
		$( this.sid_selected_min_info ).text(this.formatNumber(value));
	}

	this.setInputMax = function (value){
		$( this.sid_max ).val(this.formatNumber(value));
		$( this.sid_selected_max_info ).text(this.formatNumber(value));
	}

	this.gotoWithSliderValues = function(){
		this.goTo(this.getSliderMin(),this.getSliderMax());
	}
	
	this.goTo = function(min,max) {
		reconvertedMin=Math.floor(min/factor);
		reconvertedMax=Math.ceil(max/factor);
		/* 
		* Das mit dem Page-Reload lassen wir mal lieber, wenn es keine Änderung gab.
		*/
		if (reconvertedMin == this.displayedMin && reconvertedMax == this.displayedMax) {
		  return;
		}
		/*
		* Das mit dem Page-Reaload lassen wir auch bei sinnlosen Eingaben.
		*/
		if ( reconvertedMin > reconvertedMax ) {
		  this.setInputMin(this.displayedMin);
		  this.setInputMax(this.displayedMax);
		  return;
		}
		var replacedUrl=this.url.replace("%23min%23",reconvertedMin).replace('%23max%23',reconvertedMax);
  		document.location.assign(replacedUrl);
	}
	this.formatNumber = function(value){
		if (value==0){
			return 0;
		}
		return $.formatNumber(value,{format:"#,###",locale:this.locale});
	}
}

var eundvJSFN = function() {
	
	var openSubform = function(e) {
		YAHOO.util.Event.preventDefault(e);
		//var subformID = "sub_" + YAHOO.util.Event.getTarget(e).id;
		//if (YAHOO.util.Dom.getStyle(subformID, "display") == "block") {
		//} else {
			hideSubforms();
			//YAHOO.util.Dom.setStyle(subformID, "display", "block");
			YAHOO.util.Event.getTarget(e).blur();
			YAHOO.util.Dom.addClass(YAHOO.util.Event.getTarget(e).parentNode, "active");
		//}
	}
	
	var hideSubforms = function() {
		//var subforms = YAHOO.util.Dom.getElementsByClassName("subform");
		//YAHOO.util.Dom.setStyle(subforms, "display", "none");
		var navpoints = YAHOO.util.Dom.getChildren("businessareas");
		YAHOO.util.Dom.removeClass(navpoints, "active");
	}
	
	
	var init = function() {
		
		if(YAHOO.util.Dom.get("businessareas")) {
			varlistitems = YAHOO.util.Dom.getChildren("businessareas");
			for(var i=0;i<listitems.length;i++) {
			    if ( !YAHOO.util.Dom.hasClass(listitems[i], "realLink") ) {
				YAHOO.util.Event.addListener(YAHOO.util.Dom.getChildren(listitems[i])[0], "click", openSubform)
				}
			}
		}
		
	}
	
	
	
	YAHOO.util.Event.addListener(window, "load", init);

	
}

var eundvJS = new eundvJSFN();

var ev = new evFn();


