﻿

//---------------------------------------------------------  PAGE LOAD

$().ready(function ()
{
	$('input').each(function ()
	{
		if (this.getAttribute("default") && this.type == "text")
		{
			this.onfocus = setDefault;
			this.onblur = getDefault;
			if (this.value == "") { this.value = this.getAttribute("default"); }
		}
	});

});

function setDefault() { if (this.getAttribute("default")) { if (this.value == this.getAttribute("default")) { this.value = ""; } } }
function getDefault() { if (this.getAttribute("default")) { if (this.value == "") { this.value = this.getAttribute("default"); } } }


//---------------------------------------------------------  USER LIST

function toggleMenu(id)
{
	if (menu = document.getElementById(id))
	{
		if (menu.style.display == "block") { menu.style.display = "none"; }
		else
		{
			menu.style.display = "block";

			if ($(menu).parent().children('ul')[0].id == "dealerListTopMenu")
			{
				var map = document.getElementById("myPreferredMap");
				displayPreferredMap(map.id, map.getAttribute("lat"), map.getAttribute("long"), 15);
			}
		}
	}
}


//---------------------------------------------------------  GOOGLE MAP API3

function displayPreferredMap(argMapElementId, argTargLat, argTargLng, zoom) {
	if (container = document.getElementById(argMapElementId)) {
		if (container.innerHTML == '') {
			if (argTargLat == '' || argTargLat == 'undefined' || argTargLng == '' || argTargLng == 'undefined') {
				container.innerHTML = 'No map is available for this location';
				$(container).addClass('gMapUnavailable');
			} else {
				if (!zoom) { zoom = 15; }
				var map = new google.maps.Map(container, {
					zoom: zoom,
					center: new google.maps.LatLng(argTargLat, argTargLng),
					disableDefaultUI: false,
					mapTypeId: google.maps.MapTypeId.ROADMAP
				});

				var marker = new google.maps.Marker({
					position: new google.maps.LatLng(argTargLat, argTargLng),
					map: map,
					flat: false,
					clickable: true
				});
			}
		}
	}
}

function handleViewMap(argDealerID, argTargLat, argTargLng)
{
	$('#dealerMap' + currentDealerID).hide();
	$('#dealerLoc' + currentDealerID).removeClass('active');
	$('#dealerMap' + argDealerID).show();
	$('#dealerLoc' + argDealerID).addClass('active');

	displayPreferredMap(("dealerMap" + argDealerID), argTargLat, argTargLng);

	location.href = "#dealerLoc" + argDealerID + "a";
	currentDealerID = argDealerID;
}

function toggleOnlineDealers()
{
	$('.sidebar .dealerListingBlock').hide();
	$('#' + this.parentNode.getAttribute("target")).show();
}

function ValidateDealerForm(zipcode, city, state)
{
	try
	{
		var isValid = true;

		var canadaReg = /^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$/;

		$('.validator').css('display', 'none');

		if (zipcode.value != "" && zipcode.value != zipcode.getAttribute("default"))
		{
			if (!canadaReg.test(zipcode.value))
			{
				$('#validZip').css('display', 'block');
				isValid = false;
			}
			else
			{
				$('#validZip').css('display', 'none');
				isValid = true;
			}
		}
		else if ((city.value != "" && city.value != city.getAttribute("default"))
		&& (state.value != ""))
		{
			$('#validCityState').css('display', 'none');
			isValid = true;
		}
		else
		{
			$('#validCityState').css('display', 'block');
			isValid = false;
		}

		return isValid;
	} 
	catch (e)
	{
		alert(e)
	}

	return false;
}

//--------------------------------------------------------- AUTOCOMPLETE

var AutoComplete =
{
	LookupPath: "/includes/searchBarAutoCompleteHandler.ashx"
	, Options:
	{
		minChars: 3,
		max: 8,
		width: '408px',
		matchContains: true,
		highlightItem: false,
		scroll: false,
		multiple: false,
		selectFirst: true,
		matchSubset: false,
		css: { "position": "absolute" },
		formatItem: function (data, i, n, value, term) { return value.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + term.replace(/([\^\$\(\)\[\]\{\}\*\.\+\?\|\\])/gi, "\\$1") + ")(?![^<>]*>)(?![^&;]+;)", "gi"), "<strong>" + (this.control.value) + "</strong>"); },
		formatResult: function (data, value) { return value; }
	}
};

function initSearchAutocomplete(oControl, width)
{
	$(oControl).autocomplete();

	$(oControl).autocomplete(AutoComplete.LookupPath, AutoComplete.Options);
	$(oControl).result(sitewideSearchCallback);
	$(oControl).setOptions({
		width: width+'px'
	});

	$(oControl).keypress(function(e)
	{
		if (e.which == 13)
		{
			if ($('.ac_results').css('display') != 'block')
			{
				if (this.value == this.getAttribute("default")) { this.value = ""; }
				location.href = searchPath(this);
			}
			return false;
		}
	});
}

function sitewideSearchCallback(event, data, formatted) {}

function searchPath(oArg)
{
	var k = "";
	if (oArg) { k = oArg.value }
	return "/searchResults.aspx?k=" + escape(k);
}

function doSearch()
{
	var oTxt = document.getElementById('txtSearchKeywords');
	if (oTxt.value == oTxt.getAttribute("default")) { oTxt.value = ""; }
	window.location = searchPath(oTxt);
}

//---------------------------------------------------------  MODAL SETUP

function initModals()
{
	var options = { onHide: modalCleanup, overlay: 75 };

	$('#modalHMC').jqm($.extend({ ajax: '@href', trigger: '.jqModalHMC', onLoad: modalSetup }, options));

	$('#regLoginDialog').jqm($.extend({ trigger: 'a.jqModalRegLogin', onShow: modalSetup }, options));

	$('#modalWindow').jqm($.extend({ ajax: '@href', trigger: 'a.jqModal', onShow: function () { /*do nothing; stay hidden until onLoad().*/ }, onLoad: modalSetup }, options));
	$('#modalWindowSmall').jqm($.extend({ ajax: '@href', trigger: 'a.jqModalSmall', onShow: function () { /*do nothing*/ }, onLoad: modalSetup }, options));
	$('#modalWindowLarge').jqm($.extend({ ajax: '@href', trigger: 'a.jqModalLarge', onShow: function () { /*do nothing; stay hidden until onLoad().*/ }, onLoad: modalSetup }, options));
}

function launchAjaxModal()
{

}

//---------------------------------------------------------  MODAL FUNCTIONS

// * w: (jQuery object) The dialog element
// * c: (object) The config object (dialog's parameters)
// * o: (jQuery object) The overlay
// * t: (DOM object) The triggering element

var gModal = null;
var gOverlay = null;
var gCloseFunction = null;

var setModalStyle = function (modal) {
	if (modal.t) {
		if (ValueToInt(modal.t.getAttribute("width")) > 0) {
			modal.w.css(
		{
			"width": ValueToInt(modal.t.getAttribute("width")) + "px"
			, "min-width": ValueToInt(modal.t.getAttribute("width")) + "px"
		});

			if (ValueToInt(modal.t.getAttribute("height")) > 0) {
				modal.w.css(
		{
			"height": ValueToInt(modal.t.getAttribute("height")) + "px"
			, "min-height": ValueToInt(modal.t.getAttribute("height")) + "px"
		});
			}
		}
	}

	//delay processing the modal style until it's contents are loaded, as to better calculate 
	//the height and margin offset. 0.1 seconds aught to do it.
	setTimeout(
	function () {
		try {
			if (modal) {
				modal.w.show();

				if ($.browser.msie && (parseInt($.browser.version) < 7)) {
					modal.w.hide();
					// don't adjust the position styles
					modal.w.css('top', '50%');
					modal.w.css('margin-top', modal.w.height() + 'px');
					modal.w.css('margin-left', "-" + (modal.w.width() / 2) + 'px');
					modal.w.css('left', '50%');
					modal.w.show();
				}
				else {
					modal.w.css('position', 'fixed');
					modal.w.css('top', '50%');
					modal.w.css('margin-top', '-' + (modal.w.height() / 2) + 'px');
					modal.w.css('margin-left', "-" + (modal.w.width() / 2) + 'px');
					modal.w.css('left', '50%');
				}
			}
		} catch (e) { throwException('setModalStyle()', e.lineNumber, e, e.message); }
	}
	, 200);
}

function modalSetup(modal) {
	if (modal.t) {
		if (!Contains(modal.t.className, "disabled")) {
			gModal = modal;
			modal.w.hide();
			setModalStyle(modal);
		}
	} else {
		gModal = modal;
		modal.w.hide();
		setModalStyle(modal);
	}
}

function modalCleanup(modal)
{
	if (typeof (gCloseFunction) == 'function')
	{
		gCloseFunction();
		gCloseFunction = null;
	}

	modal.w.hide();
	modal.o.remove();
}

function hideModal() {return;  if (gModal) { gModal.css({ "visibility": "hidden" }); } }
function showModal() { if (gModal) { gModal.css({ "visibility": "visible" }); } }

