(function($){
	$(document).ready(function() {
		$("#product_price").html(parent_price);
		check_option_one();
	});
	
	$(function() {
	    $("#option_one").change(function() {
			$("#product_price").html(parent_price);
			build_option_two();
	    });
	});
	
	$(function() {
	    $("#option_two").change(function() {
	      var sel = jQuery('#option_one :selected');
	      var prod = $("#prod").val();
	      var cid = $("#cid").val();
		  $(function(){
		  	var opt_two = $("#option_two").val();
	        $.each(prod_options, function(h_in, o_opt){
	          if (o_opt.id == opt_two) {
		        if (o_opt.photo_file != null && o_opt.photo_file != '' ) {
					photo_link = unescape(o_opt.photo_link);
					photo_file = unescape(o_opt.photo_file);
					if (photo_zoom_size == "large") {
						photo_zoom = unescape(o_opt.photo_large_file);
					} else {
						photo_zoom = unescape(o_opt.photo_source);
					}
			    }
				
				if (o_opt.description != null && o_opt.description != '' && o_opt.description != '<p></p>') {
			      	$("#product_description").html(o_opt.description);
			    }		
			    build_product_photo(photo_link, photo_file, photo_zoom);
				build_quantity(o_opt.id, o_opt.inv, o_opt.inv_word, inv_show, registry_show);
				build_price(o_opt.price);
	          }
	        });
	        $('#product_price').show();
	        $('#product_order').show();	
	      });
	    });
	});
	
	function build_quantity(id, inv, inv_word, inv_show, registry_show) {
	    var quantity_val = "";
		if (inv_show) {
	      quantity_val += '<p class="stock">There ' + inv_word + ' <span class="color"> ' + inv + ' </span> in stock.</p>';
	      'Quantity: ';
	    }
		quantity_val += '<div class="product_button"><span class="qty">QTY:</span> ' +
		  '<input type="text" name="cart_quan[' + id + ']" class="product_quantity">' +
		  '<input type="hidden" name="cart_avail[' + id + ']" value="' + inv + '">' +
		  '<input type="hidden" name="store_action" value="add_to_cart">' +
		  '<input type="hidden" name="store_action" value="add_to_cart">' +
		  '<input type="hidden" name="product" value="' + id + '">' +
		  '<input type="hidden" name="registry" value="">' +
		  '<input type="hidden" name="quantity" value="">' +
		  '<button type="submit">Add to Cart</button>';
		  if (registry_show) {
		  	quantity_val += '<button type="button" onclick="show_registry(\'' + section + '\',' + id + ');">Gift Registry</button>';
		  }	
		  quantity_val += '</div>';
		$("#product_order").html(quantity_val);	
	}
	
	function build_product_photo(photo_link, photo_file, photo_zoom) {
	    var prod_photo = '<img src="' + photo_file + '" alt="zoom" jqimg="' + photo_zoom + '">';
	    $("#product_photo").hide();
	    $("#product_photo").html(prod_photo).fadeIn();
	}
	
	function build_price(price) {
		var price_val = "" + price;
		$("#product_price").html(price_val);
	}
	
	function build_option_two() {
		var sel = jQuery('#option_one :selected');
		var prod = $("#prod").val();
		var cid = $("#cid").val();
		$(function(){
			var options = '';
			var opt_twos = new Array();
			var opt_one_sel = '';
			$.each(prod_options, function(ind, o_opt){
		    if (sel.val() == o_opt.id) {
		      if (o_opt.photo_file != null && o_opt.photo_file != '' ) {
				photo_link = unescape(o_opt.photo_link);
				photo_file = unescape(o_opt.photo_file);
				if (photo_zoom_size == "large" && ( o_opt.photo_large_file != null && o_opt.photo_large_file != '' ) ) {
					photo_zoom = unescape(o_opt.photo_large_file);
				} else {
					photo_zoom = unescape(o_opt.photo_source);
				}
		      }
		      opt_one_sel = o_opt.option_one;
		    }
		});
			$.each(prod_options, function(h_in, o_opt){			
			    if (o_opt.option_one == opt_one_sel) {
			  if (jQuery.inArray(o_opt.option_two, opt_twos) == -1) {       
		    	options += '<option value="' + o_opt.id + '">' + o_opt.option_two + '</option>';
		    	opt_twos.push(o_opt.option_two);
		    	build_quantity(o_opt.id, o_opt.inv, o_opt.inv_word, inv_show, registry_show);
				build_price(o_opt.price);
		      }
		    }
		});
		build_product_photo(photo_link, photo_file, photo_zoom);
		if (opt_twos.length == 1) {
			$("select#option_two").html(options);
			$('#product_price').show();
				$('#product_order').show();
		} else {
			options = '<option value="">Select a ' + opt_two_val + ' </option>' + options;
			$("select#option_two").html(options);
				$('#product_order').hide();
		}	
		});
		$('#option_two').removeAttr("disabled");
	}
	
	function check_option_one() {
		
		if (typeof opt_one_count != 'undefined') {
			if (opt_one_count == 1) {
				build_option_two();
			}
			else {
				$('#option_two').attr("disabled", true);
			}
		} else {
			$('#option_two').attr("disabled", true);
		}
	}	
	

})(jQuery); 

