var TopMenu;

window.addEvent('domready',function() {
	new mooInputLabel({
		inputs: $$('input.label')
	});
	
	CreateSliders('div.sidebar_navigation.left>ul>li>a',false,'ul');
//	CreateSliders('div.sidebar_navigation.left>h4',false,'ul');
	CreateSliders('div.sidebar_navigation.right>div>h4',false,'ul');
	CreateSliders('div.sidebar_navigation.left.services>h4',false,'ul');
	CreateSliders('div.sidebar_navigation.left.holidays>h4',false,'ul');
	
	TopMenu = new MenuMatic({ id:'product_navigation_top' });
});

function highlight_missing() {
	var missing = highlight_missing.arguments;
	if (missing.length > 1) {
		for (var i = 0; i < missing.length; i++) {
			document.getElementsByName(missing[i])[0].setStyle('border','1px solid red');
		}
	}
}

function toggle_other_recipient() {
	if ($('recipient').value == 'X') {
		$('recipient_other').setStyle('visibility','visible');	
	}
	else {
		$('recipient_other').setStyle('visibility','hidden');
	}
}

function toggle_other_date(target_id) {
	if ($('arrive_by|'+target_id).value == 'XXXX-XX-XX') {
		$('arrive_by_other|'+target_id).setStyle('visibility','visible');	
	}
	else {
		$('arrive_by_other|'+target_id).value = '';
		$('arrive_by_other|'+target_id).setStyle('visibility','hidden');
	}
}

function toggle_shipping() {
	if ($('shipping_same').checked) {
		$('shipping_info').style.display = 'none';
		$$('#shipping_info input, #shipping_info select').each(function(element) {
			element.value = '';
			element.disabled = true;
		});
	}
	else {
		$('shipping_info').setStyle('display', '');
		$$('#shipping_info input, #shipping_info select').each(function(element) {
			element.disabled = false;
		});
	}
}


function CreateSliders(parent_css,child_css,sibling_css,aunt_css) {

	var prevFx = null;																	// Holds the prior slider so that we can collapse it upon the next click
	var prevElem = null;

	$('wrapper').getElements(parent_css).each( function( elem ) {						// Loop through the categories
		var list;
		if (child_css) {
			list = elem.getElement(child_css);
		}
		if (sibling_css) {
			list = elem.getNext(sibling_css);
		}
		if (aunt_css) {
			list = elem.getParent().getNext(aunt_css);
		}
			
		if (list) {																		// If it exists...
			//list.style.display = 'block';												// Make the slider object display (we hid it in CSS to prevent "blinky" page loads
			
			var myFx = new Fx.Slide(list, { duration: 200 });
			if (list.hasClass('open')) {
				prevFx = myFx;
			}
			else {
				myFx.hide();
			}
			
			//var myFx = .hide();					// Create a new slider object tied to the list
			
			elem.addEvents({															// Add events to the parent element
				'click' : function(e) {													// Add a "click" event
					e = new Event(e).stop();											// Stop any default clickiness
					
					myFx.cancel();														// Cancel anything happening already
				
					if (Browser.Engine.trident) {
						if (myFx.open) myFx.hide();
						else myFx.slideIn();
						if (prevFx) prevFx.hide();
					}
					else {
						if (myFx.open) myFx.slideOut();
						else {
							myFx.slideIn();
							if (prevFx) prevFx.slideOut();
						}
					}
					
					prevFx = myFx;														// Keep this effect as the previous

					elem.toggleClass('active');	
					if (prevElem && prevElem != elem) {
						prevElem.removeClass('active');
					}
					prevElem = elem;
				}
			});
		}
	})
}

var mooInputLabel = new Class({
	Implements: [Options, Events],
	options: {
		inputs: [ ]
	},
	initialize: function(options) {
		this.setOptions(options);
		var inputs = this.options.inputs;
		var self = this;
		inputs.each(function(e) {
			e.addEvent('focus', function() {
				self.inputFocus(e);
			})
			.addEvent('blur', function() {
				self.inputBlur(e);
			})
			.setProperty('value', e.title)
			.addClass('inactive');
		});
	},
	inputFocus: function(x) {
		if ($(x).value == x.title) {
			$(x).value = '';
		}
		$(x).morph('input.active');
	},
	inputBlur: function(x) {
		if ($(x).value == '') {
			$(x).setStyle('color', '#fff');
			$(x).value = x.title;
			$(x).morph('input.inactive');
		}
		else if ($(x).value == x) {
			$(x).morph('input.inactive');
		}
		else {
			$(x).morph('input.active');
		}
	}
});