var Blaugrana = {
	user_agent: navigator.userAgent.toLowerCase(),	
	platform: function ()
	{
		if (/mac/.test(this.user_agent))
		{
			return 1;
		}
		else if (/win/.test(this.user_agent))
		{
			return 2;
		}
		else
		{
			return 3;
		}
	},
	filter: function (element)
	{
		$(element).each
		(
			function ()
			{
				var content = $(this).html();
				$(this).html(content.replace(/<("[^"]*"|'[^']*'|[^'">])*>/gi, ""));
			}
		);
	},
	init: function ()
	{
		$(document).ready
		(
			function ()
			{
			
				// Activate root link in header
				$('#header').click
				(
					function (object)
					{
						if ( $(object.target).attr('id') == 'header' )
						{
							location.href = '/';
						}
					}
				);
				
				// Start Google Search helper
				Blaugrana.search_helper();

				// Start Twitter helper		
				Blaugrana.twitter_helper();
				
				// Start primary navigation helper
				Blaugrana.navigation_helper();				

				// Fix overflow on articles
				Blaugrana.dynamic_trim('#top-story h1', 2);
				Blaugrana.dynamic_trim('#top-story p', 4);
				Blaugrana.dynamic_trim('.features h2', 2);
				Blaugrana.dynamic_trim('.features p', 4);
				Blaugrana.dynamic_trim('#headlines a', 1);
				Blaugrana.dynamic_trim('#archive .post p', 2);
				
			}
		);
	}
};

Blaugrana.init();

Blaugrana.search_helper = function ()
{

	// Remove Google's inline styles
	if (Blaugrana.platform != 1)
	{
		$('#search .wrapper input').css('padding', '0 0 0 0');
		$('#search .wrapper input').css('border', 'none');
	}

	// Set label on focus
	$('#search #box').focus
	(
		function ()
		{
			$(this).val('');
		}
	);

	// Set label on blur
	$('#search #box').blur
	(
		function ()
		{
			if ( $(this).val() == '' )
			{
				$(this).val('Søk…');
			}
		}
	);

	// Activate hover on search button
	$('#search #box, #search #button').hover
	(
		function ()
		{
			$('#search #button').attr('src', '/wp-content/themes/reig/images/search-red.png');
			$('#search #box').css('color', '#801f26');
			$('#search input, #search .wrapper').css('background', 'white');
		},
		function ()
		{
			$('#search #button').attr('src', '/wp-content/themes/reig/images/search-blue.png');
			$('#search #box').css('color', '#3b4978');
			$('#search input, #search .wrapper').css('background', '#f7f7f7');
		}
	);
	
}

Blaugrana.navigation_helper = function ()
{
	$('#navigation ul li').hover
	(
		function ()
		{

			if ( $(this).attr('id') == 'home' )
			{
				$('#home img').attr('src', '/wp-content/themes/reig/images/home-blue.png');
			}

			$(this).find('a span').addClass('active');
			$(this).addClass('active');
			$(this).find('ul').show();
			$(this).hover
			(
				function ()
				{},
				function ()
				{
		
					if ( $(this).attr('id') == 'home' )
					{
						$('#home img').attr('src', '/wp-content/themes/reig/images/home-red.png');
					}
		
					$(this).removeClass('active');
					$(this).find('ul').hide();
					
				}
			);
		
		},
		function ()
		{
			$(this).find('a span').removeClass('active');
		}
	);
}

Blaugrana.twitter_helper = function ()
{

	// Get Twitter stream
	$.ajax
	({
		url: '/wp-content/themes/reig/helpers/twitter.php',
		success: function (html)
		{
			$('#twitter').append(html);
		}
	});
	
	// Fade out first item in stream
	function first ()
	{
		$('#twitter ul li:first').fadeOut(100, function() { last(this); });
	}
	
	// Fade in last item in stream
	function last (first)
	{
		$(first).insertAfter('#twitter ul li:last').fadeIn(100);
	}
		
	// Set ticker interval
	ticker = setInterval(first, 6500);

	// Pause on hover
	$('#twitter').hover
	(
		function ()
		{
			clearInterval(ticker);
		},
		function ()
		{
			ticker = setInterval(first, 6500);
		}
	);

}

Blaugrana.dynamic_trim = function (elements, lines)
{
	
	// Set number of attempts before falling back on default factor
	var attempts = 5;
	
	// Set which percentage to trim in each iteration
	var trim_factor = .15;
	
	// Set default factor
	var default_factor = .20;
	
	// Begin trimming
	$(elements).each
	(
		function (i)
		{
		
			// Set this
			var element = $(this);
			
			// Get line-height from element
			var line_height = element.css('line-height');
				line_height = line_height.substr(0, line_height.length - 2);
				
			// Get actual height and calculate max height
			var height = element.height();
			var max_height = line_height * lines;
			
			// Check overflow
			if ( height > max_height )
			{
				
				for (i = 0; i < attempts; i++)
				{
				
					// Get content of element
					var content = element.html();
						content = content.replace("\n", "");
						content = content.replace("\r", "");
						content = $.trim(content);
						
					// Calculate new length
					var length = Math.floor(content.length * (1 - trim_factor));
					
					// Cut out new content string
					content = content.substr(0, length-1);
					content = $.trim(content) + '…';
					
					// Cut string if we have a full sentence to fall back on
					if ( content.indexOf('.') > 100 )
					{
						content = content.substr(0, content.indexOf('.')+1);
					}
					
					// Replace old content with new
					element.html(content);
					
					// Get new height
					height = element.height();
					
					// Test new content length
					if ( height <= max_height )
					{
						var result = 1;
						break;
					}
					else
					{
						var result = 0;
					}
					
				}
				
				if ( result == 0 )
				{
				
					// Fall back on default factor
					var default_length = length * (1 - default_factor);
					var default_content = content.substr(0, default_length);
						default_content = $.trim(default_content) + '…';
					
					// Replace content
					element.html(default_content);
					
					// Get parent id
					var parent = element.parent().parent().attr('id');
					
				}
				
			}
						
		}
	);
	
}

Blaugrana.test_header = function (source)
{
	$('#header').css('background', "url('http://blaugrana.no/wp-content/themes/reig/images/" + source + "') no-repeat");
}
