$(document).ready(function() {
	gift_basket_record_remove();
	gift_plus_minus_click();
	gift_option_dropdown_button_click();
	gift_option_dropdown_click();
});

//UPDATES THE SUBTOTAL VALUE ON THE GIFT BASKET PAGE
function gift_update_subtotal_value() {
	$.ajax({ url: site_http+'/ajax/gift_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));
		}});
}

function gift_update_total_value() {
	$.ajax({ url: site_http+'/ajax/gift_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));
		}});
}




function gift_basket_record_remove() {
	$('.basket_record_remove.gift_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/gift_basket_remove.php?basket_id='+basket_id,  cache: false, async: false});
		$('#basket_record_'+basket_id).slideUp();
		gift_update_subtotal_value();
		gift_update_total_value();
		});
}

function gift_plus_minus_click() {
	$('.plus_minus.gift_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 min_qty = $('#min_qty_'+basket_id).html();
		var item_price = $('#clean_price_'+basket_id).html();
		if (direction == 'minus') {
			gift_minus_click(basket_id,curr_val,item_price,min_qty);
		}
		else {
			gift_plus_click(basket_id,curr_val,item_price, max_qty);
		}
	});
}

// PLUS CLICK
function gift_plus_click(basket_id,curr_val,item_price, max_qty) {
	curr_val++;
	if (curr_val <= max_qty) { 
		gift_update_qty_change(basket_id,curr_val,item_price);
		gift_write_qty_change(basket_id,curr_val);
		gift_update_subtotal_value();
		gift_update_total_value();
		}
	else {
		display_qty_change_limit(basket_id,max_qty);
	}

}

//MINUS CLICK
function gift_minus_click(basket_id,curr_val,item_price, min_qty) {
		curr_val--;
		if (curr_val >= min_qty) {
			gift_update_qty_change(basket_id,curr_val,item_price);
			gift_write_qty_change(basket_id,curr_val);
			gift_update_subtotal_value();
			gift_update_total_value();
		}
		else {
			$('#remove_'+basket_id).hide();
			$('#stock_qty_message_'+basket_id).css('width','84px');
			$('#stock_qty_message_'+basket_id).html(min_qty+' ordered');
		}
		
}


function gift_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 gift_write_qty_change(basket_id,new_value) {
	$.ajax({ type: 'POST', url: site_http+'/ajax/gift_basket_qty_change.php?basket_id='+basket_id+'&qty='+new_value,  cache: false, async: false});
	
}

function gift_option_dropdown_button_click() {
	$('.options_dropdown_button.gift_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/gift_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 gift_write_basket_option_value(option_number,basket_id,new_value,new_price) {
	$.ajax({ type: 'POST',cache: false, async: false, url: site_http+'/ajax/gift_basket_option_change.php?basket_id='+basket_id+'&opt_no='+option_number+'&opt_val='+new_value+'&price='+new_price});
}


function gift_option_dropdown_click() {
	$('.option_dd_each.gift_page').live("click",function(){
		//alert('clicked');
		
		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 parent_id_split = parent_id.split('_');
		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);
		gift_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);
		
		gift_update_subtotal_value();
		
		
		gift_update_total_value();
		gift_check_merge_basket(basket_id);
		
	});
	
}


function gift_check_merge_basket(basket_id) {
	$.ajax({ url: site_http+'/ajax/gift_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];
				gift_update_qty_change(new_basket_id,new_qty,new_price);
				gift_update_subtotal_value();
				
				gift_update_total_value();
				
				var max_qty = $('#max_qty_'+new_basket_id).html();
				//basket_page_load_qty_check(new_basket_id,max_qty);
			}
			
	}});
}



