var site_http = "http://www.stuartgardiner.co.uk";
var site_https = "https://www.stuartgardiner.co.uk";



$(document).ready(function() {
	plus_minus_hover();
	product_plus_minus_click();
	option_dropdown_button_click_product_page();
	option_dropdown_click_product_page();
	z_index_modify();
	options_option_hover();
	basket_image_hover();
	add_basket_jqform();
	product_grid_slideshow();
});


function product_grid_slideshow() {
	$('.shop_item_grid_thumbnail').cycle({ fx: 'fade', speed: 100, timeout: 1000 }).cycle('pause'); 
				$('.shop_item_grid_thumbnail').hover(
  					function () {
   						 $(this).cycle('resume');
   						 $(this).addClass('pointer');
  					}, 
  					function () {
    					$(this).cycle('pause');
    					 $(this).removeClass('pointer');
 					 }
				);
}




function basket_image_hover() {
$('#basket_image').hover( function() {
	$(this).addClass('pointer');
	},
	function() {
	$(this).removeClass('pointer');
	});
}

function add_basket_jqform(){
	var cookie_value = $.cookie('sgd_basket_id');
	if (!cookie_value) {
		$.get(site_http+"/ajax/basket_id.php", function(data){
		$.cookie('sgd_basket_id',data,{ expires: 1000, path: '/' });
   		});
	}	
	
	
	//else {
		var options = { data: { basket_id: cookie_value }, beforeSubmit: add_basket_showRequest, success: add_basket_showResponse };	
		$('.add_basket').ajaxForm(options);
	//}
}


function add_basket_showRequest(formData, jqForm, options){		
		var cookie_value = $.cookie('sgd_basket_id');
		if (!cookie_value) {
			alert('Unfortunately your browser is configured to not accept cookies from this (or all sites). \n\nOur site requires the ability to store a basket identification number as part of the checkout process. \n\nPlease enable cookies for this site and try again.');
			return false;
		}
		else {
				var passes_validation = true;
				passes_validation = add_basket_validate(formData, jqForm, options);
				if (passes_validation) {
					return true;
				}
				else {
					return false;
				}
		}
} 
 
function add_basket_showResponse(responseText, statusText)  { 
	$('#checkout_value').html(responseText);
} 
	
function add_basket_validate(formData, jqForm, options) { 
    var form = jqForm[0]; 
	var passes_validation = true;
	if ($('#opt_one_val').length > 0) {
		if (!form.opt_one_val.value) { $('#options_dropdown_closed_text_one').css('color','#ec008c'); passes_validation = false; }
		else { $('#options_dropdown_closed_text_one').css('color','#555555'); }	
		}
	
	if ($('#opt_two_val').length > 0) {
		if (!form.opt_two_val.value) { $('#options_dropdown_closed_text_two').css('color','#ec008c'); passes_validation = false; }
		else { $('#options_dropdown_closed_text_two').css('color','#555555'); }	
		}
	
	if (form.qty.value == 0) { $('#qty').css('color','#ec008c'); passes_validation = false; }
	else { $('#qty').css('color','#555555'); }
	
  return passes_validation;
}



function option_dropdown_click_product_page() {
	$('.option_dd_each.product_page').live("click", function () {
		var new_display_value = $(this).children('.option_dd_each_text').html();
		var new_value = $(this).children('.option_dd_each_text').children('.option_dd_each_value').html();
		var parent_id = $(this).parent().attr('id');
		// IF OPTION HAS PRICE - UPDATE PRICE VALUE
		var this_id = $(this).attr('id');
		if ( $(this).children('.option_dd_each_text').children('.option_dd_each_price').length != 0 ) {
			var new_price = $(this).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);
			$(this).parent().siblings('.options_dropdown_closed').children('.option_input_hidden_price').val(new_price);
		}
		$(this).parent().siblings('.options_dropdown_closed').children('.options_dropdown_closed_text').html(new_display_value);
		$(this).parent().siblings('.options_dropdown_closed').children('.option_input_hidden').val(new_value);
		
		replace_other_dropdown_contents(this_id,new_value);
		$('#stock_qty_message').html('');
		get_max_qty();
		
		
		$(this).parent().fadeOut();
		
		setTimeout( function() { 
			$('#'+parent_id).children('.option_dd_each').show(); 
			new_value = new_value.replace(' ','_'); 
			$('#'+new_value).hide();
			//alert(new_value);
			}, 1000);
		
		
	});
}


function replace_other_dropdown_contents(this_id,new_value) {
	var full_attr_id = $('#'+this_id).parent().siblings('.options_dropdown_closed').children('.options_dropdown_closed_text').attr('id');
	var option_number = full_attr_id.replace('options_dropdown_closed_text_', '');
	var product_name = $('#product_name').val();
	if (option_number == 'one') {
		var change_option_number = 'two';
		var other_value = $('#opt_two_val').val();
		}
	else {
		var change_option_number = 'one';
		var other_value = $('#opt_one_val').val();
		}
	$.ajax({ url: site_http+'/ajax/option_values.php?opt_no='+change_option_number+'&product_name='+product_name+'&set_value='+new_value+'&other_value='+other_value, success: function(data){
		$('#options_dropdown_'+change_option_number).html(data);
		}});
}




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

function product_plus_minus_click() {
	$('.plus_minus.product_page').click(function () {
		var curr_val = $('#qty').val();
		var max_qty = $('#max_qty').html();
		var this_id = $(this).attr('id');
		var this_id_split = this_id.split('_');
		var direction = this_id_split[0];
		
		if (direction == 'minus') {
			product_minus_click(curr_val);
		}
		else {
			product_plus_click(curr_val,max_qty);
		}
	});
}



function option_dropdown_button_click_product_page() {
	$('.options_dropdown_button.product_page').live("click", function () {
		var this_id = $(this).attr('id');
		var this_id_split = this_id.split('_');
		var this_option_number = this_id_split[3];
		$('#options_dropdown_'+this_option_number).slideToggle();
	});
}


function z_index_modify() {
	var zIndexNumber = 8000;
	//$('.main_content div').not('.options_dropdown_outer, .options_dropdown').each(function() {
	$('.main_content div').each(function() {
		$(this).css('zIndex', zIndexNumber);
		zIndexNumber -= 10;
	});
}

function product_plus_click(curr_val,max_qty) {
	curr_val++;
	if (curr_val <= max_qty) { 
		$('#qty').val(curr_val);
		$('#stock_qty_message').html('');
		}
	else {
		$('#stock_qty_message').html('Only '+max_qty+' available');
	}
}

function product_minus_click(curr_val) {
	curr_val--;
	if (curr_val >= 1) {
		$('#qty').val(curr_val);
		$('#stock_qty_message').html('');
	}	
}	

function get_max_qty() {
	var opt_one_val = $('#opt_one_val').val();
	var opt_two_val = $('#opt_two_val').val();
	var product_name = $('#product_name').val();
	$.ajax({ url: site_http+'/ajax/option_max_qty.php?opt_one_val='+opt_one_val+'&product_name='+product_name+'&opt_two_val='+opt_two_val, success: function(data){
		var qty_available = parseFloat(data);
		var max_qty = qty_available;
		if ($('#qty').val() == 0) { $('#qty').val(1) }
		if ($('#qty').val() > qty_available) {
			$('#qty').val(qty_available);
			$('#stock_qty_message').html('Only '+qty_available+' available');	
		}
		$('#max_qty').html(max_qty);
		}});
}



function product_page_load_check() {
	get_max_qty();
	if ($('.option_dd_each_price').length != 0) {
		var new_price = $('.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 options_option_hover() {
$('.option_dd_each').live(
        'hover',
        function (ev) {
            if (ev.type == 'mouseover') {
            	$(this).addClass('pointer'); 
        		$(this).children().css('color','#ed1c24'); 
            }

            if (ev.type == 'mouseout') {
            	$(this).removeClass('pointer'); 
        		$(this).children().css('color','#808285');
            }
        });
}


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


