$(function()
{
  /**
   * Kinyíló menü
   */
  var menutimer = null;
  var openmenu = null;
  $('.navigation > ul > li').hover(function()
  {
    if (menutimer)
    {
      clearTimeout(menutimer);
      menutimer = null;
    }
    
    if (openmenu)
    {
      openmenu.hide();
      openmenu = false;
    }
    
    openmenu = $(' > ul', this);
    openmenu.show();
    
  }, function()
  {

    menutimer = setTimeout(function()
    {
      if (openmenu)
      {
        openmenu.hide();
        openmenu = null;
      }
      menutimer = null;
    }, 700);
  });
  
  
  /**
   * Kék <label> elemek
   */
  $('.designCheckbox').click(function()
  {
    var meta = $(this).metadata();
    var chk = $(meta.elem, this);
    var checked = chk.hasClass(meta['class'][1]);

    if (meta.type == 'checkbox')
    {
      chk
        .removeClass(checked ? meta['class'][1] : meta['class'][0])
        .addClass(checked ? meta['class'][0] : meta['class'][1]);

      if (meta.name)
        $('input[name=' + meta.name + ']').val(checked ? '' : meta.value);
    }

    else if (meta.type == 'radio')
    {
      $('.designCheckbox').each(function()
      {
        var meta2 = $(this).metadata();

        if (meta2.name != meta.name) return;

        var chk2 = $(meta2.elem, this);
        if (meta2.value == meta.value)
        {
          chk2
            .removeClass(meta['class'][0])
            .addClass(meta['class'][1]);

          if (meta.name)
            $('input[name=' + meta.name + ']').val(meta.value);
        }
        else
        {
          chk2
            .removeClass(meta['class'][1])
            .addClass(meta['class'][0]);
        }
      });
    }
  });


  /**
   * IE hack - A h1-eknek az absolut pozicio miatt fix szélesség kell
   */
  if ($.browser.msie && $.browser.version == 6)
  {
    $("h1").each(function()
    {
      $(this).width($(this).parent().width() - 20);
    });
  }
  
  defaultInputTexts =
  [
    {
      'input'    : $("#searchForm input[name=search]"),
      'errorMsg' : $("#emptyErrorText").html(),
      'button'   : $("#searchBtn")
    },
    {
      'input'    : $('#newsLetterSubscribe input[name=name]')
      //,'button'   : $('#newsLetterSubscribe .okButton')
    },
    {
      'input'    : $('#newsLetterSubscribe input[name=email]')
      //,'button'   : $('#newsLetterSubscribe .okButton')
    },
    {
      'input'    : $('#newsLetterSubscribe input[name=address]')
      //,'button'   : $('#newsLetterSubscribe .okButton')
    }
  ];

  for (var i=0; i<defaultInputTexts.length; i++)
  {
    var o = defaultInputTexts[i];

    if (!o.input || !o.input.length) continue;
    if (o.input.hasClass('noFocus')) continue;
    o.text = o.input.val();
    o.input[0].defIdx = i;

    o.input.focus(function()
    {
      if ($(this).val() == defaultInputTexts[this.defIdx].text)
        $(this).val('');
    });

    o.input.blur(function()
    {
      if ($(this).val() == '')
        $(this).val(defaultInputTexts[this.defIdx].text);
    });

    if (o.button)
    {
      o.button.click(function()
      {
        for (var i=0; i<defaultInputTexts.length; i++)
        {
          var o = defaultInputTexts[i];
          if (o.button[0] != this) continue;

          var inp = o.input;
          if (inp.val() == defaultInputTexts[i].text)
          {
            if (typeof defaultInputTexts[i].errorMsg == 'undefined')
              $(inp).val('');
            else
            {
              alert(defaultInputTexts[i].errorMsg);
              return false;
            }
          }
        }
      });
    }
  }

});

function openPopup(anchor, w, h)
{
  if (!anchor.href) return;
  window.open(anchor.href, "","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,top=-1,left=-1,width=" + (w?w:640) + ",height=" + (h?h:480));
  return false;
}

