/**
 * General triggered behaviour on document load
 */
var general_rules = {
    '#onestat_placeholder' : function() {
        var d=document;
        var sid=Element.getClassParameter(this,'sid');
        var CONTENTSECTION="";
        var CUSTOMDATA="";
        var osp_URL=d.URL;
        var osp_Title=d.title;
        var t=new Date();
        var p="http"+(d.URL.indexOf('https:')==0?'s':'')+"://stat.onestat.com/stat.aspx?tagver=2&sid="+sid;
        p+="&url="+escape(osp_URL);
        p+="&ti="+escape(osp_Title);
        p+="&section="+escape(CONTENTSECTION);
        p+="&custom="+escape(CUSTOMDATA);
        p+="&rf="+escape(document.referrer);
        p+="&tz="+escape(t.getTimezoneOffset());
        p+="&ch="+escape(t.getHours());
        p+="&js=1";
        p+="&ul="+escape(navigator.appName=="Netscape"?navigator.language:navigator.userLanguage);
        if(osp_URL!=d.URL) p+="&ol="+escape(d.URL);
        if(typeof(screen)=="object"){
           p+="&sr="+screen.width+"x"+screen.height;p+="&cd="+screen.colorDepth;
           p+="&jo="+(navigator.javaEnabled()?"Yes":"No");
        }
        this.innerHTML='<img id="ONESTAT_TAG" width="1" height="1" src="'+p+'" >';
    },
    '.hilite_keywords' : function() {
        
       var hiliter = new KeywordHiliter("ajax.php",this);
       hiliter.options.elementTag = 'SPAN';
       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.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' : 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;
    }
}
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();
    },
    'input.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');
            }
        }
    },
    '.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.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');
            }
        }
    },
    'input.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;
  },
    'img.gray_rollover:mouseover' : function(el) {
      /**
       * Event:  mouseover
       * Action: show hover version
       */       
    this.src = this.src.replace('_gray','_normal');
    return false;

  },
  'img.gray_rollover:mouseout' : function(el) {
    /**
     * Event:  mouseout
     * Action: show normal version
     */
    this.src = this.src.replace('_normal','_gray');
    return false;
  },
  '.gallery_thumbs_rollover:mouseover' : function(e) {
    /**
     * Event:  mouseover
     * Action: show related element, hide related element's siblings
     */
    var link = e.findElement('A');
    if (link) {
      var rel = $(link.getAttribute('rel'));
      if (rel) {
        rel.removeClassName('hidden');
        $A(rel.siblings()).each(function(value) {
          value.removeClassName('hidden');
          value.addClassName('hidden');
        });
      }
    }
  },
  '.gallery_thumbs_rollover:click': function(e){
    /**
     * Event:  click
     * Action: do nothing
     */
    return false;
  }
}
Event.addBehavior(rollover_rules);

/**
 * Rules for intellilinks
 */
var intellilink_rules = {
    'span.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;
    },
    'span.intellilink:mouseout': function(e) {
        /**
         * Event:  mouseover
         * Action: show the intellilink DIV and fill with title text
         */
        $('tooltip').hide();
    }
}
Event.addBehavior(intellilink_rules);

var flash_rules = {
    '#flashheader' : function() {		
        var params = Element.getClassParameters(this);
        var FOShowReel = {
          movie:"/flash/header_showreel.swf",
          bgcolor: '#3e4851',
          width: "100%",
          height: "319",
          menu:"false",
          align:"middle",
          allowscriptaccess:"sameDomain",
          allowfullscreen:"false",
          xi:"false",
          majorversion:"9",
          build:"0",
          flashvars: "language=" + params.language + "&gatewayUrl=/flashservices/gateway.php&clickUrl=/index.php?pageID=" + params.page + "%26projectID=" 
        }
        UFO.create(FOShowReel,this.id);
    },
    '#video_player' : function() {
        var params = Element.getClassParameters(this);
        var FOVideoPlayer = {
          movie:"/flash/videoplayer.swf",
          width: "474",
          height: "270",
          menu:"false",
          xi:"false",
          majorversion:"9",
          build:"0",
          wmode:"opaque",
      	  allowscriptaccess:"always",
  	      allowfullscreen:"true",
          flashvars: "language=" + params.language + "&video=" + params.video + "&channel=" + params.channel + "&gatewayUrl=/flashservices/gateway.php"
        }
        if (params.showMenu) {
          FOVideoPlayer.flashvars += "&showMenu=1"
        }
        UFO.create(FOVideoPlayer,this.id);
    }
}
Event.addBehavior(flash_rules);

var google_maps_rules = {
    '#location_map': function(){
      var params = Element.getClassParameters(this);
      var zoom = 12;
      if (GBrowserIsCompatible() && params.latitude && params.longitude) {
        if (params.zoom) {
          zoom = parseInt(params.zoom);
        }
        var map = new GMap2(this);
        map.setCenter(new GLatLng(params.latitude, params.longitude), zoom);
        map.addControl(new CustomMapControl('mapcontrol'));
        var url = "/api/geodata/?type=xml"
        if (params.project) {
          url += "&id=" + params.project;
        }
        GDownloadUrl(url, map.getMarkersXML.bind(map));
      }
    },  
    '#location_map_route': function(){
      var params = Element.getClassParameters(this);
      var zoom = 12;
      if (GBrowserIsCompatible() && params.latitude && params.longitude) {
        if (params.zoom) {
          zoom = parseInt(params.zoom);
        }
        var map = new GMap2(this);
        map.setCenter(new GLatLng(params.latitude, params.longitude), zoom);
        map.addControl(new CustomMapControl('mapcontrol'));
        var markerPoint = new GLatLng(params.latitude, params.longitude);
        map.addOverlay(map.createMarker(markerPoint, params.name, 'balloon'));
      }
    }  
}
Event.addBehavior(google_maps_rules);