﻿/// <reference path="jquery/jquery-1.3.2.min-vsdoc.js" />

$(document).ready(function() {
    wireUpMenus();
    wireUpSearchForm();
    wireUpSponsorshipLinks();
    wireUpAnalyticsLinkage();
    wireUpPopups();
    wireUpContactUs();
    wireUpRegistrationLinks();
});

$(window).load(function() {
    setContentHeight();
})

var puProfiles =
{
    default_opts: {
        height: 450,
        width: 600,
        scrollbars: 1,
        resizable: 0,
        center: 1,
        createnew: 1,
        location: 0,
        statusbar: 1
    },
    
    pdf: {
        height: 600,
        width: 700,
        scrollbars: 1,
        resizable: 1,
        location: 0,
        createnew: 1,
        statusbar: 1
    },
    
    updatesForm : {
        height: 600,
        width: 475,
        scrollbars: 1,
        resizable: 0,
        center: 1,
        createnew: 1,
        location: 0
    },

    registration : {
        height: 650,
        width: 750,
        location: 1,
        menubar: 0,
        resizable: 1,
        statusbar: 1,
        toolbar: 0
    }
}

function getTrackedPageRef(jqLink, defaultPrefix) {
    var trackRef = jqLink.attr('data-tracking');
    if (trackRef == '' || trackRef == null) {
        trackRef = defaultPrefix + jqLink.attr('href');
    }
    if (trackRef.charAt(0) != '/') {
        trackRef = '/' + trackRef;
    }
    return trackRef
}

function wireUpRegistrationLinks() {
    $('a.beginReg').csBeginReg({ readOptsFromLink: true });
}


function wireUpAnalyticsLinkage(){
    $('a').each(function() {
        if (this.hostname != location.hostname) {
            $(this).click(function() {
                var trackTarget = getTrackedPageRef($(this), "/External/");
                $.trackPageview(trackTarget);
                return true;
            });
        }
        if ($(this).hasClass('tracked')) {
            $(this).click(function() {
                var trackTarget = getTrackedPageRef($(this), "/Tracked/");
                $.trackPageview(trackTarget);
                return true;
            });
        }
    });
}

function wireUpPopups() {
    $('a#updatesform').pmPopup({ profileName: 'updatesForm' });
    $('a.pdf').popupwindow({ profileName : 'pdf'});
}


function wireUpSponsorshipLinks() {
    $(".sponsorship_description_link").each(function() {
        var id = $(this).attr("value");
        var thisDescBox = $("#description_box_" + id);
        $(this).click(function() {
            if (thisDescBox.height() > 0) {
                thisDescBox.animate({ height: 0 });
                $("#sdl_" + id).attr("src", $("#sdl_" + id).attr("src").replace("up.png", "down.png"));
            } else {
                var ajaxUrl = $("#printable_" + id).attr("href")
                $("#ajax_content_" + id).load(ajaxUrl, function() {
                    var aniheight = $(this).outerHeight() + 15 + $("#description_box_" + id + " span").height();
                    thisDescBox.animate({ height: aniheight });
                    $("#sdl_" + id).attr("src", $("#sdl_" + id).attr("src").replace("down.png", "up.png"));
                });
            }
            return false;
        });
    });
    
    $(".close_description").each(function() {
        var id = $(this).attr("value");
        var thisDescBox = $("#description_box_" + id);
        $(this).click(function() {
            thisDescBox.animate({height:0});
            $("#sdl_" + id).attr("src", $("#sdl_" + id).attr("src").replace("up.png", "down.png"));
            return false;
        });
    });
}

function wireUpMenus()
{
    var menu = $('#topNavContainer ul');
    if (menu) {
        menu.superfish({autoArrows:'images/pointer.gif'});
    }
    menu = $('#sideNavContainer ul');
    if (menu) {
        menu.superfish();
    }
}

function wireUpSearchForm() {
    var target = $('#s');
    target.focus(function() {
        if ($(this).val() == 'Search . . . ') {
            $(this).val('');
        }
        $(this).addClass('active');
    });
    target.blur(function() {
        if ($(this).val()=='') {
            $(this).val('Search . . . ');
        }
        $(this).removeClass('active');

    });
}

function setContentHeight() {
    var containerPadding = $('#contentcontainer').css('padding-bottom').split('p')[0]
    var tallestHeight = 0;
    
    if ($('#lc').height() > tallestHeight) {
        tallestHeight = $('#lc').height();
    }
    if ($('#rc').height() > tallestHeight) {
        tallestHeight = $('#rc').height();
    }
    if($('#contentcontainer').height() < tallestHeight) {
        $('#contentcontainer').css('height', $(document).height() - containerPadding);
    }
}

function wireUpContactUs() {
    $('body').append('<div class="jqmWindow" id="cuTarget">Loading . . . </div>');
    var opts = {
        ajax: '@href',
        trigger: "a.contactLink"
    };
    $('#cuTarget').jqm(opts);
}

