jQuery(document).ready( function(){


	jQuery('a.basketButton').qtip(
      {
         content: {
              text: 'hello'
           },

         show: false,

         style: {
            classes: 'ui-tooltip-wiki ui-tooltip-light ui-tooltip-shadow'
         }
      })

	jQuery('#report_filters div select').change(function() {
	  jQuery('#filter_reports').submit();
	});

	jQuery('.extend_button').click(function() {

	if (jQuery(this).prev('.extend').css('height')=='auto') {
		jQuery(this).prev('.extend').css('height','160px');
		jQuery(this).text('Find out more');
	} else {
		jQuery(this).prev('.extend').css('height','auto');
		jQuery(this).text('Show less');
	}

	});

	jQuery('#basket p.view').click(function() { //View basket in header
		jQuery.ajax({
		  url: '/basket/viewBasket',
		  dataType: 'json',
		  success: function(data) {
		  if (data.error) {
		  	jQuery('#basketHover').html(data.error);
		  } else {
		  	jQuery('#basketHover').html(data.html);
		  }

		  }
		});

		if (jQuery('#basketHover').css('display')==='none') {
			jQuery('#basketHover').slideDown('fast');
		} else {
			jQuery('#basketHover').slideUp('fast');
		}
	});

	jQuery('#basket').bind('mouseleave',function() {
		if (jQuery('#basketHover').css('display')!=='none') {
			setTimeout(function() {
				jQuery('#basketHover').slideUp('fast');
			},2000);
		}
	});

	jQuery('#addProductaa').click(function() { //Add product on product detail page
		jQuery.ajax({
		  url: '/basket/addProduct',
		  dataType: 'json',
		  data: {
		  	quantity: jQuery('#quantity').attr('value'),
		  	product: jQuery('#product').attr('value')
		  },
		  success: function(data) {
		  	jQuery('#headerBasket').html(data.text);
		  }
		});
	});

	jQuery('#addProduct').click(function() {
		jQuery('.errorMessage').html(null);
	});

	jQuery('a.basketButton').click(function() { //Add product on product list page //goode
		that = jQuery(this);
		jQuery.ajax({
		  url: '/basket/addProduct',
		  dataType: 'json',
		  data: {
		  	quantity: 1,
		  	product: jQuery(this).attr('rel')
		  },
		  success: function(data) {
			  if (data.error) {
				  	that.qtip({
				     	content: {
			               text: data.error
			            },
					position: {
						at: 'top center',
						my: 'bottom center',
						viewport: jQuery(window)
					},
					show: false,
		            hide: 'unfocus'
			        });
			     	that.qtip("redraw");
			     	that.qtip("show");

		     	} else {
			  	//console.log(data);
			  	jQuery('#basket_summary').html(data.text);

			  	that.qtip({
			     	content: {
		               text: data.message
		            },
				position: {at: 'top center', my: 'bottom center'},
			    adjust: { screen: true }, // Keep the tooltip on-screen at all times
				show: false,
	            hide: 'unfocus'
		        });
		     	that.qtip("redraw");
		     	that.qtip("show");


			  	jQuery('.addedBasket').html(data.message);
			  }

		  	Cufon.refresh('#basket_summary span');
		  	Cufon.refresh('#basket_summary span strong');
		  }
		});

//		jQuery(this).next('.addedBasket').fadeOut();
//		jQuery(this).next('.addedBasket').fadeIn('slow');
//		jQuery(this).next('.addedBasket').addClass('show');

	});

	jQuery('.product_price_long').bind('mouseleave',function() { //goode
		if (jQuery(this).children('.addedBasket').hasClass('show')) {
			setTimeout(function() {
				jQuery(this).children('.addedBasket').removeClass('show');
				jQuery(this).children('.addedBasket').fadeOut('slow');
			},2000);
		}
	});

	jQuery('.product_price').bind('mouseleave',function() { //goode

		if (jQuery(this).children('.addedBasket').hasClass('show')) {
			setTimeout(function() {
				jQuery('.addedBasket').fadeOut('slow');
				jQuery('.addedBasket').removeClass('show');

			},2000);
		}
	});

	jQuery('a.more').click(function() { //Add one to quantity of product
		var quantity = parseInt(jQuery(this).parent().siblings('.quantity').attr('value'));
		jQuery(this).parent().siblings('.quantity').attr('value',quantity+1);

		list = jQuery(this).parent().parent().parent('li');
		last = list.children('div.quantity_container').children('div.product_options').last();
		if (last.length) {
			clone = last.clone().appendTo(list.children('div.quantity_container'));
			var id = list.children('div.quantity_container').children('div.product_options').length;

			clone.children('label.item_title').children('span').html(id);
			list.children('.product_quantity_amount').attr('value',id);
			list.children('span.item_quantity').html(id);

			jQuery.each(clone.children('.errorMessage'),function() {
				var old_id = jQuery(this).attr('id');
				var num = old_id.substring(old_id.length - 1,old_id.length);
				var new_num = parseInt(num)+1;
				var new_id = old_id.substring(0, old_id.length - 1)+new_num;
				jQuery(this).attr('id',new_id);

			});
		} else {
			//var value = parseInt(list.children('.product_quantity_amount').attr('value'));
			//list.children('.product_quantity_amount').attr('value',value+1);
			list.children('span.item_quantity').html(quantity+1);
		}

		if (jQuery('#additional_cost').text()) {
			var additional = parseFloat(jQuery('#additional_cost').text());
		} else {
			var additional = 0;
		}

		var add_cost = parseFloat(jQuery(this).parent('div').parent().children('.extra_price').attr('value'));
		var new_cost = additional+add_cost;
		jQuery('#additional_cost').text(new_cost.toFixed(2));

		if (new_cost!=0) {
			jQuery('.additional_cost_container').css('display','block');
		}

	});

	jQuery('a.less').click(function() { //Subtract one to quantity of product //
		var quantity = parseInt(jQuery(this).parent().siblings('.quantity').attr('value'));
		if (quantity>parseInt(jQuery(this).parent().siblings('.quantity').attr('rel'))) {
			jQuery(this).parent().siblings('.quantity').attr('value',quantity-1);
			list = jQuery(this).parent().parent().parent('li');
			list.children('span.item_quantity').html(quantity-1);
			last = list.children('div.quantity_container').children('div.product_options').last().remove();

			if (jQuery('#additional_cost').text()) {
				var additional = parseFloat(jQuery('#additional_cost').text());
			} else {
				var additional = 0;
			}

			var add_cost = parseFloat(jQuery(this).parent('div').parent().children('.extra_price').attr('value'));
			var new_cost = additional-add_cost;
			jQuery('#additional_cost').text(new_cost.toFixed(2));

			if (new_cost==0) {
				jQuery('.additional_cost_container').css('display','none');
			}
		}





	});

	jQuery('a.addAnother').click(function(){ //Add another instance of a product item
		list = jQuery(this).parent('li');
		last = list.children('div.quantity_container').children('div.product_options').last();
		if (last.length) {
			clone = last.clone().appendTo(list.children('div.quantity_container'));
			var id = list.children('div.quantity_container').children('div.product_options').length;

			clone.children('label.item_title').children('span').html(id);
			list.children('.product_quantity_amount').attr('value',id);
			list.children('span.item_quantity').html(id);
		} else {
			var value = parseInt(list.children('.product_quantity_amount').attr('value'));
			list.children('.product_quantity_amount').attr('value',value+1);
			list.children('span.item_quantity').html(value+1);
		}

	});



	jQuery('#customer_date_of_birth').datepicker(
	{
		changeYear: true,
		changeMonth: true,
		yearRange: '1910:2015'
	}
	);

	jQuery('#same').change(function() {
		if (jQuery('#same:checked').val() !== undefined) {
			jQuery('#delivery_address_line_1').attr('value',jQuery('#billing_address_line_1').attr('value'));
			jQuery('#delivery_address_line_2').attr('value',jQuery('#billing_address_line_2').attr('value'));
			jQuery('#delivery_town').attr('value',jQuery('#billing_town').attr('value'));
			jQuery('#delivery_county').attr('value',jQuery('#billing_county').attr('value'));
			jQuery('#delivery_country').attr('value',jQuery('#billing_country').attr('value'));
			jQuery('#delivery_postcode').attr('value',jQuery('#billing_postcode').attr('value'));
		} else {
			jQuery('#delivery_address_line_1').attr('value',null);
			jQuery('#delivery_address_line_2').attr('value',null);
			jQuery('#delivery_town').attr('value',null);
			jQuery('#delivery_county').attr('value',null);
			jQuery('#delivery_country').attr('value',null);
			jQuery('#delivery_postcode').attr('value',null);
		}
	});


//	jQuery('.removeGiftCard').change(function() {
//		if (jQuery(this).attr('checked')==true) {
//			jQuery(this).parent('label').parent('.giftContainer').children('.chooseGiftCard').css('display','none');
//			jQuery(this).parent('label').parent('.giftContainer').children('.giftCardForm').css('display','none');
//			jQuery(this).parent('label').parent('.giftContainer').children('.giftProduct').css('display','none');
//		} else {
//			jQuery(this).parent('label').parent('.giftContainer').children('.chooseGiftCard').css('display','block');
//			jQuery(this).parent('label').parent('.giftContainer').children('.giftCardForm').css('display','block');
//			jQuery(this).parent('label').parent('.giftContainer').children('.giftProduct').css('display','block');
//		}
//	});


	jQuery('.productItem').click(function() { //Basket, checkout, confirm template product item list show/hide
		if (jQuery(this).children('.productItemList').length==true) {
			if (jQuery(this).children('.productItemList').css('display')=='block') {
				jQuery(this).css('list-style-image','url(/images/bullet.png)');
				jQuery(this).children('.productItemList').css('display','none');
			} else {
				jQuery(this).children('.productItemList').css('display','block');
				jQuery(this).css('list-style-image','url(/images/bullet-down.png)');
			}
		}
	});



});

function confirmation(link) {
	var answer = confirm("Are you sure you wish to remove this item from your basket?")
	if (answer){
		window.location = link;
	}
}

function roundNumber(rnum, rlength) { // Arguments: number to round, number of decimal places
  var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
	return newnumber;
}









