// Metal Mule custom scripts

$(document).ready(function(){
	// Tabs
	$('#product_tabs .tab:not(:first)').hide();
	$('#product_tabs #tabs li').click(function(e) 	{
		$('#product_tabs .tab').hide(); 
		$('#product_tabs li.current').removeClass("current"); 
		$(this).addClass('current');
		var clicked = $(this).find('a:first').attr('href'); 
		$('#product_tabs '+clicked).fadeIn('fast');
		return false;
	});
	
	$('.tab-link').click(function(){
		$('#product_tabs .tab').hide(); 
		$('#product_tabs li.current').removeClass("current"); 
		var clicked = $(this).attr('href'); 
		$('#tabs a[href$="'+clicked+'"]').parent().addClass('current');
		$('#product_tabs '+clicked).fadeIn('fast');
		return false;
	});
	
	
	// bikes
	
	$('#bike-make .make a').click(function(){
		$('.make').parent().removeClass('active-make');
		$(this).parent().parent().addClass('active-make');
		return false;
	});
	
	
	// Video lightbox
	$('.video').fancybox({
		transitionIn: 'elastic',
		transitionOut: 'elastic',
		autoSize : true,
		title: false
	});

	
	
	// Calculate option price
	$('.option').change(function() {
		var product_option_value_id = $(this).val();
		var str = $(this).closest('.add_form').serialize();
		$.ajax({
			type: "POST",
			url: "/ajax/get-price.php",
			data: str,
			success: function(msg){
				total_price = parseFloat(msg);
				$('.total_price').html(total_price);
			}
		});
		$('.option').each(function() {
			var product_option_value_id = $(this).val();
			var product_option_id = $(this).attr('name');
			$.ajax({
				type: "POST",
				url: "/ajax/get-price.php",
				data: str+'&product_option_value_id='+product_option_value_id+'&product_option_id='+product_option_id,
				success: function(msg){
					var total_price = $('.total_price').html();
					difference_price = parseFloat(msg) - parseFloat(total_price);
					difference_price = difference_price.toFixed(2)
					if(difference_price > 0) {
						sign = '+';
					} else {
						sign = '-';
						difference_price = difference_price * -1;
					}
					if(difference_price) {
						$('#option_price_'+product_option_value_id).html('['+sign+'\u00A3'+difference_price+']');
					}
				}
			});
        });
		$('#option_price_'+product_option_value_id).html('');
	});
	
	
	// Learn More
	$('.option_descriptions').hide();
	$('.learn_more').click(function() {
		var option = $(this).attr('rel');
		$('#'+option).slideToggle();
		return false;
	});
	
	
	// Add to basket
	$('.addToCart').live('click',function() {
		var go_to_basket = $(this).attr('data-basket');
		var num_items = $('#num_items').html();
		var price = $('.total_price').html();
		var basket_total = $('#header_basket_total').html();
		var str = $(this).closest('.add_form').serialize();
		$.ajax({
			type: "POST",
			url: "/controllers/general.php",
			data: str+'&add_to_basket=1&package_product_id=0&price='+price,
			success: function(msg){
				if(go_to_basket=='true') {
					window.location = '/basket';
				} else {
					$('#basket_info').load(location.href+" #basket_info","");
					$('#added_to_basket').slideDown();
					$('#added_to_basket').delay(2000).slideUp();
				}
			}
		});
		
		return false;
	});
	$('#update').live('click',function() {
		return false;
	});

$('.bike_shown').show();
$('.bike_shown').delay(5000).slideUp();
$('.show_bike').click(function() {
	$('#bike_model').slideToggle();
	return false
});

$('.close a').click(function() {
	$('#bike_model').slideUp();
	return false;
});
	// Gallery
	$('#productImg .larges a').fancybox();
	
	$('#productImg .thumbs li a').click(function() {
		$('#productImg .larges li').fadeOut();
		var img_id = $(this).attr('rel');
		$('#'+img_id).fadeIn();
		return false;
	});
	
	// Stars
	$('.stars').stars({
		inputType: "select",
		cancelShow: false
	});	
	
	// Slider
	$("#slider").jCarouselLite({
        btnNext: ".next",
        btnPrev: ".prev",
		visible: 6,
		auto: 4000
    });
	
	$('#slider a').live('click',function() {
		var banner_id = $(this).attr('data-banner');
		var video = $(this).attr('data-video');
		var url = location.href;
		var url_parts = url.split('?');
		var main_url = url_parts[0]; 
		$('#load-banner').fadeOut('fast',function(){
			// load content/video
			$('#load-banner').load(main_url+"?banner=" + banner_id + " #banner","",function(){
				if(video!='') {
					_V_("banner_video").ready(function(){
						$('#load-banner').delay(600).fadeIn();
					});
				} else {
					$('#load-banner').delay(600).fadeIn();
				}
			});
		});
		return false;
	});
	
	$('#click').click(function() {
			_V_("banner_video").ready(function(){
				  var myPlayer = this;		
				//  alert('hello');
				  // EXAMPLE: Start playing the video.
				 // myPlayer.play();
			});
			return false;
	});
	
	
	// Basket
	$('.remove-Item').live('click',function() {
		var msg = $(this).attr('title');
		var show_confirm = $(this).attr('data-confirm');
		var remove = 1;
		if(show_confirm=='true') {
			if(!confirm('Are you sure you would like to '+msg)) {
				remove = false;
			}
		}
		
		if(remove) {
			$(this).parent('li').fadeOut();
			$(this).parents('tr').fadeOut();
			var purchase_product_id = $(this).attr('data-purchase-product-id');
			var package_product_id = $(this).attr('data-package-product-id');
			var basket_id = $('#basket_id').val();
			$.ajax({
				type: "POST",
				url: "/controllers/general.php",
				data: 'remove_from_basket=1&package_product_id='+package_product_id+'&purchase_product_id='+purchase_product_id+'&basket_id='+basket_id,
				success: function(msg){
					if(show_confirm == 'true') {
						$('#basket').load(location.href+" #basket_form","");
					} else {
						$('#add_luggage_'+package_product_id).removeClass('added_luggage_item');
						$('#add_luggage_'+package_product_id).addClass('add_luggage_item');
						$('#add_luggage_'+package_product_id).html('add');
						$('#luggage-basket').load(location.href+" #luggage-basket section","");
					}
				}
			});
		}
		return false;
		
	});
	$('#empty').live('click',function() {
		if(!confirm('Are you sure you would like to empty your basket?')) {
			return false;
		}
	});
	
	$('.quantity').live('change',function() {
		var str = $('#basket_form').serialize();
		$.ajax({
			type: "GET",
			url: "/controllers/general.php",
			data: str,
			success: function(msg){
				$('#basket').load(location.href+" #basket_form","");
			}
		});
		return false;
	});

	$('#submit_review').click(function() {
		var error = true;
		var str = $('#review_form').serialize();
		var name = $('#name').val();
		var email = $('#review_email').val();
		var summary = $('#summary').val();
		var review = $('#review').val();
		if(name=='') {
			$('#name').parent().addClass('error');
			error = true;
		} else {
			error = false;
		}
		if(email=='') {
			$('#review_email').parent().addClass('error');
			error = true;
		} else {
			error = false;
		}
		if(review=='') {
			$('#review').parent().addClass('error');
			error = true;
		} else {
			error = false;
		}
		if(summary=='') {
			$('#summary').parent().addClass('error');
			error = true;
		} else {
			error = false;
		}
		if(!error) {
			$.ajax({
				type: "POST",
				url: "/includes/form-processor.php",
				data: str+'&submit_review=1',
				success: function(msg){
					//alert(msg);
					$('#form_content').html('<li>Thank you for your review. It will appear on the website once it has been approved.</li>')
				}
			});
		}
		return false;
	});


	
	$('body').delegate('#shipping_country','change',function() {
		var shipping_country_id = $(this).val();
		$('#shipping_amount').html('\u00A3'+'0.00');
		$('#shipping_rates').load(location.href+"?shipping_country_id="+shipping_country_id+" #shipping_rates_select","");
	});
	
	$('body').delegate('#shipping_rates_select','change',function() {
		var shipping_country_id = $('#shipping_country').val();
		var basket_id = $('#basket_id').val();
		var shipping_rate_id = $(this).val();
		$.ajax({
			type: "POST",
			url: "/ajax/shipping.php",
			data: 'shipping_country_id='+shipping_country_id+'&shipping_rate_id='+shipping_rate_id+'&basket_id='+basket_id,
			success: function(msg){
				$('#shipping_amount').html('\u00A3'+msg);
				current_total = $('#basket_total').val();
				total = parseFloat(current_total) + parseFloat(msg);
				total = total.toFixed(2)
				$('#basket-totals').load(location.href+" #basket-totals ul","");
			}
		});
		return false;
	});


	// Checkout
	$('#email').blur(function() {
		var email = $(this).val();
		if(email != '') {
			$.ajax({
				type: "POST",
				url: "/ajax/check-email.php",
				data: 'email='+email,
				success: function(msg){
					if(msg==1) {
						$('#login_email').val(email);
						$('#login_password').focus();
						$('#email').after('<p class="error">Email address already has an account, please login or use another email address</p>');
					}
				}
			});
		}
	});
	
	$('#login_email').blur(function(){
		var email = $(this).val();
		$('#forgot').attr('href','/forgot?email='+email);
	});
	
	
	//$('#forgot_dialog').dialog();
	
	$('.address .button label, .address .button input').click(function() {
		var button_type = $(this).parent().attr('title');
		$('.button[title="'+button_type+'"]').removeClass('selected');
		$(this).parent().addClass('selected');
	});
	
	
	// Luggage builder
	
	if($('#luggage-basket').length) {
		var msie6 = $.browser == 'msie' && $.browser.version < 7;
		
		var luggaage_basket_height = $('#luggage-basket').height();
		var window_height = $(window).height();
		
		if (msie6 || luggaage_basket_height > window_height) { } else {
			var top = $('#luggage-basket').offset().top - parseFloat($('#luggage-basket').css('margin-top').replace(/auto/, 0));
			$(window).scroll(function (event) {
				// what the y position of the scroll is
				var y = $(this).scrollTop();
				
				// whether that's below the form
				if (y >= top) {
					// if so, ad the fixed class
					$('#luggage-basket').addClass('fixed');
				} else {
					// otherwise remove it
					$('#luggage-basket').removeClass('fixed');
				}
			});
		}  
	}
	
	
	$('.add_luggage_item').live('click',function() {
		selector = $(this);
		var str = $(this).closest('.add_form').serialize();
		$.ajax({
			type: "POST",
			url: "/controllers/general.php",
			data: str+'&add_to_basket=1',
			success: function(msg){			
				$('#luggage-basket').load(location.href+" #luggage-basket section","");
				$(selector).removeClass('add_luggage_item');
				$(selector).addClass('added_luggage_item');
				$(selector).html('added');
			}
		});
		return false;
	});
	
	
	// Account
	$('.rowlink').click(function() {
		var url = $(this).attr('data-link');
		if(url) {
			window.location.href = url;
		}
		return false;
	});
	
	
});


