$(function(){
	$("#map").googleMap({address:"1491 Brace Road Cherry Hill, NJ 08034", zoom:16});
	$("#photo.home").cycleItem({navigation:"#rotatenav a", limit:$("#photo.home span").length, onCycle:rotatePhotos});
	
	// AJAX forms.
	ajaxify("#form_contact");
	ajaxify("#form_parties");
	ajaxify("#form_newsletter");
	//displayErrors();
});

/**
 * Turns forms with special "ajax" class into AJAX forms.
 * Note: Requires compatible PHP code on server-side in order to
 * receive and properly display HTML based on passed "ajax" variable.
 * NOTE: Made some modifications to use with two forms on the same page
 */
var ajaxconfig = {};
function ajaxify(selector) {
	var form_selector = "form.ajax";
	if (selector != null) form_selector = selector;
	
	$(form_selector).submit(function(){
		// Get form properties.
		action = $(this).attr("action");
		if (action.indexOf("#") != -1) {
			// Parse out trailing hash.
			action = action.substring(0, action.indexOf("#"))
		}
		method = $(this).attr("method").toUpperCase();
		if (method != "GET" && method != "POST") method = "POST";
		formid = $(this).attr("id");
		if (formid == "") {
			// Dynamically assign ID.
			formid = "form" + parseInt(Math.random() * 9999);
			$(this).attr("id", formid);
		}
		
		// Add "ajax" field to form and serialize data.
		$(this).append("<input type=\"hidden\" name=\"ajax\" value=\"1\" />");
		data = $(this).serialize();
		
		// Disable form fields to show it is processing.
		$("#" + formid + " input").attr("disabled", "disabled");
		$("#" + formid + " textarea").attr("disabled", "disabled");
		
		// Configure AJAX call.
		ajaxconfig = {
			type: method,
			url: action,
			data: data,
			success: function(data, textStatus, XMLHttpRequest) {
				// Update form contents and animate error messages, if applicable.
				$("#" + this.formid).html(data);// alert(data);
				displayErrors(form_selector);
			},
			formid: formid
		};
		
		// Give error message time to scroll up before submitting AJAX call.
		if ($(formid + " .errmsg").length > 0) {
			$(formid + " .errmsg").slideUp(200, function() {
				ajaxify_submit();
			});
		} else {
			ajaxify_submit();
		}
		
		// Prevent form from submitting.
		return false;
	});
}

// AJAX submit callback.
function ajaxify_submit() {
	$.ajax(ajaxconfig);
}


/**
 * Animates error messages.
 */
function displayErrors(selector) {
	var form_selector = ".errmsg";
	if (selector != null) form_selector = selector + " " + form_selector;
	
	$(form_selector).css({
		"display": "none"
	});
	$(form_selector).slideDown(500);
}

function rotatePhotos(data){
	var elements = $("#photo.home").find("span");
	elements.eq(data.current).fadeOut("fast");
	elements.eq(data.next).fadeIn("fast");
}

/**
* jQuery Google Map Helper
* Copyright (c) 2011 Kevin Doyle
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
**/

(function(c){c.fn.googleMap=function(a){var e={address:!1,LatLng:[0,0],zoom:8,icon:!1,alt:!1,mapType:"roadmap",onGeocodeError:function(){}},a=c.extend(e,a);return this.each(function(){var f=c(this)[0],g=new google.maps.LatLng(a.LatLng[0],a.LatLng[1]),b;switch(a.mapType){case "roadmap":b=google.maps.MapTypeId.ROADMAP;break;case "satellite":b=google.maps.MapTypeId.SATELLITE;break;case "hybrid":b=google.maps.MapTypeId.HYBRID;break;case "terrain":b=google.maps.MapTypeId.TERRAIN}var d=new google.maps.Map(f, {zoom:a.zoom,center:g,mapTypeId:b});a.address&&(new google.maps.Geocoder).geocode({address:a.address},function(b,c){c==google.maps.GeocoderStatus.OK?(d.setCenter(b[0].geometry.location),a.icon?new google.maps.Marker({map:d,position:b[0].geometry.location,icon:a.icon}):new google.maps.Marker({map:d,position:b[0].geometry.location})):c==google.maps.GeocoderStatus.ZERO_RESULTS&&e.onGeocodeError.call(this)})})}})(jQuery);

/**
* jQuery Cycle Item
* Copyright (c) 2011 Kevin Doyle
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
**/

(function(e){var a={delay:1E4,timeoutDelay:5E3,section:".cycleitem",initialIndex:0,navigation:!1,limit:null,onCycle:function(){}},j,b,c,g,m,h,k,n,l,i,d={init:function(f){f&&e.extend(a,f);return this.each(function(){j=e(this);if(!a.limit)a.limit=j.find(a.section).length;g=m=!1;i=!0;b=-1;c=0;a.limit>1&&d._createTimer();a.navigation&&e(a.navigation).bind("click",d._forceCycle)})},cycle:function(){a.navigation&&(e(a.navigation+".current").removeClass("current"),e(a.navigation).eq(c).addClass("current")); a.onCycle.call(j,{current:b,next:c});g&&(g=!1,h=!0)},resetCycle:function(){b=a.initialIndex-1;c=0},disableNavigation:function(){i=!1},enableNavigation:function(){i=!0},_controlCurrent:function(f){h?(b=c,c=b+1,b===a.limit-1&&(c=0),h=!1):b===a.limit-1&&f==="up"?(b=0,c=1):(b+=1,f==="up"&&b===0?c=1:f==="up"&&b===a.limit-1?c=0:f==="up"&&b>0&&b<a.limit-1&&(c=b+1))},_createTimer:function(){rotate=setInterval(d._autoCycle,a.delay);g&&(g=!1,h=!0)},_autoCycle:function(){d._controlCurrent("up");d.cycle()},_forceCycle:function(f){!g&& i&&(d._delayTimer(),n=e(f.currentTarget),l=e(a.navigation).index(n),c!==l&&(g=!0,b=c,c=l,d.cycle()));return!1},_stopTimer:function(){clearInterval(rotate);typeof k!=="undefined"&&clearTimeout(k)},_delayTimer:function(){d._stopTimer();m||(k=setTimeout(d._createTimer,a.timeoutDelay))}};e.fn.cycleItem=function(a){if(d[a])return d[a].apply(this,Array.prototype.slice.call(arguments,1));else if(typeof a==="object"||!a)return d.init.apply(this,arguments);else e.error("Method "+a+" does not exist on cycleItem")}})(jQuery);
