$(document).ready(function(){
	var useSwatch = false;
	var defaultMsg = 'Please Choose...';
	var warningMsg = 'Please choose an option from each dropdown.';
	
	// Add Please Select... to all product select boxes
	$('#product select').each (function() {
		$(this).prepend('<option value="">'+defaultMsg+'</option>');
		$(this).val('');
	});
	
	// Show warning if there are any unselected options
	$('#add_to_cart').click(function (event) {
    	$('#product select :selected').each (function(i, select) {
			if ($(select).val() == '') {
		    	alert(warningMsg);
		    	event.stopImmediatePropagation();
		    	return false;
		    }
		});
	});
	
	// If using swatch, and not using the please select, then default the swatch to the image of the current options
	if (useSwatch) {
		$('#product select option:selected').each(function(i, option) {
			var select_id 	= $(option).parent().attr('id');
			var swatch 		= $(option).attr('swatch');
			var popup 		= $(option).attr('popup');
			var title 		= $(option).attr('title');
			var alt 		= $(option).attr('alt');
			if (swatch) {
				$(option).parent().parent().after('<td><a title="'+title+'" href="'+popup+'" class="thickbox_option"><img id="option_image_'+select_id+'" src="'+swatch+'" alt="'+alt+'" title="'+title+'" class="vtip" /></a></td>');
			}
	    });     
	    vtip();
	    tb_init('.thickbox_option');
	}
	
	// Store original image source	
	var origSrc = $('#image').attr('src');
	var origTitle = $('#image').attr('title');
	var origAlt = $('#image').attr('alt');
	var origPopup = $('#image').parent().attr('href');
	
	$('#image').addClass('vtip');
	
	// Bind select change to image swap
	$('#product select').bind('change', function() {
		
		// Set variables...
		var select_id	= $(this).attr('id');
		var option 		= $(this).find("option:selected");
		var swatch 		= $(option).attr('swatch');
		var thumb 		= $(option).attr('thumb');
		var popup 		= $(option).attr('popup');
		var title 		= $(option).attr('title');
		var alt 		= $(option).attr('alt');
		
		// Swap Image if exists...
		if (swatch && useSwatch) {
			// Remove existing image
			$('#option_image_'+select_id).parent().parent().remove();
			$(option).parent().parent().after('<td><a title="'+title+'" href="'+popup+'" class="thickbox_option"><img id="option_image_'+select_id+'" src="'+swatch+'" alt="'+alt+'" title="'+title+'" class="vtip" /></a></td>');
			tb_init('.thickbox_option');
		} else if (thumb) {
			$('#image').attr('src', thumb);
			$('#image').attr('title', title);
			$('#image').attr('alt', alt);
			$('#image').parent().attr('href', popup);
		} else {
			$('#image').attr('src', origSrc);
			$('#image').attr('title', origTitle);
			$('#image').attr('title', origAlt);
			$('#image').parent().attr('href', origPopup);
		}
		vtip();
	});
});
