// JavaScript Document

function submit_buy (_id)
{
   var _form = $('#cat_form_'+_id);

   if (_form.length == 1)
   {
      _form[0].submit();
      return false;
   }
   
   return true;
}

var _rotate_timer = 0;
var _rotate_length = 0;
var _counter = 0;

$(document).ready(function()
{
	$('div.booster').hide();
	$('h3.booster a').addClass('toggle');
	$('h3.booster a.toggle').click(function()
	{
		$(this).parent().next().next().toggle('slow');
		return false;
	});

	$('form.selected').submit();

	$("a[rel^='prettyPhoto']").prettyPhoto({showTitle: false, allowresize: true, theme: 'dark_square'});
	
	$('select.manu').change(function()
	{
   		$('select.chassis').html('<option value="">Please wait</option>');
   		$('select.model').html('');
   		$('select.brand').html('');
   		$('#wheel_size').html('');
		$.getJSON('/wheel-search.html',{man: $(this).val(), ajax: 1}, function(_response)
		{
      		var _options = '';
        	$.each(_response, function(_i,_chassis)
         	{
				_options += '<option value="' + _chassis.Id + '">' + _chassis.Name + '</option>';
			});

			if (_options.length > 0)
			{
				$('select.chassis').html('<option value="">Please select</option>'+_options);
			}
			else
			{
				$('select.chassis').html('<option value="">None found</option>');
			}
    	});
    	$('select.manu').attr('selectedIndex',this.selectedIndex);
  	});

	$('select.chassis').change(function()
	{
   		$('select.model').html('<option value="">Please wait</option>');
   		$('select.brand').html('');
	   		$('#wheel_size').html('');

		$.getJSON('/wheel-search.html',{cha: $(this).val(), ajax: 1}, function(_response)
		{
      		var _options = '';
        	$.each(_response, function(_i,_model)
         	{
				_options += '<option value="' + _model.Id + '">' + _model.Name + '</option>';
			});

			if (_options.length > 0)
			{
				$('select.model').html('<option value="">Please select</option>'+_options);
			}
			else
			{
				$('select.model').html('<option value="">None found</option>');
			}
    	});
    	$('select.chassis').attr('selectedIndex',this.selectedIndex);
  	});

	$('select.model').change(function()
    {
      var _man = this.form.manu;
  		$('#wheel_size').html('');

      var _car = _man.options[_man.selectedIndex].text + ' ' + this.options[this.selectedIndex].text;
      $('input.selected_car').val(_car);
    	$('select.model').attr('selectedIndex',this.selectedIndex);
	});

	$('#product_size').change(function()
	{
      var _id = $(this).val();

		$('.select_tyres').hide();
		$('#tyres_'+_id).show().trigger('change');
 	});


   $('select.select_tyres').change(function()
   {
		var _id = $(this).val();
		var _count = 0;

   	$('form.buy_package').each(function()
		{
        if (this.size_id.value == _id)
        {
            _count++;
            if (_count > 1)
            {
               $(this.parentNode).show();
            }
            else
            {
               $(this.parentNode).hide();
               $('#main_product_id').val(this.product_id.value);
               $('#main_tyre_id').val(this.tyre_id.value);
               $('#main_price').html(this.price.value);
            }
        }
        else
        {
            $(this.parentNode).hide();
        }
        if (_count > 1)
        {
          $('#othertyres_title').show();
        }
        else
        {
          $('#othertyres_title').hide();
        }
      });

   });
   $('.btn_buynow').live('click', function()
   {
      var _id = this.form.product_id.value;
      var _tyre_id = this.form.tyre_id.value;
      var _car = $('#car').val();
  		$.getJSON('/setcart.html',{id: _id, tyre_id: _tyre_id, car: _car, ajax: 1}, function(_response)
		{
        window.location='/checkout.html';
    	});
    	return false;
   });


   $('.btn_buy_tyre').click(function()
   {
      var _id = this.form.product_id.value;
      var _car = $('#car').val();
      var _qty = $('#product_qty').val();
  		$.getJSON('/setcart.html',{id: _id, car: _car, q: _qty, ajax: 1}, function(_response)
		{
        window.location='/checkout.html';
    	});
    	return false;
   });

	$('select.model').change(function()
	{
		$.getJSON('/tyre-search.html',{mod: $(this).val(), ajax: 1}, function(_response)
		{
     		var _options = '';
        	$.each(_response, function(_i,_size)
         	{
				_options += '<option value="' + _size.Id + '">' + _size.Name + '</option>';
			});

			if (_options.length > 0)
			{
				$('#wheel_size').html('<option value="">Please select</option>'+_options);
			}
			else
			{
				$('wheel_size').html('<option value="">None found</option>');
			}

  	   });
  });

  $('#find_tyres').click(function()
  {
    if (this.form.size.selectedIndex == 0)
    {
      alert('Please select a size');
      return false;
    }
  	else
  	{
   		var _car = this.form.manu.options[this.form.manu.selectedIndex].text + ' ' + this.form.model.options[this.form.model.selectedIndex].text;
   		_car = _car.toLowerCase().replace(/[^a-z0-9-]+/g, '-').replace(/-$/,'');
   		var _action = this.form.action;
   		this.form.action = _action + '&car=' + escape(_car);
   	}
    return true;
  });

  $('#wheelSearch .search_btn, #ProductContainer .search_btn2 ').click(function()
  {
    if (this.form.model.selectedIndex == 0)
    {
      return false;
    }
	else
	{
		var _car = this.form.manu.options[this.form.manu.selectedIndex].text + ' ' + this.form.model.options[this.form.model.selectedIndex].text;
   		_car = _car.toLowerCase().replace(/[^a-z0-9-]+/g, '-').replace(/-$/,'');
		var _action = this.form.action;
		this.form.action = _action + '&car=' + escape(_car);
	}
    return true;
  });

  $('#tyre_size_buy').change(function()
  {
    var _price = $('#price_'+this.value).val();
      $('#main_price').html('&pound;' + _price);
      $('#product_price').val(_price);
      $('#product_qty').trigger('change');
  });

  $('#quick_search').focus(function() {if (this.value == 'Quick Search'){this.value = '';}}).blur(function() {if (this.value == ''){this.value = 'Quick Search';}});

  $('#quick_search_form').submit(function()
  {
	  var _q = $('#quick_search').val();
	  if (_q != 'Quick Search' && _q != '')
	  {
		  return true;
	  }

	  return false;
	});

   $('a.enlarge_link').click(function()
   {
	   var _container = $(this).parent().parent();
	   $("a[rel^='prettyPhoto']",_container).trigger('click');
	   return false;
   });

   $('#product_qty').change(function()
   {
     var _qty = parseInt($(this).val());
     var _price = $('#product_price').val();
     if (_qty == 0) _qty = 1;
     _price = parseFloat(_price).toFixed(2) * _qty;
      $('#main_price').html('&pound;' +  _price.toFixed(2));
   });
   
   $('#shipping_id').change(function()
   {
     if (this.selectedIndex == 1)
     {
       $('#order_total').html($('#sub_total').html());
       $('#delivery_total').html('Free');
     }
     else
     {
       $('#order_total').html($('#cart_total').val());
       $('#delivery_total').html($('#delivery_cost').val());
     }
   });
   
   $('#tyre_search_width').change(function()
   {
	   $.getJSON("/tyresize.php",{width: $(this).val()}, function(_j)
	   {
		   $('#tyre_search_profile').html('<option value="">Please wait</option>');
		   var _options = '<option value="">Profile</option>';
		   for (var _i=0;_i<_j.length;_i++) 
		   {
			   _options += '<option>' + _j[_i] + '</option>';
		   }
		   $('#tyre_search_profile').html(_options);
	   });
	   return false;
   });

   $('#tyre_search_profile').change(function()
   {
	   $.getJSON("/tyresize.php",{profile: $(this).val(),width: $('#tyre_search_width').val()}, function(_j)
	   {
		   $('#tyre_search_size').html('<option value="">Please wait</option>');
		   var _options = '<option value="">Size</option>';
		   for (var _i=0;_i<_j.length;_i++) 
		   {
			   _options += '<option>' + _j[_i] + '</option>';
		   }
		   $('#tyre_search_size').html(_options);
	   });
	   return false;
   });
   
	_length = $('.homeFeaturedBanner div').length - 1;
	_counter = _length;
   	_rotate_timer = setInterval ('rotate()', 6000 );

});

function rotate()
{
	if (_counter == _length)
	{
		$('.homeFeaturedBanner div').eq(_length).fadeIn('slow',function()
		{
			$('.homeFeaturedBanner div').not(_length).fadeIn(0);
		});
		_counter = 0;
	}
	else
	{
		var _hide = _length-_counter;
		$('.homeFeaturedBanner div').eq(_hide).fadeOut('slow');
		_counter++;
	}
}
