var gameCurClass = "game-current";
var gameObj = { "games" : [
    {
    "name":"Air Drop",
    "sourceURL":"http://cdn.racing.ups.com/wp-content/themes/upsracing/games/Loader_airdrop.swf",
    "thumbUrl":"http://cdn.racing.ups.com/wp-content/themes/upsracing/images/games/airdropGameBtn.jpg",
    "thumbAlt":"thumbnail rollover text",
    "gameID":"1000002",
    "base":"http://www.racing.ups.com/wp-content/themes/upsracing/games/"
    },
    {
    "name":"Laserscape",
    "sourceURL":"http://cdn.racing.ups.com/wp-content/themes/upsracing/games/Loader_laserscape.swf",
    "thumbUrl":"http://cdn.racing.ups.com/wp-content/themes/upsracing/images/games/laserscapeGameBtn.jpg",
    "thumbAlt":"thumbnail rollover text",
    "gameID":"1000001",
    "base":"http://www.racing.ups.com/wp-content/themes/upsracing/games/"
    },
    {
    "name":"Rock Paper Scissors",
    "sourceURL":"http://cdn.racing.ups.com/wp-content/themes/upsracing/games/Loader_rockpaperscissors.swf",
    "thumbUrl":"http://cdn.racing.ups.com/wp-content/themes/upsracing/images/games/rockpaperscissorsGameBtn.jpg",
    "thumbAlt":"thumbnail rollover text",
    "gameID":"1000003",
    "base":"http://cdn.racing.ups.com/wp-content/themes/upsracing/games/"
    },
]};

jQuery(function($){
    var viewPortId     = '#sidebar-accordion-list-wrapper';
    var contentId      = '#sidebar-accordion-list';
    var scrollButton   = '#sidebar-content .scroll-button';
    var viewportHeight = $(viewPortId).height();
    var hash = location.hash.replace("#","");
    
    if(hash == '') {
        gameCurrent(0);
		updateFlashvarsGuid(gameObj.games[0].gameID);
		updateParamsBaseURL(gameObj.games[0].base);
        upsRacingLoadRatingControl(gameObj.games[0].gameID);
        gameLoad(gameObj.games[0].sourceURL,flashvars, params, attributes);
    }
    
    $('.game-thumb').each(function (i) {
        if(hash == gameObj.games[i].gameID) {
            $('a', this).addClass(gameCurClass);
            $('#flash-gallery-title').text(gameObj.games[i].name);
			updateFlashvarsGuid(gameObj.games[i].gameID);
			updateParamsBaseURL(gameObj.games[i].base);
            upsRacingLoadRatingControl(gameObj.games[i].gameID);
            gameLoad(gameObj.games[i].sourceURL,flashvars, params, attributes);
        }
        $('a', this).css('background-image',"url('" + gameObj.games[i].thumbUrl + "')");
        
        $('a', this).click(function () { 
            $('#flash-gallery-title').text(gameObj.games[i].name);
            gameCurrent(i);
    		updateFlashvarsGuid(gameObj.games[i].gameID);
			updateParamsBaseURL(gameObj.games[i].base);
            upsRacingLoadRatingControl(gameObj.games[i].gameID);
            gameLoad(gameObj.games[i].sourceURL,flashvars, params, attributes);
        });
        
    });    
    
    $(scrollButton).hover(function(){
        $(this).addClass('on-hover');
    },function(){
        $(this).removeClass('on-hover');
    });
    $(scrollButton).click(function(){
        var contentHeight = $(contentId).height();
        if ( contentHeight > viewportHeight) {
            var scrollIncrement = Math.round(viewportHeight);
            var top = getContentTop();
            switch (buttonDirection(this)) {
                case "up":         if (canScroll(this,"up"))     top = String(top+scrollIncrement);
                    break;
                case "down":  if (canScroll(this,"down")) top = String(top-scrollIncrement);
                    break;
            }
            setContentTop(top+"px");
        }
    });
    $("#sidebar-accordion-nav div.album-header").click(function(){
        var all_headers = $("#sidebar-accordion-nav div.album-header");
        all_headers.css("cursor","wait");
        var all_items = $("#sidebar-accordion-nav .album-photosets");
        all_items.removeClass('selected');

        var groupID = this.id.replace("album-header-","")
        var these_items = $("#sidebar-accordion-nav #photosets-of-album-" + groupID)
        these_items.show();
        if (!these_items.hasClass("loaded")) {
            var url = "http://www.racing.ups.com/wp-content/themes/upsracing/upsracing-gallery-items.php?group_name=album&group_id=" + groupID;
            var html = jQuery.ajax({url:url,async:false}).responseText;
            these_items.html(html);
            these_items.addClass("loaded");
        }
        setScrollable();
        all_items.css("display","none")
        these_items.show("fast").addClass("selected");
        all_headers.css("cursor","pointer");
        this.style.cursor= "default";
    });    function buttonDirection(button) {
        return button.id.replace("sidebar-accordion-nav-","");
    }
    function getContentTop() {
        var top = Number($(contentId).css("top").replace(/[a-z]+/i,""));
        return top;
    }
    function setContentTop(top) {
        $(contentId).animate({top:top},375,function(){
            setScrollable();
        });
    }
    function setScrollable() {
        $(scrollButton).each(function(){
            switch (buttonDirection(this)) {
                case "up":
                    if (canScroll(this,"up"))
                        $(this).addClass('scrollable');
                    else
                        $(this).removeClass('scrollable');
                    break;
                case "down":
                    if (canScroll(this,"down"))
                        $(this).addClass('scrollable');
                    else
                        $(this).removeClass('scrollable');
                    break;
            }
        });
    }
    function canScroll(button,direction) {
        var contentHeight = $(contentId).height();
        var scrollable;
        switch (direction) {
            case "up":
                scrollable = (getContentTop()<0);
                break;
            case "down":
                scrollable = (Math.abs(getContentTop())<(contentHeight-viewportHeight));
                break;
            default:
                scrollable = false;
        }
        return scrollable;
    }
    
    function gameCurrent(gi) {
        $('.game-thumb').each(function (i) {
            if(gi == i) {
                $('a', this).addClass(gameCurClass);
            } else {
                $('a', this).removeClass(gameCurClass);
            }
        });
    }
    setScrollable();
});

function gameLoad(gameFile,flashvars, params, attributes) {
    /*
    var obj = swfobject.getObjectById("gameWindow");
    var video = "/games/"+gameFile;
    var link = "";
    obj.setVideoData(video,link);
    */
    swfobject.embedSWF(gameFile, "gameWindow", width="640", height="450", "9.0.0", "expressInstall.swf", flashvars, params, attributes);
};