/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


/*  
 * This will replace the "Search" text in the search box when the 
 * user clicks it - will replace with search when nothing is 
 * entered - and will not clear the box if anything other
 * than "Search" is the value of the input box.
 * 
 */
jQuery(document).ready(function(){
    jQuery("#search_input")
			.focus(function () {
        if(jQuery(this).val() == this.defaultValue){
          jQuery(this).val('');
        }
			})
			.blur(function () {
        if(jQuery(this).val() == ""){
            jQuery(this).val(this.defaultValue);
        }
			})
		;
 });
jQuery.noConflict();

/**
 * sets focus to the first field when loaded, and triggers
 *   the link with the id defaultSubmit when enter is pressed

function formEventInit(formId, firstFieldId) {
	Event.observe(window, 'load', function() {
			$(formId+':'+firstFieldId).focus();
			Event.observe(formId, 'keypress', function(event) {
					if (Event.KEY_RETURN == event.keyCode)
						$(this.id+':defaultSubmit').onclick();
				}
			);
		}
	);
}
 */
/**
 * Js for adding to the cart without a page reload/redirect
 *  looks for '.addToCartActions a' for links
 *  and the rich:toolTip 'toolTip' in the a4j:form 'cartAddDialog'
 
jQuery(document).ready(function() {
		if($ && $('cartAddDialog:toolTip')) {
			jQuery(".addToCartActions a").click(function(event) {
					var tip = $('cartAddDialog:toolTip').component;
					tip.toolTipContent.innerHTML = "Adding...";
					tip.show(event);
					clearTimeout(tip.timeout);
					new Ajax.Request(this+'&ajax=1', { method:'get',
							onSuccess: function(transport, json){
								$('cartLink').innerHTML = 'View Cart ('+json.cartSize+(json.cartSize!=1 ? ' Items)' : ' Item)');
								tip.toolTipContent.innerHTML = "Added to Cart.<p class='note'>(scroll down to checkout)</p>";
								tip.timeout = setTimeout("$('cartAddDialog:toolTip').component.hide({});", 3500);
							},
							onFailure: function() {
								alert("Could not add to cart, please try again later or disable javascript.");
							}
						}
					);
					return false;
				}
			);
		}
	}
);
*/
function verifyAddress(el) {
        if(el.value.indexOf('@') != -1) {
                alert("Make sure that the address you just entered is a street address.");
        }
}


Ext.ns('Lighthouse.main');

/** callback for a link click that does AJAX JSON processing and displays the contents of 'message' */
Lighthouse.main.messageCall = function(/*message function(data, link)*/ successCallback) {
	return function() {
		// set loading cursor
		jQuery('body, a').css('cursor', 'wait');

		var link = this;
		jQuery.getJSON(link.href,
			function(data) {
				jQuery('body, a').css('cursor', null);

				if(!data) {
					// failed request
					data.message = "Error processing action.";
				} else {
					// passed callback
					var newMessage;
					if(successCallback) {
						newMessage = successCallback(data, link);
						if(newMessage)
							data.message = newMessage;
					}
				}

				// display flash message
				var messageBox = Ext.Msg.show({
					animEl: link,
					title:'Complete',
					msg:data.message,
					closable:false,
					modal:false,
					width:300
				});
				messageBox.getDialog().getEl()
					.stopFx()
					.fadeIn({easing: 'easeOut', duration: .5})
					.pause(2)
					.fadeOut({ callback: function() { messageBox.hide(); } })
				;
			}
		);
		return false;
	}
}
