/* ===============
	
	Yodle Local Behavior & UI script Library

==================*/

/*===	Customizable Elements. Change Copy & etc. ===*/
//Message Submissions via Modal
var submitMessageF = "Message Send Failed.";
var submitMessageS = "Your Message was Sent!"; 
//Look inside the validation function to get more copy.

//Crowd Blurbing for sponsored items add more comments[] if desired.
var ctr = 0;	
comment = new Array();		
comment[0] = "Taking New Customers!";
comment[1] = "Cool! Call these guys!";
comment[2] = "These guys can help!";

/*===	Intiation Script for a Listings Page.	===*/
function initListing() {
	
	//initCrowdBlurb(); Disabled Crowd blurb
	initModal();
	
	//listing behavior on hovers	


	//ExtraLink Show phone behavior		
	$('.phone').click(function(){  
		$(this).parent().parent().find(".tel").toggle("fast");
	}); 			
}

//Deprecate this Possibly...
var numOfNav,navLength,currentNav;
function initNavigation() {

  //Relys on jscrollPane.js and makes sub contents in .scroller in the nav scrollable items.
  $('.scroller').jScrollPane({showArrows:true});		
  	
	$(".navList").hide();
	numOfNav = $(".navList");
  navLength = numOfNav.length;
  
  //show first 1 and select first.
  $(numOfNav[0]).show();
  currentNav = 0; 	
  
  //Toggle Filter Button
	$("#filterToggle").click(function() { 			
		$("#filterContent").slideToggle();
		$("#filterToggle").toggleClass("minimize");			
	});//end of filtertoggling. 
}

function navSwitch(num){
	if (num < navLength && num != currentNav){ 		
		$(numOfNav[currentNav]).hide();
 		$(numOfNav[num]).fadeToggle(function(){
 		 		currentNav = num; 		
 		});	
 	}
}

/* Navigation Hide Toggler */
function togglingFilter(domElement) {
	var filterList = $(domElement).parent().parent();
	$(filterList).children(".hide").fadeToggle();	
	$(domElement).parent().toggleClass("expand");	

	$(domElement).toggleText("Shorten this List","View All");
}

/* ===============
	Modal Library
 	 Opening a Contact Bubble: contact("Business Name Inc.", 1231);
==================*/
function initModal() {		 	 
	$('#hideModal').click(function(){ $("#modal").fadeOut();});	
	$(window).scroll(function() {  $('#modal').css('top', $(window).scrollTop() + "px");	});
}
	//Show Modal Command.
	function openModal(){
		$("#modal").fadeIn();
	}
	//Close Modal Commmand.
	function closeModal(){
		$("#modal").fadeOut( function(){
			$("#msgFlash").html(""); //erase message
			$("#msgFlash").hide();
			$("#modal").removeClass("fail"); //reset status
			$("#modal").removeClass("success");			
		});
	}	
	//Opening a Contact Modal. Changes value in the forms if necessary.
	function contact(businessName, listingId){
		openModal();
		$("#modalTitle span").html(businessName);
		$('#listingId').val(listingId);
	}
	//closing a modal with a contact form
	function closeModalForm() {
		$("#modal").fadeOut(function(){
			$("#msgFlash").html("");
			$("#modal").removeClass("success");			
			$("#modal form").show();
			$("#msgFlash").hide();		
		});		
	}
	//Used heavily with validation script
	function alertModal(alertMsg,msgType){
		//msgType is boolean, true for success, false for fail.
		
		var displayStatus = $("#modal").css("display");
		if (displayStatus == "none"){
			if (!(msgType))
				$("#modal").addClass("fail");	
			$("#modal").fadeIn();
		}
		
		
		//true for a success or false for error
		$("#msgFlash").hide();
		$("#msgFlash").html(alertMsg);
		
		if (msgType){
			$("#modal").addClass("success");			
			$("#msgFlash").fadeIn();	
			setTimeout("closeModalForm()",2000);	
		} else {
			$("#msgFlash").fadeIn();
		}					

	}

/*=== 
	Validation on Yodle Local's form. LISTING PAGE's Modal & YLPROFILES
		Pre-requisite: same Ids and only one form.
		Validiates Name, Email & Phone.	
		Returns an error message.
===*/		
function validateForm(){
	
	var errorMsg = "";
	
	//check for phone #.	
	var phone =	$('#fieldPhone').val();	
	var email = $('#fieldEmail').val();	

	
	if (phone == null || phone == ""){
		errorMsg = "I need your <strong>PHONE NUMBER</strong>, please.";
	} else if (phone != null) {
		phone.replace(/\D/g, "");
		 if (phone.length < 10){
			errorMsg = "A <strong>PHONE NUMBER</strong> needs to be<br/> at least <strong>10 Digits</strong>. <br/> Example:(888)555 - 1234";
		} else if (email == null || email == ""){ 
			errorMsg = "Need an <strong>EMAIL</strong> just in case.";
		} else if (email.length < 6) {
			errorMsg = "Wait, <strong>" + email + "</strong> is way too short to be a <br/> <strong>VALID EMAIL</strong>."
		} else {
			var apos = email.indexOf("@");
			var	dotpos = email.lastIndexOf(".");
			if (apos<1||dotpos-apos<2)
				errorMsg = "Hold on! <strong>" + email + "</strong> doesn't look like a<br/> <strong>VALID EMAIL</strong>."		
		}
	}
	
	if (errorMsg.length > 0) {
		alertModal(errorMsg,false);
		return;
	}
	
	//Submit AJAX POST HERE		
	alertModal(submitMessageS,true);		
}

function loadDirectionsTab() {
		var lastTabIndex = $('#tabNav li').length - 1;
		if (lastTabIndex > 0) {
			$('#tabs').tabs('select',lastTabIndex)
		}
}

//User Experience effect to create an animated skyfield.
function animateSkybox(){		
		$("#wrapper").animate({backgroundPosition: '(0 0)'},{queue: false, duration: 5000});
}

//Add a crowd bubble flasher to the sponsored Holder.
function initCrowdBlurb() {						
	$("#sponsoredResults").append("<div id='sponsorIco' class='clearfix'><span id='siblurb'></span></div>");
	rotateCrowdBlurb();
	setInterval(rotateCrowdBlurb, 5000);
}

	function rotateCrowdBlurb(){	
		ctr++;
		if(ctr >= comment.length)
			ctr = 0;
		$("#siblurb").fadeOut( function(){
			$("#siblurb").fadeIn();	
			$("#siblurb").html(comment[ctr]);		
		});
	}

//Equal height for 2 elements	
jQuery.fn.vjustify=function() {
    var maxHeight=0;
    this.each(function(){
        if (this.offsetHeight>maxHeight) {maxHeight=this.offsetHeight;}
    });
    this.each(function(){
        $(this).height(maxHeight + "px");
        if (this.offsetHeight>maxHeight) {
            $(this).height((maxHeight-(this.offsetHeight-maxHeight))+"px");
        }
    });
};

//Fade Toggler
jQuery.fn.fadeToggle = function(speed, easing, callback) {
   return this.animate({opacity: 'toggle'}, speed, easing, callback);
}; 

jQuery.fn.toggleText = function(a, b) {
	return this.each(function() {
		jQuery(this).text(jQuery(this).text() == a ? b : a);
	});
};
