/* Smooth Scrolling ------------------------------------------------------- */

jQuery.fn.animateScrolling = function(){
  jQuery(this).click(function() {
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
    && location.hostname == this.hostname && this.hash) {
      var $target = jQuery(this.hash);
      $target = $target.length && $target || jQuery('[name=' + this.hash.slice(1) +']');
      if ($target.length) {
        var targetOffset = $target.offset().top;
        jQuery('html,body').animate({scrollTop: targetOffset}, 600);
       return false;
      }
    }
  });
};


/* Page Scaling und Cookies ----------------------------------------------- */

function scalePage(value) {
  if (!value) { value = '70'; }
  myHtml = document.getElementById('thishtml');
  myHtml.style.fontSize = value + "%";
  update_cookie(value);
};

function trim(str) {
  return str.replace(/^\s*|\s*$/g,"");
};

function read_cookie(name) {
  var ar = document.cookie.split(';');
  for(var i=0; i<ar.length; i++) {
    var c = ar[i].split('=');
    if (trim(c[0]) == name) {
      return c[1];
    }
  }
  return null;
};

function update_cookie(size) {
  var ablauf = new Date();
  var infuenfTagen = ablauf.getTime() + (10 * 24 * 60 * 60 * 1000);
  ablauf.setTime(infuenfTagen);
  document.cookie = "size="+size+"; path=/; expires=" + ablauf.toGMTString();
};

/* Livesearch ------------------------------------------------------------- */

var MorphoSysSearchAndJump = function() {

    var _this = this;
    this.url = searchUrl;
    this.state = "quickandjump";
    this.visible = false;
    this.field = jQuery("#global_searchfield");
    this.searchresults = jQuery("#livesearch_matches").hide();
    this.quickandjump = jQuery("#quickandjump").hide();
    this.active = false;
    this.current = -1;
    this.jumpelements = this.quickandjump.find("a");
    this.initString = this.field.val();
    this.click = false;
    
    this.field.bind("focus", function(){
      if(_this.field.val() == _this.initString) _this.field.val("");
      _this.quickandjump.show();
      _this.active = true;
    });

    this.field.bind("blur", function(){
      var func = arguments.callee;
      if(_this.click){
        setTimeout(function(){
          func();
        }, 100);
        return true;
      }
      
      _this.quickandjump.hide();
      _this.searchresults.hide();
      _this.active = false;
    });

    jQuery("#global-navi").bind("click", function(event){
      var target = event.target;

      _this.click = false;
      if(target.className.indexOf("close") != -1) {
        target.parentNode.style.display = "none";
        _this.active = false;
        return false;
      }

    });
    
    jQuery("#global-navi").bind("mousedown", function(event){
      _this.click = true;
    });

    jQuery(document).bind("keyup", function(event){
      var code = event.keyCode;

      if(!_this.active || code == 27){ 
        _this["searchresults"].hide();
        _this["quickandjump"].hide(); return true;
        _this.current = -1;
      }

      if(_this.field.val().length > 2) {
        _this[_this.state].hide();
        _this.state = "searchresults";
      } else {
        _this.searchresults.hide();
        _this.state = "quickandjump";
        _this.quickandjump.show();
      }

      if(_this.state == "searchresults") {
        _this.sendSearchRequest();
      }

    });
};
  
    
MorphoSysSearchAndJump.prototype = {
    sendSearchRequest: function(){
    var searchresults = this.searchresults;
    
    jQuery.get(this.url, { q: encodeURI(this.field.val()), xhr: 1}, function(data){
      searchresults.html(data);
      searchresults.show();      
    });
  } 
};


/* Toggle-Navigation ------------------------------------------------------ */

var MorphoSysNavigation = function(){
    // $$("#sub-navi *").invoke("cleanWhitespace");
		
    jQuery("#sub-navi li").each(function(){
      var li = jQuery(this);
      var ul = li.find("> ul");
			if(ul.length) {
				li.addClass("togglethis");
				ul.css({overflow:"hidden", display: "none"});
				li.children().eq(0).click(function(){
				  ul.slideToggle(300);
				});
			}
			
			if(jQuery(li).hasClass("active") && li.parent().attr("id") == "" )
        jQuery(li).parent().css("display", "block");
		});
};

/* IR-Cockpit */

var MorphoSysIrCockpit = function() {
  jQuery("#cockpit dl").each(function(index, dl){

    var tabs = jQuery(this).find("dt a");

      tabs.each(function(index){
        var tab = this;
        var dd = jQuery("#"+tab.rel);
        (index != 0) ? dd.hide() : dd.addClass("opened");
        
        var observer = function(event){
          
          try{
            jQuery(dl).find(".active").removeClass("active");
          }catch(e){}
          
          jQuery(dl).find(".opened").each(function(index, opened){
            jQuery(opened).hide().removeClass("opened");
          });

          current = jQuery("#"+this.rel);

          current.show();
          current.addClass("opened");
          jQuery(this).parent().addClass("active");

          return false;
        };
        
        jQuery(tab).bind("mouseover", observer);
        jQuery(tab).bind("click", observer);
      });

  });
};

jQuery(function() {
	new MorphoSysNavigation();
	new MorphoSysIrCockpit();
	new MorphoSysSearchAndJump();
	jQuery('a[@href*="#"]').animateScrolling();
	jQuery("table").tableHover();
});