$(document).ready(function()
{
	$('a:not(.submit)[href*="#"]').click(
		function()
		{
			if($(this).attr('href').length > 1 && location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname)
			{
				var $target = $(this.hash);
					$target = $target.length && $target || $('[@name=' + this.hash.slice(1) +']');
					if ($target.length)
					{
						var targetOffset = $target.offset().top;
						$('html,body').animate({scrollTop: targetOffset}, 1000);

						return false;
					}
			}
		}
	);

	$('.submit', '#main').each(
		function()
		{
			$(this).parent().append('<input type="submit" class="hidealways" />');
		}
	);

	$('.submit', '#main').click(
		function()
		{
			var form = getParentForm($(this));
				if(form != null) form.submit();

			return false;
		}
	);

	$('.alert:not(noscript)', '#main').each(
		function(i)
		{
			$(this).delay((i+1) * 300).fadeIn(1500);
		}
	);
	
	$('input[type="checkbox"]', '#filterform').click(
		function()
		{
			$('input[type="checkbox"]', '#filterform').each(
				function()
				{
					if(!$(this).attr('checked')) $(this).attr('disabled', 'disabled');
				}
			);
			
			$('#filterform').submit();
		}
	);
	
	$('#tags_reset').click(
		function()
		{
			$('input[type="checkbox"]', '#filterform').each(
				function()
				{
					$(this).removeAttr('checked').attr('disabled', 'disabled');
				}
			);
			
			$('#filterform').submit();
			
			return false;
		}
	);
	
	$('.select-sort', '#main').change(
		function()
		{
			var url = window.location.href;
				url = url.indexOf('?') > 0 ? url.substr(0, url.indexOf('?')) : url;
			
			var s = parseInt($(this).val());
				if(isNaN(s)) s = 1;
				url += '?s=' + s;
			
			window.location.href = url;
			
			return;
		}
	);
	
	$('.voting', '#main .product-list').each(
		function()
		{
			var vv = parseFloat($(this).attr('title'));
			for(var i = 1; i <= 5; i++)
			{
				var star = $('.star_' + i + ' span', this);
				if(i <= Math.floor(vv))
				{
					star.css('width', '20px');
				}
				else
				{
					if(i - 1 < vv)
					{
						star.css('width', (20 * (vv - Math.floor(vv))) + 'px');
					}
					else
					{
						star.css('width', '0');
					}
				}
			}
		}
	);
	
	initVoting();
});


var getParentForm = function(element)
{
	var check = element.parent();
	while(!check.is('form') && !check.is('html'))
	{
		check = check.parent();
	}

	if(check.is('form'))
	{
		return check;
	}

	return null;
}


var initVoting = function()
{
	initVotingDisplay();
	
	$('#voting').mouseout(
		function(event)
		{
			initVotingDisplay();
		}
	);
	
	$('.star', '#voting').mouseover(
		function(event)
		{
			var nodeId = $(this).prevAll().length;
			for(var i = 1; i <= 5; i++)
			{
				if(nodeId >= i - 1)
				{
					$('span', '#star_' + i).css('width', '20px');
				}
				else
				{
					$('span', '#star_' + i).css('width', '0px');
				}
			}
		}
	);
	
	$('.star', '#voting').click(
		function(event)
		{
			$('#vote_value').val($(this).prevAll().length + 1);
			initVotingDisplay();
		}
	);
}

var initVotingDisplay = function()
{
	var selectedVoteValue = $('#vote_value').val();
	var currentVoteValue = selectedVoteValue == '' ? parseFloat($('#vote_current_value').val()) : parseFloat(selectedVoteValue);
	
	for(var i = 1; i <= 5; i++)
	{
		if(i <= Math.floor(currentVoteValue))
		{
			$('span', '#star_' + i).css('width', '20px');
		}
		else
		{
			if(i - 1 < currentVoteValue)
			{
				$('span', '#star_' + i).css('width', (20 * (currentVoteValue - Math.floor(currentVoteValue))) + 'px');
			}
			else
			{
				$('span', '#star_' + i).css('width', '0');
			}
		}
	}
}
