/**
 * Zivio HTML Site - Store JS
 *
 * @creator Michael Huang, Joby Inc
 * @file zivio-store.js
 *
 */ 
 
/* Generates links for the color swatches */

$(document).ready(function(){
	$(".color_swatch").each(function () {
		$(this).wrap('<a href="#" class="swatch_select">');
	});
	$(".swatch_select").click(function(){return swatch_select(this, $('#zivio_box_us_type'));}); 
});
function swatch_select(link, input) { 
	// go through select options until we find one that is a class name in the link, use that
	$(input).children('option').each(function() {
		if ($(link).children('.color_swatch').hasClass($(this).val())) {
			$(input).val($(this).val()).change(); 
		}
	}); 
	return false;
}

/* Generates appropriate HTML for all input.plusminus_input
 *
 * <div class="plusminus">
 *		<a href="#" class="plusminus_decrease">Decrease quantity</a>
 *		<span><input class="plusminus_input" value="0" /></span>
 *		<a href="#" class="plusminus_increase">Increase quantity</a>
 * </div>
 * 
 * Attaches functions that allow links to increase, decrease the input value
*/
$(document).ready(function(){
	$(".plusminus_input").each(function () {
		$(this).wrap('<div class="plusminus">');
		$(this).before('<a href="#" class="plusminus_decrease">Decrease quantity</a>').after('<a href="#" class="plusminus_increase">Increase quantity</a>').wrap('<span></span>');
	});
	$(".plusminus_decrease").click(function(){return plusminus_input_change(this, -1);});
	$(".plusminus_increase").click(function(){return plusminus_input_change(this,  1);});
});
function plusminus_input_change(link, change) { 
	input = $(link).siblings("span").children(".plusminus_input");
	changed_val = parseInt(input.val()) + change;
	input.val(changed_val < 0 ? 0 : changed_val);	
	$(link).blur(); // for ie6
	return false;
}

/* Earpiece image changes based on earpiece size dropdown menu change */
$(document).ready(function(){  
	// earpieces
	ready_select_menu($("#scoop_earpieces select.size"));
	ready_select_menu($("#mushroom_earpieces select.size"));
	// zivio colors
	ready_select_menu($("#zivio_item select.size"));
});
function ready_select_menu(input) {
	input.change(function(){select_menu_set(this)});
	select_menu_set(input); // in case a value has already been selected
}
function select_menu_set(input, options) {
	// find the image selector parent
	var img_parent = $(input).parents(".shop_item");
	// remove any existing classname based on chosen input value
	$(input).children('option').each(function() {
		$(img_parent).removeClass($(this).val());
	}); 
	// classname = value of chosen input
	$(img_parent).addClass($(input).val()); 
	
	// find the color selector parent if it exists
	var color_swatches = img_parent.find('.color_swatch');
	color_swatches.each(function() {
		dis = $(this);
		if (dis.hasClass($(input).val())) {
			dis.addClass('current');
		} else {
			dis.removeClass('current');
		}
	});
}

/* Disable submit buttons until the person adds at least 1 item */
$(document).ready(function(){
	check_store_item_quantity();
	$("input.plusminus_input").change(function() {
		check_store_item_quantity();
	});
	$("a.plusminus_decrease").click(function() {
		check_store_item_quantity();
	});
	$("a.plusminus_increase").click(function() {
		check_store_item_quantity();
	});
	$("button[type='submit']").click(function() {
		check_store_item_quantity(); // <-- probably superfluous. unless they haxing with firebug
		if ($(this).hasClass("disabled")) {
			return false;
			// FIX perhaps also display some sort of alert?
		}
	});
	// set up hover and active that ie is missing
	$("button[type='submit']").mouseover(function() { $(this).addClass("hover") });
	$("button[type='submit']").mouseout(function()  { $(this).removeClass("hover"); $(this).removeClass("active") });
	$("button[type='submit']").mousedown(function() { $(this).addClass("active") });
	$("button[type='submit']").mouseup(function()   { $(this).removeClass("active") });
}); 
function check_store_item_quantity() {
	if (store_item_quantity() <= 0) {
		$("button[type='submit']").addClass("disabled");
	} else {
		$("button[type='submit']").removeClass("disabled");
	}
}
function store_item_quantity() {
	var total = 0;
	$("input.plusminus_input").each(function(){ total += $(this).val() });
	return (isNaN(total) ? 0 : total);
}