jQuery(document).ready(function(){
 jQuery("a.category-link").corners("2px");
});

/*******************************************************************************

	GENERAL

*******************************************************************************/

function initNavigation()
{
	var navItems = document.getElementById("service").getElementsByTagName("li");

	for (var i=0; i<navItems.length; i++) {
		navItems[i].onmouseover=function() { this.className += " over"; }
		navItems[i].onmouseout=function() { this.className = "menuparent"; }
	}
}

function validateNewsletterForm()
{
	if ($F('newsletter_name').length == 0) {
		$('newsletter_name').focus();
		alert("Please enter your name.");
	}
	else if ($F('newsletter_asi').length != 6) {
		$('newsletter_asi').focus();		
		alert("Please enter a valid ASI number.");
	}
	else if (isNaN($F('newsletter_asi'))) {
		$('newsletter_asi').focus();		
		alert("Please enter a valid ASI number.");
	}
	else return true;
	
	return false;
}


/*******************************************************************************

	SIGNUP

*******************************************************************************/

function copyBillingAddy()
{
	if ($('copy_billing_addy').checked == true) {		
		$('user_shipping_address1').value = $('user_billing_address1').value;
		$('user_shipping_address2').value = $('user_billing_address2').value;
		$('user_shipping_city').value     = $('user_billing_city').value;
		$('user_shipping_state').value    = $('user_billing_state').value;
		$('user_shipping_zip').value      = $('user_billing_zip').value;
	}
}

/*******************************************************************************

	TIE DYE FILTERS

*******************************************************************************/

function toggleFilterPanel(panel)
{
	// first, hide any that are already open
	var panels = $A(Element.getElementsByClassName('filter', 'filter_sheet'));
	
	panels.each(function(node){
		if (node != $(panel+'_filter_options')) Element.hide(node);
	});
	
	if ($(panel+'_filter_options').style.display == 'none') {
		Element.show(panel+'_filter_options'); 			
		$(panel+'_filter_trigger').innerHTML = "Cancel&uarr;";
	}
	else {
		Element.hide(panel+'_filter_options'); 	
		$(panel+'_filter_trigger').innerHTML = "Update&darr;";		
	}
}

function selectFilterChildren(parent, id, checkbox_class)
{
	var children = $A(Element.getElementsByClassName('body', checkbox_class+'_'+id));	

	children.each(function(node){
		node.checked = parent.checked;
	});			
}

function checkParentState(child, parent)
{
	// if we're unchecking, uncheck the parent and be done with it
	if (child.checked == false) {
		$(parent).checked = false;		
		return;
	}
	
	var check_parent = true;
	var children = $A(Element.getElementsByClassName('body', child.className));
	
	children.each(function(node){
		if (node.checked == false) {
			check_parent = false;
			$(parent).checked = false;
			throw $break;
		}
	});			
	
	$(parent).checked = check_parent;
}

function validateGarmentFilter()
{
	var colors       = $$('#color_filter_options input[type="checkbox"]')
	var num_selected = 0;
	
	colors.each(function(node) {
		if (node.checked) {
			if (num_selected++ > 6) {
				throw $break;
			}
		}
	});

	if (num_selected > 6) {		
		toggleFilterPanel('color');		
		alert("No tie dyes have more than 6 colors. Please choose fewer colors.");
		return false;		
	}
}

function toggleFootwearSizingChart()
{
	Element.toggle('footwear_sizing_chart');	
}

function togglePriceChart(id)
{
	Position.prepare();
	coords = Position.cumulativeOffset($('price_chart_trigger_'+id));
	$('price_chart_wrapper_'+id).style.left = coords[0]+14+'px';
	$('price_chart_wrapper_'+id).style.top  = coords[1]-2+'px';
	Element.toggle('price_chart_wrapper_'+id);	
}

function addToCart(id)
{
	Position.prepare();
	
	// first 'hide' the box in the background so if we need to shift
	// the box over away from the side of the window, we don't see a 
	// flicker
	$('add_overlay').style.zIndex = -1;
	
	// have to reveal it before we can determine the offsets
	Element.toggle('add_overlay');	
	
	coords = Position.cumulativeOffset($('product_buttons_'+id));
	if (coords[0]-40 < 0) {
		coords[0] = 40;
	}
	$('add_overlay').style.left   = coords[0]-40+'px';
	$('add_overlay').style.top    = coords[1]-80+'px';
	$('add_overlay').style.zIndex = 999;
	return false;
}

function showScreenprintingInfo(id)
{
	Position.prepare();
	
	// first 'hide' the box in the background so if we need to shift
	// the box over away from the side of the window, we don't see a 
	// flicker
	$('screenprint_overlay').style.zIndex = -1;
	
	// have to reveal it before we can determine the offsets
	Element.toggle('screenprint_overlay');	
	
	coords = Position.cumulativeOffset($('screenprint_info_trigger_'+id));
	$('screenprint_overlay').style.left   = coords[0]-40+'px';
	$('screenprint_overlay').style.top    = coords[1]-80+'px';
	$('screenprint_overlay').style.zIndex = 999;
	return false;	
}

/*******************************************************************************

	TIE DYE BUILDER

*******************************************************************************/

function toggleSelectionPanel(panel)
{
	// first, hide any that are already open
	var panels = $A(Element.getElementsByClassName('builder_controls', 'selection_sheet'));
	
	panels.each(function(node){
		if (node != $(panel+'_selection_options')) Element.hide(node);
	});
	
	if ($(panel+'_selection_options').style.display == 'none') {
		Element.show(panel+'_selection_options'); 			
	}
	else {
		Element.hide(panel+'_selection_options'); 	
	}
}

/*******************************************************************************

	CART & CHECKOUT

*******************************************************************************/

function toggleArtworkUploadPanel(id)
{
	if ($('upload_artwork_'+id).style.display == 'none') {
		Element.show('upload_artwork_'+id); 			
		Element.hide('upload_artwork_trigger_'+id); 			
		Element.hide('screenprint_copy_'+id); 			
		$('cart_item_has_artwork_'+id).value = true;
	}
	else {
		Element.hide('upload_artwork_'+id); 	
		Element.show('upload_artwork_trigger_'+id); 					
		Element.show('screenprint_copy_'+id); 					
		$('cart_item_has_artwork_'+id).value = false;
	}	
}


/*******************************************************************************

	ADMIN FUNCTIONS

*******************************************************************************/

function updateColorSwatch (swatch, input) 
{
	swatch.style.backgroundColor = "#" + input.value;
}

function toggleCustomColorPicker()
{
	if ($('camo_custom_camo').checked == true) {
		Element.show('default_color_wrapper');
	}
	else {
		Element.hide('default_color_wrapper');
	}
}
