function validate_ups(){
  var formOK = true;
  formOK = validate_inputnumber('power','Please fill power field',formOK,1,999999);
  return formOK;
}

function validate_motorgen(){
  var formOK = true;
  formOK = validate_inputnumber('power','Please fill power field',formOK,1,999999);
  return formOK;
}

function validatenumber(item){
  myval = parseInt(item.value);
  if (isNaN(myval)){
    myval = '0';
  }
  item.value= myval;
}

function validate_inputnumber(itemid,errormessage,formOK,min,max){
  myitem = document.getElementById(itemid);
	if(myitem) {
	  validatenumber(myitem);
		if(myitem.value < min || myitem.value > max){
			if(formOK)
			  myitem.focus();
			formOK = false;
			myitem.parentNode.className = 'alert';
			alert(errormessage);
		} else {
      myitem.parentNode.className = '';
    }
	}
	return formOK;
}

function mydebug(mytext){
  debugdiv = document.getElementById('debug');
  if(debugdiv)
    debugdiv.innerHTML += mytext + '<br />';
  else
    alert(mytext);
}
$(document).ready(function () {
  //* menu autoresizing 
  margin_left_and_right = 0; // margin-left + margin-right of .main_menu
  if((container = $('.main_menu').filter(":eq(0)")).length > 0) {
    if(container.width() < $('.page_menu').width()-margin_left_and_right-1) // 40 = margin-left + margin-right of .main_menu + 1px?
      total_w = $('.page_menu').width()-margin_left_and_right-1; // IE6 must rely on menu ul container
    else
      total_w = container.width();
    children_width_new = container.getChildrenWidth() -1;

    addPadding = Math.floor((total_w - children_width_new-1) / (container.children().length * 2));
    children = container.children();
    children_width_new = 0;
    for(i=0;i<children.length;i++) {
      $(children[i]).find('A').each(function () {
        if($(this).parents('.submenu').length == 0) {
          padd = {paddingLeft: parseInt($(this).css('paddingLeft')) + addPadding, paddingRight: parseInt($(this).css('paddingRight'))+addPadding};
          $(this).css(padd);
          children_width_new += $(children[i]).outerWidth();
        }
      });
    }
    children_width_new = container.getChildrenWidth();
    to_add = total_w - children_width_new;
    child = children[0];
    paddingPlace = 'paddingLeft';
    while(to_add > 0 && child) {
      $(child).children().filter('A').each(function () {
        if($(this).parents('.submenu').length == 0) {
          if(to_add > 1) {
            $(this).css(paddingPlace, parseInt($(this).css(paddingPlace)) + 1);
          }
          to_add --;
        }
      });
      child = $(child).next();
      if(child == null || typeof child == 'object' && typeof child.length != 'undefined' && child.length == 0) {
        paddingPlace = 'paddingRight';
        child = children[0];
      }
    }
  }
  //* end autoresizing

  //* dropdown submenu   
  $('li.has-submenu').live('mouseover', function (event) {
    $(this).addClass('mouse-is-over');
    i = 0;
    par = $(event.originalTarget).parent();
    while (par != null && par.length > 0) {
      if($(par).hasClass('submenu'))
        return;
      if($(this).get(0) == $(par).get(0)) {
        break;
      }
      par = $(par).parent();
      i++;
      if(i > 100) {
        return;
      }
    }
    pos = $(this).position();
    pos.top += $(this).height();
    $(this).oneTime(10, function () {
      submenu_width = $($(this).children()).filter('.submenu-container').outerWidth();
      max_pos = $('#page_menu').offset().left + $('#page_menu').width()
      if(pos.left + submenu_width > max_pos) {
        pos.left = max_pos - submenu_width;
      }
      if($(this).hasClass('mouse-is-over'))
        $($(this).children()).filter('.submenu-container').css({position: 'absolute'}).css(pos).slideDown('slow');
    })
  });
  $('li.has-submenu').live('mouseout', function (event) {
    $(this).removeClass('mouse-is-over');
    $(this).oneTime(200, function() {
      if(!$(this).hasClass('mouse-is-over')) {
        $($(this).children()).filter('.submenu-container').fadeOut('slow');
      }
    })
  });
  //* end dropdown submenu  
});
//* required functions for menu
jQuery.extend(jQuery.fn, {
  getChildrenWidth: function () {
    children = $(this).children();
    total = 0;
    for(i=0;i<children.length;i++) {
      total += $(children[i]).width()+getPxToInt($(children[i]).css('marginLeft'))+getPxToInt($(children[i]).css('marginRight'))+getPxToInt($(children[i]).css('paddingLeft'))+getPxToInt($(children[i]).css('paddingRight'));
    }
    return total;
  }
});
function getPxToInt(param) {
  param = parseInt(param);
  if(isNaN(param))
    return 0;
  return param;
}
//* end of required functions for menu 



$(document).ready(function () {
  $('.bottomBannerImage').live('mouseover', function () {
    $(this).addClass('mouse-is-over');
    $(this).oneTime(10, function () {
      //console.log("one time");
      if(($(this).hasClass('mouse-is-over') || $('.hoverEffect', this).hasClass('mouse-is-over')) && !$('.hoverEffect', this).hasClass('efect-is-shown')) {
        //console.log("one time inside, "+$('.hoverEffect', this));
        //console.log($('.hoverEffect', this));
        $('.hoverEffect', this).show().fadeOut(0).slideDown().addClass('efect-is-shown');
        $('.banerName', this).slideUp().addClass('efect-is-shown');
      }
    });
  });
  $('.hoverEffect').live('mouseover', function () {
    $(this).addClass('mouse-is-over');
  });
  $('.hoverEffect').live('mouseout', function () {
    $(this).removeClass('mouse-is-over');
  });
  $('.bottomBannerImage').live('mouseout', function () {
    $(this).removeClass('mouse-is-over');
    $(this).oneTime(100, function () {
      if(!$(this).hasClass('mouse-is-over') && !$('.hoverEffect', this).hasClass('mouse-is-over')) {
        $('.hoverEffect', this).slideUp('slow', function () {
          $(this).hide();
        }).removeClass('efect-is-shown');
        $('.banerName', this).slideDown('slow', function () {
          $(this).show();
        }).removeClass('efect-is-shown');
      }
    });
  });
});


$(document).ready(function () {
  $('#banner').changeBanner('slow').everyTime(15000, function () {
    if(!$(this).hasClass('mouse-is-over')) {
      $(this).changeBanner('slow');
    }
  }).mouseover(function () {
    $(this).addClass('mouse-is-over');
  }).mouseout(function () {
    $(this).removeClass('mouse-is-over');
  });
});
//* change banner
jQuery.extend(jQuery.fn, {
  changeBanner: function (speed, where) {
    if(typeof where == 'undefined' || where == null)
      where = 1;
    cur_i = 0;
    cont = $(this).find('.bannerItem.item-shown').parent();
    children = cont.children();
    if(where > 0) {
      n = $(this).find('.bannerItem.item-shown').next('.bannerItem');
      if(n.length == 0 && $(this).find('.bannerItem.item-shown').length > 0) {
        for(i=$.inArray($(this).find('.bannerItem.item-shown').get(0), children)+1;i<children.length;i++) {
          if($(children[i]).hasClass('bannerItem') && ++cur_i == where){
            n = $(children[i]);
            break;
          }
        }
      }
      if(typeof n == 'undefined' || n == null || n.length == 0) {
        n = $(this).children('.bannerItem').filter(':eq(0)');
      }
    } else {
      n = $(this).find('.bannerItem.item-shown').prev('.bannerItem');
      if(n.length == 0 && $(this).find('.bannerItem.item-shown').length > 0) {
        for(i=$.inArray($(this).find('.bannerItem.item-shown').get(0), children)-1;i>=0;i--) {
          if($(children[i]).hasClass('bannerItem') && --cur_i == where){
            n = $(children[i]);
            break;
          }
        }
      }
      if(typeof n == 'undefined' || n == null || n.length == 0) {
        n = $(this).children('.bannerItem').filter(':last');
      }
    }
    if ($(this).find('.bannerItem').length > 1 || $('.bannerItem.item-shown').length == 0) {
      $(this).find('.bannerItem.item-shown').fadeOut(speed).removeClass('item-shown');
      n.hide().addClass('item-shown').fadeIn(speed);
      items = $(this).children('.bannerItem');
      $(this).find('.page_number').html(($.inArray(n.get(0), items)+1) + '/' + items.length);
    }
    return this;
  }
});

