(function($)
{
	$(function()
	{

    var articlePage = 0;
    var cachedPages = 0;
    var commentPage = 0;
    var cachedComments = 0;
    var errorContainer = $('<div class="comments-error">Alla obligatoriska fält måste fyllas i!</div>').appendTo("#commentform").hide();
    var errorLabelContainer = $('<div class="error errorlabels comments-errorlabels"></div>').appendTo("#commentform").hide();

		$.ca = {
			functions: {
				followURL: function(url) {
					window.location = url;
					return false;
				}
			}
		}
		
		$("#sub-navigation:not('.home-team') li a").click(function() 
		{ 
		  $(this).attr("target","_blank"); 
		});
		
		$("a.external-url, #sub-navigation.home-team a.external-url").click(function() 
		{ 
		  $(this).attr("target","_blank"); 
		});
		
		$(".ajax-nav a").click(function()
		{
			$(this).blur();
		});

	  $("#news-main,div.list-older li,#news-older-content li, #recent-quote-contents .quote").livequery(function()
		{
			$(this).hover(function() {
			 $(this).addClass("hover");
		  }, function() 
		  {
			 $(this).removeClass("hover");
		  }).click(function()
		  {
			 $.ca.functions.followURL($(this).find("a:first").attr("href"));
		  }).css('cursor','pointer');
		});
		
		$('#small-nav-next').click(function()
		{
			if($("#news-older").is(".in-motion")) return false;
			if(articlePage >= cachedPages)
			{
				$("#news-older").addClass("in-motion");
				$.ajax({
					url: '?action=article&page='+(articlePage+1)+'&ajax=true',
					cache: false,
					success: function(html)
					{
						if(html.length > 10)
						{
							var w = $('#news-older-content-list').width() + 624;
							$("#news-older-content-list").css('width', w + 'px').append(html);
							
							var l = parseInt($('#news-older-content').css('left'));
							$("#news-older-content").animate({ left: (l-624) + 'px' }, 500, "easeInOutQuart", function() {
                $("#news-older").removeClass("in-motion");
              });
							
							articlePage++;
							cachedPages++;
						} else
						{
							$("#news-older").removeClass("in-motion");
						}
					}
				});
			}
			else
			{
				$("#news-older").addClass("in-motion");
				articlePage++;
				var l = parseInt($('#news-older-content').css('left'));
				$("#news-older-content").animate( { left:(l-624)+'px' }, 500, "easeInOutQuart", function() {
          $("#news-older").removeClass("in-motion");
        });
			}
			return false;
		});
		
		$('#small-nav-prev').click(function()
		{
			if($("#news-older").is(".in-motion")) return false;
			if(articlePage < 1) return false;
			$("#news-older").addClass("in-motion");
			var l = parseInt($('#news-older-content').css('left'));
			$("#news-older-content").animate( { left:(l+624)+'px' }, 500, "easeInOutQuart", function() {
        $("#news-older").removeClass("in-motion");
      });
			articlePage--;
			return false;
		});
		
		$('#comment-nav-next').click(function()
		{
			if($("#recent-comments").is(".in-motion")) return false;
			if(commentPage >= cachedComments)
			{
				$("#recent-comments").addClass("in-motion");
				$.ajax({
					url: '/?action=comment&comment='+(commentPage+1)+'&ajax=true',
					cache: false,
					success: function(html)
					{
						if(html == 0)
						{
							commentPage--;
							cachedComments--;
							$("#recent-comments").removeClass("in-motion");
						}
						else
						{
							var w = $('#recent-quote-contents').width() + 624;
							$("#recent-quote-contents").css('width',w + 'px').append(html);
							
							var l = parseInt($('#recent-quote-contents').css('left'));
							$("#recent-quote-contents").animate( { left:(l-624)+'px' }, 500, "easeInOutQuart", function() {
								$("#recent-comments").removeClass("in-motion");
							});						
						}
					}
				});
				cachedComments++;
			}
			else
			{
				$("#recent-comments").addClass("in-motion");
				var l = parseInt($('#recent-quote-contents').css('left'));
				$("#recent-quote-contents").animate( { left:(l-624)+'px' }, 500, "easeInOutQuart", function() {
          $("#recent-comments").removeClass("in-motion");
        });
			}
			commentPage++;
			return false;
	  });
	
		$('#comment-nav-prev').click(function()
		{
      if($("#recent-comments").is(".in-motion")) return false;
			if(commentPage < 1) return false;
			$("#recent-comments").addClass("in-motion");
			var l = parseInt($('#recent-quote-contents').css('left'));
			$("#recent-quote-contents").animate( { left:(l+624)+'px' }, 500, "easeInOutQuart", function() {
        $("#recent-comments").removeClass("in-motion");
      });
			commentPage--;
			return false;
		});
		
	  $("#commentform").validate({
	    rules: {
	      author: "required",
	      email: {
	        required: true,
	        email: true
	      },
	      url: "url",
	      comment: "required"
	    },
	    errorContainer: errorContainer,
	    errorLabelContainer: errorLabelContainer,
	    ignore: ":hidden"
	  });
	  $.validator.messages.required = "";
	  $.validator.messages.email = "<strong>E-post:</strong> Ange en giltig e-postadress";
	  $.validator.messages.url = "<strong>Sajt:</strong> Ange en giltig URL/webbadress";
		
  });
})(jQuery);

