﻿var _devFBKey = "3915917daa63ce419de2818875db4d4e";
var _prodFBKey = "7c6486ad6ef8ccebbb4def5c8e0a676f";

var _colors = {'Ash': 'DCDEE4', 'Athletic Heather': 'ACAAB4', 'Azalea': 'FF78D1', 'Black': '000000', 'Candy Pink': 'DC7E9C', 'Cardinal': '76042D', 'Colonial Blue': '6CAEE4', 'Daisy': 'F9D320', 'Dark Chocolate': '462A19', 'Dark Green': '042E2C', 'Dark Heather Grey': '4C4A4C', 'Dill Green': '8CAE7C', 'Dusty Brown': '84765C', 'Fiery Red': 'CC2A3C', 'Forest Green': '2C4E3C', 'Gold': 'F7B016', 'Indigo Blue': '3C4866', 'Jade Green': '04966C', 'Kelly Green': '04865C', 'Kiwi': '8AA140', 'Light Blue': 'C4D8F6', 'Light Pink': 'FDD3E2', 'Lime': 'C2FF6E', 'Maroon': '6C1039', 'Medium Grey': '8C868C', 'Military Green': '554D2E', 'Natural': 'FFF2DB', 'Orange': 'FF6600', 'Pistachio': 'A4B674', 'Purple': '510090', 'Red': 'AC1223', 'Sand': 'CCC6AD', 'Sangria': 'DC2A7C', 'Sapphire': '0D69BE', 'Sky': 'A6CCD4', 'Spruce': '24524C', 'Steel Blue': '3C5264', 'Stonewashed Blue': '8CA2B4', 'Stonewashed Green': 'A4A69C', 'Tangerine': 'FFA64D', 'Texas Orange': 'C46E2C', 'Turquoise': '4CB6DC', 'Ultramarine Blue': '446AAC', 'Violet': '8371C4', 'Yellow Haze': 'FFE58C'};

$(document).ready(function() {
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);

    if ("https:" != document.location.protocol) {
        // init Facebook
        FB_RequireFeatures(["XFBML"], function() {
            FB.Facebook.init(fbAPIKey(), "http://" + window.location.hostname + "/xd_receiver.htm");
            FB.Facebook.get_sessionState().waitUntilReady(function() { });
        });
    }
    $('.main-nav ul a').css({ fontWeight: "normal" });
    $('.main-nav ul li:nth-child(4)').find('a').css({ fontSize: "17px", position: "absolute", top: "18px", lineHeight: "16px" }).text("Current Leagues & Schedules");

    // Navigation highlights
    // Using the current path (e.g. /Section/Page)
    var path = location.pathname;

    if (path.indexOf("/YMCA") >= 0) {
        $("#header .sponsor-1").hide();
    }

    // Handle links to the current path
    if (path) {
        $('.main-nav a[href$="' + path + '"]:first').parents().filter('li').addClass('active');
    }

    // Handle links to parts of the current path
    var parts = path.split('/')
    for (i = 0; i < parts.length; i++) {
        if (parts[i].length > 0) {
            $('.main-nav a[href$="/' + parts[i] + '"]:first').parents().filter('li').addClass('active');
            break;
        }
    }

    SetupFlyout(1, "Sports");
    SetupFlyout(2, "Register");
    SetupFlyout(3, "Games");
    SetupFlyout(4, "Socialize");

    $("#sub-nav").append('<li><a href="http://delawaresportsleague.us1.list-manage1.com/subscribe?u=d4f2bdfc94160e2b0a04180ce&id=fb7bc6298c">Subscribe to Newsletter</a></li>');
    $("#sub-nav").append('<li><a href="/Account">Manage Your Account</a></li>');

    $("a[href*='eventbrite.com']").each(function() {
        $(this).colorbox({
            iframe: true,
            opacity: 0.5,
            initialWidth: 100,
            initialHeight: 100,
            width: $(window).width() - 30,
            height: $(window).height() - 30
        });
    });

    //$("#footer").hide();

    $("input, select", "#signup").focus(function() {
        $(".row", "#signup").removeClass("active");
        $(this).parents(".row").addClass("active");
    });

    $("div.dislcaimer", "#signup").click(function() {
        $(".row", "#signup").removeClass("active");
        $(this).parents(".row").addClass("active");
    });

    $("input, select", "#register").live("focus", function() {
        $(".row", "#register").removeClass("active");
        $(this).parents(".row").addClass("active");
    });

    $("input", "#register").live("click", function() {
        $(".row", "#register").removeClass("active");
        $(this).parents(".row").addClass("active");
    });

    var photos = $("#photos");
    var tags = photos.text();
    $("#photos").html("<div class='items'></div>");

    if ("https:" != document.location.protocol) {
        // Get the Flickr photos based on the indicated key
        var flickrAPIkey = "195f91f8d31a112f75cd41ccee3320a9";
        var page = 1; // Math.floor(Math.random()*7)+1;
        $.getJSON("http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=" + flickrAPIkey + "&tag_mode=all&tags=" + tags + "&user_id=23066504@N07&per_page=30&page=" + page + "&format=json&jsoncallback=?", function(rsp) {
            if (rsp.stat != "ok") {
                return;
            }
            var foundPics = false;
            $.each(rsp.photos.photo, function(i, item) {
                var photoURL = 'http://farm' + item.farm + '.static.flickr.com/' + item.server + '/' + item.id + '_' + item.secret + '.jpg'
                $("<img/>").attr({ "src": photoURL }).load(function() {
                    if ($(this).height() > 300) {
                        //$(this).remove();
                        foundPics = true;
                    }
                    else {
                        foundPics = true;
                    }
                }).appendTo("#photos div.items");
            });
        });

        photos.scrollable({ size: 1, loop: true, clickable: true }).autoscroll(5000);
    }

    $("div.br").wrapInner("<div class='content'></div>");
    equalHeight($(".news .content-box .content, .highlights .content-box .content"));
    equalHeight($(".teamslist li"));

    //$(".sidebar h3:eq(3)").css("marginTop", "200px");	
    //$(".sidebar h3:eq(4)").css("marginTop", "50px");

    $("#participants a[rel!='0']").each(function() {
        var fbID = $(this).attr("rel");
        $(this).before("<fb:profile-pic uid='" + fbID + "' width='50' height='50' linked='true' facebook-logo='true' class='fb-pic'/>");
        // $(this).after(" <fb:user-status uid='" + fbID + "' linked='true'/>");
    });
    $("#participants a[rel='0']").each(function() {
        $(this).before("<img class='fb-pic' src='/Site/Images/profile-pic' width='50' height='50'></div>");
    });

    $("#teamslist li h2").each(function() {
        var colorName = $(this).attr("rel");
        try {
            $(this).parents("li").css("border-color", "#" + _colors[colorName]);
        }
        catch (e) { }
    });

    $("#sponsor-logos div.AdSpeed a:not(:has('img'))").hide();

    $(".team-page h1").each(function() {
        var colorName = $(this).attr("rel");
        var colorHex = "#" + _colors[colorName];
        // $(this).css("background-color", colorHex);
        try {
            $(".teammembers").before("<img src='/Media/Images/tshirt?width=100' style='float: right; background: " + colorHex + " ' />");
        }
        catch (e) { }
    });

    $("#form1").submit(function() {
        if (($("#form1 .join-button").length == 1) && (Page_ClientValidate())) {
            $(".join-button").hide().before("<h4>Processing.... Please wait...</h4>");
            $(".error-message").hide();
        }
    });

    ApplyShirtColors();

});

function SetupFlyout(index, name) {
    $(".main-nav ul li:eq(" + index + ") a").hover(
        function() {
            $("#" + name + "-Flyout").show();
        },
        function() {
            $("#" + name + "-Flyout").hide();
        }
    );

    $("#" + name + "-Flyout").hover(
        function() {
            $("#" + name + "-Flyout").show();
        },
        function() {
            $("#" + name + "-Flyout").hide();
        }
    );
}

function EndRequestHandler(){
    ApplyShirtColors();
}

function ApplyShirtColors() {
    $("table[id$='TeamColor'] label").each(function() {
        var colorName = $(this).text();
        if (_colors.hasOwnProperty(colorName)) {
            try {
                $(this).prepend("<span style='border: 1px solid #000000; background: #" + _colors[colorName] + ";'>&nbsp;&nbsp;&nbsp;&nbsp;</span> ");
            }
            catch (e) { }
            //$(this).prev("input").hide();
        }
    });
}

function fbAPIKey() {
	var host = window.location.hostname;
	if (host == "dsl.a-b-c.com") {
		return _devFBKey;
	}
	else {
		return _prodFBKey;
	}
}

function fbLogin() {
	var redirectUrl;
	var QS = null;
	if (QS == null || QS == 'undefined' || QS == "") {
		redirectUrl = "/Login/OpenRegistration";
	}
	else {
		redirectUrl = "/Login/OpenRegistration?ReturnUrl=" + QS;
	}
	window.location = redirectUrl;
	// alert(redirectUrl);
}

function equalHeight(group) {
    var tallest = 0;
    group.each(function() {
        var thisHeight = $(this).height();
        if(thisHeight > tallest) {
            tallest = thisHeight;
        }
    });
    group.height(tallest);
}


