// javascript for adding to the basket
$(function () {

	$('#ajax-continue').click(function () {
		$("#add-to-basket-modal").dialog('close');	
		return false;
	});
	
	$('#header-view-basket').mouseover(function () {
		// stop any animation and show
		$('#basket-preview').stop(true, true);
		clearTimeout(timeout);
		// css fixes for IE
		$('#basket-preview').css('display', 'block');
		$('#basket-preview').parent().css('z-index', '999');		
		$('#basket-preview').parent().addClass('over');			
	}).mouseleave(function () {
		// hide
		$('#basket-preview').css('display', 'none');
		// css fixes for IE
		$('#basket-preview').parent().css('z-index', '0');		
		$('#basket-preview').parent().removeClass('over');	
		$('#basket-preview').prev().css('float', 'none');
	});
	
	$('#basket-preview').mouseover(function () {
		// stop any animation and show
		$(this).stop(true, true);
		clearTimeout(timeout);
		$(this).css('display', 'block');	
		// css fixes for IE
		$('#basket-preview').parent().css('z-index', '999');
		$(this).parent().addClass('over');	
	}).mouseleave(function () {
		// hide
		$(this).css('display', 'none');		
		/// css fixes for IE
		$('#basket-preview').parent().css('z-index', '0');
		$('#basket-preview').prev().css('float', 'none');
		$(this).parent().removeClass('over');	
	});
	
	var timeout;
	var fadeBasketPreview = function () { $('#basket-preview').fadeOut('slow', function() { $('#basket-preview').prev().css('float', 'none'); }); }

	$("#add-to-basket-modal").dialog({ bgiframe: true,
									   modal: true,
									   height: 188,
									   width: 590,
									   autoOpen: false,
									   dialogClass: 'ajax-add-to-basket',
									   close: function () {
									   	   /// css fixes for IE	
									   	   $('#basket-preview').prev().css('float', 'left');	
									   	   $('#basket-preview').parent().css('z-index', '999');	
									   	   // show the basket preview	
									       $('#basket-preview').fadeIn('fast', function () {
									           timeout = setTimeout(fadeBasketPreview, 2000);	
									       });	
									   }
									 });	

	$('INPUT.single-add-to-basket').click(function () {
		// find the products info	
		if ($(this).parents('DIV#main-prod-details').length > 0) {
			// this is the main product	
			theParent = $(this).parents('DIV#main-prod-details');
			upsoldCount = 0;
		}
		else if ($(this).parents('DIV.upsold-prod-details').length > 0) {
			theParent = $(this).parents('DIV.upsold-prod-details');
			// this is an upsold product - find out which one
			count = 1;
			upsoldCount = 0;
			$('DIV.upsold-prod-details').each(function () {
				if($(theParent).get(0) == $(this).get(0)) {					
					upsoldCount = count;
				}
				else {
					count++;
				}
			});				
		}
		// run validation checks - seems like jquery doesn't like our square brackets in the id name so we have to do the first part of this check the old fashion way
		if (document.getElementById('quantity[' + upsoldCount + ']').value != 0 && window['submitCheck' + upsoldCount]()) {
			// checks ok 	
			// remove error styling			
			$('#option-selector-wrapper-' + upsoldCount).removeClass('invalid');
			// get the relevant inputs and make post string
			var data = '';
			$(theParent).contents().find('input, select').each(function () {
				if($(this).attr('name') == 'products_id[' + upsoldCount + ']') {
					data = data + 'products_id=' + $(this).val() + '&';
				}
				else if ($(this).attr('name') == 'quantity[' + upsoldCount + ']') {
					data = data + 'quantity=' + $(this).val() + '&';
				}
				else {
					idData = $(this).attr('id') + '=' + $(this).val();
					var start = idData.indexOf('[');
					var end = idData.indexOf(']') + 1;
					replaceIdData = idData.substring(start, end);
					idData = idData.replace(replaceIdData, '');
					if (idData != '=') {
						data = data + idData + '&';
					}
				}					
			});
			data = data + 'sourceCatID=' + $('input[name=sourceCatID]').val();
			data = data + '&type=ajax';
			// post to server
			var urlToPost = window.location.pathname;
			$.post(urlToPost + '?action=add_product', data, function (data, textStatus) {
				// reset inputs on page
				$(theParent).contents().find('input, select').each(function () {
					// reset the quantity
					if ($(this).attr('name') == 'quantity[' + upsoldCount +']') {
						$(this).attr('value', '0');
					}
					// check for id values
					idValue = $(this).attr('name');
					idValue = idValue.substring(0, 2);
					if (idValue == 'id' && $(this).attr('type') != 'hidden') {
						$(this).attr('value', '-1');
					}
				});
				// update the basket preview
				$('#basket-preview').html(data);
			}, 'html');			
			// update product name on modal pop-up
			var prodName = $(theParent).contents().find('h5.prod-code').html();
			$('P#ajax-product-name').html(prodName);
			$('P#ajax-product-name SPAN').css('display', 'none');
			// trigger modal pop-up
			$("#add-to-basket-modal").dialog('open');	
			// stop the default form submit
			return false;
		}	
		else {
			// add error styling
			$('#option-selector-wrapper-' + upsoldCount).addClass('invalid');
			return false;
		}	
	}); 
	
});