

$(document).ready(function() {
	basket_plus_minus_click();
	options_hover();
	option_dropdown_button_click();
	option_dropdown_click();
	basket_remove_hover();
	basket_record_remove();
	delivery_option_change();
	continue_shopping();
	basket_proceed();
});


function basket_plus_minus_click() {
	$('.plus_minus.basket_page').click(function () {
		var this_id = $(this).attr('id');
		var this_id_split = this_id.split('_');
		var direction = this_id_split[0];
		var basket_id = this_id_split[1];
		var curr_val = $('#qty_'+basket_id).val();
		var max_qty = $('#max_qty_'+basket_id).html();
		var item_price = $('#clean_price_'+basket_id).html();
		if (direction == 'minus') {
			basket_minus_click(basket_id,curr_val,item_price);
		}
		else {
			basket_plus_click(basket_id,curr_val,item_price, max_qty);
		}
	});
}

// PLUS CLICK
function basket_plus_click(basket_id,curr_val,item_price, max_qty) {
	curr_val++;
	if (curr_val <= max_qty) { 
		update_qty_change(basket_id,curr_val,item_price);
		write_qty_change(basket_id,curr_val);
		update_subtotal_value();
		//update_promo_value();
		update_delivery_value();
		update_total_value();
		update_delivery_panel();
		}
	else {
		display_qty_change_limit(basket_id,max_qty);
	}

}

//MINUS CLICK
function basket_minus_click(basket_id,curr_val,item_price) {
	//$('#minus_'+basket_id).click(function () {
		curr_val--;
		if (curr_val >= 1) {
			update_qty_change(basket_id,curr_val,item_price);
			write_qty_change(basket_id,curr_val);
			update_subtotal_value();
			//update_promo_value();
			update_delivery_value();
			update_total_value();
			update_delivery_panel(); 
		}
	//});
}


//UPDATES THE QTY FEILD AND LINE TOTAL
function update_qty_change(basket_id,new_value,item_price) {
		$('#qty_'+basket_id).val(new_value);
		$('#remove_'+basket_id).show();
		$('#stock_qty_message_'+basket_id).html('');
		$('#stock_qty_message_'+basket_id).css('width','1px');
		var line_total = new_value*item_price;
		$('#line_total_'+basket_id).html('&pound;'+add_commas(line_total.toFixed(2)));
}

function add_commas(sValue) {
	var sRegExp = new RegExp('(-?[0-9]+)([0-9]{3})');

	while(sRegExp.test(sValue)) {
	sValue = sValue.replace(sRegExp, '$1,$2');
	}
	return sValue;
}

function update_subtotal_value() {
	$.ajax({ url: site_http+'/ajax/basket_subtotal.php', cache: false, success: function(data){
			var new_total = parseFloat(data).toFixed(2);
			$('#basket_subtotal').html('&pound;'+add_commas(new_total));
			//$('#basket_value').html('&pound;'+add_commas(new_total));
		}});
}

//WRITES THE NEW QTY TO THE BASKET
function write_qty_change(basket_id,new_value) {
	$.ajax({ type: 'POST', url: site_http+'/ajax/basket_qty_change.php?basket_id='+basket_id+'&qty='+new_value,  cache: false, async: false});
	
}

function display_qty_change_limit(basket_id,limit_qty) {
	$('#remove_'+basket_id).hide();
	$('#stock_qty_message_'+basket_id).css('width','120px');
	$('#stock_qty_message_'+basket_id).html('Only '+limit_qty+' available');
	
}

function options_hover() {
	$('.options_dropdown_button').hover( function () {
		$(this).addClass('pointer'); }, 
		function () { $(this).removeClass('pointer'); 
		});
}	


function option_dropdown_button_click() {
	$('.options_dropdown_button.basket_page').live("click", function () {
		var this_id = $(this).attr('id');
		var this_id_split = this_id.split('_');
		var basket_id = this_id_split[4];
		var this_option_number = this_id_split[3];
		if (this_option_number == 'one') {
		var other_option_number = 'two';
		}
		else {
			var other_option_number = 'one';
		}
		var this_option_value = $('#opt_'+this_option_number+'_val_'+basket_id).val();
		if ($('#opt_two_val_'+basket_id).length == 0) {
			var other_option_value = '';
			}
		else {
			var other_option_value = $('#opt_'+other_option_number+'_val_'+basket_id).val();
		}
		$.ajax({ url: site_http+'/ajax/option_values.php?opt_no='+this_option_number+'&basket_id='+basket_id, cache: false, async: false, success: function(data){
			$('#options_dropdown_'+this_option_number+'_'+basket_id).html(data);
			
			}});
		 $('#options_dropdown_'+this_option_number+'_'+basket_id).slideToggle();
	});	
}

function option_dropdown_click() {
	$('.option_dd_each.basket_page').live("click",function(){		
		var this_id = $(this).attr('id');
		var this_id_split = this_id.split('_');
		var new_display_value = $(this).children('.option_dd_each_text').html();
		var parent_id = $(this).parent().attr('id');
		var new_price = $(this).children('.option_dd_each_text').children('.option_dd_each_price').html();
		var option_number = this_id_split[2];
		var basket_id = this_id_split[3];
		var new_value = $(this).children('.option_dd_each_text').children('.option_dd_each_value').html();
		amend_option_price(this_id);
		update_option_box(this_id, basket_id,new_value,new_display_value);
		write_basket_option_value(option_number,basket_id,new_value,new_price);
		option_change_update_price_line_total(basket_id,new_price);
		$(this).parent().fadeOut();
		fade_out_option(parent_id,new_value);
		option_change_revise_max_qty(basket_id);
		update_delivery_panel();
		update_subtotal_value();
		//revise_promo_value();
		update_total_value();
		check_merge_basket(basket_id);
		
	});
}


function amend_option_price(this_id) {
	if ($('#'+this_id).children('.option_dd_each_text').children('.option_dd_each_price').length != 0) {
		var new_price = $('#'+this_id).children('.option_dd_each_text').children('.option_dd_each_price').html();
		var new_formatted_price = parseFloat(new_price).toFixed(2);
		$('.product_price').html('&pound;'+new_formatted_price);
			$('#price').val(new_price);
		}
}


function update_option_box(this_id,basket_id,new_value,new_display_value) {
	$('#remove_'+basket_id).show();
	$('#stock_qty_message_'+basket_id).html('');
	$('#'+this_id).parent().siblings('.options_dropdown_closed').children('.options_dropdown_closed_text').html(new_display_value);
	$('#'+this_id).parent().siblings('.options_dropdown_closed').children('.option_input_hidden').val(new_value);
}

function write_basket_option_value(option_number,basket_id,new_value,new_price) {
	$.ajax({ type: 'POST',cache: false, async: false, url: site_http+'/ajax/basket_option_change.php?basket_id='+basket_id+'&opt_no='+option_number+'&opt_val='+new_value+'&price='+new_price});
}


function option_change_update_price_line_total(basket_id,new_price) {
	$('#price_'+basket_id).html('&pound;'+parseFloat(new_price).toFixed(2));
	var quantity_value =  $('#qty_'+basket_id).val();
	var line_total = quantity_value*new_price;
	$('#line_total_'+basket_id).html('&pound;'+line_total.toFixed(2));
}

function fade_out_option(parent_id,new_value) {
	setTimeout( function() { 
		$('#'+parent_id).children('.option_dd_each').show(); 
		new_value = new_value.replace(' ','_'); 
		$('#'+new_value).hide();
		}, 1000);
}


function option_change_revise_max_qty(basket_id) {
	$.ajax({ url: site_http+'/ajax/option_max_qty.php?basket_id='+basket_id, cache: false,  success: function(data){
		var max_qty = parseFloat(data);
		$('#max_qty_'+basket_id).html(max_qty);
		var curr_val = $('#qty_'+basket_id).val();
		if (curr_val > max_qty) {
			$('#qty_'+basket_id).val(max_qty);
			var line_total = max_qty*item_price;
			$('#line_total_'+basket_id).html('&pound;'+line_total.toFixed(2));
			display_qty_change_limit(basket_id,max_qty);
			write_qty_change(basket_id,max_qty);
			update_subtotal_value();
			//update_promo_value();
			update_delivery_value(); 
			update_total_value(); 
			update_delivery_panel();	
		}
	}});
}

function basket_remove_hover() {
	$('.basket_record_remove').hover( function () { 
		$(this).css('color', '#ED1C24'); 
		$(this).addClass('pointer'); 
		}, 
		function () { 
			$(this).css('color', '#808285'); 
			$(this).removeClass('pointer'); 
	});
}


function basket_record_remove() {
	$('.basket_record_remove.basket_page').click(function(){
		var this_id = $(this).attr('id');
		var this_id_split = this_id.split('_');
		var basket_id = this_id_split[1];
		$.ajax({ type: 'POST', url: site_http+'/ajax/basket_remove.php?basket_id='+basket_id,  cache: false, async: false});
		$('#basket_record_'+basket_id).slideUp();
		update_subtotal_value();
		update_delivery_panel();
		//update_promo_value();
		update_delivery_value();
		update_total_value();
		});
}


//CHECKS IF CAN STILL BUY THIS QTY WHEN BASKET LOADS
function basket_page_load_qty_check(basket_id,max_qty) {
	//alert(gift_list_access_cookie);
	var item_price = $('#clean_price_'+basket_id).html();
	var curr_val = $('#qty_'+basket_id).val();
	if (curr_val > max_qty) {
		$('#qty_'+basket_id).val(max_qty);
		var line_total = max_qty*item_price;
		$('#line_total_'+basket_id).html('&pound;'+line_total.toFixed(2));
		display_qty_change_limit(basket_id,max_qty);
		write_qty_change(basket_id,max_qty);
		update_subtotal_value();
		//update_promo_value();
		update_delivery_value(); 
		update_total_value(); 
		update_delivery_panel();
	}
	
}



function delivery_option_change() {
	$("input[name*='del_option_']").live(
	        'click',
	        function(){
			var this_id = $(this).attr('name');
			var this_id_split = this_id.split('_');
			var option_set = this_id_split[2];
			var option_value = $(this).val();
			$.ajax({ type: 'POST', url: site_http+'/ajax/basket_del_option_choice.php?option_set='+option_set+'&choice='+option_value,  cache: false, async: false});
			update_delivery_value();
			update_total_value();
			});
}

//UPDATES THE DELIVERY VALUE
function update_delivery_value() {
$.ajax({ url: site_http+'/ajax/basket_delivery_value.php', cache: false, success: function(data){
	var new_delivery = parseFloat(data).toFixed(2);
	$('#basket_delivery_total').html('&pound;'+add_commas(new_delivery));
	}});
}


//UPDATES THE TOTAL VALUE 
function update_total_value() {
	$.ajax({ url: site_http+'/ajax/basket_total_value.php', cache: false, success: function(data2){
			var total_val = parseFloat(data2).toFixed(2);
			var new_value = '&pound;'+total_val;
			$('#basket_total').html(add_commas(new_value));
		}});
}


//UPDATES THE TEXT IN THE DELIVERY PANEL
function update_delivery_panel() {
$.ajax({ url: site_http+'/ajax/refresh_delivery_panel.php', cache: false, success: function(data){
	$('.shipping_panel_inner').html(data);
	}});
}


function check_merge_basket(basket_id) {
	$.ajax({ url: site_http+'/ajax/check_merge_basket.php?basket_id='+basket_id, cache: false, async: false, success: function(data){
		if (data != 'noaction') {
				$('#basket_record_'+basket_id).slideUp();
				var data_parts = data.split('~');
				var new_basket_id = data_parts[0];
				var new_qty = data_parts[1];
				var new_price = data_parts[2];
				update_qty_change(new_basket_id,new_qty,new_price);
				update_subtotal_value();
				//update_promo_value();
				update_delivery_value();
				update_total_value();
				update_delivery_panel();
				var max_qty = $('#max_qty_'+new_basket_id).html();
				basket_page_load_qty_check(new_basket_id,max_qty);
			}
			
	}});
}


function continue_shopping() {
	$("#cont_shopping").hover(
		   function () { $(this).addClass('pointer'); }, 
		   function () { $(this).removeClass('pointer'); });
	$('#cont_shopping').click(function(){
			window.location.replace(site_http+"/home");
			});	
}


function basket_proceed() {
	$("#basket_proceed").hover(
		    function () { $(this).addClass('pointer'); }, 
		    function () { $(this).removeClass('pointer'); });
	$('#basket_proceed').click(function(){ window.location.replace(site_https+"/your_info"); });	
}
