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.google_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.google_search_helper = function ()
{
	if ( Blaugrana.platform != 1 )
	{
		$('#search .wrapper input').css('padding', '0 0 0 0');
		$('#search .wrapper input').css('border', 'none');
	}
}

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

			if ( $(this).attr('id') == 'home' )
			{
				$('#home img').attr('src', '/wp-content/themes/soler/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/soler/images/home-red.png');
					}
		
					$(this).removeClass('active');
					$(this).find('ul').hide();
					
				}
			);
		
		},
		function ()
		{
			$(this).find('a span').removeClass('active');
		}
	);
}

Blaugrana.twitter_helper = function ()
{

	$.ajax
	({
		url: '/wp-content/themes/soler/helpers/twitter.php',
		success: function (html)
		{
			$('#twitter').append(html);
		}
	});
	
	function first ()
	{
		$('#twitter ul li:first').fadeOut(100, function() { last(this); });
	}
	
	function last (first)
	{
		$(first).insertAfter('#twitter ul li:last').fadeIn(100);
	}
		
	ticker = setInterval(first, 6500);

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

}

Blaugrana.dynamic_trim = function (element, lines)
{
	
	// Set number of attempts before falling back
	// on default factor
	var attempts = 5;
	
	// Set percentage to trim in each iteration
	var squeeze_factor = .15;
	
	// Set default factor
	var default_factor = .15;
	
	// Begin trimming
	$(element).each
	(
		function (i)
		{
			
			// Get line-height from element
			var line_height = $(this).css('line-height');
				line_height = line_height.substr(0, line_height.length - 2);
				
			// Get actual height and calculate max height
			var height = $(this).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 = $(this).html();
						content = content.replace("\n", "");
						content = content.replace("\r", "");
						content = $.trim(content);
						
					// Calculate new length
					var length = Math.floor(content.length * (1 - squeeze_factor));
					
					// Cut out new content string
					content = content.substr(0, length-1);
					content = $.trim(content) + '…';
					
					// Cut string back 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
					$(this).html(content);
					
					// Get new height
					height = $(this).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
					$(this).html(default_content);
					
					// Get parent id
					var parent = $(this).parent().parent().attr('id');
					//console.log("#" + parent);
					
				}
				
			}
						
		}
	);
	
}

Blaugrana.tab = function (button, element)
{

	// Check if already active
	if ( $(button).attr('class') != 'active' )
	{
		
		// Hide active tab legend
		$(button).parent().children().each
		(
			function ()
			{
				$(this).removeClass('active');
			}
		);
		
		// Add active to self
		$(button).addClass('active');
		
		// Hide active tab content
		$(button).parent().parent().find('.tab').toggle();
		
	}
		
}
