/*  steamwalker.net
	copyright 2010 */
$(document).ready(function() {
    //
    //global vars
	var initialLoad = true;
    var rolloverOpacity = .5;
    var contentAnimDuration = 500;
    var previousActive = "/";
    var previousActiveBTN;
	var allAbout_externalsTop = 325;
    //action vars
    var goHomeTopProperty = $('#goHome').css("top");
	//
    var allMenuHighlights = new Array($('#menuLeftHighlight'), $('#menuCenterHighlight'), $('#menuRightHighlight'));
    var allMenuSelected = new Array($('#menuLeftSelected'), $('#menuCenterSelected'), $('#menuRightSelected'));
    var allContent = new Array($('#contentHome'), $('#contentAllAbout'), $('#contentOurWork'), $('#contentDigDeeper'));
    //
    //preloader
	initializeContactFormBehaviour();
	handleWindowResize();
	setDayNightState();
    $('#preloader').load(function() {
        $.preloadCssImages();
    });
    $(window).load(function() {
        if (!$.browser.msie) {
            $('#preloader').stop().animate({ opacity: 0 }, 700, 'easeInOutSine');
            $('#preloaderDiv').stop().animate({ opacity: 0 }, 300, 'easeInOutSine');
            setTimeout(function() {
                $('#preloader').css("display", "none");
                $('#preloaderDiv').css("display", "none");
            }, 700);
        } else {
            $('#preloader').css("display", "none");
            $('#preloaderDiv').css("display", "none");
        }
        config();
    });
    //
    //config
    function config(){
        initializeBehaviour("all");
		onGalleryInteract();
		if (!$.browser.msie) historyHandler();
		var doNote = (contactFormActive)? false : true;
        animElementsIn(false, true, doNote);
    }
    function animElementsIn(doHome, doHome, doNote) {
        if (!$.browser.msie) {
            $('#main').stop().animate({ opacity: 1 }, 400, 'easeInOutSine');
            if(doHome) {
                setTimeout(function() {
                    $('#contentHome').stop().animate({ opacity: 1 }, 700, 'easeInOutSine');
                }, 400);
            }
			if(doFooter) {
				$('#footer').stop().animate({ opacity: 1 }, 400, 'easeInOutSine');
			}
			if(doNote){
				setTimeout(function() {
					$('#contactNote').stop().animate({ opacity: 1, right: 40 }, 500, 'easeOutSine');
				}, 1500);
				setTimeout(function() {
					$('#contactNote').stop().animate({ opacity: 0 }, 700, 'easeInOutSine');
				}, 4500);
			}
        } else {
			$('#platformBlurbB img').css('margin', '-7px 0px 1px 0px');
            $('#footer').css("display", "block");
            $('#main').css("display", "block");
            $('#contentHome').css("display", "block");
			if (doNote){
				$('#contactNote').css("display", "block");
				$('#contactNote').css("right", "60px");
				setTimeout(function() {
					$('#contactNote').css("display", "none");
				}, 3000);
			} else {
				$('#contactNote').css("display", "none");
			}
        }
    }
    function setDayNightState() {
        //set day cycle 
        var currentDate = new Date();
        var hour = currentDate.getHours();
        var currentHour = hour / 1;
        if (currentHour >= 19 && currentHour < 22) {
            $('head').append('<link rel="stylesheet" type="text/css" href="assets/daycycle_eve.css" />');
        } else if (currentHour >= 22 || currentHour < 5) {
            $('head').append('<link rel="stylesheet" type="text/css" href="assets/daycycle_night.css" />');
        } else if (currentHour >= 5 && currentHour < 10) {
            $('head').append('<link rel="stylesheet" type="text/css" href="assets/daycycle_dawn.css" />');
        } else {
            $('head').append('<link rel="stylesheet" type="text/css" href="assets/daycycle_midday.css" />');
        }
    }
    //
    //behaviour
    function initializeBehaviour(type) {
        //global
        if (type == "all") {
            $.address.change(historyHandler);
            $(window).bind('resize', handleWindowResize);
            $('#contactFormHomeLink').click(expandContactForm);
            $('#contactFormTab').click(hrefToContactForm);
            $('#contactFormCloseBTN').click(collapseContactForm);
        }
		//home
		$('#platformSelectorA').mouseover(function() { onPlatformSelectorOver($('#platformHighlightA'), $('#platformBlurbA')) });
		$('#platformSelectorA').mouseout(function() { onPlatformSelectorOut($('#platformHighlightA'), $('#platformBlurbA')) });		
		$('#platformSelectorB').mouseover(function() { onPlatformSelectorOver($('#platformHighlightB'), $('#platformBlurbB')) });
		$('#platformSelectorB').mouseout(function() { onPlatformSelectorOut($('#platformHighlightB'), $('#platformBlurbB')) });		
		$('#platformSelectorC').mouseover(function() { onPlatformSelectorOver($('#platformHighlightC'), $('#platformBlurbC')) });
		$('#platformSelectorC').mouseout(function() { onPlatformSelectorOut($('#platformHighlightC'), $('#platformBlurbC')) });		
        //navigation
        $('#menuLeft').css("cursor", "pointer");
        $('#menuLeft').mouseover(function() { onMenuRollOver($('#menuLeftHighlight')) });
        $('#menuLeft').mouseout(function() { onMenuRollOut($('#menuLeftHighlight')) });
        $('#menuCenter').css("cursor", "pointer");
        $('#menuCenter').mouseover(function() { onMenuRollOver($('#menuCenterHighlight')) });
        $('#menuCenter').mouseout(function() { onMenuRollOut($('#menuCenterHighlight')) });
        $('#menuRight').css("cursor", "pointer");
        $('#menuRight').mouseover(function() { onMenuRollOver($('#menuRightHighlight')) });
        $('#menuRight').mouseout(function() { onMenuRollOut($('#menuRightHighlight')) });
		//about
		$('.personSelector').mouseover(function(){onPersonInteract($(this).attr('id'))});
		$('.personSelector').mouseout(function(){onPersonInteract()});
		//ourWork
		$('.highslide').mouseover(function(){onGalleryInteract($(this).attr('id'))});
		$('.highslide').mouseout(function(){onGalleryInteract()});
    }
    function resetPreviousActiveBTN() {
    	if (previousActiveBTN != "" || previousActiveBTN != null){
			var previousActiveHighlight;
			switch (previousActiveBTN) {
				case "menuLeft":
					previousActiveHighlight = $('#menuLeftHighlight');
					break;
				case "menuCenter":
					previousActiveHighlight = $('#menuCenterHighlight');
					break;
				case "menuRight":
					previousActiveHighlight = $('#menuRightHighlight');
					break;
			}
			$('#' + previousActiveBTN).css("cursor", "pointer");
			$('#' + previousActiveBTN).mouseover(function() { onMenuRollOver(previousActiveHighlight) });
			$('#' + previousActiveBTN).mouseout(function() { onMenuRollOut(previousActiveHighlight) });
	    }
	}
    //
    //events
    function historyHandler(event) {
		if($.address.value() == "contactForm"){
			expandContactForm();
			if(initialLoad){
				switchSections();
				previousSectionActive = "home";
			}
		} else {
			switch ($.address.value()) {
				case "contactSubmit":
					break;
				case "/":
				case "home":
					switchSections();
					break;
				case "allAbout":
					switchSections($('#menuLeft'), $('#menuLeftHighlight'), $('#menuLeftSelected'), $('#contentAllAbout'));
					break;
				case "ourWork":
					switchSections($('#menuCenter'), $('#menuCenterHighlight'), $('#menuCenterSelected'), $('#contentOurWork'));
					break;
				case "digDeeper":
					switchSections($('#menuRight'), $('#menuRightHighlight'), $('#menuRightSelected'), $('#contentDigDeeper'));
					break;
			}
			if(contactFormActive == true){
				collapseContactForm();
			}
		}
		initialLoad = false;
    }
    function handleWindowResize(event) {
        /*var vOffset = $(window).height() - 776;
        if (vOffset < -100) {
            vOffset = -100;
        } else if (vOffset > 0) {
            vOffset = 0;
        }*/

		/*
        $('#footer').css("top", (730 + vOffset));
        $('#main').css("height", (680 + vOffset));
        var goHomeTop = goHomeTopProperty.slice(0, 3) / 1;
        //$('#debug').append('  ght: ' + goHomeTop);
        $('#goHome').css("top", (goHomeTop + vOffset));
        $('#bg').css("top", vOffset);
        $('#main').css("top", -vOffset);
		*/
		
		//contact form placement
		if(contactFormActive){
			var newCenterX = calcContactFormOffset();
			$('#contactForm').css("right", newCenterX + "px");
		}
    }
	function onPersonInteract(targetID){
		//set all previous invisible
		$('.personInfo').css('display', 'none');
		if(targetID){
			var targetIDarray = targetID.split("_");
			$('.personInfo').each(function(i){
				var thisID = $(this).attr('id');
				var thisIDarray = thisID.split("_");
				//
				if(thisIDarray[1] == targetIDarray[1]){
					$(this).css('display', 'block');
					$('#externals').css('top', allAbout_externalsTop + 38);					
				}
			});
		} else {
			$('#externals').css('top', allAbout_externalsTop);
		}
	}
	function onGalleryInteract(targetID){
		//set all previous invisible
		$('.galleryInfo').css('display', 'none');
		if(targetID){
			var targetIDarray = targetID.split("_");
			$('.galleryInfo').each(function(i){
				var thisID = $(this).attr('id');
				var thisIDarray = thisID.split("_");
				//
				if(thisIDarray[1] == targetIDarray[1]){
					$(this).css('display', 'block');
				}
			});
		}
	}
	function onPlatformSelectorOver(target, elseTarget){
		if (!$.browser.msie) {
			target.css('background', 'rgba(255,255,255,.1)');
		}
		elseTarget.css('display', 'block');
		$('#platformsCTA').css('display', 'none');
	}
	function onPlatformSelectorOut(target, elseTarget){
		target.css('background', '');
		elseTarget.css('display', 'none');		
		$('#platformsCTA').css('display', 'block');
	}	
    function onMenuRollOver(target) {
        //$('#debug').append('  Animating: ' + target.attr('id') + '  ');
        if (!$.browser.msie) {
            target.stop().animate({ opacity: rolloverOpacity }, 150, 'easeInOutSine');
        } else {
            //target.css("display", "block");
        }
    }
    function onMenuRollOut(target) {
        if (!$.browser.msie) {
            target.stop().animate({ opacity: 0 }, 150, 'easeInOutSine');
        } else {
            //target.css("display", "none");
        }
    }
    function switchSections(targetButton, targetHighlight, targetSelected, targetContent) {
        //fade previous content area
        if (!targetContent) {
            targetContent = $('#contentHome');
        }
        for (var i in allContent) {
            if (allContent[i].attr('id') != targetContent.attr('id')) {
                if (!$.browser.msie) {
                    allContent[i].stop().animate({opacity: 0}, contentAnimDuration, 'easeInOutSine', function() { allContent[i].css("visible", "hidden") });
                } else {
                    allContent[i].css("display", "none");
                }
				allContent[i].css("z-index", "30");
            } 
			targetContent.css("z-index", "35");
        }
        //fade previous elements
        for (var i in allMenuHighlights) {
            if (targetHighlight == null || allMenuHighlights[i].attr('id') != targetHighlight.attr('id')) {
                if (!$.browser.msie) {
                    allMenuHighlights[i].stop().animate({ opacity: 0 }, 400, 'easeInOutSine');
                } else {
                    allMenuHighlights[i].css("display", "none");
                }
            }
        }
        for (var i in allMenuSelected) {
            if (targetSelected == null || allMenuSelected[i].attr('id') != targetSelected.attr('id')) {
                if (!$.browser.msie) {
                    allMenuSelected[i].stop().animate({ opacity: 0 }, 300, 'easeInOutSine');
                } else {
                    allMenuSelected[i].css("display", "none");
                }
            }
        }
        if (targetButton != undefined) {
            //content
            if (!$.browser.msie) {
                setTimeout(function() {
                    //section
                    targetContent.stop().animate({ opacity: 1, top: 0 }, contentAnimDuration, 'easeInOutSine');
                    targetContent.css("z-index: 31");
                    targetContent.css("display", "block");
                    //seperator
                    if ($('#contentSeperator').css("opacity") == 0) {
                        $('#contentSeperator').stop().animate({ opacity: 1 }, contentAnimDuration, 'easeInOutSine');
                    }
                }, 300);
            } else {
                targetContent.css("display", "block");
				//alert(targetContent);
                if ($('#contentSeperator').css("display") == "none") {
                    $('#contentSeperator').css("display", "block");
                }
            }
            //menu item
            targetButton.css("cursor", "default");
            targetButton.unbind();
            //menu highlight & selected
            if (!$.browser.msie) {
                targetHighlight.stop().animate({ opacity: 1 }, 300, 'easeOutSine');
                targetSelected.stop().animate({ opacity: 1 }, 300, 'easeInOutSine');
            } else {
                targetHighlight.css("display", "block");
                //targetHighlight.css("z-index", "2");
                targetSelected.css("display", "block");
            }
            //externals
            $('#externals').css("z-index", "36");
            var newTop;
            switch (targetContent.attr('id')) {
                case 'contentAllAbout':
                    newTop = allAbout_externalsTop + 5;
                    break;
                case 'contentOurWork':
                    newTop = 254;
                    break;
                case 'contentDigDeeper':
                    newTop = 334;
                    break;
                case 'contentHome':
                    break;
            }
            if (previousActive == "/" || previousActive == "home") {
                if (!$.browser.msie) {
                    setTimeout(function() {
                        $('#externals').stop().animate({ opacity: 1, top: newTop + "px" }, 600, 'easeOutCubic');
                        $('#externals').css("display", "block");
                    }, 400);
                } else {
                    $('#externals').css("top", newTop + "px");
                    $('#externals').css("display", "block");
                }
            } else {
                if (!$.browser.msie) {
                    setTimeout(function() {
                        $('#externals').stop().animate({ opacity: 1, top: newTop + "px" }, 600, 'easeInOutCubic');
                        $('#externals').css("display", "block");
                    }, 50);
                } else {
                    $('#externals').css("top", newTop + "px");
                    $('#externals').css("display", "block");
                }
            }
        } else {
            if (!$.browser.msie) {
                if ($('#contentSeperator').css("opacity") > 0) {
                    $('#contentSeperator').stop().animate({ opacity: 0 }, contentAnimDuration, 'easeInOutSine');
                }
            } else {
                if ($('#contentSeperator').css("display") == "block") {
                    $('#contentSeperator').css("display", "none");
                }
            }
            animElementsIn(true, true, false);
            //$('#externals')
            $('#externals').css("z-index", "9");
            if (!$.browser.msie) {
                $('#externals').stop().animate({ opacity: 0 }, 400, 'easeInOutCubic');
                setTimeout(function() {
                    $('#externals').css("top", 300);
                    $('#externals').css("display", "none");
                }, 400);
            } else {
                $('#externals').css("display", "none");
            }
        }
        resetPreviousActiveBTN();
        if (targetButton){
	        previousActiveBTN = targetButton.attr('id');
        }
        previousActive = $.address.value();
    }
	//
	//
	//
	//contact form
	//
	//global vars
	var previousSectionActive = "";
	var contactFormActive = false;
	var contactFormBodyText = null;
	var contactFormRight = $('#contactForm').css('right');
	//
	//structure
	function calcContactFormOffset() {
		var contactFormWidth = $('#contactForm').css('width');
		var contactFormWOffset = contactFormWidth.slice(0, 3) / 2;
		var newCenterX = ($(window).width() / 2) - contactFormWOffset;
		return newCenterX;
	}
	//
	//bahaviour
	function initializeContactFormBehaviour() {
		$('#contactFormSubmitBTN').click(function() { onContactFormSubmit() });
		$('#cbDiv1').click(function() { handleCheckbox($('#cbDiv1'), $('#cbWebDevelopment')) });
		$('#cbDiv1').css("cursor", "pointer");
		$('#cbDiv2').click(function() { handleCheckbox($('#cbDiv2'), $('#cbMobileDevelopment')) });
		$('#cbDiv2').css("cursor", "pointer");
		$('#cbDiv3').click(function() { handleCheckbox($('#cbDiv3'), $('#cbBrandingDesign')) });
		$('#cbDiv3').css("cursor", "pointer");
		$('#cbDiv4').click(function() { handleCheckbox($('#cbDiv4'), $('#cbRichMedia')) });
		$('#cbDiv4').css("cursor", "pointer");
		$('#cbDiv5').click(function() { handleCheckbox($('#cbDiv5'), $('#cbRenderingAnimation')) });
		$('#cbDiv5').css("cursor", "pointer");
	}
	//
	//events
	function hrefToContactForm(){
		//set location
		previousSectionActive = $.address.value();
		//alert(previousSectionActive);
		window.location = "#contactForm";
	}
	function expandContactForm() {        
		var newCenterX = calcContactFormOffset();
		resetContactForm();
		$('#contactForm').stop().animate({right: newCenterX}, 600, 'easeInOutCubic');
		//if user is using IE7 then we need to set #bg z-index to 51. This is because #fullscreenBlocker gets a z-index of 50 and for some reason unless
		//#bg (which is the root of the contact form div) gets a higher z-indexthan #fullScreenBlocker, it doesnt matter what the z-index of its children is in IE7, 
		//#fullScreenBlocker always stays above them and so the form cant be filled.  This may cause other abnormalities but atleast the user can fill the form. 		
		jQuery.each(jQuery.browser, function(i, val) {
			if(i=="msie" && parseInt(jQuery.browser.version.substr(0,3), 10) < 8.0) {
				$('#bg').addClass('bgIE7');
				$('#cbDiv1, #cbDiv2, #cbDiv3, #cbDiv4, #cbDiv5').css('margin', '-6px, 0px, 8px, 2px') ;
				$('.highslide-link').css('bottom', '125px') ;
				$('#cbWebDevelopment, #cbMobileDevelopment, #cbBrandingDesign, #cbRichMedia, #cbRenderingAnimation').css('margin', '-6px, 0px, 8px, 2px') ;
				$('#contactFormSubmitBTN').css('margin-right', '-26px');
			}
		});
		$('#fullScreenBlocker').addClass('blocker');
		$('#fullScreenBlocker').click(collapseContactForm);
		//debug
		$('#debug').append(' CFanim+ ');
		//set state
		contactFormActive = true;
	}
	function collapseContactForm(){
		$('#debug').append(" " + contactFormRight + " ");
		$('#contactForm').stop().animate({right: contactFormRight}, 600, 'easeInOutCubic');
		$('#fullScreenBlocker').removeClass('blocker');
		$('#fullScreenBlocker').unbind();
		//removing IE 7 hack that was added in expandContactForm()
		//remove thank you message if currently visible
		jQuery.each(jQuery.browser, function(i, val){
			if(i=="msie" && parseInt(jQuery.browser.version.substr(0,3), 10) < 8.0) {
				$('#bg').removeClass('bgIE7');
			}
		});
		//debug
		$('#debug').append(' CFanim- ');
		//set state
		contactFormActive = false;
		//alert(previousSectionActive + " vs " + $.address.value());
		if(previousSectionActive != $.address.value()){
			window.location = "#" + previousSectionActive;
		}
	}
	function disableContactForm(){
		$('#firstName').attr("disabled", "disabled");
		$('#firstName').attr("className", "contactFormFieldDisabled");
		$('#lastName').attr("disabled", "disabled");
		$('#lastName').attr("className", "contactFormFieldDisabled");
		$('#email').attr("disabled", "disabled");
		$('#email').attr("className", "contactFormFieldDisabled");
		$('#phone').attr("disabled", "disabled");
		$('#phone').attr("className", "contactFormFieldDisabled");
		$('#additionalInfo').attr("disabled", "disabled");
		$('#additionalInfo').attr("className", "contactFormBigFieldDisabled");
		//
		$('#contactFormSubmitBTN').unbind();
		$('#contactFormSubmitBTN').removeClass('submitBTN').addClass('processingBTN');	
	}
	function resetContactForm(){
		$('#firstName').attr("disabled", "");
		$('#firstName').val("First Name");
		$('#firstName').attr('className', 'contactFormField');
		$('#lastName').attr("disabled", "");
		$('#lastName').val("Last Name");
		$('#lastName').attr('className', 'contactFormField');
		$('#email').attr("disabled", "");
		$('#email').val("e-Mail");
		$('#email').attr('className', 'contactFormField');
		$('#phone').attr("disabled", "");
		$('#phone').val("Phone Number");
		$('#phone').attr('className', 'contactFormField');
		$('#additionalInfo').attr("disabled", "");
		$('#additionalInfo').val("Add any additional information that you feel is relevant here.");
		$('#additionalInfo').attr('className', 'contactFormBigField');
		//
		$('#cbDiv1').removeClass("contactFormCheckboxTrue");
		$('#cbDiv2').removeClass("contactFormCheckboxTrue");
		$('#cbDiv3').removeClass("contactFormCheckboxTrue");
		$('#cbDiv4').removeClass("contactFormCheckboxTrue");
		$('#cbDiv5').removeClass("contactFormCheckboxTrue");
		//
		$('#cbWebDevelopment').attr('CHECKED', 'false');
		$('#cbMobileDevelopment').attr('CHECKED', 'false');
		$('#cbBrandingDesign').attr('CHECKED', 'false');
		$('#cbRichMedia').attr('CHECKED', 'false');
		$('#cbRenderingAnimation').attr('CHECKED', 'false');
		//
		$('#contactFormSubmitBTN').click(function() { onContactFormSubmit() });
		$('#contactFormSubmitBTN').removeClass('processingBTN').addClass('submitBTN');
		//
		$('#contactFormResolve').css('opacity', '0');
		$('#contactFormResolve').css('display', 'none');
	}
	function validateInputBox(inputBox) {
		var inputBoxId = inputBox.attr('id');
		$('#debug').append(inputBoxId + ' ');
		//validate basic string
		if (inputBoxId == "firstName" || inputBoxId == "lastName" || inputBoxId == "additionalInfo"){
			if ( inputBox.val() == "" || 
				 inputBox.val().toLowerCase() == "undefined" || 
				 inputBox.val() == "First Name" || 
				 inputBox.val() == "Last Name" || 
				 inputBox.val() == "Add any additional information that you feel is relevant here." || 
				 inputBox.val() == "* Required Field"){
				return false;
			} else {
				return true;
			}
		}
		//validate e-mail field
		if (inputBoxId == 'email'){
			with ({value: inputBox.val()}) {
				var atpos = value.indexOf("@");
				var dotpos = value.lastIndexOf(".");
				if (atpos < 1 || dotpos-atpos < 2) {
					return false;
				} else {
					return true;
				}
			}
		}
		//validate phone number field
		if (inputBoxId == 'phone'){
			var validChars = "()- +0123456789";
			//
			if (inputBox.val() == "" || inputBox.val() == "Phone Number"){
				return true;
			} else {
				var value = inputBox.val();
				var legal = false;
				for (var i = 0; i < value.length; i++) {
					var c = value.charAt(i);
					for (var j = 0; j < validChars.length; j++){
						$('#debug').append("c: " + c + " = j:" + validChars.charAt(j));
						if (c == validChars.charAt(j)){
							legal = true;
							break;
						}
					}
					if (!legal){
						return false;
					}
					legal = false;
				}
				return true;
			}
		}
	}
	//
	//events
	function onContactFormSubmit() {
		//validate and submit form.
		if (validateFormAndBuildContactBody() == true) {
			disableContactForm();
			submitContactForm();
		} else {
			shakeElement($('#contactForm'), 6, 180, 50);
		}
	}
	function handleCheckbox(target, partner) {
		if (!target.hasClass("contactFormCheckboxTrue")) {
			target.addClass("contactFormCheckboxTrue");
			partner.attr('CHECKED', 'true');
		} else {
			target.removeClass("contactFormCheckboxTrue");
			partner.attr('CHECKED', 'false');
		}
	}
	function validateFormAndBuildContactBody() {
		var formIsValid = true;
		//validate
		if (validateInputBox($('#firstName')) == false) {
			formIsValid = false;
			$('#firstName').attr("value", "* Required Field");
			$('#firstName').attr("className", "contactFormFieldError");
		}
		if (validateInputBox($('#lastName')) == false) {
			formIsValid = false;
			$('#lastName').attr("value", "* Required Field");
			$('#lastName').attr("className", "contactFormFieldError");
			//tell user this field is a must.
		}
		if (validateInputBox($('#email')) == false) {
			formIsValid = false;
			$('#email').attr("value", "* Required Field");
			$('#email').attr("className", "contactFormFieldError");
			//tell user this field is a must.
		}
		if (validateInputBox($('#phone')) == false) {
			formIsValid = false;
			$('#phone').attr("value", "* Illegal Characters");
			$('#phone').attr("className", "contactFormFieldError");
		}
		if (validateInputBox($('#additionalInfo')) == false) {
			//no additional info provided. This is ok (or is it??)
		}
		if (formIsValid == true) {
			contactFormBodyText = "Name: " + $('#firstName').val() + " " + $('#lastName').val() + "\n";
			contactFormBodyText += "Email: " + $('#email').val() + "\n";
			contactFormBodyText += "Phone: " + $('#phone').val() + "\n";
			contactFormBodyText += "\nRequires help with...\n";
			contactFormBodyText += "Web Development: " + $('#cbWebDevelopment').attr('CHECKED') + "\n";
			contactFormBodyText += "Mobile App Development: " + $('#cbMobileDevelopment').attr('CHECKED') + "\n";
			contactFormBodyText += "Branding & Design: " + $('#cbBrandingDesign').attr('CHECKED') + "\n";
			contactFormBodyText += "Rich Media (Flash, etc): " + $('#cbRichMedia').attr('CHECKED') + "\n";
			contactFormBodyText += "3d Renders/Animation: " + $('#cbRenderingAnimation').attr('CHECKED') + "\n";
			contactFormBodyText += "\nAdditional Information:\n";
			contactFormBodyText += $('#additionalInfo').val();
		} else {
			contactFormBodyText = null;
		}
		return formIsValid;
	}
	function submitContactForm() {
		//create and submit the JSON message to webservice.
		if (contactFormBodyText != null) {
			//tracking
			pageTracker._trackEvent('ContactForm', 'Submit')
			//post
			var first_name = $("#firstName").val();
			var last_name = $("#lastName").val();
			var email = $("#email").val();
			var phone = $("#phone").val();
			var help = '';
					
			if($("#cbDiv1").hasClass('contactFormCheckboxTrue')){
				 help = help+' Web Development';
			}
			
			if($("#cbDiv2").hasClass('contactFormCheckboxTrue')){
				help = help+', Mobile Development';
				
			}
			
			if($("#cbDiv3").hasClass('contactFormCheckboxTrue')){
				help = help+', Branding and Design';
			}
			
			if($("#cbDiv4").hasClass('contactFormCheckboxTrue')){
				help = help+', Rich Media Flash etc';
			}
			
			
			if($("#cbDiv5").hasClass('contactFormCheckboxTrue')){
				help = help+', 3D and Animation';
			}
			
			var additional_info = $("#additionalInfo").val();
			var datastring = 'first_name='+first_name+'&last_name='+last_name+'&email='+email+'&phone='+phone+'&help='+help+'&additional_info='+additional_info;
			$.ajax({
				type: "POST",
				url: "contactform.php",
				data: datastring,
				cache: false,
				success: function(msg) {
					if(msg == 'submitted'){
					 onContactFormSubmitSuccess(msg);
					}
					if(msg == 'error'){
					  onContactFormSubmitError(msg);
					}
				},
				error: function(e) {
					onContactFormSubmitError(e);
				}
			});
		
		}
	}
	function onContactFormSubmitSuccess(successMessage) {
		$('#contactFormResolve').stop().animate({opacity: 1}, 500, 'easeInOutSine');
		$('#contactFormResolve').css("z-index", "105");
		$('#contactFormResolve').css("display", "block");
		$('#contactFormMessage').css("display", "none");
		$('#contentHomeTextEnd').html(".");
		//invoke collapse after timeout
		setTimeout(function() {
			//when yo = -600;
			collapseContactForm();
		}, 4000);
	}
	function onContactFormSubmitError(errorMessage) {
		//some error occured. We need to handle this more gracefully.
		shakeElement($('#contactForm'), 6, 210, 50);
	}
	//
	//extras
	function shakeElement(target, offset, length, speed) {
		var oldPositionStr = target.css("right");
		var oldPosition = oldPositionStr.slice(0, 3) / 1;
		var counter = 0;
		var animate = setTimeout(function() {
			if (counter >= length) {
				target.css("right", oldPosition);
			} else if (((counter / speed) % 2) == 0) {
				target.css("right", oldPosition - offset);
				setTimeout(arguments.callee, speed);
			} else if (((counter / speed) % 2) != 0) {
				target.css("right", oldPosition + offset);
				setTimeout(arguments.callee, speed);
			}
			counter += speed;
		}, speed);
	}
});
