var GymJones = {
    init: function() {
        $(".entity_count").click(function() {
            document.location = $(this).attr("href");
        });
        
        $(".video_play").hover(function() {
            $(this).find(".hover").show();
        }, function() {
            $(this).find(".hover").hide();
        });
        
        $(".video_play").click(function() {
            var href = $(this).attr("href");
            var width = parseInt($(this).attr("data-width"));
            var height = parseInt($(this).attr("data-height")); 
            $("#fullscreen_viewer .content").addClass("video_player");
            $("#fullscreen_viewer .content").attr("href", href);
            $("#fullscreen_viewer .content").css({
                width: width,
                height: height + 120 // flowplayer seems to pad by default
            });
    		
            var image = $(this).find("img");
            // fancy animation
            $("#fullscreen_viewer").css({
                left: image.offset().left,
                top: image.offset().top,
                width: image.width(),
                height: image.height()
            });
            $("#fullscreen_viewer").show();
            $("#fullscreen_background").show();
            $("#fullscreen_viewer").animate({
                left: ($(window).width() / 2) - ((width + 60) / 2),
                top: $(window).scrollTop() + ($(window).height() / 2) - ((height + 100) / 2),
                width: width + 60,
                height: height + 120
            }, 'slow', function() {
        		$f(".video_player", "/media/flash/flowplayer/flowplayer-3.2.7.swf", {
					version: [10, 0],
					clip: {
				        url: href,
				        autoPlay: true,
				        autoBuffering: true
				    }
				});
                GymJones.resizeBackgroundOverlay();
            });
            
            return false;
        });
        
        $("#fullscreen_viewer .close").click(function() {
            $("#fullscreen_viewer,#fullscreen_background").fadeOut();
            return false;
        });
        
        $(".workout").click(function() {
            document.location = $(this).attr("href");
        });
        
        $(".search_content input.search_bar").focusin(function() {
            if($(this).val() == "SEARCH") {
                $(this).val("");
            }
        });
    },
    equalizeColumnHeights: function(selector) {
        var elements = $(selector);
        var tallest = 0;
		elements.each(function(index, element) {
		    if($(element).height() > tallest) {
		        tallest = $(element).height();
		    }
		});
		$(selector).height(tallest);
    }, 
    resizeBackgroundOverlay: function() {
        $("#fullscreen_background").css({
            width: $(window).width(),
            height: $(document).height()
        });
    }, 
    applyWorkoutTileStyle: function() {
        $(".workout").ellipsis();
		$(".entity_count div").fullyJustify();
		$(".entity_count .shrink").css("line-height", "0.8em");
		$(".entity_count .neg_margin").css("margin-bottom", "-7px");
    },
    submitSearch: function() {
        var val = $(".search_content input.search_bar").val();
        if(val == "SEARCH" || val == "") {
            return false;
        }
        return true;
    },
    membershipsRollover: function() {
        $(".memberships_rollover .top").animate({left: 20}, "easeInExpo");
        setTimeout(function() {
            $(".memberships_rollover .middle").animate({left: 18}, "easeInExpo");
        }, 100);
        setTimeout(GymJones.setStatHeights, 500);
    },
    setStatHeights: function() {
     	var maxHeight = $(".graph").height();

    	var biggest = 0;
    	$(".stats .stat .quantity").each(function(index, qty) {
    		if(parseInt($(qty).html()) > biggest) {
    			biggest = parseInt($(qty).html());
    		}
    	});

    	var multiplier = maxHeight / biggest;
    	var colors = ["#e2e3e4", "#c4c4c4", "#7b7c7c", "#5d5e60", "#343536"];

    	$(".stats .stat .quantity").each(function(index, qty) {
    		var bar = $(".stats .stat[data-id='" + $(this).attr("data-id") + "'] .bar");
    		bar.css("background-color", colors.splice(0,1));
    		setTimeout(function() {
    		    GymJones.animateBar(bar, Math.floor(parseInt($(qty).html()) * multiplier));
    		}, index * 200);
    	});
    },
    animateBar: function(bar, height) {
    	bar.animate({
    	    height: height
        }, "easeInExpo");
    }
};

$(document).ready(function() {
    GymJones.init(); 
    
    // Preload images
    $(["/media/images/video_play_button_hover.png"]).preload();
});

$(window).resize(function() {
    $("#fullscreen_viewer div").not(".close").width($(window).width());
    GymJones.resizeBackgroundOverlay();
});

