﻿/*
* Copyright (C) 2009-2012 Solmead Productions
*
* == BEGIN LICENSE ==
*
* Licensed under the terms of any of the following licenses at your
* choice:
*
*  - GNU General Public License Version 2 or later (the "GPL")
*    http://www.gnu.org/licenses/gpl.html
*
*  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
*    http://www.gnu.org/licenses/lgpl.html
*
*  - Mozilla Public License Version 1.1 or later (the "MPL")
*    http://www.mozilla.org/MPL/MPL-1.1.html
*
* == END LICENSE ==
*/
Namespace.Register("System.Lightcast");
/*
var _st = window.setTimeout;

window.setTimeout = function(fRef, mDelay) {
    if (typeof fRef == "function") {
        var argu = Array.prototype.slice.call(arguments, 2);
        var f = (function() { fRef.apply(null, argu); });
        return _st(f, mDelay);
    }
    return _st(fRef, mDelay);
}
*/
System.Lightcast = {
    PlayVideo: function (ID, LS_EmbedCode, LS_Video_ID, LS_Client_ID, LS_Base, Autoplay) {
        var Area = $("#" + ID);
        if (!(System.IsIphone())) {
            var Width = $(Area).width() + 0;
            var Height = $(Area).height() + 0;

            var AP = 1;
            if (!Autoplay) {
                AP = 0;
            }

            $(Area).html('<object  width="' + Width + '" height="' + Height + '" ><param name="movie" value="http://' + LS_Base + '/embed/' + LS_EmbedCode + '"></param><param name="allowFullScreen" value="true"></param><param name="Flashvars" value="autoStart=1&playertriggers=2"></param><param name="wmode" value="transparent"></param><embed src="http://' + LS_Base + '/embed/' + LS_EmbedCode + '" type="application/x-shockwave-flash" allowFullScreen="true"  width="' + Width + '" height="' + Height + '" FlashVars="autoStart=' + AP + '&playertriggers=2" wmode="transparent"></embed></object>');
            $(Area).unbind('click');
            $(Area).click(function () {
                return false;
            });
        }
    },
    PlayLiveVideo: function (ID, pHeight, FlashVars, ConsoleType, URL, Client_ID, Console_ID, SecondsIntoVideo) {
        var Area = $("#" + ID);
        var Width = $(Area).width() + 0;
        var Height = $(Area).height() + 0;
        System.DebugWrite("Video Size=<" + Width + ", " + Height + ">");
        var obe_top = "<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http:\/\/fpdownload.macromedia.com\/pub\/shockwave\/cabs\/flash\/swflash.cab#version=\" width=\"" + Width + "\" height=\"" + (Height + pHeight) + "\" id=\"player\" align=\"middle\" VIEWASTEXT>\n\t\t<param name=\"allowScriptAccess\" value=\"\" \/>\n\t\t<param name=\"movie\" value=\"";
        var obe_middle = "\" \/>\n\t\t<param name=\"quality\" value=\"best\" \/>\n\t\t<param name=\"bgcolor\" value=\"#ffffff\" \/>\n\t\t<param name=\"allowFullScreen\" value=\"true\"></param>\n\t\t<param name=\"FlashVars\" value=\"" + FlashVars + "\"></param>\n\t\t<embed src=\"";
        var obe_bottom = "\" quality=\"best\" FlashVars=\"" + FlashVars + "\" bgcolor=\"#ffffff\" width=\"" + Width + "\" height=\"" + (Height + pHeight) + "\" name=\"player\" align=\"middle\" allowScriptAccess=\"\"  allowFullScreen=\"true\" type=\"application\/x-shockwave-flash\" pluginspage=\"http:\/\/www.macromedia.com\/go\/getflashplayer\" \/>\n\t<\/object>";
        if (ConsoleType && (ConsoleType == 4 || ConsoleType == 5)) {

            if (ConsoleType != 5) {
                URL += '%26sec%3D' + Math.abs(SecondsIntoVideo);
            }
        }
        var Area = $("#" + ID);
        if ((System.IsIphone() || System.IsIpad()) && ($(Area).length>0)) {
            parent.location.replace("http://customers.lightcastmedia.com/cupertinoconsole/" + Client_ID + "/" + Console_ID + ".m3u8");
            return true;
        }

        $(Area).html(obe_top + URL + obe_middle + URL + obe_bottom);
    },
    PlayLiveVideoWithCall: function (ID, Client_ID, Console_ID, Video_ID, SecondsIntoVideo) {
        var IPAddress = "192.168.1.1"
        //customers.lightcastmedia.com/console_js.php?u=3246&c=705930016&dataformat=json&ip=216.68.121.20&vid=38256
        var post_url = "http://" + "customers.lightcastmedia.com/console_js.php?u=" + Client_ID + "&c=" + Console_ID + "&dataformat=json&ip=" + IPAddress + "&vid=" + Video_ID
        $.getJSON(post_url, function (data) {
            if (data.length == 0) {
                System.DebugWrite("Error on get LightCastInfo");
            } else {
                System.DebugWrite("Showing Lightcast Video");
                var ConsoleType = 0;
                if (data.ctype == "P") {
                    ConsoleType = 3;
                }
                if (data.ctype == "L") {
                    ConsoleType = 4;
                }
                if (data.ctype == "a") {
                    ConsoleType = 2;
                }
                if (data.ctype == "S") {
                    ConsoleType = 1;
                }
                if (data.ctype == "F") {
                    ConsoleType = 5;
                }
                System.Lightcast.PlayLiveVideo(ID, data.pheight, data.flashvars, ConsoleType, data.url, Client_ID, Console_ID, SecondsIntoVideo);
            }
        });
    }
}



