﻿$(document).ready(function () {

    // do full bg
    $("#background").fullBg();

    // do screen detection and message on unfavorable res    

    // setup scroller
    var api = $(".scrollable").scrollable({
        vertical: true,
        onSeek: function (event, i) {

        }
    }).navigator({
        navi: 'ul.menu-list',
        naviItem: 'a'
    });

    // setup current link
    if (window.location.hash) {
        var theHash = window.location.hash.replace('#', '');
        var currentLink = $('a.' + theHash);
        var selectedIndex = $('a.menu-link').index(currentLink);
        currentLink.addClass('active');
        api.data("scrollable").move(selectedIndex);
    } else {
        $('a.about').addClass('active');
        setTimeout(function () {
            api.data("scrollable").move(1)
        }, 50);
    }

    $('a.menu-link').click(function () {
        var currHash = window.location.hash;
        var currUrl = 'index.htm'
        window.location = currUrl + $(this).attr('href');
    });

    var is_mobile = function () {
        var agents = ['android', 'webos', 'iphone', 'ipad', 'blackberry'];
        for (i in agents) {
            if (navigator.userAgent.match('/' + agents[i] + '/i')) {
                return true;
            }
        }
        return false;
    }



    var isFormValid = function () {
        var valid = true;
        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][\d]\.|1[\d]{2}\.|[\d]{1,2}\.))((25[0-5]|2[0-4][\d]|1[\d]{2}|[\d]{1,2})\.){2}(25[0-5]|2[0-4][\d]|1[\d]{2}|[\d]{1,2})\]?$)/i);
        var nameField = $('#name')
        var emailField = $('#email')
        var detailField = $('#Details')

        if (nameField.val().length == 0) {
            nameField.addClass('invalid');
            valid = false;
        } else {
            nameField.removeClass('invalid');
        }

        if (emailField.val().length == 0) {
            emailField.addClass('invalid');
            valid = false;
        } else {
            if (!pattern.test(emailField.val())) {
                emailField.addClass('invalid');
                valid = false;
            } else {
                emailField.removeClass('invalid');
            }
        }

        if (detailField.val().length == 0) {
            detailField.addClass('invalid');
            valid = false;
        } else {
            detailField.removeClass('invalid');
        }

        return valid;

    };

    $('#contact-form').submit(function (e) {
        e.preventDefault();
        if (isFormValid()) {
            var nameField = $('#name')
            var emailField = $('#email')
            var detailField = $('#Details')
            $('#send-preloader').show();
            $.post('sendmessage.aspx', { name: nameField.val(), email: emailField.val(), content: detailField.val() }, function () {
                $('#send-preloader').text("Message Sent!").delay(1000).fadeOut('slow', function () {
                    $('#send-preloader').addClass('hide');
                    $('#send-preloader').text("Sending...");
                    nameField.val('');
                    emailField.val('');
                    detailField.val('');
                });
            })
        } else {

        }

    });

    $('a.portfolio-link').lightBox();

    $('a.portfolio-link').hover(function () {
        var label = $('#portfolio-label');
        if ($(this).hasClass('offline')) {
            label.text('* currently offline')
        } else if ($(this).hasClass('revised'))
            label.text('* curently revised')
        else {
            label.text('');
        }

    }, function () { });

    $(window).resize(function () {
        var viewportWidth = parseInt($(window).width());
        var viewportHeight = parseInt($(window).height());

        if (viewportWidth <= 1123) {
            $('#maincontent').hide();
            $('#viewport-message').show();
        } else {
            if (!$('#maincontent').is(':visible')) {
                $('#maincontent').show();
            }

            if ($('#viewport-message').is(':visible')) {
                $('#viewport-message').hide();
            }
        }

    });



    // setup tipsy
    $('.n-tip').tipsy({ gravity: 'n', title: 'title', live: true });
    $('.s-tip').tipsy({ gravity: 's', title: 'title', live: true });
    $('.e-tip').tipsy({ gravity: 'e', title: 'title', live: true });
    $('.w-tip').tipsy({ gravity: 'w', title: 'title', live: true });


    $('.js-hide').remove();

    yepnope([{
        test: is_mobile(),
        yep: ['http://code.jquery.com/mobile/1.0a1/jquery.mobile-1.0a1.min.js', 'http://code.jquery.com/mobile/1.0a1/jquery.mobile-1.0a1.min.css'],        
        callback: function (url, result, key) {
        }
    }]);



});
