/**
 * General triggered behaviour on document load
 */
 
Event.addBehavior.reassignAfterAjax = true;

var general_rules = {
    '.hilite_keywords' : function() {
       var hiliter = new KeywordHiliter("ajax.php",this);
       hiliter.options.elementTag = 'abbr';
       hiliter.options.elementClassName = 'intellilink';
       hiliter.options.useGetFunction = 'listKeywords';
       hiliter.options.elementClass = true;
       hiliter.options.useTitle = false;
       hiliter.options.useItemID = true;
       hiliter.fetchKeywords();
    },
    '.no_javascript' : function() {
      this.hide();
    },
    '.show_no_javascript' : function() {
      this.show();
    }
}
Event.addBehavior(general_rules);

/**
 * Rules for hyperlink behaviour
 */
var hyperlink_rules = {

    'a.popup:click' : function(e) {

        /**
         * Event:  click
         * Action: open a popup window
         */
        var params = Element.getClassParameters(this);
        var width = params.width || 300; //280;
        var height = params.height || 510; //420;
        var top = params.top || 200;
        var left = params.left || '50%';
        window.open(this.href,
            'PopUp',
            'width=' + width + ',height=' + height + ',top=' + top + ',left=' + left + ',scrollbars=1,status=no,resizable=0,toolbar=0,titlebar=0,menubar=0,location=0');
        return false;
    },
    'a.external:click' : function(e) {

        /**
         * Event:  click
         * Action: open a new window
         */
        if (!e.ctrlKey && !e.altKey && !e.shiftKey) {
          window.open(this.href);
          return false;
        }
    },
    'form.external:submit' : function(e) {

        /**
         * Event:  submit
         * Action: open a new window
         */
        if (!e.ctrlKey && !e.altKey && !e.shiftKey) {
          this.target = 'FormPopUp';
          window.open(this.href,'FormPopUp');
          return true;
        }
    },
    'a.popup_flash:click' : function(e) {

        /**
         * Event:  click
         * Action: open a popup window
         */
        window.open(this.href,
            'PopUp_flash',
            'width=720,height=576,top=200,left=50%,scrollbars=0,status=no,resizable=0,toolbar=0,titlebar=0,menubar=0,location=0');
        return false;
    },

    'a.status:mouseover' : function(e) {

        /**
         * Event:  mouseover
         * Action: display the hyperlinks title in the status bar
         */
        window.status=this.title;
        return true;
    },
    'a.status:mouseover' : function(e) {

        /**
         * Event:  mouseout
         * Action: clear the status bar
         */
        window.status='';
        return true;
    },
    'a.category_image:mouseover' : function(e) {

        /**
         * Event:  mouseover
         * Action: change link and image for sub sub category
         */
        var params = Element.getClassParameters(this);
        if(params.subcategory > 0 && params.src){
            $('category_link_' + params.subcategory).href = this.href;
            $('category_link_' + params.subcategory).title = this.title;
            $('category_image_' + params.subcategory).alt = this.title;
            $('category_image_' + params.subcategory).src = params.src;
        }
        return true;
    },
    'a.details_tab:click' : function(e) {
        /**
         * Event:  click
         * Action: 1. show / hide the tab with the same ID minus "_tab" and close all other tabs,
         * Action: 2. give active class to clicked tab_header and 'inactivate' others
         */
        var inactivate_tabs = document.getElementsByClassName('details_tab');
        if(inactivate_tabs.length > 0){
            for(i=0;i < inactivate_tabs.length;i++){
                Element.removeClassName(inactivate_tabs[i],'active');
            }
        }
        var tabs = document.getElementsByClassName('tabs');
        if(tabs.length > 0){
            for(i=0;i < tabs.length;i++){
                Element.addClassName(tabs[i],'hidden')
                //tabs[i].style.display = 'none';
            }
        }
        var c = $(this.id.replace('_tab',''));
        if (c) {
            Element.removeClassName(c,'hidden')
            //Element.show(c);
        }
        Element.addClassName(this,'active');
        return false;
    },
    'a.switch:click': function(e) {
        /**
         * Event:  click
         * Action: show / hide the tab with the same ID minus "_switch"
         */
        var c = $(this.id.replace('_switch',''));
        var content = $(c.id + '_content');
        if (c) {
          c.toggle();
          //new Effect.Fade(c);
        }
        return false;
    },
    'a.zoomswitch:click': function(e) {
        /**
         * Event:  click
         * Action: show / hide the tab with the same ID minus "_switch"
         */
        var c = $(this.id.replace('_switch',''));
        if (c) {
        	c.toggle();
        	return false;
        }
        
    },
    'a.product_infographics_close:click': function(e) {
        /**
         * Event:  click
         * Action: show / hide the infographic
         */
        var infographic = $('product_infographics')
        if (infographic) {
          infographic.hide();
        }
        var filter_form = $('form_product_filter')
        if (filter_form) {
          filter_form.style.visibility = 'visible';
        }
        return false;
    },
    'a.product_infographics_switch:click': function(e) {
				/**
         * Event:  click
         * Action: show the infographic DIV and fill with Ajax updated content
         */
        var container = $('product_infographics');
        Element.setClassParameter(container,'category',Element.getClassParameter(this,'category'));
        var content = $('product_infographics_content');
        if (content) {
          content.hide();
        }

				var params = Element.getClassParameters(this);
        var updater = new Ajax.Updater(
          container,
          'ajax.php',
          {
            parameters: {
              action: 'getInfographic',
              ic: params.category,
			  			language: params.language
            },
            onComplete: function(transport) {
              var filter_form = $('form_product_filter')
              if (filter_form) {
                filter_form.style.visibility = 'hidden';
              }
              new Effect.toggle($('product_infographics'));
            }
          }
        );
        window.location = '#header_back';
        return false;
    },
    'a.product_infographics_infopoint:click': function(e) {
        /**
         * Event:  click
         * Action: show the infographic DIV and fill with Ajax updated content
         */
        var container = $('product_infographics');
        var params = Element.getClassParameters(this);
        var updater = new Ajax.Updater(
          container,
          'ajax.php',
          {
            parameters: {
              action: 'getInfographic',
              ic: params.category,
              ip: params.infopoint || '',
			  language: params.language
            }
          }
        );
        return false;
    }
}
Event.addBehavior(hyperlink_rules);

/**
 * Rules for intellilinks
 */
var intellilink_rules = {
    'abbr.intellilink:mouseover': function(e) {
        /**
         * Event:  mouseover
         * Action: show the intellilink DIV and fill with title text
         */
        var container = $('tooltip');
        var params = Element.getClassParameters(this);
        var containerItemID = Element.getClassParameter(container,'itemID');
        /*
         * If already loaded, just show in the right place...
         */
        container.setStyle({'top': (Position.cumulativeOffset(this)[1] + 20) + 'px','left': (Position.cumulativeOffset(this)[0] + 15) + 'px'});
        if (containerItemID == params.itemID) {
          container.show();
        } else {
          container.removeClassName('param-itemID=' + containerItemID);
          container.innerHTML = '<p>laden...</p>';
          container.show();
          var updater = new Ajax.Updater(
            container,
            'ajax.php',
            {
              parameters: {
                action: 'getTooltip',
                itemID: params.itemID
              },
              onSuccess: (function(transport) {
                Element.addClassName(this,'param-itemID=' + params.itemID);
              }).bind(container)
            }
          );
        }
        return false;
    },
    'abbr.intellilink:mouseout': function(e) {
        /**
         * Event:  mouseover
         * Action: show the intellilink DIV and fill with title text
         */
        $('tooltip').hide();
    }
}
Event.addBehavior(intellilink_rules);

/**
 * Behaviour rules for form elements
 */

var form_rules = {
    'form.auto_submit' : function(e) {
      this.submit();
    },
    'select.auto_submit:change' : function(e) {
      this.form.submit();
    },
    /**
     * Event:  blur
     * Action: convert field value to upper case
     */
    'input.auto_upper:blur' : function(e) {
      this.value = this.value.toUpperCase();
    },

    /**
     * Event:  change
     * Action: convert field value to upper case
     */
    'input.auto_upper:change' : function(e) {
      this.value = this.value.toUpperCase();
    },

    'input.auto_blur:focus' : function(e) {

      /**
       * Event:  focus
       * Action:
       *   - replace field's classname from "_off" to "_on"
       *   - if labeled, replace label's classname from "_off" to "_on"
       *   - if label is image, replace image with "_hover" version
       */
        this.className = this.className.replace('_off','_on');
        var fieldLabel = $(this.id + '_label');
        if (fieldLabel) {
            fieldLabel.className = fieldLabel.className.replace('_off','_on');
            var image = fieldLabel.getElementsByTagName('img')[0];
            if (image) {
                image.src = image.src.replace('_normal','_hover');
            }
        }
    },
    'input.auto_blur:blur' : function(e) {

        /**
         * Event:  blur
         * Action:
         *   - replace field's classname from "_on" to "_off"
         *   - if labeled, replace label's classname from "_on" to "_off"
         *   - if label is image, replace image with "_normal" version
         */
        this.className = this.className.replace('_on','_off');
        var fieldLabel = $(this.id + '_label');
        if (fieldLabel) {
              fieldLabel.className = fieldLabel.className.replace('_on','_off');
            var image = fieldLabel.getElementsByTagName('img')[0];
            if (image) {
                image.src = image.src.replace('_hover','_normal');
            }
        }
    },
    'input.auto_clear:focus' : function(e) {

        /**
         * Event:  focus
         * Action:
         *   - clear value
         *   - remove auto_clear class
         */
        if (Element.hasClassName(this,'auto_clear')) {
            this.value='';
        }
        Element.removeClassName(this,'auto_clear');
    },
    'input.rollover:mouseover' : function(el) {

        /**
         * Event:  mouseover
         * Action:
         *   - if not "active", replace classname by "_hover" classname
         *   - if type is "image", replace image by "_hover" version
         */
        if (!Element.hasClassName(this,'active')) {
            this.className = this.className.replace('_normal','_hover');
            if (this.type == 'image') {
                this.src = this.src.replace('_normal','_hover');
            }
        }
    },
    'input.rollover:mouseout' : function(e) {
        /**
         * Event:  mouseout
         * Action:
         *   - replace classname by "_hover" classname
         *   - if type is "image", replace image by "_hover" version
         */
        if (!Element.hasClassName(this,'active')) {
            this.className = this.className.replace('_hover','_normal');
            if (this.type == 'image') {
                this.src = this.src.replace('_hover','_normal');
            }
        }
    },
    'input.details_tab:click' : function(e) {
        var inactivate_tabs = document.getElementsByClassName('details_tab');
        if(inactivate_tabs.length > 0){
            for(i=0;i < inactivate_tabs.length;i++){
                Element.removeClassName(inactivate_tabs[i],'active');
            }
        }
        var tabs = document.getElementsByClassName('tabs');
        if(tabs.length > 0){
            for(i=0;i < tabs.length;i++){
                Element.addClassName(tabs[i],'hidden');
//                tabs[i].style.display = 'none';
            }
        }
        var c = $(this.id.replace('_tab',''));
        if (c) {
            Element.removeClassName(c,'hidden')
//            Element.show(c);
        }
        Element.addClassName(this,'active');
        return false;

    },
    'input.compare:click': function(e) {
        /**
         * Event:  click
         * Action: set or unset product itemID for comparison as hidden field in chunk_nav form
         */
        if(this.checked){
          var chunk_nav = $('chunk_nav');
          var chunk_nav2 = $('chunk_nav2');
          // add hidden field
          new Insertion.After(chunk_nav, '<input type="hidden" id="hidden_'+this.value+'" class="compare_product" name="compare_products['+this.value+']" value="'+this.value+'" />');
          new Insertion.After(chunk_nav2, '<input type="hidden" id="hidden2_'+this.value+'" class="compare_product" name="compare_products['+this.value+']" value="'+this.value+'" />');
        } else {
          // remove hidden field
          $('hidden_'+ this.value).remove();
          $('hidden2_'+ this.value).remove();
        }
    },
    'input.submit_compare:click': function(e) {
        /**
         * Event:  click
         * Action: set or unset product itemID for comparison as hidden field in chunk_nav form
         */
        var hidden_c = $('c');
        var hidden_compare_products = document.getElementsByClassName('compare_product');
        // add hidden fields
        if(hidden_compare_products.length > 0){
            for(i=0;i < hidden_compare_products.length;i++){
                new Insertion.After(hidden_c, '<input type="hidden" id="hidden_'+hidden_compare_products[i].value+'" name="compare_products['+hidden_compare_products[i].value+']" value="'+hidden_compare_products[i].value+'" />');
            }
        }
    },
    'input.manuals:keyup': function(e) {
	    /**
	    * Event: change
	    * Action: find all manuals with the keyword (min.length 3)
	    **/
        if (this.value.length > 2){
            var container = $('show_manuals');
            var updater = new Ajax.Updater(
                container,
                'ajax.php',
                {
                    parameters: {
                        action: 'getManuals',
                        model: this.value
                    }
                }
            );
        }
        return false;
    },
	'select#location_form_country:change' : function(e) {
		var container = $('category_selector');
            var updater = new Ajax.Updater(
                container,
                'ajax.php',
                {
                    parameters: {
                        action: 'getDealerCategories',
                        country: this.value
                    }
                }
            );
    },
	'input.dataSelector:click': function(e) {
	    
	    var openSet = this.getValue();
	    $$('.dataset').each(function(el){
	    	el.hide();
	    });
        if(openSet == "companydataset"){
       		$('companydataset').show();
       		$$('.certificate-fields').each(Element.hide);
        } else {
        	$$('.certificate-fields').each(Element.show);
        }
        if(openSet == "consumerdataset"){
        
        }
        if(openSet == "dealerdataset"){
        	$('dealerdataset').show();
        }
        
    }
}
Event.addBehavior(form_rules);

var rollover_rules = {
    'img.rollover:mouseover' : function(el) {
      /**
       * Event:  mouseover
       * Action: show hover version
       */
    this.src = this.src.replace('_normal','_hover');
    return false;

  },
    'img.rollover:mouseout' : function(el) {
      /**
       * Event:  mouseout
       * Action: show normal version
       */
    this.src = this.src.replace('_hover','_normal');
    return false;
  }
}
Event.addBehavior(rollover_rules);
var flash_rules = {
	'#flash-besparing' : function() {
    var params = Element.getClassParameters(this);
    var FO = {
          movie:"/flash/liebherr_besparing_spotlight.swf",
          width:"310",
          height:"280",
          menu:"false",
          flashvars: "pageID="+params.pageID,
          wmode:"transparent",
          xi:"false",
          majorversion:"8",
          build:"0"
    };
    UFO.create(FO,this.id);
  },
	'#product_infographics_content' : function() {
      var params = Element.getClassParameters(this);
      var FO = {
            movie:"/flash/info_graphics.swf",
            width:"870",
            height:"470",
            menu:"false",
            flashvars: "gatewayUrl=http://" + params.server + "/flashservices/gateway.php&infoGraphicId=" + params.infographic + "&language=" + params.language,
            wmode:"transparent",
            xi:"false",
            majorversion:"8",
            build:"0"
      };
      UFO.create(FO,this.id);
    },
    '#flash_animation_home' : function() {
      var params = Element.getClassParameters(this);
      var FO = {
            movie:"/flash/header_homepage.swf",
            width:"1000",
            height:"302",
            menu:"false",
            flashvars: "gatewayUrl=http://" + params.server + "/flashservices/gateway.php&language=" + params.language,
            wmode:"transparent",
            xi:"false",
            majorversion:"8",
            build:"0"
      };
      UFO.create(FO,this.id);
    },
    '#flash_animation_home_selector' : function() {
      var params = Element.getClassParameters(this);
      var FO = {
            movie:"/flash/header_homepage_selector.swf",
            width:"1000",
            height:"302",
            menu:"false",
            flashvars: "gatewayUrl=http://" + params.server + "/flashservices/gateway.php&language=" + params.language,
            wmode:"transparent",
            xi:"false",
            majorversion:"8",
            build:"0"
      };
      UFO.create(FO,this.id);
    },
    '#flash_animation_next' : function() {
      var params = Element.getClassParameters(this);
      var FO = {
            movie:"/flash/header_nextpage.swf",
            width:"1000",
            height:"172",
            menu:"false",
            flashvars: "language=" + params.language,
            wmode:"transparent",
            xi:"false",
            majorversion:"8",
            build:"0"
      };
      UFO.create(FO,this.id);
    },
    '#homedialog' : function() {
      var FO = {
            movie:"/flash/homedialog.swf",
            width:"720",
            height:"576",
            menu:"false",
            wmode:"opaque",
            xi:"false",
            majorversion:"6",
            build:"0"
      };
      UFO.create(FO,this.id);
    },
    '#flash_demo' : function() {
      var FO = {
            movie:"/flash/demo/start.swf",
            width:"720",
            height:"576",
            menu:"false",
            wmode:"transparent",
            xi:"false",
            majorversion:"6",
            build:"0"
      };
      UFO.create(FO,this.id);
    },
  '#flash_demo_biofresh' : function() {
      var FO = {
            movie:"/flash/demo/start_biofresh.swf",
            width:"720",
            height:"576",
            menu:"false",
            wmode:"transparent",
            xi:"false",
            majorversion:"6",
            build:"0"
      };
      UFO.create(FO,this.id);
    },
  '#flash_demo_biofresh_function' : function() {
      var FO = {
            movie:"/flash/demo/biofreshfunction.swf",
            width:"388",
            height:"388",
            menu:"false",
            wmode:"opaque",
            xi:"false",
            majorversion:"6",
            build:"0"
      };
      UFO.create(FO,this.id);
    },
  '#flash_demo_klimaat' : function() {
      var FO = {
            movie:"/flash/demo/klimaatklasse.swf",
            width:"720",
            height:"576",
            menu:"false",
            wmode:"transparent",
            xi:"false",
            majorversion:"6",
            build:"0"
      };
      UFO.create(FO,this.id);
    },
    'div.besparingOUD' : function() {
        var params = Element.getClassParameters(this);
        var FO = {
          movie:"/flash/besparing.swf",
          width: "600",
          height: "430",
          menu:"false",
          flashvars: "language=" + params.language +"&baseUrl=/flash/xml/besparing.xml",
          xi:"false",
          majorversion:"9",
          build:"0"
        }
        UFO.create(FO,this.id);
    },
    'div#besparing' : function() {
    	
        var params = Element.getClassParameters(this);
        var FO = {
          movie:"/flash/besparing_2010.swf",
          width: "620",
          height: "530",
          menu:"false",
          flashvars: "language=" + params.language + "&apiUrl=" + params.apiUrl + "&baseUrl=/flash/xml/besparing_2010.xml&trackingUrl="+ params.trackingUrl,
          xi:"false",
          majorversion:"9",
          build:"0"
        }
        UFO.create(FO,this.id);
    },
    'div#image_zoom2' : function() {
        var params = Element.getClassParameters(this);
        var FO = {
          movie:"/flash/imagezoom.swf",
          width: "800",
          height: "800",
          menu:"false",
          flashvars: "lan=" + params.language + "&mod=" + params.model + "&img=" + params.imgUrl,
          xi:"false",
          majorversion:"7",
          build:"0"
        }
        UFO.create(FO,this.id);
    },
	'#imagezoom:click' : function(e) {
		e.stop();
		var dialog = new pH8.Dialog.getInstance({
			dialogId : 'liebDialog'
		});
		
		var params = Element.getClassParameters(this);
		dialog.insertContent('<div id="flashcontainer"><img src="' + params.imgUrl + '"/></div>');
		var FO = {
          movie:"/flash/photozoom.swf",
          width: "960",
          height: "675",
          menu:"false",
          flashvars: "lan=" + params.language + "&mod=" + params.model + "&img=" + params.imgUrl,
          xi:"false",
          majorversion: "9",
          allowScriptAccess: "always",
          allowfullscreen: "false",
          build:"0",
		  wmode:"window",
		  name:"photozoom",
		  id: "photozoom"
        }
        UFO.create(FO,'flashcontainer');
		dialog.show();
	}
}
Event.addBehavior(flash_rules);
/*
 * Rules for googlemaps
 * The whole div got one clickhandler, listClickHandler.
 */
var google_maps_rules = {
    '#location_map': function(){
    	var params = Element.getClassParameters(this);
        if (GBrowserIsCompatible()) {
          var map = new GMap2(this);      
          map.language = params.language;            
          //map.moveEndEventStop = true;  	
          map.openInfoWindowStatus = false;    
    	    var geocoder = new GClientGeocoder;
	        geocoder.getLatLng(map.getAddress(), map.createOverlay.bind(map));
	        Event.observe('location_results', 'click', map.listClickHandler.bindAsEventListener(map));
        }
    }  
}
Event.addBehavior(google_maps_rules);
