if ( !paramsSorterTable ) {
	var paramsSorterTable = {};
}
if ( !paramsFilterSorterTable ) {
	var paramsFilterSorterTable = {};
}
Menu = function(params)
{
	var that = {};

	var findControls = function() {
		that.menu.find("li a").each(function() {
			var id = this.id.replace("_menu", "_submenu");
			var submenu = jQuery('#' + id);
			if (submenu[0]) {
				that.items_menu.push(jQuery(this));
				that.items_submenu.push(submenu);
			}
		});
	}

	var highlightCurrentLink  = function() {
		var url = window.location.toString();
		var max = 0;
		var link = null;

		that.menu.find('li a').each(function() {
			if (url.indexOf(this.href) >= 0 && this.href.length > max) {
			  link = this;
			  max = this.href.length;
			}
		});

		if (link) {
			that.defaultSubmenu = link.id;
		}

		link = null;
		max = 0;

		that.submenu.find('li a').each(function() {
			if (url.indexOf(this.href) >= 0 && this.href.length > max) {
			  link = this;
			  max = this.href.length;
			}
		});

		if (link) {
			jQuery(link).parent('li').addClass('current');
			that.defaultSubmenu = jQuery(link).parents('ul').attr('id').replace("_submenu","_menu");
		}

		that.activateDefaultSubmenu();
	}

	var setEvent = function(){
		for(var i = 0; i < that.items_menu.length; i++) {
			that.items_menu[i].bind("mouseover", function(){var link = this;that.activateSubmenu(link.id)});
			that.items_menu[i].bind("mouseout", function(){that.timeoutHide = setTimeout(function(){that.activateDefaultSubmenu()}, 5000)});
		}
		that.submenu.find('li a').each(function(){
			jQuery(this).bind("mouseover", function(){clearTimeout(that.timeoutHide);that.timeoutHide = null;});
			jQuery(this).bind("mouseout", function(){that.timeoutHide = setTimeout(function(){that.activateDefaultSubmenu()}, 5000)});
		});
	}

	return new function (params){

		that = this;

		if(!params || !params.menuId || !jQuery("#" + params.menuId)[0])
			return;

		that.menu = jQuery("#" + params.menuId);
		that.submenu = jQuery("#" + params.submenuId);
		that.items_menu = [];
		that.items_submenu = [];

		that.activateSubmenu = function(id) {
			clearTimeout(that.timeoutHide);
			that.timeoutHide = null;

			for(var i = 0; i < that.items_menu.length; i++) {
				if(that.items_menu[i].attr('id') != id) {
					that.items_submenu[i].stop(false,true);
					that.items_submenu[i].slideUp();
					that.items_menu[i].parent('li').removeClass('current');
				} else {
					that.items_submenu[i].slideDown();
					that.items_menu[i].parent('li').addClass('current');
				}
			}
		}

		that.activateDefaultSubmenu = function() {
			clearTimeout(that.timeoutHide);
			that.timeoutHide = null;
			that.activateSubmenu(that.defaultSubmenu);
		}

		findControls();
		highlightCurrentLink();
		setEvent();

	}(params)
}


var separator = '-';
var z_actual = 100;

jQuery.fn.jLook = function(options, func){
  var self = this;
  $(this).addClass('jLookForm');
  this.each(function(){
    var actualForm = this;

    $('select', this).each(function(){
      var disabled;
      var disableClass = "";

      if($(this).attr("disabled"))
        disabled = true;
      var nam = $(this).attr('name')+'_wrapper';
      if (disabled)
        disableClass = "jLookDisableSelect";
      $(this).wrap('<div class="jLookSelectWrapper ' + disableClass + '" id="' + nam + '"></div>');
      var width = $(this).css('width');

      $(this).parent().width(width);

      var name = $(this).attr('name');
      var indiceSeleccionado = this.selectedIndex;
      if (disabled)
        disableClass = "jLookDisableSelectOpen";
      var selectContent = '<ul>';
      width = width.replace("px","")-12;
      var selectContent = '<ul style="overflow:auto;width:' + (width+4) + 'px">';
      var index = 0;
      var selectContentHeader= '<input type="hidden" value="" name="' + name + '" id="' + name + '" /><div class="jLookSelectHeader"><div></div><a href="#" class="jLookSelectOpen ' + disableClass + '"></a></div>'; // xhtml replace

      $('option', this).each(function(){
        if (disabled)
          disableClass = "jLookDisableSelectOption";
        selectContent += '<li><a href="javascript:void(0)" class="jLookSelectOption ' + disableClass + '" id="' +  name + separator + $(this).attr('value') + '">' + $(this).html() + ' </a></li>';
        if (disabled)
          disableClass = "jLookDisableSelectOpen";
        if(indiceSeleccionado == index){
          selectContentHeader = '<input type="hidden" value="' + $(this).attr('value') + '" name="' + name + '" id="' + name + '" /><div class="jLookSelectHeader"><div>' + $(this).html() + '</div><a href="#" class="jLookSelectOpen ' + disableClass + '"></a></div>'; // xhtml replace
        }
        $(this).remove();
        index = index + 1;
      });
      selectContent += '</ul>';
      $('#' + nam).html(selectContentHeader + selectContent);
      $('#' + nam + " .jLookSelectHeader").width(width).find('div').width(width-33);
	  func();
    });
    $('.jLookSelectOpen').not(jQuery('.jLookDisableSelectOpen')).bind('click', selectClick);
    $('.jLookSelectOption').not(jQuery('.jLookDisableSelectOption')).bind('click', optionClick);

  });
}


selectClick = function(){
  z_actual = z_actual + 10;
  // id del contenedor
  wrapperId = $(this).parent().parent().attr('id');
  // veo si est� abiero
  if($('#' + wrapperId).is('.jLookSelectOpened')){
    // lo vuelvo al estado cerrado
    $('#' + wrapperId).removeClass('jLookSelectOpened');
    // cierro
    $('ul', $('#' + wrapperId)).hide('fast', function(){
      // vuelvo todo a posiciones relativas
      $('.jLookSelectWrapper').each(function(){
        if($(this).attr('id') != wrapperId){
          $(this).css('position', 'relative');
        }
      });
    });
  }else{ // expando
    // cambio las posiciones de los otros selects
    $('.jLookSelectWrapper').each(function(){
      if($(this).attr('id') != wrapperId){
        $(this).removeClass('jLookSelectOpened').find('ul').hide();
      }
    });
    $('#' + wrapperId).addClass('jLookSelectOpened');
    $('ul', $('#' + wrapperId)).show();//slideDown();
  }

  return false;
};

optionClick = function(){
  var id = $(this).attr("id");
  var separatorPos = id.lastIndexOf(separator);
  if(separator == id.charAt(separatorPos-1))
  {
    var value = id.substring(separatorPos,id.length);
    var name = id.substring(0,separatorPos-1);
  }
  else
  {
    var value = id.substring(separatorPos+1,id.length);
    var name = id.substring(0,separatorPos);
  }
  var wrapperId = name + '_wrapper';
  $('#' + name).attr('value', value);
  // muestro el valor seleccionado
  $('div', $('#' + name + '_wrapper .jLookSelectHeader')).html($(this).html());
  // cierro el select

  // lo vuelvo al estado cerrado
  $('#' + wrapperId).removeClass('jLookSelectOpened');
  // cierro
  $('ul', $('#' + wrapperId)).hide('fast', function(){
    // vuelvo todo a posiciones relativas
    $('.jLookSelectWrapper').each(function(){
      if($(this).attr('id') != wrapperId){
        $(this).css('position', 'relative');
      }
    });
  });
  return false;
};

jQuery(document).ready(function(){
	jQuery("a[rel^='prettyPhoto']").prettyPhoto({
		allowresize: false,
		theme: 'dark_square'
	});

	jQuery("a[rel^='mainPrettyPhoto']").prettyPhoto({
		allowresize: false,
		theme: 'dark_square'
	});

	if (window.showPrettyPhoto)
		jQuery(showPrettyPhoto).trigger('click');;
	menu = new Menu({menuId:'main_menu', submenuId:'submenu'});
	
	jQuery('body').click(function(event){
    if(!event) event = window.evenqst;
    var target =  event.target;
    if (jQuery(target).parents('.jLookSelectWrapper')[0] || jQuery(target).hasClass("jLookSelectWrapper"))
      return;
    jQuery('.jLookSelectWrapper').removeClass('jLookSelectOpened').find('ul').hide();
  });
  
	jQuery(".tablesorter").tablesorter(paramsSorterTable).tablesorterFilter(paramsFilterSorterTable);
	jQuery(".tablesorter").find('.header').prepend('<div class="sorter_icon"><div>');
});

function clear_input(obj) {
  var obj = document.getElementById(obj);
  obj.value = '';
}

function get_height_block() {
}

function menu_selector(obj,obj_stat) {
  var obj = document.getElementById(obj);
  var obj_stat = document.getElementById(obj_stat);

  var main_elements = document.getElementById('main_elements').getElementsByTagName('div');
  for(var j=0; j<main_elements.length; j++) {
	if(main_elements[j].id) {
	  main_elements[j].style.marginTop = '-40px';
	}
  }

  obj_stat.style.marginTop = '-21px';

  var inner_elements_obj = document.getElementById('inner_elements');
  inner_elements_obj.style.width = 'auto';
  var inner_elements_arr = document.getElementById('inner_elements').getElementsByTagName('ul');

  for(var i=0; i<inner_elements_arr.length; i++) {
	inner_elements_arr[i].style.display = 'none';
  }

  obj.style.display = 'block';
  inner_elements_obj.style.width = inner_elements_obj.clientWidth + 'px';

  inner_elements_obj.style.left = (301 - (inner_elements_obj.clientWidth / 2)) + 'px';
}

function view_block(obj) {
		var vobj = document.getElementById(obj);
		vobj.style.height = 'auto';
		vobj.style.overflow = 'visible';
}

function resizeByContent() {
}

