// JavaScript Document
$(document).ready(function() 
{
	$('#product_colours img.product_image').click(function()
	{
		var _src = $(this).attr('src');
		var _parts = _src.split('_',3);
		_src = _src.replace(/_thumb/,'');
		_t_src = _src.replace(/media\/images\//,'images/thumb.html?img=');
		$('#product_image img').attr('src',_t_src+'&h=420&w=300').parent().attr('href',_src);
		if (_parts.length == 3)
		{
			$('#product_options_colour').val(_parts[1]);
		}
		return false;
	});
	
	$('#product_matching a').click(function()
	{
		$('html, body').animate({scrollTop: '600px'}, 500);
		return false;
	});
	
	$('#product_image li.image a').prettyPhoto({animationSpeed:'normal',padding:40,opacity:0.35,showTitle:false,allowresize:true,counter_separator_label: ' of ',theme:'light_square'});

	$('#product_image li.zoom').show();

	$('#product_image li.zoom a').click(function()
	{
		$('#product_image li.image a:first').trigger('click');
		return false;
	});
	
	var _colour_select = $('#product_options_colour');
	var _size_select = $('#product_options_size');
	if (_colour_select.length == 1 && _size_select.length == 1)
	{
		var _id = $('#p_id').val();

		_colour_select.attr("disabled", true).change(function()
		{
			$('#product_options_price').html('&nbsp;');
			
			$.getJSON("/ajax.php",{f: 2, id: $('#p_id').val(), si: $('#product_options_size').val(), ci: $(this).val()}, function(_j)
			{
				if (_j.length == 1)
				{
					var _html = '&pound;'+_j[0].Price;
					if (parseFloat(_j[0].RRP) > parseFloat(_j[0].Price))
					{
						_html += '<span>RRP<br /><del>&pound;'+_j[0].RRP+'</span>';
					}
					$('#product_options_price').html(_html);
				}
			});
		});
			
		$('#product_options_size').change(function()
		{
			_colour_select.attr("disabled", true);
			_cur_colour = $('#product_options_colour').val();
			if ($(this).val() != '0')
			{
				$.getJSON("/ajax.html",{f: 1, id: $('#p_id').val(), si: $(this).val()}, function(_j)
				{
					if (_j.length == 0)
					{
						var _options = '<option value="0" selected="selected">Not available</option>';
					}
					else if (_j.length == 1)
					{
						var _options = '<option value="'+_j[0].Id+'" selected="selected">' + _j[0].Description + '</option>';
						$('#colour_'+_j[0].Id).addClass('av');
						var _html = '&pound;'+_j[0].Price;
						if (parseFloat(_j[0].RRP) > parseFloat(_j[0].Price))
						{
							_html += '<span>RRP<br /><del>&pound;'+_j[0].RRP+'</span>';
						}
						$('#product_options_price').html(_html);
				}
					else
					{
						var _options = '<option value="0" selected="selected">Now select colour</option>';
						for (var _i = 0; _i < _j.length; _i++) 
						{
							if (_cur_colour == _j[_i].Id)
							{
								_options += '<option selected="selected" value="' + _j[_i].Id + '">' + _j[_i].Description + '</option>';
							}
							else
							{
								_options += '<option value="' + _j[_i].Id + '">' + _j[_i].Description + '</option>';
							}
							$('#colour_'+_j[_i].Id).addClass('av');
						}
						var _html = '&pound;'+_j[0].Price;
						if (parseFloat(_j[0].RRP) > parseFloat(_j[0].Price))
						{
							_html += '<span>RRP<br /><del>&pound;'+_j[0].RRP+'</span>';
						}
						$('#product_options_price').html(_html);
					}
					
					$('#product_colours ul.product_colour').each(function()
					{
						var _this = $(this);
						if (_this.hasClass('av'))
						{
							_this.show('normal');
							_this.removeClass('av');
						}
						else
						{
							_this.hide('normal');
						}
					});
					
					_colour_select.html(_options).attr('disabled',false);
					_colour_select.selectedIndex = 0;
				});
			}
			else
			{
				_colour_select.html('<option value="" selected="selected">Then select colour</option>');
				$('#product_colours ul.product_colour').each(function()
				{
					$(this).show('normal');
				});
			}
		});
	}
	else
	{
		$('#product_options_size').change(function()
		{
			if ($(this).val() != '0')
			{
				$.getJSON("/ajax.html",{f: 2, id: $('#p_id').val(), si: $(this).val(), ci: this.form.ci.value}, function(_j)
				{
					if (_j.length == 1)
					{
						var _html = '&pound;'+_j[0].Price;
						if (parseFloat(_j[0].RRP) > parseFloat(_j[0].Price))
						{
							_html += '<span>RRP<br /><del>&pound;'+_j[0].RRP+'</span>';
						}
						$('#product_options_price').html(_html);
					}
				});
			}
		});
	}
	
	$('#product_options_add_basket input.button').click(function()
	{
		var _size_select = $('#product_options_size');
		if (_size_select.length == 1 && $('#product_options_size').val() == 0)
		{
			alert ('You must select a size');
		}			
		else if (_colour_select.length == 1 && $('#product_options_colour').val() == 0)
		{
			alert ('You must select a colour');
		}
	
		else
		{
			var _form = this.form;
			$('li.activity:first').html('&nbsp;<br/>&nbsp;');
			$.getJSON("/buy.html",{pid: $(_form.pid).val(), si: $(_form.si).val(), ci: $(_form.ci).val(), q: $(_form.q).val(), ajax: 1}, function(_j)
			{
				var _activity = '';
				switch (_j.sts)
				{
					case 1:
						_activity = 'You have added an item to your basket';
					break;
					
					case 2:
						_activity = 'You have increased the quantity of an item in your basket';
					break;
					
					default:
						return false;
				}
				
				if (_activity != '')
				{
					if ($('li.activity').length > 0)
					{
						$('li.activity:first').html(_activity);
					}
					else
					{
						$('#product_options ul').append('<li class="activity">'+_activity+'</li><li class="activity" id="activity_basket"><a href="/basket.html" title="Checkout now">Checkout now</a></li>');
					}
				}
				
				if (_j.basket)
				{
					$('#header_right li.basket a').text(_j.basket);
				}
			});
		}
		return false;
	});

	var _ranges = $('#colour_ranges');
	if (_ranges.length == 1)
	{
		var _content = _ranges.html();
		var _height = $('#results').height(); 
		$('#results').css('min-height',$('#results').height());
		_ranges.remove();
		$('#left_navigation ul:first').after('<h2 class="heading">Narrow by colour</h2><ul id="colour_ranges">'+_content+'</ul>');
		$('#colour_ranges li a').click(function()
		{
			var _parent = $(this).parent();
			var _range = $(this).attr('title');
			if (_parent.hasClass('open'))
			{
				// Remove all filters
				$('#colour_ranges li').removeClass('open');
				$('ul.category').removeClass('fc');
			}
			else
			{
				// Add filter
				$('#colour_ranges li').removeClass('open');
				_parent.addClass('open');
				$('span.colour_ranges').each(function()
				{
					var _ranges = $(this).html();
					var _root = $(this).parent().parent();
					
					if (_ranges.indexOf('-'+_range+'-') >= 0)
					{
						_root.removeClass('fc');
					}
					else
					{
						_root.addClass('fc');
					}
				});
				
			}
			
			var _counter = 0;
			$('ul.category').each(function()
			{
				var _this = $(this);
				_this.removeClass('end');
				if (_this.hasClass('fc'))
				{
					_this.hide('normal');
				}
				else
				{
					if (_counter++ == 3)
					{
						_this.addClass('end');
						_counter = 0;
					}
					_this.show('normal');
				}
			});
			return false;
		});
	}
	
	var _search_q = $('#search_q').focus(function() {if ($(this).val() == 'Search...') $(this).val('').removeClass('default');}).blur(function() {if ($(this).val() == '') $(this).val('Search...').addClass('default');}); 
	if (_search_q.val() == '') {_search_q.val('Search...').addClass('default');}
	_search_q.parent().submit(function(){return (_search_q.val() != 'Search...' && _search_q.val() != '');});
	
});
