﻿
$(function() {
/*
    $('.nav-block > ul > li, .nav-block > ul > li > ul > li').hover(
        function() {
            $(this).addClass('active');
        },
        function() {
            $(this).removeClass('active');
        });
        */
    $.tablesorter.addParser({
        id: 'mydate',
        is: function(s) {
            return false
        },
        format: function(s) {
            alert(s);
            return new Date(s);
        },
        type: 'date'
    });
    
    $('.advanced-search').click(function() {
        $('#map').hide();
        $('.search-holder').show();
        $(this).hide();
        
        return false;
    });
    
    $('.video-gallery .play-overlay')
        .css('cursor', 'pointer')
        .click(function() {
            $(this).parent().find('a:eq(0)').click();
            return false;
        });
        
    $('.threeboxes .content .play-overlay')
        .css('cursor', 'pointer')
        .click(function() {
            $(this).closest('.content').find('a:eq(0)').click();
            return false;
        });
        
    $('.submit-btn').click(function() {
        $(this).closest('form').submit();
        return false;
    });
    
    var originalImage = $('.show-box .image img').attr('src');

    $('.show-box li a')
        .click(function() { return false; })
        .hover(function() {
            $(this)
                .addClass('hovered')
                .closest('.show-box')
                .find('.image img')
                .attr('src', $(this).attr('href'));
        },
        function() {
            $(this).removeClass('hovered');
            
            setTimeout(function() {
                if (!$('.show-box li a.hovered').length) {
                    $('.show-box .image img').attr('src', originalImage);        
                }
            }, 200);
        });
        
       
    $('a.new-window').click(function() {
        window.open($(this).attr('href'), '_blank', 'fullscreen=yes');
        return false;
    });
    
    $('.iframe:not(.new-window)').each(function() {
        var md = $(this).metadata();

        $(this).fancybox({
            width: md.width,
            height: md.height
        });
    });
    
    $('.video-gallery li a:not(.new-window)').each(function() {
        if (!$(this).is('.new-window')) {
            var md = $(this).metadata();

            $(this).fancybox({
                width: md.width,
                height: md.height,
                showNavArrows: false
            });
        }
    });
    
    $('.leadership .name').each(function() {
        var anchor = $('<a/>').text($(this).text()).attr('href', '#');
        $(this).replaceWith(anchor);
        var info = anchor.parent().find('.info');
        
        anchor.click(function() {
            if (info.is(':visible')) {
                info.hide();
            } else {
                $('.leadership .info').hide();

                info
                    .css('visibility', 'hidden')
                    .show(1000, function() {
                        $(this)
                            .css('opacity', '0')
                            .css('visibility', 'visible')
                            .animate({'opacity': '1.0'});
                    });
            }
            return false;
        });
    });
        /* Ajax contact */
    $('form.ajaxify').bind('submit', function() {
        ajaxForm($(this));
        return false;
    });

   function ajaxForm(form) {
        var fileUpload = (form.hasClass('file')); 
        
        var data = {};
        var valid = true;
        form.find(':input').each(function() {
            var thisValid = true;
            
            if ($(this).hasClass('ajax-required') && $.trim($(this).val()).length == 0) {
                thisValid=false;
            } else if ($(this).hasClass('ajax-email') && !$(this).val().match('.+?@.+?\\..+?$')) {
                if ($(this).val().length > 0) {
                thisValid=false;
                }
            }
            
            if (!thisValid) {
                var label = $(this).prev('label');
                if (label.length == 0)
                    label = $(this).parent().parent().parent().find('label');

                
                if (!label.data('oldColor')) {
                    label.data('oldColor', label.css('color'));
                }
                
                label
                    .css('color','red');
                
                $(this).focus(function() {
                    label.css('color', label.data('oldColor'));
                });
                
                valid = false;
            }

            data[$(this).attr('name')] = $(this).val();
        });

        if (!valid) {
            return false;
        }

        var doEffect = function() {
        var container = form
            .closest('.form-slide-container');

        var effectContainer = $('<div/>')
            .css('overflow', 'hidden')
            .css('position', 'relative');


        var effectHolder = $('<div/>')
            .css('position', 'relative')
            .append(container.children()).appendTo(effectContainer);

        effectContainer.appendTo(container);

        var confirmContainer = $('<div/>')
            .addClass('form-confirmation')
            .css('position', 'absolute')
            .css('left', (-effectHolder.width()) + 'px')
            .css('width', '100%')
            .css('top', '0px')
            .css('bottom', '0px')
            .css('vertical-align', 'middle')
            .css('line-height', container.height() + 'px')
            .css('text-align', 'center')
            .text(form.attr('title') || 'Thanks for your submission.')
            .appendTo(effectContainer);

        confirmContainer.animate({ left: '0px' }, 1000);

        effectHolder.animate({ left: effectHolder.width() + 'px' }, 1000, function() {
            effectHolder
                .css('left', -effectHolder.width() + 'px');

            setTimeout(function() {
                form.find(':input:not([type=submit]):not(.dont-clear)').val('');
                if ($('#fancy_outer:visible').length > 0) {
                    $.fn.fancybox.close();
                }
                confirmContainer.animate({ left: effectHolder.width() + 'px' }, 1500);
                effectHolder.animate({ left: '0px' }, 1500, function() {
                    effectHolder.children().appendTo(container);
                    effectContainer.remove();
                    effectHolder.remove();
                });
            }, 1600)
        });
        };
        
        if (fileUpload) {
            $.ajaxFileUpload({
                url: form.attr('action') + '?' + form.serialize(),
                fileElementId: 'file',
                success: function() {
                    doEffect();
                }
            });
        } else {
            $.ajax({
                url: form.attr('action'),
                data: data,
                type: 'POST',
                success: function() {
                    doEffect();
                }
            });
        }
        return false;
    }

});










