function subscribe()
{
  var state = $('#subscribe input[name=state]').val();
  if (state == 1 || state == 2)
  {
    $('#subscribe').ajaxSubmit(
    {
      success : function(data)
      {
        var result = eval('(' + data + ')');
        if (result['result'] != true)
        {
          var questionText = '';
          for (var i in result.error)
          {
            questionText += result.error[i] + '<br />';
          }
          $(document).setQuestion(
          {
            title            : 'Hiba',
            question         : questionText,
            okButtonText     : 'OK',
            cancelButtonText : '',
            questionIconSrc  : 'images/question/error.png',
            cancelFunct      : function() { return true; },
            okFunct          : function() { return true; }
          })
          return false;
        }

        $('#subscribe input[name=state]').val(parseInt(state)+1);
        if (state == 1)
        {
          $('#nlDatas').hide('slow');
          $('#nlAddress').show('slow');
          $('#subscribeButton1').hide();
          $('#subscribeButton2').show();
        }
        else
        {
          $('#nlAddress').hide('slow');
          $('#nlCategories').show('slow');
          $('#subscribeButton2').hide();
          $('#subscribeButton3').show();
        }
      }
    });
  }
  else
  {
    $('#subscribe').ajaxSubmit(
    {
      success :
      function(data)
      {
        var result = eval('(' + data + ')');
        if (result['result'] != true)
        {
          var questionText = '';
          for (var i in result.error)
          {
            questionText += result.error[i] + '<br />';
          }
          $(document).setQuestion(
          {
            title            : 'Hiba',
            question         : questionText,
            okButtonText     : 'OK',
            cancelButtonText : '',
            questionIconSrc  : 'images/question/error.png',
            cancelFunct      : function() { return true; },
            okFunct          : function() { return true; }
          });
          return false;
        }
        else
        {
          $(document).setQuestion(
          {
            title            : 'Sikeres feliratkozás!',
            question         : 'Feliratkozását rögzítettük!<br /><br/>Hamarosan kap egy emailt, amiben meg kell erősítenie feliratkozási szándékát.<br />Erre azért van szükség, hogy véletlenül se tudjanak az ön email címével visszaélni a feliratkozáskor.<br />Amennyiben a visszaigazolás nem történik meg a regisztrációt követő 72 órában úgy a rendszerből az adatok automatikusan törlődnek.<br /><br />Megértésüket köszönjük.',
            okButtonText     : 'OK',
            cancelButtonText : '',
            questionIconSrc  : 'images/question/tick.png',
            cancelFunct      : function() { return true; },
            okFunct          : function() { return true; }
          });

          $('#subscribe input[name=name]').val('');
          $('#subscribe input[name=email]').val('');
          $('#subscribe input[name=address]').val('');

          $('#subscribe input[type=checkbox]').each(function ()
          {
           $(this).attr({checked : false});
          });

          $('#nlCategories').hide('slow');
          $('#nlAddress').hide('slow');
          $('#nlDatas').show('slow');

          $('#subscribeButton3').hide();
          $('#subscribeButton2').hide();
          $('#subscribeButton1').show();
          $('#subscribe input[name=state]').val('1');
        }
      }
    });
  }
}

function subscribeBack()
{
  var state = $('#subscribe input[name=state]').val();
  if (state < 2) return;
  
  $('#subscribe input[name=state]').val(state - 1);

  if (state == 2)
  {
    $('#nlAddress').hide('slow');
    $('#nlDatas').show('slow');

    $('#subscribeButton1').show();
    $('#subscribeButton2').hide();
  }
  else if (state == 3)
  {
    $('#nlCategories').hide('slow');
    $('#nlAddress').show('slow');

    $('#subscribeButton2').show();
    $('#subscribeButton3').hide();
  }
}

$(function()
{
  if ($.browser.msie && $.browser.version == 6)
  {
    fixAllPNG();
    fixbgpng.fnLoadPngs();
  }

  
  /**
   * Topmenu hover, submenu open
   */
  var topmenuTimer = null;
  var openTopmenu = null;
  $('.topMenu > ul > li').hover(function()
  {
    if (topmenuTimer)
    {
      clearTimeout(topmenuTimer);
      topmenuTimer = null;
    }

    if (openTopmenu && openTopmenu != this)
    {
      $('ul', openTopmenu).hide();
      $('.bgDiv', openTopmenu).hide();
      openTopmenu = null;
    }
    openTopmenu = this;

    var width = 0;
    var $ul = $('> ul', this);
    if ($ul.width() < 150) $ul.css('width', '150px');
    $ul.show();

    if ($.browser.msie && $.browser.version < 7)
    {
      $ul.css('width', '120px');
      $('li', $ul).css('width', '120px');
    }

    var ulMargin = parseInt($ul.css('margin-left'));
    if ($.browser.msie && $.browser.version < 7)
    {
      ulMargin += parseInt($('> a:eq(0)', this).css('padding-left'));
      ulMargin += parseInt($('> a:eq(0)', this).css('padding-right'));
    }

    width = $ul.width();
    width += ulMargin;


    var margin = $('> a', $ul).outerWidth() - $('> a', $ul).width()
    $('> a', $ul).css('width', $ul.width() - margin);

    if ($.browser.msie && $.browser.version < 7)
      width -= margin;

    $('> .bgDiv', this)
      .css({'width': (width+5) + 'px', 'height': $ul.height() + 'px'})
      .show();

  }, function()
  {
    var li = this;

    if (topmenuTimer)
    {
      clearTimeout(topmenuTimer);
      topmenuTimer = null;
    }

    topmenuTimer = setTimeout(function()
    {
      $('ul', li).hide();
      $('.bgDiv', li).hide();
      if (topmenuTimer)
        topmenuTimer = null;
      openTopmenu = null;
    }, 1000);
  });


  /**
   * Submenu hover, second submenu open
   */
  var openSubmenu = null;
  $('.topMenu > ul > li > ul > li').hover(function()
  {
    if (topmenuTimer)
    {
      clearTimeout(topmenuTimer);
      topmenuTimer = null;
    }

    if (openSubmenu && openSubmenu != this)
    {
      $('ul', openSubmenu).hide();
      $('.bgDiv', openSubmenu).hide();
      openSubmenu = null;
    }
    openSubmenu = this;

    var width = 0;
    var $ul = $('> ul', this);
    if ($ul.width() < 150) $ul.css('width', '150px');
    $ul.show();



    var ulMargin = 6;
    if ($.browser.msie && $.browser.version < 7)
    {
      ulMargin += parseInt($('> a:eq(0)', this).css('padding-left'));
      ulMargin += parseInt($('> a:eq(0)', this).css('padding-right'));
    }

    width = $ul.width();
    width += ulMargin;


    var margin = $('> a', $ul).outerWidth() - $('> a', $ul).width()
    $('> a', $ul).css('width', $ul.width() - margin);

    if ($.browser.msie && $.browser.version < 7)
      width -= margin;

    $('> .bgDiv', this)
      .css({'width': (width+5) + 'px', 'height': $ul.height() + 'px'})
      .show();
  });
  
  
  /**
   * Submenu
   */
  $('.sideMenu .nodeMenu')
    .css('cursor', 'pointer')
    .click(function()
    {
      var ul = $(this).find('+ ul');
      ul[ul.is(':visible') ? 'slideUp' : 'slideDown']();
    });
});

