(function($){
    // $("ul").gctabs(options)
    // options = { onshow : function(tab) {}, oninit : function() {}, selectEvent : "click" | "mouseover" | "hoverIntent", defaultTab: n | tabid }
    
    $.fn.gctabs = function(options) {
        if ( typeof(options) == "string" ) {
            switch(options) {
                case "currenttabid" :
                    var c = $(this).find(".current");
                    return ( c.length > 0 ) ? c.attr("id") : null;
                    break;
            }
            return null;
        } else {
            options = $.extend( {
                onbeforeshow : function(tab) {},
                onshow : function(tab) {},
                oninit : function() {},
                selectEvent: "click",
                defaultTab : 0
            }, options);
            
            var tabs = this;
            var pod = tabs.parent("*");
            
            function selectTab(tabid) {
                if ( !tabs.find("#" + tabid).is(".current") ) {

                    tabs.find(".current").removeClass("current");
                    tabs.find("#" + tabid).addClass("current");

                    pod.find(".tabPanel:not(." + tabid + ")").hide();

                    var thisTab = pod.find(".tabPanel." + tabid);

                    if (typeof(options["onbeforeshow"]) == "function") {
                        options["onbeforeshow"](thisTab.get()[0]);
                    }
                    
                    thisTab.show();

                    if (typeof(options["onshow"]) == "function") {
                        options["onshow"](thisTab.get()[0]);
                    }
                
                }
            }

            function init() {
                if (typeof(options["oninit"]) == "function") {
                    options["oninit"]();
                }
                var dt = options["defaultTab"];
                if ( typeof(dt) != "string" ) {
                    dt = tabs.find("li:nth(" + dt + ")").attr("id");
                }
                
                tabs.find(".current").removeClass("current");
                
                selectTab(dt);
            }
            
            tabs.find("li").each(function(){
                if ( options["selectEvent"] == "hoverIntent" ) {
                    $(this).hoverIntent(function() { selectTab(this.id) }, function() {});
                } else if (options["selectEvent"] == "click" ) {
                    $(this).bind(options["selectEvent"],function(event) {
                        event.preventDefault();
                        selectTab(this.id);
                    });
                } else {
                    $(this).bind(options["selectEvent"],function() { selectTab(this.id); });
                }
            });
            
            $(init);
            
        }
    }
})(jQuery);

(function($) {
    var pod;

    function filter(tabid, filterValue) {

        try {
            // no filter value so show first item set
            if (filterValue == null)
                filterValue = pod.find(".tabPanel." + tabid + " .articles li:first").attr("class").replace(tabid, "").replace(" ", "");

            pod.find(".articles li:visible").hide();
            pod.find(".articles li.last-visible").removeClass("last-visible");
            pod.find(".articles li." + tabid + "." + filterValue + ":lt(4)").show();
            pod.find(".articles li:visible:last").addClass("last-visible");
        } catch (e) {

        }
    }
    function goButtonClick() {
        var tabid = pod.find(".tabs .current").attr("id");
        filter(tabid, pod.find("select:visible").val());
    }

    $(function() {
        $("#newsAndEvents .tabs").gctabs({
            defaultTab: "news",
            oninit: function() {
                pod = $("#newsAndEvents");

                pod.find("form").submit(function() { return false; });
                pod.find("input:button").click(goButtonClick);
            },
            onshow: function(tab) {
                goButtonClick();
                //filter(tab.id, $(tab).find("select").val());
            }
        });

        $("#newsFilter, #eventFilter").each(function() {
            var tabPanel = $(this).parents(".tabPanel");

            $(this).find("option").each(function() {
                if (tabPanel.find(".articles ." + this.value).length == 0) {
                    $(this).remove();
                }
            });

            if ($("#newsAndEvents .tabs .current").attr("id") == $(this).attr("id")) {
                if (this.options[this.selectedIndex].value != "featured") {
                    var tabId = tabPanel.get()[0].className.replace(/tabPanel /, "");
                    filter(tabId, this.options[this.selectedIndex].value);
                }
            }
        });
    });
})(jQuery);

$(function() {
    var vidPanelsOpacity = 0.9;

    $("#newMP .tabs").gctabs({
        selectEvent: "click",
        onbeforeshow : function(tab) {
            $("#newMP .vidLinks").hide();
            $("#newMP .vidLinks li.current").removeClass("current");
            $("#newMP .hearfs").hide();
        },
        onshow : function(tab) {
            $("#newMP #hearfswrapper").hide();
            $("#newMP .hearfs a.player").html("&nbsp;"); // stop any video that might be playing
            $("#newMP .hearfs").hide();
            $("#newMP .tabPanel:visible .bgholder").show();
        
            var div = $(tab).find(".vidLinks");

            // push out of view before showing
            div.animate( { right: (-1 * parseInt(div.outerWidth())) + "px", opacity: vidPanelsOpacity }, 0).show();
            // then slide in from right
            div.animate ( { right: "0px" }, 700);
        }
    });
    
    // remove videos if more than 3
    $("#newMP .vidLinks").each(function(){
        if ( $(this).find("li").length > 3 ) {
            var notpicked = $(this).find("li:not(.pick)");
            for ( var i = 0; i < 3; i++ ) {
        	    var randomNumber = Math.floor(Math.random()*notpicked.length);
        	    $(notpicked.get()[randomNumber]).addClass("pick");
                notpicked = $(this).find("li:not(.pick)");
            }
            notpicked.remove();
            $(this).find("li").removeClass("pick"); //.each(function(i){ $(this).addClass("vid" + i); });
        }
    });
    
    $("#newMP .vidLinks li, #newMP .closeVideo").click(function(event){
        event.preventDefault();

        if (!$(this).is(".current")) {
            var bgholder = $("." + $("#newMP .tabs").gctabs("currenttabid")).find(".bgholder");
            var previousVid = $("#newMP .vidLinks li.current");
            var current = $(this);
            var vidurl = current.find("a").attr("href");
            var h2text = current.find("h3").text() + ": ";
            var ptext = current.find("p").text().replace("Env.","Environmental");
            var hearfs = $("#newMP .hearfs");
            var player = hearfs.find(".player");
            var hearfswrapper = $("#newMP #hearfswrapper");
            var right = hearfs.find(".right");
            var playCloseVideoAnimation = true;

            hearfswrapper.show();
            bgholder.show();
            previousVid.removeClass("current");
            
            if ($.browser.msie) {
                current.css({"background-position": "4px 50%"});
                if ( parseInt($.browser.version) == 7) {
                    hearfswrapper.css({position:"relative"});
                    
                    playCloseVideoAnimation = false;
                    
                    // calculate offset based on IE7 zoom level
                    var rect = document.body.getBoundingClientRect(); 
                    var zoomLevel = Math.round( (rect.right - rect.left) / document.body.clientWidth * 100 );
                    var ml = {
                        75 : "94em", 100 : "-70.6em", 125 : "-56.5em", 150: "-47.05em",
                        keys : [75,100,125,150], min: 75, max: 150
                    };
                    var zml = ml[zoomLevel];
                    if ( typeof(zml) == "undefined") {
                        if ( zoomLevel < ml.min ) {
                            zml = ml[ml.min];
                        } else if ( zoomLevel > ml.max ) {
                            zml = ml[ml.max];
                        } else {
                            var first = ml.min, last = ml.max;
                            for ( var k = 0; k < ml.keys.length; k++ ) {
                                if ( zoomLevel < ml.keys[k] ) {
                                    first = ml.keys[k-1];
                                    last = ml.keys[k];
                                    break;
                                }
                            }
                            var lower = parseFloat(ml[first]);
                            var upper = parseFloat(ml[last]);
                            
                            zml = (lower - ((zoomLevel-first)/(last-first)) * (lower - upper)) + "em";
                        }
                    }
                    hearfswrapper.css("margin-left",zml);
                }
            } else if ($.browser.safari) {
                if (player.css) {
                    player.css({"margin-bottom":"2.09em"});
                }
            }

            hearfs.css({display: "block", width: "100em"});
            right.css({ opacity: vidPanelsOpacity });

            player.find("object,embed").hide();
            
            right.animate({
                marginLeft: "48em"
            }, ( playCloseVideoAnimation && (previousVid.length > 0) ? 700 : 0)
            ,function(){
                if ( current.is(".closeVideo") ) {
                    hearfs.hide();
                    hearfswrapper.hide();
                } else {
                    current.addClass("current");

                    hearfs.find(".right h2").text(h2text);
                    hearfs.find(".right p").text(" " + ptext);
                    player.html("<div id='home_videoPlayer'>&nbsp;</div>");

                    right.animate({
                        marginLeft: "0em"
                    },700, function() {

                        var videoID = vidurl.substring(vidurl.lastIndexOf("/") + 1);
                        var flashvars = { vid: videoID, altURL: "/media/video/" };
                        var params = {wmode: "transparent"};
                        var attributes = {};
                        swfobject.embedSWF("/media/videoPlayer.swf", "home_videoPlayer", "440", "280", "9.0.0", "expressInstall.swf", flashvars, params, attributes);

                        bgholder.hide();
                    });
                    hearfswrapper.css({position:"absolute"});
                    hearfs.animate({
                        width: "49em"
                    },800);
                }
            });
        }
        
        return false;
    });

    $("#newMP .vidLinks li a").click(function(event) {
        event.preventDefault();
    });
    
    if ( $.browser.msie ) {
        // workaround for Flash/MSIE # in title bug / adapted from http://groups.google.com/group/swfobject/browse_thread/thread/cb9db7324f8ab382?pli=1
        var oldtitle = document.title;
        var docpropchange = function(){
            if ( event.propertyName == "title" ) {
                document.onpropertychange = function(){};
                document.title = oldtitle;
                document.onpropertychange = docpropchange;
            }
        }
        document.onpropertychange = docpropchange;
    }
});

function stopAnim() {
    $(".right, .hearfs").stop(true,false);
}
