if (navigator.userAgent.match(/iphone|ipod|android/i) && document.cookie.indexOf("notouch=1") == -1
	&& window.location.hostname == 'www.parkwhiz.com') { 
	window.location = 'http://touch.parkwhiz.com'+window.location.pathname+window.location.search;
}

var DAYS = Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
var DAYS_ABBR = Array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');

$(function(){
	$('input.date').each(function(){
		var $this = $(this);
		var opts = {};
		
		if ($this.hasClass('future')) {
			opts.minDate = new Date();
		}
		
		if ($this.hasClass('start')) {
			opts.onSelect = function(startDate) {
				var end = $(this).parents('.datepair').find('.end');
				startDate = new Date(startDate);
				if (end.datepicker('getDate') < new Date(startDate)) {
					end.datepicker('setDate', startDate);
				}
				
				return true;
			}
		} else if ($this.hasClass('end')) {
			opts.onSelect = function(endDate) {
				var start = $(this).parents('.datepair').find('.start');
				endDate = new Date(endDate);
				if (start.datepicker('getDate') > new Date(endDate)) {
					start.datepicker('setDate', endDate);
				}
				
				return true;
			}
		}
		
		$this.datepicker(opts);
	});
	
	$('input.textCopy').each(function() {
		$(this).bind('focus', function() {
			this.select();
		});
	});
	
	$('a.newWin').each(function() {
		$(this).bind('click', newWin);
		if (document.createElement) {
			var img = document.createElement('img');
			img.src = "/images/new-win-icon.gif";
			this.appendChild(img);
		}
	});
	
	$('input.focusField:first').focus();
	$('input[placeholder]').placeHeld();
});

function newWin(e)
{
	var event = new Event(e);
	var obj = event.target;
	var newwin = window.open(obj.href);
	if (newwin)
		event.stop();                               
};	


function isChild(el, parent)
{
	if (el == parent)
		return true;
	else if (el.parentNode == null)
		return false;
	else
		return isChild(el.parentNode, parent);
}

function getScrollXY() {
	var scrOfX = 0, scrOfY = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
	return [ scrOfX, scrOfY ];
};

function isNumeric(sText)
{
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;
	
	for (i = 0; i < sText.length && IsNumber == true; i++) { 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1)
			IsNumber = false;
	}
	return IsNumber;
};

function showHowItWorks(e)
{ 
	e.preventDefault();
	
	if ($('#howitworks').length) {
		return false;
	}
	
	$.get('/how.html', function(resp) {
		$('body').prepend(resp);
		var offsets = getScrollXY();
		$('#howitworks').css('top', offsets[1]+50+'px');
		jQuery('#howitworks a.close').bind('click', closeHowItWorks);
	
		$(document).bind('click', closeHowItWorks);
	})
	
	return false;
}
function closeHowItWorks(e)
{
	if (e.target.id == 'howitworks') {
		return true;
	}
	
	var extTarget = $(e.target)
	if (extTarget.hasClass('close') || extTarget.parents('#howitworks').length == 0) {	
		$('#howitworks').remove();
		$(document).unbind('click', closeHowItWorks);
		return false;
	} else {
		return true;
	}
}



function isValidEmail(str) {
	return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}

function removeChildrenFromNode(node)
{
	if(!node) {
		return;
	}
	
	var len = node.childNodes.length;
	
	while (node.hasChildNodes()) {
		node.removeChild(node.firstChild);
	}
};

function confirmSubmit()
{
	var agree=confirm("Are you sure you wish to do that? There is no undo.");
	if (agree)
		return true;
	else
		return false;
};

function makePhoneString(phone)
{
	return '('+phone.substr(0,3)+') '+phone.substr(3,3)+'-'+phone.substr(6,4);
};

function cloneObject(what)
{
	for (i in what) {
		if (what[i] == null)
			this[i] = null;
		else if (typeof what[i] == 'object')
			this[i] = new cloneObject(what[i]);
		else
			this[i] = what[i];
	}
};

function compareObjects(obj1, obj2)
{
	for (i in obj1) {
		if (obj1[i] != obj2[i])
			return false	
	}
	
	return true;
};

function getQueryVar(varName, qstring)
{
	if (!qstring)
		qstring = window.location.search.substring(1);
	
	if (qstring.lastIndexOf('?') > 0)
		qstring = qstring.split('?')[1];

	var vars = qstring.split("&");

	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if (pair[0] == varName) {
			return pair[1];
		}
	}
	return '';
}

(function($){
	$.PopupClose = function(containerId)
	{
		var container = jQuery('#'+containerId);
		container.hide();
	}
	
	$.fn.Popup = function(linkId, positionMe)
	{
		var openLink = jQuery('#'+linkId);
		var extendedThis = jQuery(this);

		function open(e)
		{
			if (jQuery('#'+extendedThis.attr('id')+':visible').length) {
				extendedThis.hide();
			} else {
				extendedThis.show();
			}
			
			if (positionMe) {
				extendedThis.css({
				'position': 'absolute',
				'top': e.pageY+'px',
				'left': e.pageX+'px',
				'z-index': 1000 });
			}
			
			return false;
		}
		
		function close(e)
		{
			if ($(e.target).parents().index(extendedThis) == -1 && jQuery('#'+extendedThis.attr('id')+':visible').length) {
				extendedThis.hide();
				return false;
			}
		}
		
		openLink.bind('click', open);
		jQuery(document).bind('click', close);
		
		extendedThis.hide();
		return this;
	}
})(jQuery);

(function($){
	$.fn.Flasher = function()
	{
		return this.each(function(){
			var extThis = $(this);
			var currentColor = extThis.css('backgroundColor');
			$(this).animate({backgroundColor: '#fff'}, 300).animate({backgroundColor: currentColor}, 300)
				.animate({backgroundColor: '#fff'}, 300).animate({backgroundColor: currentColor}, 300)
				.animate({backgroundColor: '#fff'}, 300).animate({backgroundColor: currentColor}, 300);
		});
	}
})(jQuery);

/* placeheld for jquery - http://github.com/makenosound/jquery-placeheld */ 
(function($){
	$.placeHeld = function(el, options){
		var base = this;
		base.$el = $(el);
		base.el = el;
		base.$el.data("placeHeld", base);
		base.placeholderText = base.$el.attr("placeholder");

		base.init = function(){
			base.options = $.extend({},$.placeHeld.defaultOptions, options);
			base.$el.bind('blur', base.holdPlace).bind('focus', base.releasePlace).trigger('blur');
			base.$el.parents('form').bind('submit', base.releasePlace);
		};
		// Hold with the default value attribute
		base.holdPlace = function() {
			var value = base.$el.val();
			if (!value) base.$el.val(base.placeholderText).addClass(base.options.className);
		};
		// Refill with the default value attribute
		base.releasePlace = function() {
			var value = base.$el.val();
			if (value == base.placeholderText) base.$el.val('').removeClass(base.options.className);
		};
		base.init();
	};

	$.placeHeld.defaultOptions = { className: "placeheld" };

	$.fn.placeHeld = function(options) {

		// Check for placeholder attribute support
		if (!!("placeholder" in $('<input>')[0])) return;
		
		return this.each(function() {
			(new $.placeHeld(this, options));
		});
	};
})(jQuery);

/* jQuery cookie plugin http://plugins.jquery.com/project/cookie */
jQuery.cookie=function(b,j,m){if(typeof j!="undefined"){m=m||{};if(j===null){j="";m.expires=-1}var e="";if(m.expires&&(typeof m.expires=="number"||m.expires.toUTCString)){var f;if(typeof m.expires=="number"){f=new Date();f.setTime(f.getTime()+(m.expires*24*60*60*1000))}else{f=m.expires}e="; expires="+f.toUTCString()}var l=m.path?"; path="+(m.path):"";var g=m.domain?"; domain="+(m.domain):"";var a=m.secure?"; secure":"";document.cookie=[b,"=",encodeURIComponent(j),e,l,g,a].join("")}else{var d=null;if(document.cookie&&document.cookie!=""){var k=document.cookie.split(";");for(var h=0;h<k.length;h++){var c=jQuery.trim(k[h]);if(c.substring(0,b.length+1)==(b+"=")){d=decodeURIComponent(c.substring(b.length+1));break}}}return d}};

// Simulates PHP's date function
Date.prototype.format=function(format){var returnStr='';var replace=Date.replaceChars;for(var i=0;i<format.length;i++){var curChar=format.charAt(i);if(replace[curChar]){returnStr+=replace[curChar].call(this);}else{returnStr+=curChar;}}return returnStr;};Date.replaceChars={shortMonths:['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],longMonths:['January','February','March','April','May','June','July','August','September','October','November','December'],shortDays:['Sun','Mon','Tue','Wed','Thu','Fri','Sat'],longDays:['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'],d:function(){return(this.getDate()<10?'0':'')+this.getDate();},D:function(){return Date.replaceChars.shortDays[this.getDay()];},j:function(){return this.getDate();},l:function(){return Date.replaceChars.longDays[this.getDay()];},N:function(){return this.getDay()+1;},S:function(){return(this.getDate()%10==1&&this.getDate()!=11?'st':(this.getDate()%10==2&&this.getDate()!=12?'nd':(this.getDate()%10==3&&this.getDate()!=13?'rd':'th')));},w:function(){return this.getDay();},z:function(){return"Not Yet Supported";},W:function(){return"Not Yet Supported";},F:function(){return Date.replaceChars.longMonths[this.getMonth()];},m:function(){return(this.getMonth()<9?'0':'')+(this.getMonth()+1);},M:function(){return Date.replaceChars.shortMonths[this.getMonth()];},n:function(){return this.getMonth()+1;},t:function(){return"Not Yet Supported";},L:function(){return(((this.getFullYear()%4==0)&&(this.getFullYear()%100!=0))||(this.getFullYear()%400==0))?'1':'0';},o:function(){return"Not Supported";},Y:function(){return this.getFullYear();},y:function(){return(''+this.getFullYear()).substr(2);},a:function(){return this.getHours()<12?'am':'pm';},A:function(){return this.getHours()<12?'AM':'PM';},B:function(){return"Not Yet Supported";},g:function(){return this.getHours()%12||12;},G:function(){return this.getHours();},h:function(){return((this.getHours()%12||12)<10?'0':'')+(this.getHours()%12||12);},H:function(){return(this.getHours()<10?'0':'')+this.getHours();},i:function(){return(this.getMinutes()<10?'0':'')+this.getMinutes();},s:function(){return(this.getSeconds()<10?'0':'')+this.getSeconds();},e:function(){return"Not Yet Supported";},I:function(){return"Not Supported";},O:function(){return(-this.getTimezoneOffset()<0?'-':'+')+(Math.abs(this.getTimezoneOffset()/60)<10?'0':'')+(Math.abs(this.getTimezoneOffset()/60))+'00';},P:function(){return(-this.getTimezoneOffset()<0?'-':'+')+(Math.abs(this.getTimezoneOffset()/60)<10?'0':'')+(Math.abs(this.getTimezoneOffset()/60))+':'+(Math.abs(this.getTimezoneOffset()%60)<10?'0':'')+(Math.abs(this.getTimezoneOffset()%60));},T:function(){var m=this.getMonth();this.setMonth(0);var result=this.toTimeString().replace(/^.+ \(?([^\)]+)\)?$/,'$1');this.setMonth(m);return result;},Z:function(){return-this.getTimezoneOffset()*60;},c:function(){return this.format("Y-m-d")+"T"+this.format("H:i:sP");},r:function(){return this.toString();},U:function(){return this.getTime()/1000;}};