$(document).ready(function() {
	setupDefaultTextHandlers();
});

/* Popup:
popup(URL, Width, Height, Left, Top, AutoCenter[0=False, 1=True], FullScreen[0=False, 1=True])
<a href="javascript:popUp('domain', 640, 480, 50, 50, 1, 0)">Popup!</a>
*/
function popup(URL, popWidth, popHeight, popLeft, popTop, autoCenter, fullScreen) {
	day = new Date();
	id = day.getTime();
	if ( autoCenter == 1 ) {
		var popLeft = (screen.width - popWidth) / 2;
		var popTop = (screen.height - popHeight) / 2;
	}
	if ( fullScreen == 1 ) {
		// Open in Full Screen window!
		eval("page"+id+" = window.open(URL, '"+id+"', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width="+(screen.width-10)+",height="+(screen.height-26)+",left=0,top=0');");
	} else {
		// Open in normal window!
		eval("page"+id+" = window.open(URL, '"+id+"', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width="+popWidth+",height="+popHeight+",left="+popLeft+",top="+popTop+"');");
	}
}

function setupDefaultTextHandlers(){
	$(".default-text").each(function(){
		this.defaultValue = this.value;
		$(this).addClass("default-text-empty");
		$(this).focus(function(){
			if(this.value == this.defaultValue){
				this.value = "";
				$(this).removeClass("default-text-empty");
			}
		});
		$(this).blur(function(){
			if(!this.value.length){
				$(this).addClass("default-text-empty");
				this.value = this.defaultValue;
			}
		});
	});
}

(function(){
	var popup = null;
	this.popupLiveChat = function(a){
		if(!popup || popup.closed){
			popup = window.open(a.href, "LiveChatPopup", "menubar=no,location=no,resizable=yes,scrollbars=yes,status=yes,width=534,height=316");
		}
		else{
			popup.focus();
		}
		return false;
	}
})();

function initAutoSearch(element){

	var search = element;
	$(search).attr("autocomplete", "off");
	
	var output = document.createElement("div");
	output.className = "search-autocomplete";
	output.style.position = "absolute";
	output.style.width = $(search).outerWidth() + "px";
	
	var outputPosition = $(search).offset();
	outputPosition.top += $(search).outerHeight();

	output.style.left = outputPosition.left + "px";
	output.style.top = outputPosition.top + "px";
	
	document.body.appendChild(output);
	$(output).hide();
	
	var timeout = null;
	$(search).keypress(function(){
		if(timeout) clearTimeout(timeout);
		timeout = setTimeout(lookup, 400);
	});
	

	$(search).blur(function(){
		setTimeout(function(){
			$(output).empty().hide();
		}, 100);
	});

	
	function lookup(){
		timeout = null;
		$(output).show();
		$(output).load("products/auto-search.php?keyword=" + encodeURIComponent(search.value));
	}
}

