/**
 * NOTOsoft Website
 *
 * Copyright 2008-2010 NOTOsoft Web Designs Inc.
 * Licensed under the GPLv3 license
 *
 * You should have received a copy of the GNU General Public License
 * along with this file.  If not, see <http://www.gnu.org/licenses/>.
 */

$(function() {
    

    $.OmniFlash.embedSWF('resources/media/noto_flash.swf', 'flashContainer', '840', '100', '10');

    $("body #wrapper").attr('class', 'showAll');

    //Extra Validator Methods
    $.validator.addMethod("alphaWSpaces", function(value, element){
        return this.optional(element) || /^[a-zA-Z\s]+$/.test(value);
    }, "May only contain letters (spaces allowed)");
    $.validator.addMethod("alpha", function(value, element){
        return this.optional(element) || /^[a-zA-Z]+$/.test(value);
    }, "May only contain letters (spaces not allowed)");

    //Show the subsection underneath the active page
    $("#navbarContainer #navbarMiddle div.inactivePage").parent().find("div.subsection").hide();

    //Setup navbar slide event
    $("#navbarContainer #navbarMiddle div.inactivePage").click(function() {
        var clickedEle = $(this);

        //If a subsection for this page exists, then slide it down and slide all others up
        if(clickedEle.siblings('.subsection').length > 0) {
            //Slide the clicked subsection down
            clickedEle.siblings('.subsection').slideDown('fast');
            //Slide all others up
            $('.subsection').not(clickedEle.siblings('.subsection')).slideUp('fast');
        }
    });

    $("#navbarContainer #navbarMiddle div.subsection div.activeSub").parent().prev().css('cursor', 'pointer');

    //If the site gallery exists, then we do not want the scrolling gallery at the bottom
    if($('#wrapper #mainArea #mainContent #dynamicContentArea #pageMiddleContent #siteGallery').length > 0) {
        $('#wrapper #footerWrapper #footer #footerthumb').hide();
		$('#wrapper #footerSpacer').hide();
        $('#wrapper #footerWrapper #footer #footercontent').css({position: 'relative', top: '30px'});
    }
    else {
        //Create the scrolling gallery at the bottom of the page
        $.OmniGallery.create($('#wrapper #footerWrapper #footer #footerthumb #webgallery'), [
            {src: 'bookkeepergirl.png', href: "http://bookkeepergirl.com/"},
            {src: 'harddrywalltx.png', href: "http://harddrywalltx.com/"},
            {src: 'sulphurata.png', href: "http://sulphurspringsata.com/"}
        ], {imgsFolder: "resources/images/gallery_images/", handleColor: '#063B57'});
    }

    //Setup click event for "PLACE AN ORDER" button
    $("#placeOrderButton").click(function() {
       $.OmniBox.activate("ajax/place_order.ajax.php", {
            //After the OmniBox is loaded, setup the client form to validate on submit
            boxload: function () {
                //Return false on form submit
                $("#clientOrderForm").submit(function() { return false; })
                //Mask the phone number input
                $("#clientOrderForm #orderFormPhone").mask("999-999-9999");
                $("#clientOrderForm #orderFormZipcode").mask("99999");

                // validate signup form on keyup and submit
                $("#clientOrderForm").validate({
                    rules: {
                        orderFormFirstname: "alpha",
                        orderFormLastname: "alpha",
                        orderFormCity: "alphaWSpaces",
                        orderFormDomainName: {url: true}
                    },
                    ignore: ":hidden"
                });
                                                                                                                
                //Validate the personal info page before moving onto the project info page
                $("#clientOrderForm #personalInfo .pagefooter div.rightArrow").click(function() {
                    if($("#clientOrderForm").valid()) {
                        $("#clientOrderForm #projectInfo").show();
                        $("#clientOrderForm #personalInfo").hide();
                        $("#omniBox #omniBoxTitle #omniBoxPageNum").text("(page 2/2)");
                    }
                });

                //Setup the click event for the user to come back to page 1 from page 2
                $("#clientOrderForm #projectInfo .pagefooter div.leftArrow").click(function() {
                    $("#clientOrderForm #projectInfo").hide();
                    $("#clientOrderForm #personalInfo").show();
                    $("#omniBox #omniBoxTitle #omniBoxPageNum").text("(page 1/2)");
                });

                //Validate the project info page before submitting the form to be emailed
                $("#clientOrderForm #projectInfo .pagefooter div.rightArrow").click(function() {
                    if($("#clientOrderForm").valid()) {
                        $.ajax({
                            type: 'POST',
                            url: 'ajax/email_form.ajax.php',
                            data: $("#clientOrderForm").serialize(),
                            success: function(response) {},
                            error: function() { throw new Error('Ajax request failed'); }
                        });

                        $("#omniBox #clientOrderForm, #omniBox #omniBoxTitle #omniBoxPageNum").hide();
                        $("#omniBox #omniBoxPageDesc").html("<h3>Your order has been submitted successfully!<br>A representitive from NOTOsoft will contact you within one business day. " +
                                                                 "Thank you for your interest in NOTOsoft, and we look forward to providing the highest quality software for you and/or your company!</h3>");
                    }
                });
            }
       });
    });
});
