(function($){
	$(document).ready(function() {
		$("#product_price").html(parent_price);
	});
	
	$(document).ready(function() {
		check_option_one();
	});
	
	$(function() {
	    $("#option_one").change(function() {
			build_option_two();
	    });
	});
	
	$(function() {
	    $("#option_two").change(function() {
	      var sel = jQuery('select :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 (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);
				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></button>';
		  }	
		$("#product_order").html(quantity_val);	
	}
	
	function build_product_photo(photo_link, photo_file) {
	    var prod_photo = '<a href="#" onclick="tb_show(\'\', \'' + photo_link + '\',\'false\');"><img src="' + photo_file + '" border="1"></a>';
	    $("#product_photo").hide();
	    $("#product_photo").html(prod_photo).fadeIn();
	}
	
	function build_price(price) {
		var price_val = "" + price;
		if(price_val == '') {
			$("#product_price").html(parent_price);
		} else {
			$("#product_price").html(price_val);
		}
	}
	
	function build_option_two() {
		  var sel = jQuery('select :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);
			      }
			      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);
	        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); 

