var SelectedButton = 0;
var Blogposition = 0;
var BlogElementWidth = 225;

//Global Declorations 
var resourcesFolder = "/resources";

//Add Event on load
window.addEvent('domready', function()
{
    AddHooks();
});

function AddHooks()
{
    $('BlogNextLink').addEvent("click", function()
    {
        Blogposition++;

        // Hide Button If Needed
        if (Blogposition == $$(".BlogPost").length - 1)
        {
            $('BlogNextLink').setStyle("opacity", 0);
        }

        // Show Back Button If Needed
        if (Blogposition > 0)
        {
            $('BlogBackLink').setStyle("opacity", 1);
        }

        $('BlogInnerMover').morph({ "left": -BlogElementWidth * Blogposition });
    });

    $('BlogBackLink').setStyle("opacity", 0);

    $('BlogBackLink').addEvent("click", function()
    {
        Blogposition--;
        
         // Hide Button If Needed
        if (Blogposition == 0)
        {
            $('BlogBackLink').setStyle("opacity", 0);
        }

        // Show Back Button If Needed
        if (Blogposition < $$(".BlogPost").length - 1)
        {
            $('BlogNextLink').setStyle("opacity", 1);
        }
        
        
        $('BlogInnerMover').morph({ "left": -BlogElementWidth * Blogposition });
    });
    

    $$("#homeCenterLinks li").each(function(e, i, a)
    {
        if (i == 0)
        {
            e.setStyle("background-position", "bottom left");
        }

        e.addEvent("click", function()
        {
            // Move Frame -960 * i
            SelectedButton = i;
            $("HomePageMover").morph({ "left": -960 * i });
            
            SortSelectedButton(null);
        });

        e.addEvent("mouseenter", function()
        {
            SortSelectedButton(e);
            e.setStyle("background-position", "bottom left");
        });

        e.addEvent("mouseleave", function()
        {
            SortSelectedButton();
        });
    });

    $("HomePageMooSchoolSearchButton").addEvent("click", function()
    {
        DoSchoolSearch();
    });

    $("HomePageMooTeamSearchButton").addEvent("click", function()
    {
        DoTeamSearch();
    });

    $('HomePageMooSchoolSearch').addEvent("keyup", function()
    {
        if ($('HomePageMooSchoolSearch').getProperty("value").length > 1)
        {
            DoSchoolSearch();
        }
        else
        {
            //Morph out the box
            $('SchoolSearchResults').morph({ "opacity": 0 });
            $('ResultsNoMatchSchool').setStyle("opacity", "0");
        }
    });

    $('HomePageMooTeamSearch').addEvent("keyup", function()
    {
        if ($('HomePageMooTeamSearch').getProperty("value").length > 1)
        {
            DoTeamSearch();
        }
        else
        {
            //Morph out the box
            $('TeamSearchResults').morph({ "opacity": 0 });
            $('ResultsNoMatchTeam').setStyle("opacity", "0");
        }
    });

    //Hide No Match Messages
    $('ResultsNoMatchTeam').setStyle("opacity", "0");
    $('ResultsNoMatchSchool').setStyle("opacity", "0");
    
    $('SearchPositionerSchoolsBackground').setStyle("opacity", "0.8");
    $('SearchPositionerTeamsBackground').setStyle("opacity", "0.8");
    $('SearchPositionerPersonalBackground').setStyle("opacity", "0.8");

    //Hide stuff ie7 displays 
    $('SchoolSearchResults').setStyle("opacity", "0");
    $('TeamSearchResults').setStyle("opacity", "0");

    //Setup mouseover and click visual handlers for 3 top buttons fomr home page moo
    $('PersonalPlayerLayerIcon').addEvent("click", function()
    {
        document.location = "/personal/";
    });


    var x = document.location.toString();
    
    // Check for ?team=true
    if (x.indexOf("/?team=true", 0) > -1)
    {
        $("HomePageMover").morph({ "left": -960 * 2 });
    }
    

    // Check for ?school=true
    if (x.indexOf("/?school=true", 0) > -1)
    {
        $("HomePageMover").morph({ "left": -960 * 1 });
    }

    //Sifr
    sIFR.replaceElement(".ContainerBackground h1", named({
        sFlashSrc: "/resources/flash/Collage.swf",
        sColor: "#ffffff", sWmode: "transparent"
    }));

    sIFR.replaceElement(".ContainerBackground h2", named({
        sFlashSrc: "/resources/flash/Collage.swf",
        sColor: "#ffffff", sWmode: "transparent"
    }));
}

function SortSelectedButton(ElementToHighlight)
{
    $$("#homeCenterLinks li").each(function(e, i, a)
    {

        if (SelectedButton == i)
        {
            e.setStyle("background-position", "bottom left");
        }
        else if (ElementToHighlight)
        {
            if (ElementToHighlight == e)
            {
                ElementToHighlight.setStyle("background-position", "bottom left");
            }
        }
        else
        {
            e.setStyle("background-position", "top left");
        }
    });
}

function DisplaySchoolResults(MatchingTeamsResult)
{
    $('ResultsNoMatchSchool').setStyle("opacity", "0"); 
    
    $('SchoolSearchResults').getChildren().each(function(e, i, a)
    {
        e.destroy();
    });

    if (MatchingTeamsResult.TeamShort.length > 0)
    {
        $('ResultsNoMatchSchool').setStyle("opacity", "0");
        
        
        $('SchoolSearchResults').getChildren().each(function(e, i, a)
        {
            e.destroy();
        });

        $('SchoolSearchResults').morph({ "opacity": 1 });

        MatchingTeamsResult.TeamShort.each(function(e, i, a)
        {
            // Add Divs to SchoolSearchResults
            var ResultDiv = new Element("div", {
                "html": e.TeamName,
                "class": "HomePageMooSearchResult",
                "events": {
                    "click": function()
                    {
                        document.location = "/schools/" + e.TeamURL;
                    }
                }
            })

            ResultDiv.addEvent("mouseenter", function()
            {
                this.setStyle("background-color", "#d5d5d5");
            });

            ResultDiv.addEvent("mouseleave", function()
            {
                this.setStyle("background-color", "#ffffff");
            });

            ResultDiv.inject($('SchoolSearchResults'));
        });
    }
    else
    {
        //alert("No Results!");
        $('ResultsNoMatchSchool').setStyle("opacity", "0.99");
    }
}

function DisplayTeamResults(MatchingTeamsResult)
{

    $('ResultsNoMatchTeam').setStyle("opacity", "0");

    $('TeamSearchResults').getChildren().each(function(e, i, a)
    {
        e.destroy();
    });

    if (MatchingTeamsResult.TeamShort.length > 0)
    {
        $('ResultsNoMatchTeam').setStyle("opacity", "0");

        $('TeamSearchResults').getChildren().each(function(e, i, a)
        {
            e.destroy();
        });

        $('TeamSearchResults').morph({ "opacity": 1 });

        MatchingTeamsResult.TeamShort.each(function(e, i, a)
        {
            // Add Divs to SchoolSearchResults
            var ResultDiv = new Element("div", {
                "html": e.TeamName,
                "class": "HomePageMooSearchResult",
                "events": {
                    "click": function()
                    {
                        document.location = "/teams/" + e.TeamURL;
                    }
                }
            })
            ResultDiv.inject($('TeamSearchResults'));
        });
    }
    else
    {
        //alert("No Results!");
        $('ResultsNoMatchTeam').setStyle("opacity", "0.99");
    }
}

function DoSchoolSearch()
{
    var jsonRequest = new Request.JSON({
        url: "/GetMatchingSchools/",
        onSuccess: function(MatchingTeamsResult)
        {
            DisplaySchoolResults(MatchingTeamsResult);
        }
    }).get({ 'q': $("HomePageMooSchoolSearch").getProperty("value") });
}

function DoTeamSearch()
{
    var jsonRequest = new Request.JSON({
        url: "/GetMatchingTeams/",
        onSuccess: function(MatchingTeamsResult)
        {
            DisplayTeamResults(MatchingTeamsResult);
        }
    }).get({ 'q': $("HomePageMooTeamSearch").getProperty("value") });
}

