/*
 * Interactive elements on all pages.
 */
 

$(function() {

	// Activate slideshows.
	$(".slideshow").each(function() {
	    $("img:first-child", this).show();
	    var height = $(this).height();
	    $(this).cycle();
	    $(this).height(height);
	});
	
	// Adds the page outline to long pages.
    if ($("body").hasClass("outline")) {
        var contentSecondary = $("div#content-secondary");
        contentSecondary.append($("<h2>Outline</h2>"));
        var outlineList = $("<ol/>");
        $("div#content-primary h2").each(function() {
            var title = $(this).html().replace(/<.+?>/g, "");
            var id = title.replace(/[\s\W]+/gi, "-").replace(/(^-+|-+$)/g, "").toLowerCase();
            $(this).attr("id", id);
            outlineList.append('<li><a href="#' + id + '">' + title + '</a></li>');
        })
        contentSecondary.append(outlineList);
    }
	
	// Embed video players.
	$("a[href*=.mp4], a[href*=.m4v], a[href*=.flv],").html("").addClass("video").flowplayer("/media/site/swf/flowplayer.swf", { 
		clip: {
			autoPlay: false, 
			autoBuffering: true
		}
	});
	
	// Embed audio players.
    $("a[href*=.mp3]").addClass("audio").each(function() {
        var audio_url = $(this).attr("href");
        var generated_id = audio_url.replace(/[^a-zA-Z 0-9]+/g, "_");
        $(this).attr("id", generated_id);
        swfobject.embedSWF(
            "/media/site/swf/playerMini.swf",
            generated_id,
            "75",
            "30",
            "9.0.0",
            "/media/site/swf/expressInstall.swf",
            {
                "autoPlay": "no",
                "soundPath": audio_url,
                "playerSkin": "2",
                "overColor": "#008000"
            }
        );
    });

    // Activate shadowbox.
    if (!$.browser.msie) {
        $("#content-primary, #content-secondary").each(function() {
            var content_area = $(this);
            content_area.find("a:has(img)").each(function() {
                var link = $(this);
                var parts = link.attr("href").split(".");
                if (parts.length > 1) {
                    var extension = parts[parts.length - 1].toLowerCase();
                    if (extension == "jpg" || extension == "jpeg" || extension == "png" || extension == "gif") {
                        link.click(function() {
                            Shadowbox.open({
                                link: link.get(0),
                                content: link.attr("href"),
                                player: "img",
                                title: link.attr("title")
                            });
                            return false;
                        });
                    }
                }
            });
        });
    }
	
});
