﻿Type.registerNamespace("manathanWeb");

manathanWeb.Quicktime = function(movie, movieId, width, height, play, pause, loadingbar, volumebar, style, autoplay) {
    this.movie = movie;
    this.loadingbar = ((loadingbar) ? loadingbar : "loading");
    this.volumebar = ((volumebar) ? volumebar : "volume");
    this.play = ((play) ? play : "play");
    this.pause = ((pause) ? pause : ((play) ? "pause" : null)); // if null and play is null, then id is pause. if null and play ain't null, value is null and play button toggles
    this.movieId = ((movieId) ? movieId : "mwQuicktimeMovie");
    this.width = ((width) ? width : 352);
    this.height = ((height) ? height : 288);
    this.style = ((style) ? style : null);
    this.playing = false;
    this.autoplay = autoplay;
    
    this.movieName = movieId + "_embedded";
    
    this.WriteLog = null;
}

manathanWeb.Quicktime.prototype = {
    GetVersion: function() {
        var agent = navigator.userAgent.toLowerCase(); 
        if (navigator.plugins != null && navigator.plugins.length > 0) {
            for (i=0; i < navigator.plugins.length; i++ ) {
                var plugin =navigator.plugins[i];
                if (plugin.name.indexOf("QuickTime") > -1) {
                    quicktimeVersion = parseFloat(plugin.name.substring(18));
                }
            }
        }
        else if (agent.indexOf("msie") != -1 && parseInt(navigator.appVersion) >= 4 && agent.indexOf("win")!=-1 && agent.indexOf("16bit")==-1) {
            document.write('<scr' + 'ipt language="VBScript"\> \n');
            document.write('on error resume next \n');
            document.write('dim obQuicktime \n');
            document.write('set obQuicktime = CreateObject("QuickTimeCheckObject.QuickTimeCheck.1") \n');
            document.write('if IsObject(obQuicktime) then \n');
            document.write('   if obQuicktime.IsQuickTimeAvailable(0) then \n');
            document.write('      quicktimeVersion = CInt(Hex(obQuicktime.QuickTimeVersion) / 1000000) \n');
            document.write('   end if \n');
            document.write('end if \n');
            document.write('</scr' + 'ipt\> \n');
        }
        else {
            quicktimeVersion = -1;
        }
        return quicktimeVersion;
    },

    SetAutoplay: function(value) {
        this.autoplay = value;
    },

    CreateParam: function(name, value) {
        this.Log('appending param ' + name + ' with value ' + value);
        var param = document.createElement('param');
        this.AppendAttribute(param, "name", name);
        this.AppendAttribute(param, "value", value);
        this.Log('done');
        return param;
    },

    AppendAttribute: function(parent, name, value) {
        try {
            this.Log('appending attribute ' + name + ' with value ' + value);
            var attr = document.createAttribute(name);
            attr.nodeValue = value;
            parent.setAttributeNode(attr);
            this.Log('done');
        } catch (e) {
            this.Log("manathanWeb.Quicktime.AppendAttribute(parent, " + name + ", " + value + ") error - " + e.message);
        }
    },
    
    CreateSlider: function(id) {
        var input = document.createElement('input');
        this.AppendAttribute(input, "id", this.id);
        this.AppendAttribute(input, "class", this.id);
        this.AppendAttribute(input, "type", "text");
        return input;        
    },
    
    CreateButton: function(id, onclickDelegate) {
        var input = document.createElement('input');
        this.AppendAttribute(input, "id", this.id);
        this.AppendAttribute(input, "class", this.id);
        this.AppendAttribute(input, "type", "button");
        $addHandler(input, 'click', onclickDelegate);
        return input;
    },

    Init: function() {
        // assign events
    },
    
    Log: function(message) {
        if (this.WriteLog)
            this.WriteLog(message);        
    },

    CreateGetQuicktime: function(parent) {
        if (!parent) return;
        parent.innerHTML = "";
        var link = document.createElement('a');
        link.href = 'http://www.apple.com/quicktime/download/';
        link.target = '_blank'
        var img = document.createElement('img');
        img.src = '/js/getquicktime.png';
        img.alt = 'get quicktime';
        img.border = '0';
        img.style.display = 'block';
        img.style.margin = '0 auto';
        link.appendChild(img);
        var span = document.createElement('div');
        span.style.textAlign = 'center';
        span.innerHTML = 'Download and install the newest version of Quicktime to view video';
        link.appendChild(span);
        parent.appendChild(link);
    },
    
    CreateVideo: function(parent) {
        if (!parent) {
            alert("manathanWeb.Quicktime.CreateVideo(eventElement) error - Parent control not found...");
            return;
        }
        try {
            parent.innerHTML = "";
            // Create dom-eventobject source
            this.Log('start');
            var domobj = document.createElement('object');
            this.AppendAttribute(domobj, "id", this.movieId + "_events");
            this.AppendAttribute(domobj, "classid", "clsid:CB927D12-4FF7-4a9e-A169-56E4B8A75598");
            this.AppendAttribute(domobj, "codebase", "http://www.apple.com/qtactivex/qtplugin.cab");
            parent.appendChild(domobj);
            this.Log('event registered');
            
            // Create video object
            var obj = document.createElement('object');
            this.AppendAttribute(obj, "id", this.movieId);
            this.AppendAttribute(obj, "width", this.width);
            this.AppendAttribute(obj, "height", this.height);
            this.AppendAttribute(obj, "classid", "clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B");
            this.AppendAttribute(obj, "codebase", "http://www.apple.com/qtactivex/qtplugin.cab");
            this.AppendAttribute(obj, "type", "video/quicktime");
            if (this.style)
                this.AppendAttribute(obj, "style", "behavior:url(#" + this.movieId + "_events);" + this.style);
            else
                this.AppendAttribute(obj, "style", "behavior:url(#" + this.movieId + "_events);");
            this.Log('attributes appended');
                
            obj.appendChild(this.CreateParam("src", this.movie));
            obj.appendChild(this.CreateParam("postdomevents", true));
//            if (Sys.Browser.agent==Sys.Browser.InternetExplorer &&
//				Sys.Browser.version < 8) 
//                obj.appendChild(this.CreateParam("autoplay", true));
//            else
                obj.appendChild(this.CreateParam("autoplay", false));
            obj.appendChild(this.CreateParam("controller", false));
            this.Log('params created');
            
            var embd = document.createElement('embed');
            this.AppendAttribute(embd, "id", this.movieId + "_embedded");
            this.AppendAttribute(embd, "name", this.movieId + "_embedded");
            this.AppendAttribute(embd, "src", this.movie);
            this.AppendAttribute(embd, "width", this.width);
            this.AppendAttribute(embd, "height", this.height);
//            if (Sys.Browser.agent==Sys.Browser.InternetExplorer &&
//				Sys.Browser.version < 8) 
//                this.AppendAttribute(embd, "autoplay", true);
//            else
            this.AppendAttribute(embd, "autoplay", this.autoplay);
            this.AppendAttribute(embd, "controller", false);
            this.AppendAttribute(embd, "EnableJavaScript", true);
            this.AppendAttribute(embd, "postdomevents", true);
            this.AppendAttribute(embd, "pluginspage", "http://www.apple.com/quicktime/download/");
            if (this.style)
                this.AppendAttribute(embd, "style", this.style);
                
            this.Log('adding embedded');
            if (!embd.outerHTML) {
                obj.appendChild(embd);
                this.Log('embedded added');
                parent.appendChild(obj);
            }
            else {
                //parent.appendChild(obj);
                //var em = embd.outerHTML;
                //alert(em);
                //obj.insertAdjacentHTML('beforeEnd', em); //innerHTML += embd.outerHTML;
                this.movieName = this.movieId;
                parent.innerHTML = obj.outerHTML;
            }
            
            this.Log('done');
                        
        } catch (e) {
            alert("manathanWeb.Quicktime.CreateVideo(eventElement) error - " + e.message);
            this.Log("manathanWeb.Quicktime.CreateVideo(eventElement) error - " + e.message);
        }
    }
}

manathanWeb.Quicktime.registerClass('manathanWeb.Quicktime');
