$(document).ready(function()
{
	// add classes
	var topNav = $('#top-nav');
	var topNavList = $('> ul:first', topNav);
	topNavList.addClass('sf-menu');

	var topNavListLinks = $('> li', topNavList);
	var lastIdx = topNavListLinks.length - 1;
	var itemIdx = 1;
	topNavListLinks.each(function(idx)
	{
		//alert(idx + ' == ' + lastIdx + ' = ' + (idx == lastIdx))
		var thisElm = $(this);
		$('> a:first', thisElm).wrapInner('<span></span>');
		switch (idx)
		{
			case (0):
				{
					thisElm.addClass('first');
					break;
				}
			case (lastIdx):
				{
					thisElm.addClass('last');
					// don't break, we want to fall through to the default
				}
			default:
				{
					thisElm.addClass('item' + itemIdx)
					.after('<li class="spacer' + itemIdx + '"><a href="#" onclick="return false;"><span></span></a><span class="hk1"></span></li>');
					itemIdx++;
					break;
				}
		}
		$('<span class="hk1"></span>').appendTo(thisElm);

		var ul = $('> ul', this);
		var lis = $('> li', ul);
		var lastIdx2 = lis.length;
		lis.each(function(idx2)
		{
			var thisElm2 = $(this);
			switch (idx2)
			{
				case (0):
					{
						thisElm2.addClass('first');
						break;
					}
				case (lastIdx2):
					{
						thisElm2.addClass('last');
						break;
					}
				default:
					{
						break;
					}
			}

			var link = $('> a:first', thisElm2);
			link.wrapInner('<span><b></b></span>');
			$('> span:first', link).append('<em>-</em>');
		});
		$('<li class="hk2"></li><li class="hk3"></li><li class="hk4"></li>').appendTo(ul);
	});

	// Set up nav
	var navProps = {
		autoArrows: false,
		dropShadows: false,
		delay: 0,
		speed: 'fast',
		animation: { toggle: 'show' },
		onBeforeShow: beforeShowNav,
		onShow: showNav,
		onHide: hideNav
	};
	//$('ul.sf-menu')
	topNavList
		.superfish(navProps)
		.find('ul').bgIframe({ opacity: true });

	topNavList.css({ 'overflow': 'visible', 'display': 'block' });

	// Show the state link
	$('#show-state-link').bind('click', showStates);
});

window.hidingLogo = false;

function beforeShowNav(evt)
{
	window.hidingLogo = true;
	_showNav(evt);
}
function showNav(evt)
{
	window.hidingLogo = false;
}
function _showNav(evt)
{
	var ids = { 'tnav_howitworks': 1 };
	var id = $('#top-nav > ul > li.' + $.fn.superfish.op.hoverClass + ' > a').attr('id');
	if (ids[id])
	{
		$('#main-logo').hide();
		$('#tnav_home').addClass('over-first');
	}
}
function hideNav(evt)
{
	if (window.hidingLogo) { return; }
	$('#main-logo').show();
	$('#tnav_home').removeClass('over-first');
}

function showStates()
{
	$('#show-state-container').css('display', 'block');
	$('#hide-state-container').css('display', 'none');
	return false;
}
function popWin(url)
{
	popWinCustom(url, '800', '600', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'PopWindow');
}
function popPDF(url)
{
	popPDFCustom(url, '800', '600', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'PopWindow');
}
function popWinName(url, name)
{

	//TODO: Reset width to 1000 per Purina request. DAK
	popWinCustom(url, '1000', '800', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', name);
}
function popPDFName(url, name)
{
	popPDFCustom(url, '800', '600', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', name);
}
function popWinCustom(url, width, height, toolbar, menubar, locationbar, scroll, resize, status, name)
{
	var spec;
	spec = 'status=' + status + ',toolbar=' + toolbar + ',menubar=' + menubar + ',location=' + locationbar + ',scrollbars=' + scroll + ',resizable=' + resize + ',height=' + height + ',width=' + width;
	remote = window.open(url, name, spec);
	remote.focus();
}
function popPDFCustom(url, width, height, toolbar, menubar, locationbar, scroll, resize, status, name)
{
	var spec;
	spec = 'status=' + status + ',toolbar=' + toolbar + ',menubar=' + menubar + ',location=' + locationbar + ',scrollbars=' + scroll + ',resizable=' + resize + ',height=' + height + ',width=' + width;
	remote = window.open(url, name, spec);
	remote.focus();
}
function replaceIllegalText(text)
{
	var cleanText;
	cleanText = text.replace(' ', '+');
	cleanText = cleanText.replace('"', '');
	cleanText = cleanText.replace('&', '+and+');
	cleanText = cleanText.replace('#', '');
	cleanText = cleanText.replace('\'', '');
	cleanText = cleanText.replace('|', '');
	cleanText = cleanText.replace('$', '');
	cleanText = cleanText.replace('%', '');
	cleanText = cleanText.replace('^', '');
	cleanText = cleanText.replace('*', '');
	cleanText = cleanText.replace(':', '');
	cleanText = cleanText.replace('!', '');
	cleanText = cleanText.replace('\\', '');
	cleanText = cleanText.replace('<', '');
	cleanText = cleanText.replace('>', '');
	cleanText = cleanText.replace('~', '');
	cleanText = cleanText.replace(';', '');
	return cleanText;
}

// footer search anim

var searchOffset = { top: '0px', left: '0px', width: '0px' };
function searchOpenClick(evt)
{
	evt.preventDefault();

	var elm = $(this);

	// Get the search element's current left position
	searchOffset.left = elm.offset().left + 'px';

	// Hide the search element
	elm.hide();

	// Hide the 800 phone number
	$('#foot-nav > li.last').hide();

	// calculate the poisition of the search element
	$('#search-form')
    .css({ display: 'block', top: searchOffset.top, left: searchOffset.left, width: searchOffset.width })
    .animate({ width: '340px' }, 100, 'swing');

	return false;
}
function searchCloseClick(evt)
{
	evt.preventDefault();
	$(this).parent().animate({ width: searchOffset.width }, 100, 'swing', searchCloseAnimComplete);
	return false;
}
function searchGoClick(evt)
{
	evt.preventDefault();

	// do go action here
	var inputElm = $(':text', $(this).parent());
	var form = inputElm.get(0).form;
	form.submit();
	//alert(inputElm.attr('value'));

	return false;
}

function searchCloseAnimComplete()
{
	// Hide the search form
	$(this).hide();

	// Show the original (search and 800 phone number) elements
	var footNavElm = $('#foot-nav');
	$("li.search", footNavElm).show();
	$("li.last", footNavElm).show();
}

function searchInit()
{

}
$(document).ready(function()
{
	// Initialize the search
	searchInit();
});


function showSurveyPopUp()
{
	
	var expires = new Date();
	expires.setMinutes(expires.getMinutes() + 20, expires.getSeconds(), expires.getMilliseconds());

	document.cookie = "hitCount=50; expires=" + expires.toGMTString();

	popWinCustom('/Survey.aspx', '300', '200', 'no', 'no', 'no', 'no', 'no', 'no', 'survey');
}


function showSurvey(event)
{

	/*var posx = 0;
	var posy = 0;
			
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) {
	posx = e.pageX;
	posy = e.pageY;
	}
	else if (e.clientX || e.clientY) {
	posx = e.clientX + document.body.scrollLeft
	+ document.documentElement.scrollLeft;
	posy = e.clientY + document.body.scrollTop
	+ document.documentElement.scrollTop;
	}
	alert(posx + ", " + posy);
	*/

	/*var e = (window.event) ? window.event : event;

	if (e.clientX < 0 || e.clientY < 0) {
	showSurveyPopUp();
	}*/
}

function getWindowSize()
{
	var size = new Object();

	if (typeof (window.innerWidth) == 'number')
	{
		//Non-IE
		size.Width = window.innerWidth;
		size.Height = window.innerHeight;
	} else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
	{
		//IE 6+ in 'standards compliant mode'
		size.Width = document.documentElement.clientWidth;
		size.Height = document.documentElement.clientHeight;
	} else if (document.body && (document.body.clientWidth || document.body.clientHeight))
	{
		//IE 4 compatible
		size.Width = document.body.clientWidth;
		size.Height = document.body.clientHeight;
	}

	if (typeof (window.pageYOffset) == 'number')
	{
		//Netscape compliant
		size.YOffset = window.pageYOffset;
		size.XOffset = window.pageXOffset;
	} else if (document.body && (document.body.scrollLeft || document.body.scrollTop))
	{
		//DOM compliant
		size.YOffset = document.body.scrollTop;
		size.XOffset = document.body.scrollLeft;
	} else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop))
	{
		//IE6 standards compliant mode
		size.YOffset = document.documentElement.scrollTop;
		size.XOffset = document.documentElement.scrollLeft;
	}


	return size;
}

function showSurveyLayer()
{
	var size = getWindowSize();
	var SurveyPopUp = document.getElementById("SurveyPopUp");
	SurveyPopUp.style.top = (((size.Height / 2) - 114) + ((size.YOffset) ? size.YOffset : 0)) + "px";
	SurveyPopUp.style.left = ((size.Width / 2) - 115) + "px";
	SurveyPopUp.style.display = "block";
}



function hideSurveyLayer()
{
	var SurveyPopUp = document.getElementById("SurveyPopUp");
	SurveyPopUp.style.display = "none";
	return false;
}




function wireUpLinks()
{


	var pos = document.cookie.indexOf("hitCount=");

	var currentHit = 1;

	if (pos != -1)
	{
		var start = pos + 9;
		var end = document.cookie.indexOf(";", start);
		if (end == -1) end = document.cookie.length;

		var value = document.cookie.substring(start, end);
		value = unescape(value);

		currentHit = value;
		currentHit++;
	}

	if (currentHit == 4)
	{
		showSurveyLayer();
	}
	else if (currentHit < 4)
	{
		var links = document.getElementsByTagName("a");
		var v = "";
		for (i = 0; i < links.length; i++)
		{
			if (links[i].hostname != location.hostname)
			{
				if (links[i].onclick)
				{
					links[i].onclick += function() { showSurveyPopUp() }
				}
				else
				{
					links[i].onclick = function() { showSurveyPopUp() }
				}
			}
		}
	}

	var expires = new Date();
	expires.setMinutes(expires.getMinutes() + 20, expires.getSeconds(), expires.getMilliseconds());

	document.cookie = "hitCount=" + currentHit + "; expires=" + expires.toGMTString();


}

function onlyNumbers(evt, promocode)
{
	var e = evt
	if (window.event)
	{ // IE
		var charCode = e.keyCode;
	} else if (e.which)
	{ // Safari 4, Firefox 3.0.4
		var charCode = e.which
	}
	if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;

	if (charCode == 13)
	{
		return submitQuoteForm(promocode);		
	}
		
	return true;
}
function submitQuoteForm(promocode)
{
	try
	{
		pet_type = document.getElementById("PetType").value;
	}
	catch (err)
	{
		pet_type = '';
	}
	zipcode = document.getElementById("Zipcode").value;
	form_url = 'https://protected.purinacare.com/qStep1.aspx?type=' + pet_type + '&zip=' + zipcode;
	if (promocode != null && promocode.length > 0)
		form_url += "&" + promocode;
	popWinName(form_url, 'PopQuote');
	return false;
}

function omTrack(key, value)
{
	try
	{
		key = value;
	}
	catch (err) { }
}

function omDownload(downloadName)
{
	try
	{
		var s = s_gi(s_account);
		s.linkTrackVars = 'eVar13,events,prop10,prop11,prop12,prop13';
		s.linkTrackEvents = 'event5';
		s.events = 'event5';
		s.eVar10 = downloadName;
		s.prop10 = s.eVar13;
		s.prop11 = s.pageName;
		s.prop12 = downloadName;
		s.prop13 = s.pageName;
		s.tl(this, 'd', downloadName);
	}
	catch (err) { }
}

function omToolUsage(tool)
{
	try
	{
		var s = s_gi(s_account);
		s.linkTrackVars = 'prop4,eVar4,events';
		s.linkTrackEvents = 'event4';
		s.prop4 = s.eVar4 = tool;
		s.events = 'event4';
		s.tl(this, 'o', tool);
	}
	catch (err) { }
}

function showCoverageLayer(ctype)
{
	var size = getWindowSize();
	var CoveragePopUp = document.getElementById("CoveragePopUp");
	var frame = document.getElementById('CoverageFrame');
	var framesrc = frame.src.split('?')[0];
	frame.src = framesrc + '?type=' + ctype;
	var top = $.browser.msie ? $.browser.version == '6.0' ? 500 : 420 : 460;
	var left = $.browser.msie ? $.browser.version == '6.0' ? 350 : 250 : 250;
	CoveragePopUp.style.top = (((size.Height / 2) - top) + ((size.YOffset) ? size.YOffset : 0)) + "px";
	CoveragePopUp.style.left = ((size.Width / 2) - left) + "px";
	CoveragePopUp.style.display = "block";
}

function hideCoverageLayer()
{
	var SurveyPopUp = document.getElementById("CoveragePopUp");
	SurveyPopUp.style.display = "none";
	return false;
}