var RecaptchaOptions = {
   theme : 'blackglass'
};


$(document).ready(function()
{
	if($.fn.equalHeights) {
		if ($('#content').length > 0 && $('#main_wrapper').length > 0) {
			$('#content').equalHeights();
		}
	}
	
	// load facebox
	if (jQuery.facebox) {
		$.facebox.settings.opacity = 0.7;
		$('a[rel*=facebox]').facebox();
	}
	
	// - creates auto striping tables
	$('table.zebra').each(function() {
		$(this.getElementsByTagName("tbody")[0].getElementsByTagName("tr")).each(function(i) {
			$(this).mouseover(function(){$(this).addClass('ruled')});
			$(this).mouseout(function(){$(this).removeClass('ruled')});
			if(i%2==1) { 
				$(this).addClass('even');
			}
		});
	});
	
	// - creates auto-posting elements for per page and sort by
	if($('#admin_option_per_page').length > 0) {
		$('#admin_option_per_page').change(function() {
			$('#admin-listing-options').submit();
		});
	}
	if($('#option_per_page').length > 0) {
		$('#option_per_page').change(function() {
			$('#product-listing-options').submit();
		});
	}
	if($('#option_sort_by').length > 0) {
		$('#option_sort_by').change(function() {
			$('#product-listing-options').submit();
		});
	}
	
	if ($('#category_selection input').length > 0) {
		$('#category_selection input').click(function() {
			ckd = ($(this).attr('checked') == true);
			if (ckd) {
				// we need to go up each level and check each parent:
				parent = $(this).parent().parent().parent().parent();
				while (parent.attr('class') == 'category_selection_element') {
					cb = $('input', parent.children()[0]);
					cb.attr('checked', true);
					parent = parent.parent().parent();
				}
			}
		});
	}
	
	// hide categories if selecting a parent collection in products:
	if($('#product_parent_id').length > 0) {
		$('#product_parent_id').change(function() {
			if ($(this).val() != '0') {
				$('#product_manufacturer_id').parent().hide();
			} else {
				$('#product_manufacturer_id').parent().show();
			}
		});
		if ($('#product_parent_id').val() != '0') {
			$('#product_manufacturer_id').parent().hide();
		}
	}
	
	if($('#product_track_inventory').length > 0) {
		$('#product_track_inventory').click(function() {
			if ($(this).attr('checked') == true) {
				$('#product_quantity').parent().show();
			} else {
				$('#product_quantity').parent().hide();
			}
		});
		if ($('#product_track_inventory').attr('checked') != true) {
			$('#product_quantity').parent().hide();
		}
	}
	
	// hide category top level options if not top level...
	if($('#top_level_categories').length > 0) {
		$('#category_parent_id').change(function() {
			if ($(this).val() != '0') {
				$('#top_level_categories').hide();
			} else {
				$('#top_level_categories').show();
			}
		});
		if ($('#category_parent_id').val() != '0') {
			$('#top_level_categories').hide();
		}
	}
	
	// category toggle:
	$('a.toggle').click(function() {
		$('#category_toggle_' + this.rel).toggle();
		$(this).toggleClass('open');
		return false;
	});
	$('a.toggle_all').click(function() {
		if ($(this).hasClass('opened')) {
			$('#category_selection a').removeClass('open');
			$('#category_selection ul.to_be_toggled').hide();
		} else {
			$('#category_selection a').addClass('open');
			$('#category_selection ul.to_be_toggled').show();
		}
		$(this).toggleClass('opened');
		
		return false;
	});
	
	// user profile
	if ($('#copy_address').length > 0) {
		$('#copy_address').click(function() {
			if (this.checked) {
				$(Array('address_1', 'address_2', 'city', 'state', 'zipcode', 'country')).each(function() {
					$('#user_detail_billing_' + this).val($('#user_detail_shipping_' + this).val());
				});
			}
		});
	}
	if ($('#copy_checkout_address').length > 0) {
		$('#copy_checkout_address').click(function() {
			if (this.checked) {
				$(Array('first_name', 'last_name', 'address_1', 'address_2', 'city', 'state', 'zipcode', 'country', 'phone')).each(function() {
					$('#order_billing_' + this).val($('#order_shipping_' + this).val());
				});
			}
		});
	}
	
	// registry signup screen
	if ($('#registry_event_type').length > 0) {
		$('#registry_event_type').change(function() {
			if ($('#registry_event_type option:selected').text().toLowerCase() == 'other') {
				$('#registry_event_type_other').attr('disabled', false);
			} else {
				$('#registry_event_type_other').attr('disabled', true);
				$('#registry_event_type_other').val('');
			}
		});
		$('#copy_registry_address').click(function() {
			$(Array('title', 'first_name', 'middle_initial', 'last_name', 'address_1', 'address_2', 'city', 'state', 'zipcode', 'phone_day', 'country', 'phone_evening', 'fax', 'email')).each(function() {
				$('#registry_coreg_' + this).val($('#registry_reg_' + this).val());
			});
		});
		$('#before_selection_registrant').click(function(){if(this.checked){copyShippingAddresses('before', 'reg', false);}});
		$('#before_selection_coregistrant').click(function(){if(this.checked){copyShippingAddresses('before', 'coreg', false);}});
		$('#before_selection_other').click(function(){if(this.checked){copyShippingAddresses('before', '', true);}});
		$('#after_selection_registrant').click(function(){if(this.checked){copyShippingAddresses('after', 'reg', false);}});
		$('#after_selection_coregistrant').click(function(){if(this.checked){copyShippingAddresses('after', 'coreg', false);}});
		$('#after_selection_other').click(function(){if(this.checked){copyShippingAddresses('after', '', true);}});
	}
	
	// lightboxes
	if ($('a.lightbox').length > 0) {
		$('a.lightbox').lightBox({
			containerResizeSpeed: 350,
			imageLoading: '/images/lightbox-ico-loading.gif',
			imageBtnClose: '/images/lightbox-btn-close.gif',
			imageBtnPrev: '/images/lightbox-btn-prev.gif',
			imageBtnNext: '/images/lightbox-btn-next.gif',
			imageBlank:   '/images/lightbox-blank.gif',
			imageFrame: '' //'/images/lightbox-frame.png'
		});
	}
});

function copyShippingAddresses(type, prefix, clear) {
	$(Array('first_name', 'last_name', 'address_1', 'address_2', 'city', 'state', 'zipcode', 'country', 'phone_day')).each(function(){
		$('#registry_' + type + '_ship_' + this).val(clear ? '' : $('#registry_' + prefix + '_' + this).val());
	});
}

// these copy product quantities for collections to pass to cart, registry and wishlist routines
function copyCollectionFor(method) {
	var f = $('#' + method + '_collection_items');
	var elms = '';
	$(':text').each(function() {
		if (elms != '') {
			elms += '&';
		}
		elms += this.id.replace(/product\_quantities\_/, '') + '=' + $(this).val();
	});
	f.val(elms);
}

function checkAll(elm, wrapper, prefix){
	if (wrapper == null){
		wrapper = document;
	}
	if (prefix == null) {
		prefix = '';
	}

	$(wrapper.getElementsByTagName('input')).each(function(){
		if (this.type.toLowerCase() == 'checkbox' && !this.disabled) {
			if (prefix.length <= 0 || !this.id.substring(0, prefix.length) != prefix) {
				this.checked = elm.checked;
			}
		}
	});
}