$(function()
{
  /**
   * Letiltjuk a labeleknél a szövegkijelölést
   */
  $("label").disableTextSelect();

  /**
   * IE6 elavulg böngésző
   */
  $("#ie6attentionClose").bind("click", function()
  {
    $("#ie6").slideUp();
    $.cookie("ie6attention", "1");
  });

  /**
   * a második submenuk animálása
   */
  if ($('#menus').length > 0)
    animateMenu();

  /**
   * Fancybox settings
   */
  setFancyBox();

  /**
   * EDOT Tooltip
   */
  $('.tooltip').tooltip();

	/**
	 * YouTube videók Javascript-re beszúrása
	 */
  insertYoutubeVideo();

  /**
   * Window load-ra
   */
  $(window).load(function()
  {
    /**
     * Beállítjuk a content magasságot
     */
    contentResizer();
    
    if ($.browser.msie && $.browser.version == 7)
    {
      setTimeout(function()
      {
        $("button:visible").css('visiblity', 'hiddden').css('visibility', 'visible');
      }, 300);
    }

    /**
     * Oldal küldése fekete hátterének beállítása
     */
    $('#sendToBg').height(Math.max($("body").height(), $(window).height()));
  });



   /**
    * Login box alapértelmezett szövegeket cserélgetjük
    */
   setLoginBoxTextEffect();

   /**
    * Onload-ra loginbox submitjére fokuszálunk
    */
   $('.login button[name=loginSubmit]').focus();

});


function animateMenu()
{
  var $menu = $('#menus');

  $("li a[href=#]", $menu).each(function()
  {
    var $ul = $(this).siblings("ul");
    if ($ul.length > 0)
    {
      $(this).bind('click', function()
      {
        if ($ul.css('display') == 'none')
        {
          $ul.slideDown('normal', function()
          {
            contentResizer();
          });

        }
        else
        {
          $ul.slideUp('normal', function()
          {
            contentResizer();
          });
        }
        return false;
      });
    }
  });
}

function setFancyBox()
{
  $("a.fancybox").fancybox({
  	overlayShow: true
  });

  $(".fancybox_iframe").fancybox({
		'width'				: 740,
		'height'			: 480,
    'autoScale'     	: false,
    'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe'
	});
}

function setLoginBoxTextEffect()
{
  var loginText = {
    username : $('.login span.login_email').html(),
    password : $('.login span.password').html()
  };
  
  if ($('.login input[name=login_email]').val() == '')
    $('.login input[name=login_email]').val(loginText.username);
    
  if ($('.login input[name=password]').val() == '')
  {
    $('.login input[name=password]').remove();
    var $input = $('<input type="text" name="password" value="' + loginText.password + '" />');
    pwdInputFocus($input, loginText.password);
    $("#passwordInput").append($input);
  }

  $('.login input[name=login_email]').focus(function()
  {
    if ($(this).val() == loginText.username) $(this).val("");
  });

  $('.login input[name=login_email]').blur(function()
  {
    if ($(this).val() == "") $(this).val(loginText.username);
  });

  pwdInputFocus($('.login input[name=password]'), loginText.password);
}

function pwdInputBlur(obj, text)
{
  obj.blur(function()
  {
    if ($(this).val() == "")
    {
      if (true || $.browser.msie)
      {
        $(this).remove();
        var $input = $('<input type="text" name="password" value="' + text + '" />');
        pwdInputFocus($input, text);
        $("#passwordInput").append($input);
      }
      else
      {
        this.type = 'text';
        $(this).val(text);
      }
    }
  });
}

function pwdInputFocus(obj, text)
{
  obj.focus(function()
  {
    if (true || $.browser.msie)
    {
      $(this).remove();
      var $input = $('<input type="password" name="password" />');
      pwdInputBlur($input, text);
      $("#passwordInput").append($input);
      $input.focus();
      $input.focus();
    }
    else
    {
      pwdInputBlur($(this), text);
      if ($(this).val() == text)
      {
        $(this).type = 'password';
        $(this).val('');
      }
    }
  });
}


function contentResizer()
{
  var navigationHeight = $("#navigation").height() + parseInt($("#navigation").css('padding-bottom'));
  if (navigationHeight >= $("#innerContainer").height())
  {
    $("#innerContainer").height("auto");
    $("#content").height($("#content").height() + (navigationHeight - $("#innerContainer").height()) +  Math.abs(parseInt($("#footer").css("margin-top"))));
  }
  else
  {
    var oldHeight = $("#innerContainer").height();

    var newHeight = navigationHeight + Math.abs(parseInt($("#footer").css("margin-top")));
    var newContentHeight = $("#content").height() - (oldHeight - newHeight);

    if (newContentHeight > $("#innerContent").height())
    {
      $("#innerContainer").height(newHeight);
      $("#content").height(newContentHeight);
    }
    else
    {
      if ($.browser.msie && $.browser.version == 7) $("#content").height($("#content").height());
    }
  }
}

function insertYoutubeVideo()
{
  var i = 0;
  $('.youtubeVideo').each(function()
  {
    var id = 'youtube_' + i;

    $(this).attr('id', id);

    insertSwf($(this).html(), id);

    i++;
  })
}

function insertSwf(youtubeid, id)
{
  var so = new SWFObject("http://www.youtube.com/v/" + youtubeid + '&hl=en_US&fs=1&color1=0x000000&color2=0x000000&rel=0', "video", "640", "375", "9", "#ffffff");
  so.addParam("allowFullScreen", "true");
  so.addParam("allowscriptaccess", "always");
  so.write(id);
}

function printSite()
{
  var page = document.URL.replace("http://" + document.domain + "/", "").replace(/\/$/, "");
  window.open("nyomtatas.php" + (page != '' ? '?pg=' + page : '') , "print", "status=0,toolbar=0,width=740,height=480,scrollbars=yes,resize=no");
}

function sendToAFriend()
{

  $('#sendToBg').css({opacity: 0, display: 'block'}).animate({opacity: 0.7}, function()
  {
    showSendTo();
  });

  return false;
}

function showSendTo()
{
  $('#sendTo').show();
  $('input').removeClass('error');
  $('#isErrorP').css('visibility', 'hidden');
  $('#friendProcess').hide();
  $('#friendSuccess').hide();
  $('#friendForm').show();

  var s = { w : $('#sendTo').width() , h : $('#sendTo').height() };
  var ws = { w : $(window).width() , h : $(window).height() };

  $('#sendTo').css({
    left : ws.w / 2 - s.w / 2 + 'px',
    top : ws.h / 2 - s.h / 2 + 'px'
  });

  $('#sendTo').hide();
  $('#sendTo').slideDown();

  $('body').bind("keyup", function(e)
  {
    if (e.keyCode == 27) sendFriendCancel();
  });
}

function sendFriendSubmit()
{
  var datas = {
    send_to_a_friend : 1,
    from_name : $('input[name=from_name]').val(),
    from_email : $('input[name=from_email]').val(),
    to_name : $('input[name=to_name]').val(),
    to_email : $('input[name=to_email]').val()
  };
  $('#friendform').hide();
  $('#friendSuccess').hide();
  $('#friendProcess').show();

  setTimeout(function()
  {
    $.post(window.location.href, datas,
    function (data)
    {
      var data = eval("(" + data + ")");

      if (!data.ok)
      {
        $('#isErrorP').css('visibility', 'visible');

        for (i in data.data)
        {
          $('input[name=' + data.data[i] + ']').addClass("error");
        }

        $('#friendform').show();
        $('#friendProcess').hide();
      }
      else
      {
        $('#friendSuccess').show();
        $('#friendForm').hide();
        $('#friendProcess').hide();
      }

    });
  }, 500);

  return false;
}

function sendFriendCancel()
{
  $('#sendTo').slideUp("normal", function()
  {
     $('#sendToBg').fadeOut();
  });

  return false;
}


