
/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;
var inputClass = '.textToPhoneNmbr';

//loading popup with jQuery magic!
function loadPopup(map){
	
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		
		$("#popupContact").fadeIn("slow");
		popupStatus = 1;
		$(".UTypeID, .selectTime").hide();
		if(map != 'mapToPhone'){
			$("#mapStuff").hide();
			$("#noMap").show();
		} else {
			$("#mapStuff").show();
			$("#noMap").hide();
		}
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popupContact").fadeOut("slow");
		popupStatus = 0;
		$(".UTypeID, .selectTime").show();
		
	}
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupContact").height();
	var popupWidth = $("#popupContact").width();
	//centering
	$("#popupContact").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});
	
}

function placeHolder(){
	// Text to Phone
	//var textNmbr = $('#textToPhoneForm label').remove().text();
	var textNmbr = 1234567890
	$(inputClass).addClass('placeholder').val(textNmbr)
		.focus(function() {
				if (this.value == textNmbr) {
					$(this).removeClass('placeholder').val('');
				};
		})
		.blur(function() {
				if (this.value == '') {
				$(this).addClass('placeholder').val(textNmbr);	
				}
	})
}

$(document).ready(function(){
	
	//LOADING POPUP
	//Click the button event!
	$(".textToPhone").click(function(){
		//if we are on the Map page
		if($(this).attr('id')== 'mapToPhone'){
			inputClass = '.textToPhoneNmbrMap';
		}else{
			inputClass= '.textToPhoneNmbr';
		}
		
		placeHolder();
		
		//centering with css
		centerPopup();
		//load popup
		loadPopup(this.id);		
	});
	//Hover over button
	$("#popupContactClose").hover(function()
		{
		$(this).css({'border-style':'inset'});
		},function()
		{
		$(this).css({'border-style':'outset'});					
	});	
					
	//CLOSING POPUP
	//Click the x event!
	$("#popupContactClose").click(function(){
		disablePopup();
	});
	
	/*
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});
	*/	
	
	// Format the phone number from 1234567890 to (123) 456-7890
	if($('.textToPhoneNmbrMap').val() != ''){
		classes = '.textToPhoneNmbrMap, .textToPhoneNmbr';
	}else{
		classes = '.textToPhoneNmbr';
	}
	$(classes).keyup(function(event){
		var sLength = $(inputClass).val().length;
	
		if ((sLength <=14 && (event.keyCode >= 48 && event.keyCode <= 57)) || 
			(sLength <=14 && (event.keyCode >= 96 && event.keyCode <= 105))) {
			
			// Format the area code once the number of digits equals 3
			if (sLength == 3) {
				$(inputClass).val('('+$(inputClass).val()+') ');
			};
			
			// Once the area code and prefix has been added add the dash ( - )
			if (sLength == 9) {
				$(inputClass).val($(inputClass).val()+'-');
			};
			
			$('#counter').empty().append($(inputClass).val().length);
		} 
		else {
				sLength--;
				$(inputClass).val($(inputClass).val().substr(0,14));
		};
	
	});
	
    
   onError = function() {
			// Create an error response
			alert ("We're sorry, we hit an error while trying to send your message.  Please try again.");
		}
        
    onSuccess = function() {
			// Create an error response
			alert ("The information has been sent to your cell phone.");
		}        
    
    
		
	$('#submitBtn, #driveSubmitBtn').click(function() {			
	('whoot');		
            $.ajax({
                dataType: 'get',
                error: onError,
                success: onSuccess,
                url: 'includes/cellitCommunicator.php?cid=' + $('.commID').val() + '&textToPhoneNumbr=' + $(inputClass).val() + (($('.startingAddress').val())?'&startingAddress=' +$('.startingAddress').val():'')
              });
			// ($('.commID'));
           disablePopup();
	});
	
	$('.inputSpan').click(function() {
			var field = this.id;
			if(field == 'areaCode') { $(inputClass).focus(); }
			if(field == 'starting') { $('#startingAddress').focus(); }
	});

    
});



