﻿// James Padolsey jQuery Regex Selector
// http://james.padolsey.com/javascript/regex-selector-for-jquery/
jQuery.expr[':'].regex = function(elem, index, match)
{
    var matchParams = match[3].split(','),
        validLabels = /^((data|css|):|(text))/,
        attr = {
            method: matchParams[0].match(validLabels) ?
                        matchParams[0].split(':')[0] : 'attr',
            property: matchParams.shift().replace(validLabels, '')
        },
        regexFlags = 'ig',
        regex = new RegExp(matchParams.join('').replace(/^\s+|\s+$/g, ''), regexFlags);

    if (attr.property == '')
        attr.property = null;
    
    return regex.test(jQuery(elem)[attr.method](attr.property));
}

function AboutOpen()
{
    $('#about_content').slideDown('normal').queue(function()
    {
        var openHeight = $(this).height() + 'px';
    
        $(this).css('height', openHeight);
        $(this).dequeue();
    });
    $('#about_btn').animate({ top: 2 }).addClass('open');
    $('#profile_blocker').css('height', $('#profiles').height()).show();
}

function AboutClose()
{
    $('#about_content').slideUp('fast');
    $('#about_btn').animate({ top: 0 }, 'fast').removeClass('open');
    $('#profile_blocker').hide();
}

function DirectoryOpen()
{
    if ($('#about_content:visible').length == 1)
    {
        AboutClose();
    }

    $('#directory_search').val('name');
    $('.directory_column > a').removeClass('selected');

    $('#directory_content').slideDown('normal').queue(function()
    {
        var openHeight = $(this).height() + 'px';
    
        $(this).css('height', openHeight);
        $(this).dequeue();
    });
    $('#directory_btn').animate({ top: 2 }).addClass('open');
    $('#profile_blocker').css('height', $('#profiles').height()).show();
}

function DirectoryClose()
{
    $('#directory_content').slideUp('fast');
    $('#directory_btn').animate({ top: 0 }).removeClass('open');
    $('#profile_blocker').hide();
}

function ContactOpen()
{
    if ($('#about_content:visible').length == 1)
    {
        AboutClose();
    }
    if ($('#directory_content:visible').length == 1)
    {
        DirectoryClose();
    }

    $('#contact_content').slideDown('normal').queue(function()
    {
        var openHeight = $(this).height() + 'px';
    
        $(this).css('height', openHeight);
        $(this).dequeue();
    });
    $('#contact_btn').animate({ top: 2 }).addClass('open');
    $('#profile_blocker').css('height', $('#profiles').height()).show();
}

function ContactClose()
{
    $('#contact_content').slideUp('fast').queue(function()
    {
        $('#contact_form').show();
        $('#thanks_message').hide();

        $(this).dequeue();
    });
    $('#contact_btn').animate({ top: 0 }).removeClass('open');
    $('#profile_blocker').hide();
}

function GetOpenProfile()
{
    var open_profile = null;

    $('.profile:has(.profile_content .stage1:visible),.profile:has(.profile_content .stage2:visible)').each(function()
    {
        if ($(this).find('.profile_content .stage1').css('display') != 'none' || $(this).find('.profile_content .stage2').css('display') != 'none')
        {
            open_profile = this;
        }
    });

    return open_profile;
}

function SubmitContactForm()
{
    $.ajax({
        type: 'POST',
        url: $('#contact_form').attr('action'),
        data: $('#contact_form').serialize(),
        dataType: 'json',
        error: function()
        {
            alert('An error has occurred, please try again later.');
        },
        success: function(data)
        {
            $('.field-validation-error').hide();
        
            if (data.success)
            {
                $('#contact_form').hide();
                $('#thanks_message').show();
                
                $('#contact_form input[type!=submit], #contact_form textarea').val('');
                $('#contact_form #Reason').val('Interested in Pledging');
            }
            else
            {
                for (var i in data.errors)
                {
                    $('#' + i + '_error').html(data.errors[i]).show();
                }
            }
        }
    });
}

$(document).ready(function()
{
    $('.profile_content .stage1,.profile_content .stage2').hide();

    $('#about_btn').click(function()
    {
        if ($('#about_content:visible').length == 1)
        {
            AboutClose();
        }
        else
        {
            AboutOpen();

            dcsMultiTrack('DCS.dcsuri', '/MainPage/AboutOpened', 'WT.ti', 'About Opened');
        }

        return false;
    });

    $('#directory_btn').click(function()
    {
        if ($('#directory_content:visible').length == 1)
        {
            DirectoryClose();
        }
        else
        {
            DirectoryOpen();

            dcsMultiTrack('DCS.dcsuri', '/MainPage/DirectoryOpened', 'WT.ti', 'Directory Opened');
        }

        return false;
    });

    $('#contact_btn').click(function()
    {
        if ($('#contact_content:visible').length == 1)
        {
            ContactClose();
        }
        else
        {
            ContactOpen();

            dcsMultiTrack('DCS.dcsuri', '/MainPage/ContactFormOpened', 'WT.ti', 'Contact Form Opened');
        }

        return false;
    });

    $('#directory_search').focus(function()
    {
        if ($(this).val() == 'name')
            $(this).val('');
    })
    .blur(function()
    {
        if ($.trim($(this).val()) == '')
            $(this).val('name');
    })
    .keypress(function()
    {
        var thisObj = this;

        setTimeout(function()
        {
            var search = $(thisObj).val();

            $('.directory_column > a').removeClass('selected').filter(':regex(text,' + search + ')').addClass('selected');
        }, 0);
    });

    $('.profile:has(.stage1)').click(function()
    {
        if ($(this).hasClass('inactive') && $(this).find('.stage1').length == 1)
        {
            var thisID = $(this).attr('id');

            $('.profile:has(.stage1),.profile:has(.stage2)').each(function()
            {
                if ($(this).attr('id') != thisID)
                    Reset(this);
            });

            InactiveToStage1(this);

            var profile_name = $(this).children('a[name]').attr('name');
            dcsMultiTrack('DCS.dcsuri', '/MainPage/ShortProfileOpened/' + profile_name, 'WT.ti', 'Short Profile Opened: ' + profile_name);
        }
    });

    $('.profile_content .stage1 .more_btn').click(function()
    {
        var profile = $('.profile.stage1');

        Stage1ToStage2(profile[0]);

        var profile_name = profile.children('a[name]').attr('name');
        dcsMultiTrack('DCS.dcsuri', '/MainPage/FullProfileOpened/' + profile_name, 'WT.ti', 'Full Profile Opened: ' + profile_name);

        return false;
    });

    $('.profile_close_btn').click(function()
    {
        var profile = $('.profile.stage2');

        Reset(profile[0]);

        return false;
    });

    $('.profile_header').click(function()
    {
        var profile = $(this).parent('.profile');

        if (profile.hasClass('stage1') || profile.hasClass('stage2'))
        {
            Reset(profile[0]);

            return false;
        }
    });

    $('.directory_column a').click(function()
    {
        DirectoryClose();

        var open_profile = GetOpenProfile();
        if (open_profile)
        {
            Reset(open_profile);
        }

        var profile = $(this).attr('href').substr(1);

        var profile_root = $('.profile:has(a[name="' + profile + '"])');
        InactiveToStage1(profile_root);

        dcsMultiTrack('DCS.dcsuri', '/MainPage/DirectoryEntryClicked/' + profile, 'WT.ti', 'Directory Entry Clicked: ' + profile);
    });

    $('#contact_form').submit(function()
    {
        SubmitContactForm();
        return false;
    });

    $('#splash_enter_btn').click(function()
    {
        $('#splash_content').fadeOut();
        $('#container').fadeIn();

        dcsMultiTrack('DCS.dcsuri', '/MainPage/SplashEnterClicked', 'WT.ti', 'Splash Enter Clicked');
    });

    $('#page_header a').click(function()
    {
        if ($('#about_content:visible').length == 1)
        {
            AboutClose();
        }

        if ($('#directory_content:visible').length == 1)
        {
            DirectoryClose();
        }

        if ($('#contact_content:visible').length == 1)
        {
            ContactClose();
        }

        var open_profile = GetOpenProfile();
        Reset(open_profile);

        return false;
    });

    $('.profile_letter_graphic').click(function()
    {
        var profile_name = $(this).parents('.profile').children('a[name]').attr('name');
        dcsMultiTrack('DCS.dcsuri', '/MainPage/HandWrittenLetterOpened/' + profile_name, 'WT.ti', 'Hand Written Letter Opened: ' + profile_name);
    });

    $('.profile_links_link').click(function()
    {
        var profile_name = $(this).parents('.profile').children('a[name]').attr('name');
        var url = $(this).attr('href');
        dcsMultiTrack('DCS.dcsuri', '/MainPage/ProfileExternalLinkClicked/' + profile_name + '/' + url, 'WT.ti', 'Profile External Link Clicked - Profile: ' + profile_name + ' URL: ' + url);
    });

    $('.privacy_policy').click(function()
    {
        $('.blocker').show();
        $('#privacy_dialog').show();

        return false;
    });

    $('.termsofuse').click(function()
    {
        $('.blocker').show();
        $('#termsofuse_dialog').show();

        return false;
    });

    $('.footer_dialog_close_btn').click(function()
    {
        $(this).parents('.footer_dialog').hide();
        $('.blocker').hide();

        return false;
    });

    $('.blocker').click(function()
    {
        $('.footer_dialog').hide();
        $('.blocker').hide();

        return false;
    });

    var url = document.location.toString();
    if (url.match('#'))
    {
        $('#splash_content').hide();
        $('#container').show();
    }
});

function InactiveToStage1(el)
{
    $(el).switchClass('inactive', 'stage1', 0).removeClass('hover');

    $(el).find('.profile_content .stage1').slideDown().queue(function()
    {
        window.location = '#' + $(el).find('a[name]').attr('name');
    
        $(this).dequeue();
    });
}

function Stage1ToStage2(el)
{
    $(el).switchClass('stage1', 'stage2', 0);
    $(el).find('.profile_content .stage1').hide().end().find('.profile_content .stage2').slideDown().queue(function()
    {
        window.location = '#' + $(el).find('a[name]').attr('name');
    
        $(this).dequeue();
    });
}

function Reset(el, post_reset_func)
{
    var fader = null;

    if ($(el).hasClass('stage2'))
    {
        fader = $(el).find('.profile_content .stage2');
    }
    else if ($(el).hasClass('stage1'))
    {
        fader = $(el).find('.profile_content .stage1');
    }

    if (fader)
    {
        fader.slideUp('fast').queue(function()
        {
            if (post_reset_func)
                post_reset_func();

            $(el).removeClass('stage1 stage2').addClass('inactive');

            $(this).dequeue();
        });
    }
    else
    {
        if (post_reset_func)
            post_reset_func();

        $(el).removeClass('stage1 stage2').addClass('inactive');
    }
}

