//-----------------------------------------
// Author: Qphoria@gmail.com
// Web: http://www.theqdomain.com/ocstore
// Special Thanks: gnarf
//-----------------------------------------
$(document).ready(function(){

	//Get Exising Select Options
	$('form#product_add_to_cart_form select').each(function(i, select){
		var $select = $(select);
		$select.hide();
		$select.find('option').each(function(j, option){
			var $option = $(option);
			// Create a radio:
			if ($option.val() != "") {
				var $radio = $('<input type="radio" />');
				// Set id, name, and value. Also a tempname to fake the add to cart script:
				$radio.attr('name', $select.attr('name')).attr('value', $option.val()).attr('id', $select.attr('name')+'_'+j).attr('tempname', $select.attr('name'));
				// Set checked if the option was selected
				
				// Insert radio before select box:
				$select.before($radio);
				// Insert a label:
				$select.before(
			  		$("<label />").attr('for', $select.attr('name')+'_'+j).text($option.text())
				);
				
			}
		});
		$select.remove();
	});

	// Remove the names of all non-checked radio options
	$('#product :radio').each(function () {
		if ($(this).attr('tempname') != undefined && !$(this).is(':checked')) {
			$(this).attr('name','');
		}
	});

	$('#product :radio').live('click', function () {
		$(this).attr('name', $(this).attr('tempname'));
		$(this).siblings($(this) + ':radio').each(function(i, radio) {
			// remove name of the non-selected to fool the ajax add
			$(radio).attr('name', '')
		});
	});

	// If using options plus or have a "Please select" type option where the value is blank, dont make a radio (buggy)
	var defcheck = false;
	$('#product :radio').each(function () {
		if ($(this).attr('checked')) {
			defcheck = true;
		}
	});
	if (!defcheck) {
		$('#product :radio').each(function (i) {
			if (i == 0) {
				$(this).click();
				return;
			}
		});
	}

});
