var Kyte = {};
Kyte.path = "http://www.kyte.tv";
Kyte.lastEmbedIndex = 0;
Kyte.getEmbedHtml = function(src,width,height,params,id) {
    var paramString = "";
    if(params==null) params = {};
    params.allowScriptAccess = "always";
    params.movie = src;
    //custom
    params.wmode = "opaque";
    function htmlattrencode(str) { return str.replace(/"/g,"&quot;").replace(/&/g,"&amp;"); }
    for(var key in params) paramString += '<param name="'+htmlattrencode(key)+'" value="'+htmlattrencode(params[key])+'"/>';
    return '<object '+(id==null?"":('id="'+id+'" '))+'type="application/x-shockwave-flash" style="display:block;margin:0" width="'+width+'" height="'+height+'" data="'+src+'">'+paramString+'</object>';
};


Kyte.Player = function(uri,auxVars,id) { // Aux vars can be a query string, or hash map.
    ++Kyte.lastEmbedIndex;

    var width = 425, height = 425;
    var path = Kyte.path+"/flash.swf?appKey=MarbachViewerEmbedded&uri="+uri;
    var params = {};
    var flashVarsText = "jsembed=true";
    if(auxVars!=null) {
        if(typeof auxVars=="string")
          flashVarsText += "&" + auxVars;
        else
          for(var key in auxVars) flashVarsText += "&" + key + "=" + auxVars[key];

        if(flashVarsText.indexOf("layoutMode=wide")!=-1) width = 850;
        if(flashVarsText.indexOf("layoutMode=tall")!=-1) height = 850;
        if(flashVarsText.indexOf("layoutMode=branded")!=-1) height = 500;
    }
    params.flashVars = flashVarsText;
    if(id==null) id = "KytePlayer_"+(Kyte.lastEmbedIndex);
    var me = this;

    document.write(Kyte.getEmbedHtml(path,width,height,params,id));
    me.element = document.getElementById(id);

    me.execute = function(methodname,param,retries) {
        var result = {value:undefined,success:false,error:undefined};
        try {
            result.value = (param == undefined) ? me.element[methodname]() : me.element[methodname](param) ;
            result.success = true;
        }
        catch (ex) {
            result.error = ex.message;
            if(retries && retries-- > 0)
                setTimeout( function(){me.execute(methodname,param,retries);},1000 );
        }
        return result;
    };

    me.sleep = function() { return me.execute("sleep").success; };
    me.wakeup = function() { return me.execute("wakeup").success; };
    me.enable = function() { return me.execute("enable").success; };
    me.disable = function() { return me.execute("disable").success; };
    me.flag = function() { return me.execute("flagCurrentShow").success; };

    me.setURI = function(uri) { return me.execute("setURI",uri).value; };
    me.signOut = function() { return me.execute("signOut").value; };

    me._syncURIretryID = 0;
    me.cancelSync = function() {
        if(me._syncURIretryID!=0) {
            clearTimeout(me._syncURIretryID);
            me._syncURIretryID = 0;
        }
    };
    me.syncURI = function() {
        me.cancelSync();
        if (me.element != null && !me.setURI(Marbach.URI.getValue()))
            me._syncURIretryID = setTimeout(me.syncURI,1000);
    };
    me.chatPaneOpen = function(open) { if(open==null) open=true; return me.execute("chatPaneOpen",open,5).value; };
    me.feedPaneOpen = function(open) { if(open==null) open=true; return me.execute("feedPaneOpen",open,5).value; };
    me.creatorPaneOpen = function(open) { if(open==null) open=true; return me.execute("creatorPaneOpen",open,5).value; };
    me.menuOpen = function(open) { if(open==null) open=true; return me.execute("menuOpen",open,5).value; };
};
