var gift_list_access_cookie = $.cookie('greenandpresent_gift_list_access');

$(document).ready(function() {
	plus_minus_hover();
	plus_minus_click();
	options_hover();
	options_option_hover();
	basket_record_remove();
	basket_remove_hover();
	option_dropdown_click();
	option_dropdown_button_click();
	delivery_option_change();
});




//  UPDATES THE SUBTOTAL VALUE ON THE BASKET PAGE
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));
		}});
}

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;
}


// 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 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);
	}});
}

//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 (OR HIDES) THE GIFT VALUE 
function update_gift_value() {
	$.ajax({ url: site_http+'/ajax/basket_gift_value.php', async: false, cache: false, success: function(data){
		if (parseFloat(data) != 0) {
			var gift_value = parseFloat(data).toFixed(2);
			$('.gift_wrap_value').html('&pound;'+add_commas(gift_value));
			}
		else {
			$('.gift_wrap_outer').animate({'margin-top': '0px'}, 'fast');
			setTimeout("$('.gift_wrap_outer').animate({'height': '0px'}, 'fast')", 200);
			}
		}});
}

// 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});
	
}

//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)));
}


// PLUS MINUS BUTTON CLICK FUNCTION
function 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') {
			minus_click(basket_id,curr_val,item_price);
		}
		else {
			plus_click(basket_id,curr_val,item_price, max_qty);
		}
	});
}

// PLUS CLICK
function 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_gift_value();
		update_total_value();
		update_delivery_panel();
		}
	else {
		display_qty_change_limit(basket_id,max_qty);
	}

}

//MINUS CLICK
function 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_gift_value();
			update_total_value();
			update_delivery_panel(); 
		}
	//});
}

// DISPLAY QTY LIMIT MESSAGE
function display_qty_change_limit(basket_id,limit_qty) {
	$('#remove_'+basket_id).hide();
	$('#stock_qty_message_'+basket_id).css('width','84px');
	//alert(gift_list_access_cookie);
	if (gift_list_access_cookie == 'adb831a7fdd83dd1e2a309ce7591dff8') {
		$('#stock_qty_message_'+basket_id).html('Only '+limit_qty+' on list');
	}
	else {
		$('#stock_qty_message_'+basket_id).html('Only '+limit_qty+' available');
	}
}



// 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_gift_value(); 
		update_total_value(); 
		update_delivery_panel();
	}
	
}



// THIS HANDLES THE PROMO CODE FIELD
function update_promo_value() {
$.ajax({ url: site_http+'/ajax/basket_promo_value.php', cache: false, success: function(data){
		if (data.indexOf('too_low') >= 0) {
			var promo_value = 0;
			clean_text = data.replace('too_low', '');
			$('.promo_input_div').hide();
			$('.promo_input_div').html("<input class='promo_input' AUTOCOMPLETE=OFF style='margin-left: 0px; font-size: 14px;' type='text' name='promo_code_entry' id='promo_code_entry'  value='Enter Code'><div class='promo_submit'></div>");
			$('.promo_input_div').css('height','21px');
			$('.promo_input_div').css('border','1px solid #c3e6dd');
			$('.promo_input_div').css('margin-top','0px');
			$('.promo_submit').hover( function () { $(this).addClass('pointer'); }, function () { $(this).removeClass('pointer'); });
			$(':input[name=promo_code_entry]').click( function() { $(this).val(''); });
			$('.promo_submit').click( function() { 
					$('.promo_input_div').hide();
					$('.promo_text').hide();
					var input_value =  $(':input[name=promo_code_entry]').val();
					$.ajax({ url: site_http+'/ajax/check_promo.php?promo_code_input='+input_value, async: false, cache: false, success: function(data){
						var clean_text = '';
						if (data != 'no_match' && data.indexOf('too_low') != 0) {
								$('.promo_text').html(data);
								$.ajax({ url: site_http+'/ajax/basket_promo_value.php', async: false, cache: false, success: function(data){
										var promo_value = parseFloat(data).toFixed(2);
										$('.promo_input_div').html('&pound;-'+promo_value);
										$('.promo_input_div').css('height','21px');
										$('.promo_input_div').css('border','0px solid white');
										$('.promo_input_div').css('margin-top','3px');
										}});
								$('.promo_text').show();
								$('.promo_input_div').show();
								$.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;
									setTimeout("$('#basket_total').html('"+new_value+"')", 500);
									}});
							} 
						else {
							if (data.indexOf('too_low') == 0) {
								clean_text = data.replace('too_low', '');
								}
							else {
								clean_text = 'Invalid Promo Code';
								}	
							setTimeout("$('.promo_text').css('width','182px')", 500);
							setTimeout("$('.promo_text').html('"+clean_text+"')", 500);
							setTimeout("$('.promo_text').fadeIn()", 500);
							$('#promo_code_entry').val('Enter Code');
							setTimeout("$('.promo_text').fadeOut('fast')", 1700);
							setTimeout("$('.promo_text').css('width','85px')", 2000);
							setTimeout("$('.promo_text').html('Promo Code')", 2000);
							setTimeout("$('.promo_input_div').fadeIn()", 2000);
							setTimeout("$('.promo_text').fadeIn()", 2000);
							}
					}});
				});
			setTimeout("$('.promo_text').css('width','182px')", 500);
			setTimeout("$('.promo_text').html('"+clean_text+"')", 500);
			setTimeout("$('.promo_text').fadeIn()", 500);
			$('#promo_code_entry').val('Enter Code');
			setTimeout("$('.promo_text').fadeOut('fast')", 1700);
			setTimeout("$('.promo_text').css('width','85px')", 2000);
			setTimeout("$('.promo_text').html('Promo Code')", 2000);
			setTimeout("$('.promo_input_div').fadeIn()", 2000);
			setTimeout("$('.promo_text').fadeIn()", 2000);
		}
	else if (parseFloat(data) != 0) {
		var promo_value = parseFloat(data).toFixed(2);
		$('.promo_input_div').html('&pound;-'+promo_value);
		}		
}});
}

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_gift_value();
		update_total_value();
		});
}

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();
	});
	
}

// THIS WRITES A CHANGE TO AN OPTION
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});
}


// THIS FADES OUT THE OPTION
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 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 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);
			//price_".$basket_id." = new_price;
		}
}


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_gift_value(); 
			update_total_value(); 
			update_delivery_panel();
			
		}
		
		
	}});
}

function option_dropdown_click() {
	$('.option_dd_each.basket_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);
		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_gift_value(); 
		update_total_value();
		check_merge_basket(basket_id);
		
	});
	
}

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') {
				//$.ajax({ type: 'POST', url: site_http+'/ajax/basket_remove.php?basket_id='+basket_id,  cache: false, async: false});
				$('#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_gift_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 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));
	//var price_+basket_id = new_price;
}



// THIS REVISES THE PRICE OF THE PROMO IF CODE HAS BEEN SET AND OPTION CHANGED.
function revise_promo_value() {
	$.ajax({ url: site_http+'/ajax/basket_promo_value.php', cache: false, success: function(data){
		if (parseFloat(data) != 0) {
			var promo_value = parseFloat(data).toFixed(2);
			$('.promo_input_div').html('&pound;-'+promo_value);
			}
	}});
}







function options_option_hover() {
$('.option_dd_each').live(
        'hover',
        function (ev) {
            if (ev.type == 'mouseover') {
            	$(this).addClass('pointer'); 
        		$(this).css('background-color','#e1e9e7'); 
            }

            if (ev.type == 'mouseout') {
            	$(this).removeClass('pointer'); 
        		$(this).css('background-color','white');
            }
        });
}

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();
			});
	
}


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




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






function plus_minus_hover() {
	$('.plus_minus').hover( 
		function () { $(this).addClass('pointer'); }, 
		function () { $(this).removeClass('pointer'); }
		);
}



