function showAudio(filepath) {
    var outputHTML = "";
    outputHTML += "<object type=\"application/x-shockwave-flash\" width=\"240\" height=\"20\" data=\"/sites/udlcenter.org/themes/udlcenter/flash/mediaplayer/player.swf?file=" + filepath  + "&autostart=false&fullscreen=false&backcolor=#EEEEEE&\" wmode=\"transparent\" allowScriptAccess=\"always\">";
    outputHTML += "<param name=\"movie\" value=\"/sites/udlcenter.org/themes/udlcenter/flash/mediaplayer/player.swf?file=" + filepath + "&autostart=false&fullscreen=false&backcolor=#EEEEEE\" />";
    outputHTML += "<param name=\"wmode\" value=\"transparent\" />";
    outputHTML += "<param name=\"allowScriptAccess\" value=\"always\" />";
    outputHTML += "<param name=\"allowfullscreen\" value=\"false\" />";
    outputHTML += "</object>";
    // Add new audio item
    document.write(outputHTML);
}

function showVideo(moviepath, imgpath, mywidth, myheight) {
    // Build Flash OBJECT item
    var newHeight = parseInt(myheight) + 20;
    var outputHTML = "";
    outputHTML += "<object type=\"application/x-shockwave-flash\" width=\"" + mywidth + "\" height=\"" + newHeight + "\" data=\"/sites/udlcenter.org/themes/udlcenter/flash/mediaplayer/player.swf?file=" + moviepath + "&autostart=false&fullscreen=true&backcolor=#EEEEEE&image=" + imgpath + "\" wmode=\"transparent\" allowScriptAccess=\"always\" allowfullscreen=\"true\">";
    outputHTML += "<param name=\"movie\" value=\"/sites/udlcenter.org/themes/udlcenter/flash/mediaplayer/player.swf?file=" + moviepath + "&autostart=false&fullscreen=true&backcolor=#EEEEEE&image=" + imgpath + "\" />";
    outputHTML += "<param name=\"wmode\" value=\"transparent\" />";
    outputHTML += "<param name=\"allowScriptAccess\" value=\"always\" />";
    outputHTML += "<param name=\"allowfullscreen\" value=\"true\" />";
    outputHTML += "</object>";
    // Add new video item
    document.write(outputHTML);
}

/*=========================================================*/

function displayVideo(videoId) {
    var moviepath = videoArray[videoId].video;
    var imgpath = videoArray[videoId].thumbnail;
    var mywidth = videoArray[videoId].width;
    var myheight = videoArray[videoId].height;
    var mytitle = videoArray[videoId].title;
    var mydesc = videoArray[videoId].description;

    // Build Flash OBJECT item
    var newHeight = parseInt(myheight) + 20;
    var outputHTML = "";
    outputHTML += "<object type=\"application/x-shockwave-flash\" width=\"" + mywidth + "\" height=\"" + newHeight + "\" data=\"/sites/udlcenter.org/themes/udlcenter/flash/mediaplayer/player.swf?file=" + moviepath + "&autostart=false&fullscreen=true&backcolor=#EEEEEE&image=" + imgpath + "\" wmode=\"transparent\" allowScriptAccess=\"always\" allowfullscreen=\"true\">";
    outputHTML += "<param name=\"movie\" value=\"/sites/udlcenter.org/themes/udlcenter/flash/mediaplayer/player.swf?file=" + moviepath + "&autostart=false&fullscreen=true&backcolor=#EEEEEE&image=" + imgpath + "\" />";
    outputHTML += "<param name=\"wmode\" value=\"transparent\" />";
    outputHTML += "<param name=\"allowScriptAccess\" value=\"always\" />";
    outputHTML += "<param name=\"allowfullscreen\" value=\"true\" />";
    outputHTML += "</object>";

    // Add new video item
    document.getElementById('videoDisplay').innerHTML = outputHTML;
    // Add new video title
    document.getElementById('videoTitle').innerHTML = mytitle;
    // Add new video description if available
    if (document.getElementById('videoDescription') != null) {
        if (mydesc != null) {
            document.getElementById('videoDescription').innerHTML = mydesc;
        }
    }
}

function getVideoJump() {
    var videoID = 0;
    var myHref = self.location.toString();
    //alert(myHref.indexOf("#video"));
    if (myHref.indexOf("#video") >= 0) {
        var tempID = parseInt(myHref.substr(myHref.indexOf("#video") + 6));
        if (isInteger(tempID)) {
            videoID = tempID;
        }
    }

    if ((videoID >= 0) && (videoID < videoArray.length)) {
        return videoID;
    }

    return 0;
}

function displayFeaturedVideos() {
    displayVideo(getVideoJump());

    var outputHTML = "";
    for (var i=0 ; i < videoArray.length ; i++) {
        var imgpath = videoArray[i].thumbnail;
        var mytitle = videoArray[i].title;

        outputHTML += "<li><div class=\"videoItem\"><a href=\"javascript:displayVideo(" + i + ");\"><img src=\"" +  imgpath+ "\" width=\"92\" height=\"68\" alt=\"" + mytitle + "\" title=\"" + mytitle + "\" />" + mytitle + "</a></div></li>";
    }

    // Display video list
    document.getElementById('videoFeatured').innerHTML = outputHTML;
}

function displayPlaylistVideos() {
    displayVideo(getVideoJump());

    var outputHTML = "";
    for (var i=0 ; i < videoArray.length ; i++) {
        var imgpath = videoArray[i].thumbnail;
        var mytitle = videoArray[i].title;

        outputHTML += "<li><div class=\"videoItem\"><a href=\"javascript:displayVideo(" + i + ");\"><img src=\"" +  imgpath+ "\" width=\"92\" height=\"68\" alt=\"" + mytitle + "\" title=\"" + mytitle + "\" /></a></div></li>";
    }

    // Display video list
    document.getElementById('videoPlaylist').innerHTML = outputHTML;
}

function videoObject() {
    this.video = null;
    this.thumbnail = null;
    this.width = null;
    this.height = null;
    this.title = null;
    this.description = null;
}

/*=========================================================*/

function collapseBox() {
    $(".collapseBox").each(function() {
        var boxElm = $(this).get(0);
        // Check to see if this box has already been parsed
        if (jQuery.data(boxElm, "ParsedBox") != true) {
            // Not alread parsed - handlers
            if ($(this).hasClass("open")) {
                $(this).addClass("expOpen");
                $(this).children("div").eq(0).show();
            }
            // Add click binding
            $(this).children().eq(0).bind("click", function(event) {
                toggleChildBox($(this).parent(), event);
            });
            // Add to tab order
            $(this).children().eq(0).attr("tabindex", "0");
            // Add keypress binding
            $(this).children().eq(0).bind("keyup", function(event) {
                var code=event.charCode || event.keyCode;
    		    if(code && code == 13) {// if enter is pressed
                    toggleChildBox($(this).parent(), event);
                }
            });
        }
        // Set parsed flag
        jQuery.data(boxElm, "ParsedBox", true);
    });
}

function toggleChildBox(n, e) {
    $(n).toggleClass("expOpen");
    if ($(n).hasClass("expOpen")) {
        $(n).children("div").eq(0).slideDown();
    } else {
        $(n).children("div").eq(0).slideUp();
    }
    e.stopPropagation();
}

/*=========================================================*/

function jumpState() {
    var jumpVal = $("select#udlcStateSel option:selected").val();
    if (jumpVal != "") {
        window.location.href = "/advocacy/state/" + jumpVal;
    }
    return false;
}

/*=========================================================*/

$(window).ready(function() {
    collapseBox();
    $("#udlcState").submit(function() {
        return jumpState();
    });
});

/*=========================================================*/

function isInteger(s){
    return parseInt(s,10)===s;
}

