/**
 * General triggered behaviour on document load
 */
var general_rules = {
    '.no_javascript' : function() {
      this.hide();
    },
    '.show_no_javascript' : function() {
      this.show();
    }
}
Event.addBehavior(general_rules);

/**
 * Rules for hyperlink behaviour
 */
var hyperlink_rules = {
    'a.void:click' : function(e) {
      return false;
    },

    'a.popup:click' : function(e) {

        /**
         * Event:  click
         * Action: open a popup window
         */
        var params = Element.getClassParameters(this);
        var width = params.width || 684;
        var height = params.height || 350;
        var top = params.top || 200;
        var left = params.left || '50%';
        window.open(this.href,
            'PopUp',
            'width=' + width + ',height=' + height + ',top=' + top + ',left=' + left + ',scrollbars=0,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;
        }
    },

    'a.status:mouseover' : function(e) {

        /**
         * Event:  mouseover
         * Action: display the hyperlinks title in the status bar
         */
        window.status=this.title;
        return true;
    },
    'a.status:mouseout' : function(e) {

        /**
         * Event:  mouseout
         * Action: clear the status bar
         */
        window.status='';
        return true;
    },

    '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();
        }
        return false;
    },

    'a.tabswitch:click' : function(e) {
        /**
        * Event: click
        * Action: hide related div, show related div's related element
        */
        var container = $(this.getAttribute('rel'));
        if(container) {
          var other = $(container.getAttribute('rel'));
          if (other) {
            container.addClassName('hide');
            other.removeClassName('hide');
          }
        }
        return false;
    },

    'a.submit:click' : function(e) {
        /**
        * Event: click
        * Action: submit the form given as a param. (used by captcha)
        */
        var params = Element.getClassParameters(this);
        var form = $(params.form);
        if (form){
          form.submit();
        }
        return false;
    },
    'a.print:click' : function(el) {
        /**
        * Event: click
        * Action: print the page
        */    
          window.print();
          return false;
    },
    'a.nav_ajax:click' : function(el) {
        var params = Element.getClassParameters(this);
        getJobs(params.categoryID,params.chunk,params.subcommand);
        return false;
    }   
}
Event.addBehavior(hyperlink_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();
    },

    '.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');
            }
        }
    },
    '.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');
    },
    '.auto_blur_multiple:focus' : function(el) {

      /**
       * This is the multiple variant of auto_blur. It will highlight the
       * label of replace the image. The id of the label must be the same
       * as the id of the field, minus the last underscore and digits.
       * Now you can do those fancy multiple fields with one label :)
       */
        this.className = this.className.replace('_off','_on');
        var fieldLabel = $(this.id.sub(/_\d+$/, '') + '_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');
            }
        }
    },
    '.auto_blur_multiple:blur' : function(el) {
        this.className = this.className.replace('_on','_off');
        var fieldLabel = $(this.id.sub(/_\d+$/, '') + '_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.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');
            }
        }
    }
}
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 table_rules = {
    'tr.tablerowhover:mouseover' : function(e) {
      /**
       * Event:  mouseover
       * Action: show hover version
       */
      this.immediateDescendants().each (function (el){
        Element.addClassName(el,'table_hover');
      });
  },
    'tr.tablerowhover:mouseout' : function(el) {
      /**
       * Event:  mouseout
       * Action: show normal version
       */
      this.immediateDescendants().each (function (el){
        Element.removeClassName(el,'table_hover');
      });       
  }
}
Event.addBehavior(table_rules);


var flash_rules = {
		
	'#flashheader' : function(el) {
		
        var classParams = Element.getClassParameters(this);
        var skipintro = classParams.skipintro || 0;        
        var FOheader= {
        movie:"/flash/header.swf",
        bgcolor: "#000000",
        width:"100%",
        height:"352",
        menu:"false",
        flashvars: "skipintro=" + skipintro,
        allowscriptaccess:"true",
        id:"flashheader_id",        
        xi:"false",
        majorversion:"7",
        build:"0"
        };
        UFO.create(FOheader,"flashheader");
    },
	'div.video' : function(el) {
        var params = Element.getClassParameters(this);
        var FOVideo = {
            movie:"/flash/videoplayer.swf?videoAutoPlay="+params.autoplay,
            width: params.width || "640",
            height: params.height || "400",
            flashvars: "flv=" + params.flv + "&amp;volume=100",
            menu:"false",
            wmode:"transparent",
            xi:"false",
            majorversion:"8",
            build:"0"
        };
        UFO.create(FOVideo,"videoplayer_mailing");
    }
}
Event.addBehavior(flash_rules);

function getJobs (categoryID,chunk,subcommand) {
    var params = 'categoryID=' + categoryID;
    params += '&chunk=' + chunk;
    params += '&subcommand=' +subcommand;
    params += '&command=getJobs';

    if (categoryID.startsWith('precies')){
        document.location.href='precies.php?categoryID=' + categoryID;
    } else if (categoryID.startsWith('advice')){
        document.location.href='advice.php?categoryID=' + categoryID;
    } else {
	    var myAjax = new Ajax.Updater('ajax_left', 'ajax.php', {
	        method: 'post',
	        parameters: params
	    });
    }
}


var google_maps_rules = {
	'div.googleMap': function(){
		var params = Element.getClassParameters(this);
		var latlng = new google.maps.LatLng(params.latitude,params.longitude);
		var myOptions = {
			zoom: 12,
			center: latlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		var map = new google.maps.Map(this, myOptions);
		
		var marker = new google.maps.Marker({
			position: latlng,
			map: map,
			title:"Hello World!"
		});
	}
	
// Ê Ê var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
//		if (GBrowserIsCompatible()) {
//			
//			var params = this.getClassParameters();
//			var options = {};
//			
//			if (params.latitude && params.longitude) {
//				options.startPoint = new GLatLng(params.latitude,params.longitude); 
//			}
//			
//			if (params.markerLatitude && params.markerLongitude) {
//				options.markerPoint = new GLatLng(params.markerLatitude,params.markerLongitude); 
//			}
//			
//			if (params.startZoom) {
//				options.startZoom = params.startZoom;
//			}
//			
//			if (params.markerImage && params.markerWidth && params.markerHeight) {
//				options.markerImage = params.markerImage;
//				options.markerWidth = params.markerWidth;
//				options.markerHeight = params.markerHeight
//			}
//			
//			if (params.scrollWheel) {
//				options.scrollWheel = true;
//			}
//			
//			options.panelElement = $('location_map_panel');
//			
//			map = new InteractiveMap(this, options);
//		}
//	}
}
Event.addBehavior(google_maps_rules);
