﻿//based on YUI namespace javascript code
if (typeof PFP == "undefined" || !PFP)
{
    var PFP = {};
}
PFP.namespace = function ()
{
    var a = arguments, o = null, i, j, d;
    for (i = 0; i < a.length; i = i + 1)
    {
        d = ("" + a[i]).split(".");
        o = PFP;
        for (j = (d[0] == "PFP") ? 1 : 0; j < d.length; j = j + 1)
        {
            o[d[j]] = o[d[j]] || {};
            o = o[d[j]];
        }
    }
    return o;
};

// avoid conflicts with MooTools
// $j will now be used instead of $
var $j = jQuery.noConflict();

// This is required by the map SWF
var startPledgeProcess;

// Create PFP namespace
if (typeof (PFP) == "undefined") {
    var PFP = {};
}

// Create a namespace for map functions and vars
if (typeof (PFP.ChallengeMap) == "undefined") {
    PFP.ChallengeMap = {
        mapLocation: "swf/Shell.swf",
        mapContainer: "flashmap",
        mapWidth: "714",
        mapHeight: "318",
        flashVersionMin: "9.0.28",
        expressInstallSwf: "swf/expressInstall.swf",
        flashvars: {
            applicationPath: "swf/Main.swf"
        },
        params: {
            allowScriptAccess: "always",
            wmode: "transparent",
            menu: "false"
        },
        attributes: {},
        embedMap: function () {
            swfobject.embedSWF(this.mapLocation, this.mapContainer, this.mapWidth, this.mapHeight, this.flashVersionMin, this.expressInstallSwf, this.flashvars, this.params, this.attributes);
        }
    }
}

// Adds watermark to textboxes
function addWaterMark(textbox, watermarktext) {
    $j(textbox).addClass('watermark').val(watermarktext);
    $j(textbox).focus(function () {
        $j(this).filter(function () {
            return $j(this).val() == "" || $j(this).val() == watermarktext;
        }).removeClass('watermark').val('');
    });
    $j(textbox).blur(function () {
        $j(this).filter(function () {
            return $j(this).val() == '';
        }).addClass('watermark').val(watermarktext);
    });
}

$j(document).ready(function () {    
    // Fix png transparancy
    $j(document).pngFix();

    // Setup main nav hover events
    $j('.topnav > ul > li').hover(
                function (e) {
                    $j(this).children('ul:first').css('display', 'block');
                },
                function (e) {
                    $j(this).children('ul:first').css('display', 'none');
                }
            );

    $j('#tabs').tabs();
    $j('#tabs2').tabs();
    
    // Pledge Process dialog
    $j('.start-pledge').click(function (e) {
        e.preventDefault();
        var horizontalPadding = 0;
        var verticalPadding = 0;
        $j('<iframe id="externalSite" class="externalSite" src="/pledgeprocess.aspx" frameborder="0" scrolling="no" />').dialog({
            autoOpen: true,
            width: 850,
            height: 800,
            modal: true,
            bigframe: true,
            dialogClass: "pledge-dialog-iframe",
            resizable: false,
            autoResize: false,
            overlay: {
                opacity: 0.5,
                background: "black"
            }
        }).width(950 - horizontalPadding).height(500 - verticalPadding);
    });
    
    $j('.team-pledge').click(function (e) {
        e.preventDefault();
        var $this = $j(this);
        var team = '/pledgeprocess.aspx?t=' + this.id
        var horizontalPadding = 0;
        var verticalPadding = 0;
        $j('<iframe id="externalSite" class="externalSite" src="' + team + '" frameborder="0" scrolling="no" />').dialog({
            autoOpen: true,
            width: 850,
            height: 600,
            modal: true,
            bigframe: true,
            dialogClass: "pledge-dialog-iframe",
            resizable: false,
            autoResize: false,
            overlay: {
                opacity: 0.5,
                background: "black"
            }
        }).width(950 - horizontalPadding).height(450 - verticalPadding);
    });

    // Already pledged
    $j('#has-pledged-dialog').dialog({
        autoOpen: false,
        modal: true,
        width: 400,
        bgiframe: true,
        title: "You have already pledeged!",
        dialogClass: "signin-dialog",
        buttons: {
            "Ok": function () {
                $j(this).dialog("close");
            },
            "Cancel": function () {
                $j(this).dialog("close");

            }
        }
    });
    $j('.has-pledged').click(function () { $j('#has-pledged-dialog').dialog('open'); return false; });
    
    // Program details dialog
    $j('#program-details').dialog({
        autoOpen: false,
        modal: true,
        width: 600,
        title: "Program Details",
        dialogClass: "program-details",
        buttons: {
            "Ok": function () {
                $j(this).dialog("close");
            },
            "Cancel": function () {
                $j(this).dialog("close");

            }
        }
    });
    
    // Respond to program details button click
    $j('#dialog_link').click(function () { $j('#program-details').dialog('open'); return false; });
    
    // Setup challenge map
    if ($j('#' + PFP.ChallengeMap.mapContainer).length < 1) return;
    
    PFP.ChallengeMap.embedMap();
    
    if ($j('.no-flash-content').length > 0)
        $j('.no-flash-content').show();
    
});

