﻿
/* -----------------------------  Modal Repositioning  -----------------------------------------*/

var MODAL_MIN_FROM_TOP = 35;
function PositionModal(argModalId)
{
	if ($(window).height() < document.getElementById(argModalId).clientHeight)
	{
		document.getElementById('overlayContentDiv').style.height = ($(window).height() - 50) + "px";
		document.getElementById('overlayContentDiv').style.overflow = "auto";
	}

	newPosition = Math.round(($(window).height() - document.getElementById(argModalId).clientHeight) / 2);

	if (newPosition < MODAL_MIN_FROM_TOP)
	{
		newPosition = MODAL_MIN_FROM_TOP;
	}

	
	if ($.browser.msie && (parseInt($.browser.version) < 7)) { }
	else
	{
		document.getElementById(argModalId).style.top = newPosition + "px"; 
	}
}
function FindPos(obj)
{
	var curleft = curtop = 0;
	if (obj.offsetParent)
	{
		do
		{
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
		return [curleft, curtop];
	}
	else
	{
		return [obj.offsetLeft, obj.offsetTop];
	}
}

/* -----------------------------  Image Swap function ----------------------------------------- 
* assumes that an image is inside of a link for example
* <a href=""><img src="someimage.gif" /></a>
* 
* DOES NOT WORK FOR INPUTS USE swapImagesInput
*/

function swapImages(btnName,imgName,imgType){
	var imgNorm = "/assets/images/" + imgName + "."+imgType;
	var imgOver = "/assets/images/" + imgName + "_over." + imgType; 
	
	
	$(btnName).hover(
			function() { $(this).find('img').attr({src:imgOver});},
			function() { $(this).find('img').attr({src:imgNorm});});
}

/* -----------------------------  Image Swap for Inputs function ----------------------------------------- */

function swapImagesInput(btnName,imgName,imgType){
	var imgNorm = "/assets/images/" + imgName + "."+imgType;
	var imgOver = "/assets/images/" + imgName + "_over." + imgType;
	
	$(btnName).hover(
			function() { $(this).attr({src:imgOver});},
			function() { $(this).attr({src:imgNorm});});
}


/* -----------------------------  document.ready (i.e. onload) ----------------------------------------- */
$(document).ready(function()
{
	$('a.roll').each(function(e)
	{
		var imgName = $(this).find('img').attr('src');
		imgName = imgName.replace('/assets/images/', '');

		var imgType = imgName.substr(imgName.lastIndexOf('.') + 1, 3);
		imgName = imgName.replace('.' + imgType, '');

		swapImages($(this), imgName, imgType);
	});

	$('input.roll').each(function(e)
	{
		var imgName = $(this).attr('src'); // note, this assumes it is an ImageButton
		imgName = imgName.replace('/assets/images/', '');

		var imgType = imgName.substr(imgName.lastIndexOf('.') + 1, 3);
		imgName = imgName.replace('.' + imgType, '');

		swapImagesInput($(this), imgName, imgType);
	});


	/*** nav dropdown ***/
	$('.mainNav li').hover(
		function()
		{
			if ($.browser.msie && (parseInt($.browser.version) < 7))
			{
				$('div.dropdownOuter', this).css('display', 'block');
				if (this.className.indexOf("ddDealer") != -1)
				{
					displayPreferredMapTop();
				}
			}
			else
			{
				$('div.dropdownOuter', this).fadeIn
				(
					'fast'
					,function()
					{
						if (this.className.indexOf("ddDealer") != -1)
						{
							displayPreferredMapTop();
						}
					}
				);
			}
		},
		function()
		{
			$('div.dropdownOuter', this).css('display', 'none');
		});

})




// JavaScript Document

// American Standard UI Javascript

$(document).ready(function ()
{


	// ---------------------  Setup Menus for IE 6 -------------------

	// main nav hovers

	if ($.browser.msie && (parseInt($.browser.version) < 7))
	{
		$('.dropdownouter').bgiframe();
		$('#myWishListOpen').bgiframe();
		$('#myOwnedListOpen').bgiframe();
		$('#myPreferredOpen').bgiframe();
		$.preloadCssImages();
	}

	$('#menuBottom ul li').hover(
			function ()
			{
				$('div.dropdownouter', this).css('display', 'block');
			},
			function ()
			{
				$('div.dropdownouter', this).css('display', 'none');
			}
		);

	$('#searchArea').hide();

	$('#searchBtn').click(function ()
	{

		if ($(".largeSearchBtn").attr("src") == "/assets/images/searchBtn_over.gif")
		{
			$(".largeSearchBtn").attr("src", "/assets/images/searchBtn.gif");
			$("#searchArea").fadeOut("fast");
		}
		else
		{
			$("#searchArea").fadeIn("fast");
			$(".largeSearchBtn").attr("src", "/assets/images/searchBtn_over.gif");
		}

	});

	$('#searchClose').click(function ()
	{
		$(".largeSearchBtn").attr("src", "/assets/images/searchBtn.gif");
		$("#searchArea").fadeOut("fast");
		return false;
	});

	// -------------- change search boxes on focus ------------//
	$('input.searchWordsText').focus(
		    function ()
		    {
		    	var curr = $(this).val();
		    	if (curr == 'by keyword or prod#')
		    	{
		    		$(this).val('');
		    	}
		    }
		);

	$('input.searchWordsText').blur(
		    function ()
		    {
		    	var curr = $(this).val();
		    	if (curr == '')
		    	{
		    		$(this).val('by keyword or prod#');
		    	}
		    }
		);



	// -------------- change register boxes on focus ------------//
	$('input.regFirst').focus(
		    function ()
		    {
		    	var curr = $(this).val();
		    	if (curr == 'first name')
		    	{
		    		$(this).val('');
		    	}
		    }
		);

	$('input.regFirst').blur(
		    function ()
		    {
		    	var curr = $(this).val();
		    	if (curr == '')
		    	{
		    		$(this).val('first name');
		    	}
		    }
		);

	$('input.regLast').focus(
		    function ()
		    {
		    	var curr = $(this).val();
		    	if (curr == 'last name')
		    	{
		    		$(this).val('');
		    	}
		    }
		);

	$('input.regLast').blur(
		    function ()
		    {
		    	var curr = $(this).val();
		    	if (curr == '')
		    	{
		    		$(this).val('last name');
		    	}
		    }
		);

	$('input.regEml').focus(
		    function ()
		    {
		    	var curr = $(this).val();
		    	if (curr == 'email address')
		    	{
		    		$(this).val('');
		    	}
		    }
		);

	$('input.regEml').blur(
		    function ()
		    {
		    	var curr = $(this).val();
		    	if (curr == '')
		    	{
		    		$(this).val('email address');
		    	}
		    }
		);

	$('input.regZip').focus(
		    function ()
		    {
		    	var curr = $(this).val();
		    	if (curr == 'zip code')
		    	{
		    		$(this).val('');
		    	}
		    }
		);

	$('input.regZip').blur(
		    function ()
		    {
		    	var curr = $(this).val();
		    	if (curr == '')
		    	{
		    		$(this).val('zip code');
		    	}
		    }
		);

	$('.loginEmailInput').focus(
		    function ()
		    {
		    	if ($(this).val() == "email address")
		    	{
		    		$(this).val('');
		    	}
		    }
		);

	$('.loginEmailInput').blur(
		    function ()
		    {
		    	if ($(this).val() == "")
		    	{
		    		$(this).val('email address');
		    	}
		    }
		);

	$('.loginPasswordText').css('display', 'block');
	$('.loginPasswordInput').css('display', 'none');

	$('.loginPasswordText').focus(
		    function ()
		    {
		    	if ($(this).val() == "password")
		    	{
		    		$(this).hide();
		    		$('.loginPasswordInput').show();
		    		$('.loginPasswordInput').val('');
		    		$('.loginPasswordInput').focus(function () { $('.loginPasswordText').val(''); });
		    	}
		    }
		);

	$('.loginPasswordInput').blur(
		    function ()
		    {
		    	if ($(this).val() == "")
		    	{
		    		$(this).hide();
		    		$('.loginPasswordText').val('password');
		    		$('.loginPasswordText').show();
		    	}
		    }
		);

	// wishlist
	if ($('#regUsers ul li#myWishListBtn'))
	{
		$('#regUsers ul li#myWishListBtn').hover(
	            function () { $('div#myWishListOpen', this).css('display', 'block'); },
			    function () { $('div#myWishListOpen', this).css('display', 'none'); });
	}

	// my owned list
	if ($('#regUsers ul li#myOwnedBtn'))
	{
		$('#regUsers ul li#myOwnedBtn').hover(
	            function () { $('div#myOwnedListOpen', this).css('display', 'block'); },
			    function () { $('div#myOwnedListOpen', this).css('display', 'none'); });
	}
	// my preferred dealers list

	if ($('#regUsers ul li#myPreferredBtn'))
	{
		$('#regUsers ul li#myPreferredBtn').hover(
	            function ()
	            {
	            	$('div#myPreferredOpen', this).css('left', '-498px');
	            },
			    function ()
			    {
			    	$('div#myPreferredOpen', this).css('left', '-5000px');
			    });
	}

	// for product detail feature icons
	$('ul li.prodFeatureItem').hover(
			function () { $('div.featureHoverOuter', this).css('display', 'block'); },
			function () { $('div.featureHoverOuter', this).css('display', 'none'); });


	// for refine search keyword and price fields
	$('#searchResultsRefineArea #refineSearchPriceLow').focus(
		    function ()
		    {
		    	if ($(this).val() == "Low price range")
		    	{
		    		$('#searchResultsRefineArea #refineSearchPriceLow').val('');
		    	}
		    }
		);

	$('#searchResultsRefineArea #refineSearchPriceLow').blur(
		    function ()
		    {
		    	if ($(this).val() == "")
		    	{
		    		$('#searchResultsRefineArea #refineSearchPriceLow').val('Low price range');
		    	}
		    }
		);

	$('#searchResultsRefineArea #refineSearchPriceHigh').focus(
		    function ()
		    {
		    	if ($(this).val() == "High price range")
		    	{
		    		$('#searchResultsRefineArea #refineSearchPriceHigh').val('');
		    	}
		    }
		);

	$('#searchResultsRefineArea #refineSearchPriceHigh').blur(
		    function ()
		    {
		    	if ($(this).val() == "")
		    	{
		    		$('#searchResultsRefineArea #refineSearchPriceHigh').val('High price range');
		    	}
		    }
		);

	$('#searchResultsRefineArea #refineSearchKeyword').focus(
		    function ()
		    {
		    	if ($(this).val() == "Keyword")
		    	{
		    		$('#searchResultsRefineArea #refineSearchKeyword').val('');
		    	}
		    }
		);

	$('#searchResultsRefineArea #refineSearchKeyword').blur(
		    function ()
		    {
		    	if ($(this).val() == "")
		    	{
		    		$('#searchResultsRefineArea #refineSearchKeyword').val('Keyword');
		    	}
		    }
		);

	// -------------- for search results in IE6 ------------//

	if ($.browser.msie && (parseInt($.browser.version) < 7))
	{
		$("img.wishListMenuShadeImg").css('display', 'block');
		$("img.wishListMenuShadeImg").attr("src", "/assets/images/x.gif");
	}


});


window.onresize = function ()
{
	if ($('#modalWindow'))
	{
		if ($(window).height() < document.getElementById('modalWindow').clientHeight)
		{
			document.getElementById('overlayContentDiv').style.height = ($(window).height() - 50) + "px";
			document.getElementById('overlayContentDiv').style.overflow = "auto";
		}

		newPosition = Math.round(($(window).height() - document.getElementById('modalWindow').clientHeight) / 2);

		if ($.browser.msie && (parseInt($.browser.version) < 7))
		{
			// fix for ie 6?
		} else
		{
			document.getElementById('modalWindow').style.top = newPosition + "px";
		}
	}

	if ($('#modalWindowSmall'))
	{
		if ($(window).height() < document.getElementById('modalWindowSmall').clientHeight)
		{
			document.getElementById('overlayContentDiv').style.height = ($(window).height() - 50) + "px";
			document.getElementById('overlayContentDiv').style.overflow = "auto";
		}

		newPosition = Math.round(($(window).height() - document.getElementById('modalWindowSmall').clientHeight) / 2);

		if ($.browser.msie && (parseInt($.browser.version) < 7))
		{
			// fix for ie 6?
		} else
		{
			document.getElementById('modalWindowSmall').style.top = newPosition + "px";
		}
	}
}

function openSearchAreaFromLink()
{
	if ($(".largeSearchBtn").attr("src") != "/assets/images/searchBtn_over.gif")
	{
		$("#searchArea").fadeIn("fast");
		$(".largeSearchBtn").attr("src", "/assets/images/searchBtn_over.gif");
		$("#searchForm .searchWordsText").focus();
	}
};

function show(argElementId)
{
	try
	{
		document.getElementById(argElementId).style.display = 'block';
	}
	catch (e)
	{
		//alert("argElementId: " + argElementId + "\n\n" + e.message)
	}
}

function hide(argElementId)
{
	try
	{
		document.getElementById(argElementId).style.display = 'none';
	}
	catch (e)
	{
		//alert("argElementId: " + argElementId + "\n\n" + e.message)
	}
}

function toggle(argElementId)
{
	obj = document.getElementById(argElementId);

	if (obj.style.display == 'block')
	{
		hide(argElementId);
	} else
	{
		show(argElementId);
	}
}
