﻿/*jwplayer embed script*/
/*global window, theform, alert, document, jwplayer*/

/*
LS Video-Popup v 1.2 
(c)2010,2011 Luther Seminary - Thad Dahlberg (Author)
LS Video-Popup is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Requirements: jQuery, jQuery UI, jwplayer
Documentation: Ask Thad to Write a Wiki
Basic Documentation:    *Grabs all link to an mp4, flv and mp3 file and makes them popup into a dialog window and play. 
*If a class videoInline has been applied to that link it will embed the video inline on the page
without a popup dialog.
*If an image is inside the link the generated icon will not appear for popups or the image will
be used as a poster frame for the video if the link has the class videoInline
*/
var hasFlash = false;
try {
    var fo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash');
    if (fo) hasFlash = true;
} catch (e) {
    if (navigator.mimeTypes["application/x-shockwave-flash"] != undefined) hasFlash = true;
}
if (!hasFlash) {
    //$("head").append('<script type="text/javascript" src="/assets/jwplayer/jwplayer.js"></script>');
}

function uuid() {
    var S4 = function () { return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1); };
    return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
}

function getTiny(tinyURL) {
    var cdomain, oldURL, splitURL, httptest, sufftest, newTiny, ajaxdomain;
	ajaxdomain = document.domain;
	cdomain = ajaxdomain.replace(/dev1\.|dev\.|dev0\.|wwwdev\./, "www.");
	
    oldURL = tinyURL.replace(/dev1\.|dev\.|dev0\.|wwwdev\./, "www.");
    splitURL = oldURL.split("/");
    httptest = /(http|https)/;
    sufftest = /\.com|\.org|\.edu/;
    if (!httptest.test(splitURL[0])) {
        if (sufftest.test(splitURL[0])) {
            oldURL = "http://" + oldURL;
        } else if (oldURL.indexOf('/') === 0) {
            oldURL = "http://" + cdomain + oldURL;
        } else {
            oldURL = "http://" + cdomain + "/" + oldURL;
        }
    }
    $.ajax({
        url: "http://" + ajaxdomain + "/assets/tinyurl/create.aspx?url=" + escape(oldURL),
        type: 'GET',
        async: false,
        success: function (data) {
            newTiny = data;
        }
    });
    return newTiny;
}

function initializeJW(LSAutoPlay, LSLinking, LSembed, LSVDivID, LSVPath, LSVheight, LSVWidth, LSPevImg) {
    var jwswf, LSshareLink, gaAccount, pluginVars, embedLink, LSControlBar = "over";
    if (LSVheight < 50) { LSControlBar = "bottom" }
    LSshareLink = getTiny(window.location.href);
    jwswf = "/assets/jwplayer/player.swf";
    gaAccount = "0000";
    if (window.googleAnalyticAccount) {
        gaAccount = window.googleAnalyticAccount;
    }
    if (LSLinking) {
        //first build embed iframe code
        var cDomain = window.location.hostname.replace(/dev\.|dev1\.|dev0\.|www\.|wwwdev\.|www2\./, "").replace(/\.edu|\.org|\.com|\.net/, "");
        embedLink = "vp=" + LSVPath + "&vs=" + $.trim(LSshareLink) + "&vi=" + LSPevImg + "&vd=" + cDomain;
        embedLink = ("http://www.luthersem.edu/Controls/video_share.aspx?" + embedLink).replace(" ", "");
        embedLink = getTiny(embedLink);
        LSembed = '<iframe src="' + $.trim(embedLink) + '" width="440" height="270" frameborder="0"></iframe>';
        pluginVars = { 'plugins': 'sharing,gapro', 'sharing.link': $.trim(LSshareLink), 'sharing.code': LSembed, 'gapro.accountid': gaAccount }; 
    }
    else { pluginVars = { 'plugins': 'gapro', 'gapro.accountid': gaAccount }; }
    if (LSVDivID) {
        if (hasFlash) {
            var flashvars = { 'file': LSVPath, 'image': LSPevImg, 'autostart': LSAutoPlay, 'controlbar': LSControlBar, 'controlbar.idlehide' : 'true' };
            $.extend(flashvars, pluginVars);
            var params = { allowfullscreen: 'true', allowscriptaccess: 'always' };
            var attributes = { id: 'LS' + LSVDivID, name: 'player1' };
            swfobject.embedSWF(jwswf, LSVDivID, LSVWidth, LSVheight, '9.0.115', 'false', flashvars, params, attributes);
        } else {
            jwplayer(LSVDivID).setup($.extend({
                flashplayer: jwswf, autostart: LSAutoPlay, file: LSVPath, height: LSVheight, width: LSVWidth, image: LSPevImg
            }, pluginVars));
        }
    }
}

function viewVideo(DialogTitle, LSLectTitle, LSSpeaker, LSSpeakerAttr, LSVPath, LSPevImg, LSVheight, LSVWidth, LSVDivID, LSAutoPlay, LSLinking) {
    var LSvideoTitle, cDomain, LSembed, LSshareLink, videoTag;
    LSvideoTitle = "";
    cDomain = window.location.hostname.replace(/dev\.|dev1\.|dev0\.|www\.|wwwdev\./, "").replace(/\.edu|\.org|\.com|\.net/, "");
    LSembed = true;
    LSshareLink = true;
    if ($("#" + LSVDivID).hasClass("videoInline-nolink")) { LSembed = false; LSshareLink = false; }
    if (LSLectTitle) { LSvideoTitle = LSvideoTitle + "<h5 style='margin-top:0px;'><em>&ldquo;" + LSLectTitle + "&rdquo;</em></h5>"; }
    if (LSSpeaker) { LSvideoTitle = LSvideoTitle + "<strong>" + LSSpeaker + "</strong>"; }
    if (LSSpeakerAttr) { LSvideoTitle = LSvideoTitle + "<br/>" + LSSpeakerAttr + "<br/><br/>"; }
    if (!LSPevImg) { LSPevImg = "/assets/jwplayer/" + cDomain + "/poster.jpg"; }
    if (!LSVheight) { LSVheight = 250; }
    if (!LSVWidth) { LSVWidth = 440; }
    if (!LSAutoPlay) { LSAutoPlay = true; }
    if (!LSVDivID) { LSVDivID = uuid();}
    //Build div tag for video to go in
    videoTag = '<div id="' + LSVDivID + '"></div>';
    //InlineVideoPlayer overides popuo and allows dynamic loading into #video player div defined in it
    if ($("#" + LSVDivID).hasClass("videoInline") || $("#" + LSVDivID).hasClass("videoInline-nolink")) {
        //Video inline
        $("#" + LSVDivID).replaceWith(videoTag);
        $("#" + LSVDivID).ready(function () { initializeJW(LSAutoPlay, LSLinking, LSembed, LSVDivID, LSVPath, LSVheight, LSVWidth, LSPevImg); });
    } else {
        //Create pop-up dialog video
        if ($("#vdialog").length) {  $("#vdialog").remove(); }
        $("body").append('<div id="vdialog" style="pading-bottom:15px;display:none;">' + videoTag + '</div>');
        $("#vdialog").dialog({ autoOpen: false, width: 505, height: 'auto', modal: true });
        $('#vdialog').dialog('option', 'title', DialogTitle);
        $('#vdialog').dialog('open');
        $('#vdialog').bind('dialogclose', function (event) { $("#vdialog").remove(); });
        $(".ui-widget-overlay").click(function () { $('#vdialog').dialog('close'); });
        initializeJW(LSAutoPlay, LSLinking, LSembed, LSVDivID, LSVPath, LSVheight, LSVWidth, LSPevImg);
        $("#vdialog").append("<br/>" + decodeURIComponent(LSvideoTitle));
    }
}

$(document).ready(function () {
    $("a[href*='.mp4'], a[href*='.flv'], a[href*='.mov'], a[href*='.m4v'], a[href*='.mp3'], a[href*='.m4a']").each(function (e) {
        var posterFrame = "", DialogTiltle = "", LSLinking = 1, LSVheight = 264, LSVwidth = 440, LSaudio = 0, LSTitle, LSURL;
        LSTitle = encodeURIComponent((this).title);
        LSURL = encodeURIComponent((this).href);
        if ($(this).hasClass("videoInline-nolink") || $(this).hasClass("video-nolink")) {
            LSLinking = false;
        }
        if ($(this).attr("href").indexOf(".mp3") >= 0 || $(this).attr("href").indexOf(".m4a") >= 0) {
            if ($(this).hasClass("videoInline")) {
                $(this).addClass("videoInline-nolink");
            }
            LSaudio = 1;
            LSLinking = false;
            LSVheight = 24;
            DialogTiltle = "Luther Seminary Audio Player";
            if (!$(this).children("img").attr("src")) {
                $(this).addClass('audioLink');
            }
        } else {
            LSVheight = 248;
            DialogTiltle = "Luther Seminary Video Player";
            if (!$(this).children("img").attr("src")) {
                $(this).addClass('videoLink');
            }
        }

        if (hasFlash) {
            if (($(this).hasClass("videoInline") || $(this).hasClass("videoInline-nolink"))) {
                if ($(this).children("img").attr("src") && !LSaudio) {
                    posterFrame = $(this).children("img").attr("src");
                    $(this).children("img").load(function () {
                        if ($(this).children("img").attr("height") > 24) {
                            LSVheight = $(this).children("img").attr("height") + 24;
                            LSVwidth = $(this).children("img").attr("width");
                        }
                    });
                }
                if (!$(this).attr("id")) {
                    $(this).attr("id", uuid());
                }
                viewVideo(DialogTiltle, "", LSTitle, "", LSURL, posterFrame, LSVheight, LSVwidth, $(this).attr("id"), "false", LSLinking);
            } else {
                $(this).attr("id", uuid());
                $(this).attr("title", LSTitle);
                $(this).click(function (e) {
                    LSVheight = 250;
                    e.preventDefault();
                    var eURL = $(this).attr("href");
                    if (eURL.indexOf(".mp3") >= 0 || eURL.indexOf(".m4a") >= 0) {
                        LSVheight = 24;
                        LSLinking = 0;
                    }
                    viewVideo('Media Player', $(this).attr("title"), '', '', eURL, null, LSVheight, "440", null, 1, LSLinking);
                });
            }
        }

    });
    return false;
});
