$(document).ready(function() {
	elevator.emailMangle();
	elevator.formPostMangle();
	//elevator.homepageTitlesFadeIn();

	$('#contactElevatorForm').submit(function(){
		var valResponse=elevator.checkForm('contactElevator','contactElevatorForm','/inquiry.html');
		return valResponse;
	});
	
	$('#newsletterForm').submit(function(){
		var valResponse=elevator.checkForm('newsletterSignup','newsletterForm','/newsletter-signup.html');
		return valResponse;
	});

    //Our work page slideshow toggle
    $('.toggle').click(function(){
		var allToggles=$('.toggle');
		for(i=0;i<=allToggles.length;i++) {
			if ($(allToggles[i]).hasClass('toggleActive')) {
				$(allToggles[i]).removeClass('toggleActive');
				$(allToggles[i]).addClass('toggleInactive');
			}
			else if ($(allToggles[i]).hasClass('toggleInactive')) {
				$(allToggles[i]).removeClass('toggleInactive');
				$(allToggles[i]).addClass('toggleActive');
			}
		}
		return false;
    });
    
	// Clear form fields on focus
	var clearMePrevious = ''
	// clear input on focus
	$('.valueControl').focus(function() {
		if($(this).val()==$(this).attr('title')) {
			clearMePrevious = $(this).val();
			$(this).val('');
		}
	});
	// if field is empty afterward, add text again
	$('.valueControl').blur(function() {
		if($(this).val()=='') {
			var prevTitle = $(this).attr('title');
			$(this).val(prevTitle);
		}
	});
	
	$('textarea').focus(function(){
		$(this).css('background','#fff');
	});
	
	$('input').focus(function() {
		$(this).css('color','');
	});
});

var elevator={
	mangleCode: 'c3po', //Set this variable to whatever you will replace the email domain name with
	gaTrackingNumber:'UA-76444-3', //Set Google Analytics Tracking Number
	contactFormAction:'/libraries/misc/form-handler-contact-us.asp', // Set contact form action
	
	emailMangle:function() {
		var trueDomain = 'elevatoragency' //Set this variable to the real domain name in the email
		var aLinks = document.getElementsByTagName('a');
		if (!aLinks) {return;}
		var aHREF;
		var aTextNode;
		for (i=0; i < aLinks.length; i++) {
			aHREF = aLinks[i].getAttribute('href');
			aTextNode = aLinks[i].innerHTML;
			
			if (aHREF!=null && aHREF.indexOf(elevator.mangleCode)!=-1) {
				var newHREF = aHREF.replace(elevator.mangleCode,'@'+trueDomain);
				aLinks[i].setAttribute('href',newHREF)
			}
			
			if (aTextNode.indexOf(elevator.mangleCode)!=-1) {
				var aTextNode = aTextNode.replace(elevator.mangleCode,'@'+trueDomain);
				aLinks[i].innerHTML= aTextNode;
			}
		}
	},
	homepageTitlesFadeIn:function() {
		var titleImage, subTitleImage
		var title=$('#hpMain .title');
		var subTitle=$('#hpMain .subTitle');
		if (!title || !subTitle) {return;}
		$(title).css('background','none');
		$(title).css('text-indent','0');
		$(title).html('<img src="/libraries/images/homepage/title-hp-making-awesome.gif" style="display:none" alt="" />');
		$(subTitle).css('background','none');
		$(subTitle).css('text-indent','0');
		$(subTitle).html('<img src="/libraries/images/homepage/title-hp-serving-culture.gif" style="display:none" alt="" />');
		$('#hpMain .title img').wait(300,'fx').fadeIn('slow',function(){
			$('#hpMain .subTitle img').wait(300,'fx').fadeIn('slow');
		});
	},
	formPostMangle:function() {
		var contactForm=$('#contactElevatorForm');
		if ($(contactForm).attr('action')==elevator.mangleCode) {
			$(contactForm).attr('action',elevator.contactFormAction);
		}
	},
	checkForm:function(formWrapId,formId,googAnalyticsPageName) {	
		// Be sure to define these variables
		var gaPageName = googAnalyticsPageName; // define the page name to report to Google
		var gaTrackingNumber='UA-76444-3'; // define the google analytics tracking code
		var email = 'inputEmail'; // define the class of the email input
		var arrNumericInput = new Array(''); // define the id's of the fields that require numeric validation
		var errorWrap = 'errorMessage' ; // define the class of the error message div
		var successWrap = 'successMessage'; // define the class of the success message div
	
		// Don't touch these
		var arrErrorInputs = $('#'+formId+' > input');
		var arrErrorSelect = $('#'+formId+' > select');
		var arrErrorTextarea = $('#'+formId+' > textarea');
		if ($('#'+formId+' > .'+email)) {
			var emailFieldClass = $('#'+formId+' > .'+email).val();
		}
		var inputReset,validateThisField,inputTitle;
		var pass = true;
	
		// Reset error message and fields
		$('#'+formWrapId+' > .'+errorWrap).css('display','none');
		$('#'+formWrapId+' > .validate').css('color','');
		$('#'+formWrapId+' > .validate').css('background','#fff');
	
		// Mark error text fields
		for (var i=0;i<arrErrorInputs.length;i++) {
			validateThisField = arrErrorInputs[i];
			inputTitle=$(validateThisField).attr('title');
			inputValue=$(validateThisField).val();
			if ($(arrErrorInputs[i]).hasClass('validate')) {
				if (inputTitle!=undefined) {
				    if (inputTitle==inputValue || inputValue=='') {
				    	$(validateThisField).css('color','#e62027');
				    	pass = false;
				    }
				}
			}
		}
	
		// Mark error select fields
		for (var i=0;i<arrErrorSelect.length;i++) {
			if ($(arrErrorSelect[i]).val()=='' && $(arrErrorSelect[i]).hasClass('validate')) {
			    $(arrErrorSelect[i]).css('color','#e62027');
			    pass = false;
			}
		}

		// Mark error textarea
		for (var i=0;i<arrErrorTextarea.length;i++) {
			if ($(arrErrorTextarea[i]).val()=='' && $(arrErrorTextarea).hasClass('validate')) {
			    $(arrErrorTextarea[i]).css('background','pink');
			    pass = false;
			}
		}
		
		if ($('#'+formId+' > .'+email)) {
			// Check to make sure email is valid
			if (emailFieldClass.indexOf('@')==-1 && $('#'+formId+' > .'+email).hasClass('validate')) {
				$('#'+formId+' > .'+email).css('color','#e62027');
				pass = false;
			}
		}
				
		// Check inputs that require numerical input
		//if (!arrNumericInput=='') {
		//    var numericExpression = /^[0-9]+$/;
		//    for (var i=0;i<arrNumericInput.length-1;i++) {
		//    	if(!document.getElementById(arrNumericInput[i]).value.match(numericExpression)){
		//    		$(arrNumericInput[i]).css('background-position','bottom left');
		//    		pass = 'fail';
		//    	}
		//    }
		//}
					
		// Decide whether to send the form or not
		if (pass==false) {
			if($('#'+errorWrap)) {
				$('#'+formWrapId+' > .'+errorWrap).css('display','inline');
			}
			return false;
		}
		else {
			//Hide/show signup elements once form is submitted
			$('#'+formWrapId+' > .swapA').css('display','none');
			$('#'+formWrapId+' > .swapB').css('display','block');
			
			// Clear any non-required default values before sending
			for (var i=0;i<arrErrorInputs.length;i++) {
				validateThisField = arrErrorInputs[i];
				inputTitle=$(validateThisField).attr('title');
				inputValue=$(validateThisField).val();
				if (!($(arrErrorInputs[i]).hasClass('validate'))) {
					if (inputTitle==inputValue) {
						$(validateThisField).val('');
					}
				}
			}
			
			// Report to google analytics
			var _gaq = _gaq || [];
			_gaq.push(['_setAccount', gaTrackingNumber]);
			_gaq.push(['_trackPageview']);
			(function() {
				var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
				ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
				var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
			})();
			try {
				var pageTracker = _gat._getTracker(gaTrackingNumber);
				pageTracker._trackPageview(gaPageName);
			} catch(err) {}
	
			return true;
		}
	}
}

$.fn.wait = function(time, type) {
    time = time || 900;
    type = type || "fx";
    return this.queue(type, function() {
        var self = this;
        setTimeout(function() {
            $(self).dequeue();
        }, time);
    });
}

