// joe's js

$(document).ready(function() {
						   
	$('.Left.Handed:not(.Gloves) #pp-photos').prepend('<div class="lefty-icon"></div>');

	$(".headcover-note").parent().css("border", "0").css("margin", "0").insertAfter($("#condition-specs"));
	
	$(".Complete #other-specs").addClass("border");
	
	$(".Complete .headcover-note").css("padding", "10px").insertAfter($("#other-specs"));
	
	$(".specs-box").fadeIn("slow");
	$(".cond-link").append($(".grips-option"));
	
	$specsHeight = $("#pp-specs").height();
	$("#pp-specs").height($specsHeight);
	$("select").change(function () {
		$(".specs-box").hide().fadeIn("slow");
	})
	
	// Product Demo/Tour Button
	$('span#easy2Container_tbal_610').clone().prependTo('#fnb-content');
	
});









function update_current_selections(selected_config_type){
	for(i=0;i<current_selection_data.length;i++){
		field = 'search_'+current_selection_data[i][0];
		value = document.getElementById(field).options[document.getElementById(field).selectedIndex].value;

		if( selected_config_type != undefined && current_selection_data[i][0] != selected_config_type && current_selection_data[i][3] == 'auto') {
			value = 'none';
		}

		current_selection_data[i][1] = value;

		if( current_selection_data[i][0] == selected_config_type ) {
			if( value == '' || value == 'none' ) {
				current_selection_data[i][3] = 'auto';
			} else {
				current_selection_data[i][3] = 'set';
			}
		}
	}
}

function update_selectors(selected_config_type)
{
	var config_type;
	var sandu_id;
	var selected;
	var size;

	update_current_selections(selected_config_type);
	for (config_type in all_options) {
		size = 0;
		i=0;
		show_options = new Array();
		empty_menus(config_type);
		for (sandu_id in all_options[config_type]) {
			available = check_availability(sandu_id, config_type);
			if (available == 'in_stock' || all_options[config_type][sandu_id][1] == '' || all_options[config_type][sandu_id][1] == 'none') {
				// this is an option we need to draw!
				show_options[i] = all_options[config_type][sandu_id];
				i++;
			}
			if( available == 'in_stock' && all_options[config_type][sandu_id][1] != '' && all_options[config_type][sandu_id][1] != 'none') {
				size++;
			}
		}

		for( i=0; i<show_options.length; i++ ) {
			this_option = show_options[i];
			if( size == 1 && (this_option[1] == '' || this_option[1] == 'none') ) {
				continue;
			}

			add_option_to_menu(this_option, selected_config_type);
		}

		update_num_options_element = $("#number_options_"+config_type);
		if( update_num_options_element ) {
			number_options_message = "(" + size + " option";
			if( size > 1 ) {
				number_options_message += "s";
			}
			number_options_message += ")";
			update_num_options_element.html(number_options_message);
		}

		$("#search_" + config_type).toggleClass("only-option", (size == 1));
	}

	update_product_information();
}


function empty_menus(config_type) {
	var config_selection_string = 'search_'+config_type;
	var config_selector = document.getElementById(config_selection_string);
	var sandu_id;
	
	config_selector.length = 0;
}

function add_option_to_menu(menu_info, selected_config_type){
	var option;
	var selected = false;
	var has_selection = true;
	var i;
	var selection_name;
	for (i=0;i<current_selection_data.length;i++) {
		if( current_selection_data[i][0] == menu_info[0] ) {
			selection_name = current_selection_data[i][2];
			if( current_selection_data[i][1] == menu_info[1] ) {
				selected = true;
			}
			if( current_selection_data[i][1] == 'none' || current_selection_data[i][1] == '' ) {
				has_selection = false;
			}
			break;
		}
	}

	if (menu_info[1] == 'none' || menu_info[1] == '') {
		if (has_selection) {
			option = new Option('Reset ' + selection_name + '...', 'none', false);
		}
		else {
			option = new Option(menu_info[2], menu_info[1], false);
			option.selected = true;
		}
	}
	else {
		option = new Option(menu_info[2], menu_info[1], false);
		option.selected = selected;
	}

	// add option to menu
	try {
		document.getElementById('search_' + menu_info[0]).add(option,null);
	} catch(e) {
		document.getElementById('search_' + menu_info[0]).add(option);
	}
}

function check_availability(config_id, config_type) {
	var i, j, k, l;
	var sku_id;
	var current_type;
	var config_skus = available_options[config_type][config_id];
	var config_skus_updated = Array();
	var available = 'in_stock';
	var exists_here;

	if (all_options[config_type][config_id][3] == 0) {
		return available;
	}

	i=0;
	for (sku_id in config_skus) {
		config_skus_updated[i] = sku_id;
		i++;
	}

	// check each selected option in turn
	for (i=0;i<current_selection_data.length;i++) {
		if (i == 'indexOf' || current_selection_data[i][1] == 'none' || current_selection_data[i][1] == '') {
			// skip if no option selected
			continue;
		}
		current_type = current_selection_data[i][0];
		selected_config = current_selection_data[i][1];
		if (current_type == config_type) {
			// skip if checking against this selection
			continue;
		}

		// set current availability and reset updated availability
		config_skus = config_skus_updated;
		config_skus_updated = Array();
		l = 0;

		// look for any skus in selected type that have passed all previous selection tests
		for (j in available_options[current_type][selected_config]) {
			for (k in config_skus) {
				if (config_skus[k] == j) {
					config_skus_updated[l] = config_skus[k];
					l++;
				}
			}
		}

		// option is unavailable if no skus pass at any test
		if( config_skus_updated.length < 1 ) {
			available = 'unavailable';
			break;
		}
	}
	return available;
}

function update_product_availability() {
	update_current_selections();

	var number_available = 0;
	var all_selected = true;

	for(i in product_availability) {
		product_availability[i][1] = 1;
		number_available++;
	}

	for(i in current_selection_data ) {
		if (i == 'indexOf' || current_selection_data[i][1] == 'none' || current_selection_data[i][1] == '') {
			all_selected = false;
			continue;
		}

		config_type = current_selection_data[i][0];
		selected_option = current_selection_data[i][1];

		for(option in available_options[config_type]) {
			if( option != selected_option ) {
				for(model_id in available_options[config_type][option]) {
					for(j in product_availability) {
						if( model_id == product_availability[j][0] && product_availability[j][1] != 0 ) {
							if( (config_type == "condition_id" || current_selection_data[i][3] == 'auto') && product_availability[j][1] != 0 ) {
								product_availability[j][1] = -1;
							} else {
								product_availability[j][1] = 0;
							}
							number_available--;
						}
					}
				}
			}
		}
	}

	return all_selected;
}

var selected_child_class = "";

function update_product_information() {
	var i, j;

	all_selected = update_product_availability();

	// find the lowest & highest price out of currently available based on options
	lowest_price = -1;
	lowest_price_condition = '';
	lowest_price_num_available = 0;

	lowest_price_by_condition = new Object();

	highest_price = 0;
	highest_price_child_id = 0;

	lowest_headcover_id = -1;

	for( i in product_availability ) {
		if( product_availability[i][1] == 0 ) {
			continue;
		}

		this_id = product_availability[i][0];
		this_available = product_availability[i][2];
		this_condition = product_availability[i][3];
		this_price = product_availability[i][4];
		this_headcover = product_availability[i][5];

		if( lowest_price_by_condition[this_condition] == undefined || this_price < lowest_price_by_condition[this_condition]) {
			lowest_price_by_condition[this_condition] = this_price;
		}

		if( product_availability[i][1] < 1 ) {
			continue;
		}

		if( lowest_price < 0 || this_price < lowest_price ) {
			lowest_price = this_price;
			lowest_price_condition = all_options["condition_id"][this_condition][2];
			lowest_price_num_available = this_available;
		}
		else if( this_price == lowest_price ) {
			lowest_price_num_available += this_available;
		}

		if( this_price > highest_price ) {
			highest_price = this_price;
			highest_price_child_id = this_id;
		}

		if( lowest_headcover_id < 0 || this_headcover < lowest_headcover_id ) {
			lowest_headcover_id = this_headcover;
		}
	}

	// set lowest price & how many available at that price
	if( lowest_price > 0 && lowest_price_num_available && lowest_price_condition ) {
		$("#big-price").html(WGformatMoney(lowest_price));
		$("#price-condition-name").html(lowest_price_condition);
		$("#only-x-left-value").html(lowest_price_num_available);

		$("#only-x-left").removeClass();
		$("#only-x-left").addClass("only-" + lowest_price_num_available + "-left");

		the_msrp = document.getElementById('hidden_msrp');
		if( the_msrp.value > 0 ) {
			lowest_price_savings = the_msrp.value - lowest_price;
			$("#you-save-dollar").html(WGformatMoney(lowest_price_savings));

			lowest_price_savings_percent = lowest_price_savings / the_msrp.value * 100.0;
			$("#you-save-percent").html(Math.round(lowest_price_savings_percent));
		}
	}
	
	// set headcover to lowest (is included -> not available -> not applicable)
	if( lowest_headcover_id > 0 && headcover_options.length > 0 ) {
		headcover_id = 'o' + lowest_headcover_id;
		var headcover_description='';
		for (j=0;j<headcover_options.length;j++) {
			if( headcover_id == headcover_options[j][0] ) {
				headcover_description = headcover_options[j][1];
				break;
			}
		}
		$("#search_headcover")[0].value= headcover_id;
		$("#headcover_description").html(headcover_description);
	}

	// show condition when option selected
	for(i in current_selection_data) {
		if( current_selection_data[i][0] == 'condition_id' ) {
			if( current_selection_data[i][1] == 'none' || current_selection_data[i][1] == '' ) {
				selected_condition = 'none';
			} else {
				selected_condition = current_selection_data[i][1];
			}
			for( option in all_options["condition_id"] ) {
				$("#condition_" + option).toggleClass("hide", !(option == selected_condition));
			}

			$("#3balls-only-single").toggleClass("hide", (selected_condition == "none"));
			$("#3balls-only-multiple").toggleClass("hide", (selected_condition != "none"));
		}
	}

	// update condition pricing in options
	condition_options = $("#search_condition_id")[0].options;
	for( i=0; i<condition_options.length; i++ ) {
		key = condition_options[i].value;
		price = lowest_price_by_condition[key];
		if( price ) {
			condition_options[i].text += " (" + WGformatMoney(price) + ")";
		}
	}

	// show and hide child-specific data
	var selected_child_id = 0;

	// remove any child-specific class
	if( selected_child_class != '' ) {
		$("#pp-wrap").removeClass(selected_child_class);
		selected_child_class = '';
	}

	for( i in product_availability ) {
		model_id = product_availability[i][0];
		headcover_id = product_availability[i][5];

		// update product availability based on headcover
		if( headcover_id != lowest_headcover_id && product_availability[i][1] == 1 ) {
			product_availability[i][1] = 0;
		}

		// if all of the options are selected, there must be a child to match
		// select the first available one, which is stock before unique and oldest to newest
		if( all_selected && selected_child_id == 0 && product_availability[i][1] == 1 ) {
			selected_child_id = model_id;

			// append class of selected child
			if( product_availability[i][6] != '' ) {
				selected_child_class = product_availability[i][6];
				$("#pp-wrap").addClass(selected_child_class);
			}
		}

		// set the image to the best available
		$("#images_" + model_id).toggleClass("hide", !(model_id == highest_price_child_id) );

		// show highlight & condition description of selected child
		$("#highlight_" + model_id).toggleClass("hide", !(model_id == selected_child_id) );
		$("#condition_description_" + model_id).toggleClass("hide", !(model_id == selected_child_id) );
	}

	// show the SKU when set
	$("#hidden_product_id")[0].value = selected_child_id;
	$("#selected_child_id").html(selected_child_id);
	$("#pp-sku-id").toggleClass("hide", (selected_child_id == 0));
}

// Author   : Jonathan Weesner (http://cyberstation.net/~jweesner/)
// Copyright: Use freely. Keep Author and Copyright lines intact.					
var WGdc=".";
var WGgc=",";
var WGcs="$";
function WGformatMoney(A) {
	var N=Math.abs(Math.round(A*100));
	var S=((N<10)?"00":((N<100)?"0":""))+N;
	S=WGcs+WGgroup(S.substring(0,(S.length-2)))+WGdc+S.substring((S.length-2),S.length)+((A<0&&WGnc=="(")?")":"");
	return S;
}
// WGgroup inspired by Bill Dortch's usenet post (www.hidaho.com)
function WGgroup(S) {
	return (S.length<4)?S:(WGgroup(S.substring(0,S.length-3))+WGgc+S.substring(S.length-3,S.length));
}
			
function switch_product_img(divName, child_id, totalImgs, newImage) {
	for (var i=1; i<=totalImgs; i++) {
		var showDivName = 'photo_' + child_id + '_' + i;
		var showObj = document.getElementById(showDivName);
		if (showDivName == divName)
			showObj.style.display = 'block';
		else
			showObj.style.display = 'none';
	}

	document.getElementById("zoom_link_"+child_id).href = newImage;
}

function validate_product_selection(the_form) {
	var initial_error_message = "Please select:\n";  
	var error_message = initial_error_message;  

	update_current_selections();

	// determine if all options are selected
	var all_selected = true;
	for(i in current_selection_data) {
		if( current_selection_data[i][1] == 'none' || current_selection_data[i][1] == '' ) {
			all_selected = false;
			error_message += "    " + current_selection_data[i][2] + "\n";
		}
	}

	// make sure the hidden product id has been set to the child
	var selected_child_id = $("#hidden_product_id")[0].value;
	if( all_selected == true && selected_child_id == 0 ) {
		error_message += "Invalid selections.\n";
	}

	if( error_message != initial_error_message ) {
		alert( error_message );
		return false;
	} else {
		return true;
	}
}

function debug(txt) {
	current_text = document.getElementById('debug_out').innerHTML;
	new_text = current_text + '<br/>' + txt;
	document.getElementById('debug_out').innerHTML = new_text;
}
function debug_reset() {
	document.getElementById('debug_out').innerHTML = '';
}





