$(document).ready(function(){
			
	$('.imageToAction').click(function(){		/* this is done because IE7 cannot pass the value of image inputs the way even */
		$('#action').val($(this).val());		/* IE6, the worst browser ever made, is able to do.  so we have to do this stupid */		
		return true;							/* hack, which puts the value of the clicked image input into a generic hidden input */
	});											/* named action.  sorry for the long explanation but this just really sucks.	*/
			
	$('#searchbox').click(function(){
		$(this).val('');
	});	
	
	$('form').submit(function(){
		var stopit = 0;
		if($(this).find('.product-option').length>0){
			$(this).find('.product-option').each(function(){
				if($(this).children('option:selected').val()=='0' && stopit == 0){
					alert('All options are required.  Please make your selection and proceed with your order.');
					$(this).focus();
					stopit = 1;
				}
			});
			if(stopit==1){
				return false;
			} else {
				return true;
			}
		} else {
			return true;
		}
	});	
	
	
	/* comparisons drag-n-drop feature */
	
	compareBoxEvents();	// initialize
	
	$('.compare-box').append('<img align="right" style="visibility: hidden; cursor: pointer;" src="/templates/default/images/prodcompare-remove.gif" class="prodcompare-remove">');
	
	$('.product-image').draggable({cursorAt: {top: 10, left: 10} ,scroll: false, helper: 
		function() { 
			$('html,body').animate({scrollTop: 0}, 500);
			var tinyImg = this.src.replace('sm_image','tiny_image');
			tinyImg = tinyImg.replace('lg_image','tiny_image');
			return $(document.createElement('img'))
					.attr("src",tinyImg)
					.attr("width",'60')
					.appendTo("body")[0]; 
		}
	});	// make all product images draggable
	
										// all products should have a classname of
										// product-image and an id of product-image-PID
	$(".comparisons").droppable({			// there are four compare boxes, which can receive
		accept: ".product-image",			// any product image
		activeClass: 'droppable-active',
		hoverClass: 'droppable-hover',
		tolerance: 'touch',
		drop: function(ev, ui) {
		
			// get productID
			var productID = $(ui.element).attr("id").replace('product-image-','');
			
			// there must be an empty box, 
			// and the product must not already be in the box
			var emptyBox = false;
			var dupProd = false;
			var counter = 0;
			$('.compare-box').each(function() {
				if  ($('.prodCompare').eq(counter).val()==productID) {
					dupProd = true;
				}
				if  ($('.prodCompare').eq(counter).val()=='') {
					emptyBox = true;
				} 
				if (emptyBox==false){
					counter++;
				}
			});
			
			if (dupProd==true) {
				alert('This product has already been added to your comparison box.');
				return false;
			}
			
			if (emptyBox==false){
				alert('You have already placed the maximum number of products (4) into the comparisons box.  You may remove one or more of your current choices to add this product.');
				return false;
			} else {
				// do our thing
				$.ajax({type:'POST',url:'productcomparecart.php',data:'action=add&product_ID='+productID,success:
					function(msg){
						//cookies first, milk second
						$('.prodCompare').eq(counter).val(productID);
						var tinyImg = $(ui.element).attr("src").replace('sm_image','tiny_image');
						tinyImg = tinyImg.replace('lg_image','tiny_image');
						var imgURL = 'url('+tinyImg+')';
						$('.compare-box').eq(counter).css('background-image',imgURL);	
						$('.compare-box').eq(counter).addClass('compare-box-filled');
						compareBoxEvents();
					}
				}); // end ajax call to add to cookies
			}
		}
	}); // end of droppable function
	
	function compareBoxEvents(){
		var i = 0;
		$('.compare-box-filled').each(function(){
			var productID = $('.prodCompare').eq(i).val();								   
			$(this).unbind();
			$(this).hover(function(){
				var thisEl = $(this);
				$(this).children(".prodcompare-remove")
					.css('visibility','visible')
					.unbind()
					.click(function(){
							a = true;
							if (a){
								$.ajax({type:'POST',url:'productcomparecart.php',data:'action=del&product_ID='+productID,success:
									function(msg){
										var thisEq = parseInt(thisEl.attr("id").replace('compare-box-',''))-1;
										thisEl.children(".prodcompare-remove").css('visibility','hidden');
										thisEl.css('background-image','url(/templates/default/images/compare-box-empty.gif)');
										thisEl.unbind();
										$('.prodCompare').eq(thisEq).val('');
									}
								});
							}
					});
			},function(){
				$(this).children(".prodcompare-remove").css('visibility','hidden');
			});
			i++;
		});	
	}
	
	$('.compare-go').click(function(){
		var numItems = 0;
		$('.prodCompare').each(function(){
			if ($(this).val()!=''){
				numItems++;
			}
		});
		var width = 100 + (150 * numItems);
		if (numItems<2) {
			alert('You must choose at least two products to compare.');
		} else {
			tb_show('Pelican Comparisons', '/productcomparisons.php?height=500&width='+width, '');	
		}		
		return false;
	});
	
	$('.compare-clear').click(function(){
		$.ajax({type:'POST',url:'productcomparecart.php',data:'action=clear',success:
			function(){	
				var counter = 0;
				$('.compare-box').each(function() {
					$(this).css('background-image','url(/templates/default/images/compare-box-empty.gif)');
					$(this).unbind();
					$('.prodCompare').eq(counter).val('');
					counter++;
				});
			}	
		});	
		return false;
	});

	
	
});