/*
Function: $get
This function provides access to the "get" variable scope + the element anchor

Version: 1.3

Arguments:
key - string; optional; the parameter key to search for in the url’s query string (can also be "#" for the element anchor)
url - url; optional; the url to check for "key" in, location.href is default

Example:
>$get("foo","http://example.com/?foo=bar"); //returns "bar"
>$get("foo"); //returns the value of the "foo" variable if it’s present in the current url(location.href)
>$get("#","http://example.com/#moo"); //returns "moo"
>$get("#"); //returns the element anchor if any, but from the current url (location.href)
>$get(,"http://example.com/?foo=bar&bar=foo"); //returns {foo:’bar’,bar:’foo’}
>$get(,"http://example.com/?foo=bar&bar=foo#moo"); //returns {foo:’bar’,bar:’foo’,hash:’moo’}
>$get(); //returns same as above, but from the current url (location.href)
>$get("?"); //returns the query string (without ? and element anchor) from the current url (location.href)

Returns:
Returns the value of the variable form the provided key, or an object with the current GET variables plus the element anchor (if any)
Returns "" if the variable is not present in the given query string

Credits:
Regex from [url=http://www.netlobo.com/url_query_string_javascript.html]http://www.netlobo.com/url_query_string_javascript.html[/url]
Function by Jens Anders Bakke, webfreak.no
*/
function $get(key,url){
if(arguments.length < 2) url =location.href;
if(arguments.length > 0 && key != ""){
if(key == "#"){
var regex = new RegExp("[#]([^$]*)");
} else if(key == "?"){
var regex = new RegExp("[?]([^#$]*)");
} else {
var regex = new RegExp("[?&]"+key+"=([^&#]*)");
}
var results = regex.exec(url);
return (results == null )? "" : results[1];
} else {
url = url.split("?");
var results = {};
if(url.length > 1){
url = url[1].split("#");
if(url.length > 1) results["hash"] = url[1];
url[0].split("&").each(function(item,index){
item = item.split("=");
results[item[0]] = item[1];
});
}
return results;
}
}



var Menu = new Class( {
  /* default options*/
  options: {
	submenuBgColorEnter: '#000000',
	submenuBgColorLeave: '#4F5255'
  }, 	
  initialize : function(menuId, options) {
    this.menu = $(menuId);
    this.options.submenuBgColorEnter = options.submenuBgColorEnter || this.options.submenuBgColorEnter
    this.options.submenuBgColorLeave = options.submenuBgColorLeave || this.options.submenuBgColorLeave
    this.menu.getChildren().each( function(li) {
      var a = li.getElements(".firstlevel")[0];
      var submenu = li.getElements(".submenu")[0];
      var immagineMenu = a.getChildren()[0];
      a.addEvent('mouseenter', function() {
        this.openMenu(immagineMenu, li, submenu);
        this.definisciPosizione(li.getCoordinates(), submenu);

      }.bind(this));

      li.addEvent('mouseleave', function() {
        this.closeMenu(immagineMenu, submenu);
      }.bind(this));

    }.bind(this));
    this.isClosed = false;
    this.isSelected = false;
  },

  definisciPosizione : function(coordinate, submenu) {
    submenu.getChildren().each( function(li) {
      /* li.getChildren()[0].setStyles({'width':coordinate.width}); */
      li.getChildren()[0].addEvent('mouseenter', function() {
        li.setStyles( {
          'background-color' :this.options.submenuBgColorEnter
        });
        var a_submenu = li.getChildren()[0].getElements(".asubmenu")[0];
        submenu.setStyles( {
          'background-color' :this.options.submenuBgColorEnter
        });

      }.bind(this));
      li.getChildren()[0].addEvent('mouseleave', function() {
        if (!(li.hasClass('sumbenuselected'))) {
          li.setStyles( {
            'background-color' :this.options.submenuBgColorLeave
          });
          var a_submenu = li.getChildren()[0].getElements(".asubmenu")[0];
          submenu.setStyles( {
            'background-color' :this.options.submenuBgColorLeave
          });
        }

      }.bind(this));

    }.bind(this));

  },
  openMenu : function(immagineMenu, li, submenu) {
    submenu.setStyles( {
      'display' :'block'
    });
    submenu.setOpacity(1);
    this.myFloatingDivShim = new IframeShim( {
      element :submenu,
      display :true,/* show it now */
      name :'myFloatingDivShimId'
    });

  },

  closeMenu : function(immagineMenu, submenu) {
    submenu.setOpacity(0);
    if ($chk(this.myFloatingDivShim)) {
      this.myFloatingDivShim.remove();
    }
  }

});

var ImageTools = new Class( {

  fillInBox : function(imageId) {
    var image = $(imageId);

    if (image.height == '150') {
      image.style.marginTop = '25px';

    }
    image.style.visibility = 'visible';

  }

});

var imageTools = new ImageTools();

// handle differnt types of navigations
// to guarantee correct tab opening based on current navigation
// tab1 search,tab2 coategory, tab3 systems
var TabTools = new Class( {

  paramBasedOpening : function(url, tab1, tab2, tab3) {
    var paramPosition = url.indexOf("isSistema");
    if (paramPosition != -1) {
      tab1.hideCorpo(tab1.tab.id);
      tab2.hideCorpo(tab2.tab.id);
      tab3.showCorpo(tab3.tab.id)
    }

  }

});

var tabTools = new TabTools();
