// Vendomatic - JQuery Code
// Developed by KKMedia
// Development Director: Adam Duro

$(document).ready(function(){
	/* --------------------------------------- */
	/* --( Main Navigation )-- */
	/* --------------------------------------- */
	
	$("#main_nav").superfish({
		hoverClass	: "sfHover",
		currentClass: "overideThisToUse", /*new to v1.2a*/
		delay		: 500,
		animation1	: {height:"show"},
		speed1		: "slow",
		animation2	: {height:"hide"},
		speed2		: "normal"
	});
	
	/* --------------------------------------- */
	/* --( Customer Login Form )-- */
	/* --------------------------------------- */
	
	if ($("body").attr("id") == "customer_login") {
		// Place AJAX Loader Img
		$('<img src="_img/ajax-loader.gif" width="16" height="16" border="0" alt="Sending..." id="ajax-loader" />').hide().insertAfter('#submit');
		
		// Place AJAX Hidden feild
		$('<input type="hidden" name="ajax" id="ajax" value="ajax" />').insertBefore("#submit");
		
		// Place Confirmation Notice
		$('<p id="confirm"><small>Your Message Has Been Sent - Thank You!</small></p>').hide().prependTo('#customer_login_form');
		
		var options = { 
			//target:        '#output1',
			beforeSubmit:  showRequest, 
			success:       showResponse,
			resetForm:	   true
		};
	 
		// bind form using 'ajaxForm' 
		$('#customer_login_form').validate({
			rules: {
				name: "required",
				email: {
					required: true,
					email: true
				},
				phone: "required",
				address: "required",
				city: "required",
				state: "required",
				zip:"required"
			},
			messages: {
				name: "Your name is Required",
				email: "Please enter a valid email address",
				phone: "A phone number is required",
				address: "An address is required",
				city: "A city is required",
				state: "A state is required",
				zip: "A zipcode is required"
				
				
			},
			errorElement: "small",
			errorPlacement: function(error, element) {
				error.hide().prependTo( element.parent("p") ).slideDown();
			},
			submitHandler: function(form) {
				$(form).ajaxSubmit(options);	
			}
		});
	}
	function showRequest(formData, jqForm, options) {
		$("#ajax-loader").show();
		$("#confirm").slideUp();
	}
	function showResponse (responseText, statusText) {
		$("#ajax-loader").hide();
		$("#confirm").slideDown();
	}
	
	
	
	/* --------------------------------------- */
	/* --( Contact Form )-- */
	/* --------------------------------------- */
	
	if ($("body").attr("id") == "contact") {
		// Place AJAX Loader Img
		$('<img src="_img/ajax-loader.gif" width="16" height="16" border="0" alt="Sending..." id="ajax-loader" />').hide().insertAfter('#submit');
		
		// Place AJAX Hidden feild
		$('<input type="hidden" name="ajax" id="ajax" value="ajax" />').insertBefore("#submit");
		
		// Place Confirmation Notice
		$('<p id="confirm"><small>Your Message Has Been Sent - Thank You!</small></p>').hide().prependTo('#contact_form');
		
		var options = { 
			//target:        '#output1',
			beforeSubmit:  showRequest, 
			success:       showResponse,
			resetForm:	   true
		};
	 
		// bind form using 'ajaxForm' 
		$('#contact_form').validate({
			rules: {
				name: "required",
				email: {
					required: true,
					email: true
				},
				message: "required"
			},
			messages: {
				name: "Your name is Required",
				email: "Please enter a valid email address",
				message: "A message is required"
			},
			errorElement: "small",
			errorPlacement: function(error, element) {
				error.hide().prependTo( element.parent("p") ).slideDown();
			},
			submitHandler: function(form) {
				$(form).ajaxSubmit(options);	
			}
		});
	}
	function showRequest(formData, jqForm, options) {
		$("#ajax-loader").show();
		$("#confirm").slideUp();
	}
	function showResponse (responseText, statusText) {
		$("#ajax-loader").hide();
		$("#confirm").slideDown();
	}
	
	/* --------------------------------------- */
	/* --( Beverages/Snacks Navigation )-- */
	/* --------------------------------------- */
	
	if ($("body").attr("class") == "bs"){
		
		// Initialize history plugin.
		// The callback is called at once by present location.hash. 
		$.historyInit(pageload);
		
		// Begin Sub Navigation Mods
		$(".bs #sub_nav")
			.addClass("js")
			.find("ul")
				.wrap('<div class="wrap"></div>')
		.end()
			.find("div.wrap")
				.prepend('<span class="bar"></span>')
				.hover(nullFunc,subNavOut)
			.find("a")
				// Setup Page Request
				.attr("href", function() {
					split_hash = $(this).attr("href").split("?"); // Split up query string
					set_hash = split_hash[0] + "#" + split_hash[1]; // Create hash out of query string
					return set_hash;
				})
				.hoverIntent({
					sensitivity: 1,
					interval: 100,
					over: subNavOver,
					timeout: 100,
					out: nullFunc			 
				})
				.click(function(){
					$(this).parent().add("#sub_nav li.selected").toggleClass("selected");
							
					var hash = $(this).attr("href");
					hash = hash.replace(/^.*#/, '');
					$.historyLoad(hash);
					
					return false;
				})
		.end()
			.find("li.selected a")
				.each(function() {
					startPos = $(this).position();			   
				})
		;
		$(".bs #sub_nav span.bar").css("top",(startPos.top+2)+"px");
	}
									
});

/* --------------------------------------- */
/* --( Callbacks )-- */
/* --------------------------------------- */

function subNavOver() {
	elemPos = $(this).position();
	$(".bs #sub_nav.js span.bar").animate(
	{
		style: 'top: ' + (elemPos.top+2) + 'px;'	
	}
	, 500);
}
function subNavOut() {
	startPos = $("#sub_nav li.selected").position();
	$(".bs #sub_nav.js span.bar").animate(
	{
		style: 'top: '+(startPos.top+2)+'px;'	
	}
	, 500);	
}
function nullFunc() {
	null;
}

/* ------------------------------------------ */
/* --( Ajax Page Load w/ History Recall )-- */
/* ------------------------------------------ */

function pageload(hash,query) {
	if(hash) {
		var query = hash.split("&"); // Splits the query string
		cat = query[1].split("="); // Grabs the sub caregory from query string
		$.ajax({
			url: "_inc/bs.php",
			data: hash + "&ajax=true",
			success: function(html){

				// Set Page Title 
				// Grab title from link with a TITLE attribute that matches the sub-category
				var title = $("#sub_nav li." + cat[1] + " a").attr("title") + ' - Vendomatic';
				document.title = title;
				
				// Set Navigation Selected
				$("#sub_nav li." + cat[1]).add("#sub_nav li.selected").toggleClass("selected");
				subNavOut();
				
				// Insert AJAX Returned HTML
				$("#display").empty().prepend(html);
				
			},
			complete: function(){
				
			}
		});
	} else {
		var cloned = $("#holder dl").length;
		if (cloned > 0) { 
			// Code to execute when landing page is hit using the back button
			document.title = og_title;
			page_id = $("body").attr("id");
			$("#display").empty();
			$("#holder dl").clone().prependTo("#display");
			$("#sub_nav li." + page_id).add("#sub_nav li.selected").toggleClass("selected");
			subNavOut();
		} else {
			// Code which grabs landing page state and stores it for the history
			og_title = document.title;
			$("body")
				.append('<div id="holder"></div>')
				.find("#display dl").clone().prependTo("#holder")
			;
		}
	}
}