/* GENERAL FUNCTIONS
-----------------------------------------------------*/
(function($) {	  
	var _manufacturerId;
	
	function _setupSubscribe() {
		var oInput = $('#newsletter-subscribe').find('input[name=email]');
		var sTitle = "Enter email address...";
		if(!oInput) return;
		
		$(oInput).focus(function() {
			if (this.value == sTitle) {
				this.value = '';
				this.select();
			};
		});
		$(oInput).blur(function() {
			if (!this.value.length) { this.value = sTitle; }
		}); 
		$("#newsletter-subscribe").submit(function(event) {
			if($("#newsletter-subscribe input[type=checkbox]:checked").length < 1) {
				$("#newsletter-subscribe p").text("You must check the box above to subscribe.");
				return false;
			};
		});
	};
	
	function _setupDropdownSearch() {
		$("select[name='manufacturer']").change(_manufacturerSelectHandler);
		if($("select[name='manufacturer']").val()) _manufacturerSelectHandler(null);
		$("select[name='model']").change(_modelSelectHandler);
	};
	
	function _manufacturerSelectHandler(event) {
		var manId = $("select[name='manufacturer']").val();
		if(!manId) return;
		
		_manufacturerId = manId;
		$("select[name='model']").attr("disabled","disabled").html("<option value=''>Loading Models...</option>");
		
		$.getJSON("/includes/_ajax.php", { display: "models", manufacturerId:_manufacturerId }, function(j) {
			var options = "<option> - Select Model - <\/option>";
			for (var i = 0; i < j.length; i++) {
				options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + "<\/option>";
			}
			$("select[name='model']").attr("disabled","").html(options);
		});
	};
	
	function _setupHeaderSearch() {
		var oInput = $('#search-bar-content').find('input[name=q]');
		var sTitle = "Search Products...";
		if(!oInput) return;
		
		$(oInput).focus(function() {
			if (this.value == sTitle) {
				this.value = '';
				this.select();
			};
		});
		$(oInput).blur(function() {
			if (!this.value.length) { this.value = sTitle; }
		}); 
	};
	
	function _setupSideSearch() {
		var oInput = $('#side-search').find('input[name=q]');
		var sTitle = "Search Products...";
		if(!oInput) return;
		
		$(oInput).focus(function() {
			if (this.value == sTitle) {
				this.value = '';
				this.select();
			};
		});
		$(oInput).blur(function() {
			if (!this.value.length) { this.value = sTitle; }
		}); 
	};
	
	function _modelSelectHandler(event) {
		if(!this.value) return;
		
		var aValues = String(this.value).split("!");
		var sLoc = "/hydraulic-seal-saver/index.php?mId="+_manufacturerId;
		if(aValues[1]) sLoc += "&cId="+aValues[1];
		sLoc += "&dId="+aValues[0];
		
		window.location = sLoc;
	};

	$(document).ready(function() {
		_setupSubscribe();
		_setupDropdownSearch();
		_setupHeaderSearch();
		_setupSideSearch();
		
		$("a[href^='http://'],a[href$='.doc'],a[href$='.pdf'],a[href$='.jpg']").attr("target", "_blank");	// EXTERNAL LINKS IN NEW WINDOW
		$("ul.nav").find("li").hover(function() { $(this).addClass("hover"); }, function() { $(this).removeClass("hover"); });		
	});
})(jQuery);
