$('document').ready(function(){
    // top menu link click
    $('#topMenuContainer, .subMenu').localScroll(
    {
        lazy : true,
        onBefore : function(eEvent, oElem, oScrollTarget )
        {
            $('.subMenu').fadeOut('fast');
            
            $('#'+ oElem.id +' .subMenu').fadeIn('fast');

        }
    });

    $('.activeSubMenu').click(function(e){
        e.preventDefault();
        $('.subMenu').fadeOut('fast');
        $(this).siblings('.subMenu').fadeIn('fast');
    });

    $('.subMenu .active').click(function(e){
        e.preventDefault();
    });

   $('#cycle').cycle({
    fx      :     'fade',
    delay   :  5000,
    speed   : 300,
    next    :   '#nextSlide',
    prev    :   '#prevSlide'
    });

    $('#cyclePlay').click(function(e){
        e.preventDefault();
        $('#cycle').cycle('resume');
    });

    $('#cyclePause').click(function(e){
        e.preventDefault();
        $('#cycle').cycle('pause');
    });

    $(".workBtnContainer a").fancybox({
        'transitionIn'	:	'elastic',
        'transitionOut'	:	'elastic',
        'speedIn'       :	600,
        'speedOut'	:	200,
        'overlayShow'	:	true
    });

    $('.workItemDisplayDesc a').click(function(e){
        e.preventDefault();
        window.open ($(this).attr('href'),"newWindow");
    });

    $('#contactSend').click(function(){
        var contactName = $('#contactName').val();
        var contactEmail = $('#contactEmail').val();
        var contactText = $('#contactText').val();
        var sValidMsg = '<p>Your email is on the way.</p>'
        var sInvalidMsg = '<p>Your email could not be sent. Check that you have filled in all fields and that you have stated a valid email address.</p>'

        var regex = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/;
        var isValid = false;
        var position = $('#contactItemContainer').position();
        var oMsgDiv = $('<div>');
            oMsgDiv.css({
                display: 'none',
                position: 'absolute',
                left: position.left + 200,
                top: position.top + 100
            })

        if((contactName != '') && (contactEmail != '') && (contactText !=''))
        {
            if(contactEmail.match(regex))
            { isValid = true; }
        }

        if(isValid == true)
        {    
            var oEmail = {
                email : contactEmail,
                message : contactText,
                name :contactName
            }
            $.ajax('sendEmail.php', {
                data: $.param(oEmail)
            })
            oMsgDiv.addClass('okMsg');
            oMsgDiv.html(sValidMsg);
        }
        else
        { 
            oMsgDiv.addClass('errorMsg');
            oMsgDiv.html(sInvalidMsg);
        }

        $('body').append(oMsgDiv);

        oMsgDiv.fadeIn('fast', function(){
            $(this).animate({ opacity: 0 }, 8000, function() {
            $(this).remove();
          });
        });

    });
});



