﻿$(document).ready(function () {


    $(window).resize(function () {
        resizeWindow();
    });

    function resizeWindow() {
        if (navigator && navigator.platform && navigator.platform.match(/^(iPad)$/)) {
            $('body').css('zoom', '86%');
            $('#footer').css('top', '469px');
        }
        var newTop = parseInt(($(window).height() - 720) / 2);
        if (newTop < 0) {
            newTop = 0;
        }
        $('#mainDiv').css('margin-top', newTop);
        $('#myavit').css('top', ($(window).height() - 488) / 2);
        $('#myavit').css('left', ($(window).width() - 648) / 2);
        $('#mask').width($(window).width());
        $('#mask').height($(window).height());
    }
    var numberOfImages = 4;
    function preloader() {

        // counter
        var i = 0;


        // create object
        imageObj = new Image();

        // start preloading
        for (i = 1; i <= numberOfImages; i++) {
            imageObj.src = 'images/rotator/' + i + '.jpg';
        }

    }



    $('.navItem').not('.navItemHot')
        .hover(function () {
            if (!$(this).hasClass('navItemHot')) {
                $(this).animate({ 'top': '-5px' }, 100)
            }
        }, function () {
            if (!$(this).hasClass('navItemHot')) {
                $(this).animate({ 'top': '0px' }, 75)
            }
        })
        .click(function () {
            clickNav($(this));
        });


    function clickNav(e) {
        if (!e.hasClass('navItemHot')) {
            $('.navItem').each(function () {
                $(this)
                    .css('top', '0px')
                    .removeClass('navItemHot')
                    .children('img').attr('src', $(this).children('img').attr('src').replace('_hot.png', '.png'));
            });
            $('#selector').animate({ 'margin-left': parseInt(e.parent().css('margin-left')) }, 200);
            e
                .addClass('navItemHot')
                .css('top', '-5px')
                .css('outline', 'none')
                .children('img').attr('src', e.children('img').attr('src').replace('.png', '') + '_hot.png');
            window.location.href = e.attr('href');
            var contentSelection = e.attr('href').replace('#', '');
            // hide & load content
            $('#htmlContent').fadeOut('fast', function () {
                var thisContent = $('.content[anchor="' + contentSelection + '"]');
                if (thisContent.attr('textWidth') == 'half') {
                    $('.contentBlock').animate({ 'width': '382px' }, 500);
                    $('#contentRight').animate({ 'left': 422 }, 500, function () {

                        $('#htmlContent').html(thisContent.html()).fadeIn();
                    });
                }
                else {
                    $('.contentBlock').animate({ 'width': 806 }, 500);
                    $('#contentRight').animate({ 'left': 846 }, 500, function () { $('#htmlContent').html(thisContent.html()).fadeIn('fast'); });
                }
            });
        }
    }


    $('#footerContact').click(function () { clickNav($('#navContact')) });

    $('#footerMyavit,#cancelMyavit').click(function () {
        toggleMask();
    });

    function toggleMask() {
        if ($('#mask').css('display') == 'none') {
            $('#mask,#myavit').fadeIn();

        } else {
            $('#mask,#myavit').fadeOut();
        }

    }

    $('#twitLink').hover(function () { $(this).attr('src', 'images/twitter_on.png') }, function () { $(this).attr('src', 'images/twitter_off.png') });

    function isValidEmailAddress(emailAddress) {
        var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
        return pattern.test(emailAddress);
    }



    $('#contactSubmit').live('click', function () {
        sendContactEmail();
    });

    function sendContactEmail() {
        var cE = $('#contactEmail').val();
        var cM = $('#contactMessage').val();

        $('#contactSubmit').die('click').children('span').text('One moment please ...');

        $('input').attr('disabled', 'disabled');
        if (!isValidEmailAddress(cE)) {
            alert('Please enter a valid email address');
            $('#contactEmail').focus();
            resetForm();
            return false;
        }
        else {
            $.ajax({
                type: "POST",
                url: "sendEmail.php?email=" + cE + "&message=" + cM
            }).done(function (msg) {
                mailSuccess();
            }).fail(function (jqXHR, textStatus) {
                alert('OOPS!\n\nThere was an error sending email.\n\nAV/IT technicians have been made aware of the error.\n\nPlease call (206) 985-2848 for immediate assistance');
                resetForm();
            });
        }


        function mailSuccess() {
            $('#htmlContent').slideUp('normal', function () {
                $(this).html('<div class="twenty">Thank you for your inquiry.<br /><br />Your email has been received by AV/IT and a representative will be in touch with you as soon as possible.<br /></br /></div>')
                .slideDown();
            });
        }
        function resetForm() {
            $('#contactSubmit').live('click', function () { sendContactEmail(); }).children('span').text('Send Message');
            $('input,textarea').removeAttr('disabled');
        }
    }

    $('#foundersLink').live('click', function () {
        $('#htmlContent').slideUp('normal', function () {
            $(this).html($('#foundersContent').html())
                .slideDown();
        });
    });

    $('#aboutLink').live('click', function () {
        $('#htmlContent').slideUp('normal', function () {
            $(this).html($('.content[anchor="about"]').html())
                .slideDown();
        });
    });

    function startRotator() {
        displayRandomImage();
        setInterval(displayRandomImage, 10000);

    }

    var currentImage = 0
    function displayRandomImage() {
        var displayImageNumber = Math.floor(Math.random() * numberOfImages) + 1;
        if (displayImageNumber != currentImage) {
            currentImage = displayImageNumber;
            $('#rotatingImage').fadeOut(500, function () {
                $('#homeImage').html('<img src="images/rotator/' + currentImage + '.jpg" style="display:none;" id="rotatingImage" />');
                $('#rotatingImage').fadeIn(1000);
            });
        }
    }

    $('#homeLink').click(function () { clickNav($('#navAbout')); });



    resizeWindow();
    preloader();
    clickNav($('#navAbout'));
    startRotator();


});
