GTV.prototype._name = "gtv_object";

function GTV(target) {
    this._target = target;
    this._version = 'gtv.js 3.0.7';
    var scope = this; //scope hack
    var instance_stamp = new Date().getTime(); //instance id's for local connections
    this._methodQueue = [];
    
    this.getFromMethodQueue = function(fname) {
        if(typeof _methodQueue != "undefined") {
            for(var i=0;i<_methodQueue.length;i++) {
                if(_methodQueue[i].id == fname) {
                    return _methodQueue[i].call;
                }
            }
        }
    }
    
    this.registerMethod = function(fname, fn) {
        if(typeof _methodQueue == "undefined") _methodQueue = new Array();
        for(var i=0;i<_methodQueue.length;i++) {//if it already exists, reassign
            if(_methodQueue[i].id == fname) {
                _methodQueue[i] = {id:fname, call:fn};
                return;
            }
        }
        _methodQueue.push({id:fname, call:fn});
    }
    
    this.timeoutRelay = function(fn,args) {
        if(!args) {
            target.setTimeout(fn,35);
        } else {
            target.setTimeout(function(){fn.apply(this,args)},35);
        }
    }
    
    //---------------------------------------
    // GTV Object Function Relays
    //---------------------------------------
    
    GTV.prototype.onClientInitialized = function (o) {
        var fn = scope.getFromMethodQueue('onClientInitialized');
        if(fn) fn(o);
    }
    GTV.prototype.onChannels = function (arr) {
        var fn = scope.getFromMethodQueue('onChannels');
        if(fn) fn(arr);
    }
    GTV.prototype.setAdBanner = function(url, href) {
        var fn = scope.getFromMethodQueue('setAdBanner');
        if(fn) fn(url, href);
    }
    GTV.prototype.clearAdBanner = function(url, href) {
        var fn = scope.getFromMethodQueue('clearAdBanner');
        if(fn) fn(url, href);
    }
    GTV.prototype.onContentLoaded = function(arr) {
        var fn = scope.getFromMethodQueue('onContentLoaded');
        if(fn) fn(arr);
    }
    GTV.prototype.resize = function() {
        var fn = scope.getFromMethodQueue('resize');
        if(fn) fn();
    }
    GTV.prototype.onPlayStarted = function(o) {
        var fn = scope.getFromMethodQueue('onPlayStarted');
        if(fn) fn(o);
    }
    GTV.prototype.onRecentLevels = function(o) {
        var fn = scope.getFromMethodQueue('onRecentLevels');
        if(fn) fn(o);
    }
    GTV.prototype.onRecentSegments = function(o) {
        var fn = scope.getFromMethodQueue('onRecentSegments');
        if(fn) fn(o);
    }
    GTV.prototype.onUserStatus = function(b,id,pw) {
        var fn = scope.getFromMethodQueue('onUserStatus');
        if(fn) fn(b,id,pw); 
    }
    GTV.prototype.onLoginError = function(id,pw) {
        var fn = scope.getFromMethodQueue('onLoginError');
        if(fn) fn(id,pw);
    }
    GTV.prototype.onAdPlaying = function() {
        var fn = scope.getFromMethodQueue('onAdPlaying');
        if(fn) fn();
    }
    GTV.prototype.onAdEnd = function() {
        var fn = scope.getFromMethodQueue('onAdEnd');
        if(fn) fn();
    }
    GTV.prototype.onDefaultPlaying = function() {
        var fn = scope.getFromMethodQueue('onDefaultPlaying');
        if(fn) fn();
    }
    GTV.prototype.onPlayingSegment = function(o) {
        var fn = scope.getFromMethodQueue('onPlayingSegment');
        if(fn) fn(o);
    }
    GTV.prototype.onNextPlayingSegment = function(o) {
        var fn = scope.getFromMethodQueue('onNextPlayingSegment');
        if(fn) fn(o);
    }
    GTV.prototype.onSegmentEnd = function(o) {
        var fn = scope.getFromMethodQueue('onSegmentEnd');
        if(fn) fn(o);
    }
    GTV.prototype.onStreamTimeout = function(o) {
        var fn = scope.getFromMethodQueue('onStreamTimeout');
        if(fn) fn(o);
    }
    GTV.prototype.onStreamNotFound = function(o) {
        var fn = scope.getFromMethodQueue('onStreamNotFound');
        if(fn) fn(o);
    }
    GTV.prototype.onContentLoadError = function(o) {
        var fn = scope.getFromMethodQueue('onContentLoadError');
        if(fn) fn(o);
    }
    GTV.prototype.onChannelLoadError = function(c) {
        var fn = scope.getFromMethodQueue('onChannelLoadError');
        if(fn) fn(c);
    }
    GTV.prototype.onDeepLinkError = function() {
        var fn = scope.getFromMethodQueue('onDeepLinkError');
        if(fn) fn();
    }
    GTV.prototype.onAncestry = function(arr) {
        var fn = scope.getFromMethodQueue('onAncestry');
        if(fn) fn(arr);
    }
    
    //---------------------------------------
    // Directly Callable Functions Into GTV
    //---------------------------------------
    
    //navigator commands
    GTV.prototype.getChannels = function() {
        this.getEngine().getChannels();
    }
    GTV.prototype.getChannelContent = function(title) {
        this.getEngine().getChannelContent(title);
    }
    GTV.prototype.openContent = function(o,x,y) {
        this.getEngine().openContent(o,x,y);
    }
    GTV.prototype.closeContent = function(o) {
        this.getEngine().closeContent(o);
    }
    GTV.prototype.playContent = function(o) {
        this.getEngine().playContent(o);
    }
    GTV.prototype.goBack = function() {
        this.getEngine().goBack();
    }
    GTV.prototype.getPlayingLevel = function() {
        this.getEngine().getPlayingLevel();
    }
    GTV.prototype.getPlayingSegmentLevel = function() {
        this.getEngine().getPlayingSegmentLevel();
    }
    GTV.prototype.getAncestry = function(o) {
        this.getEngine().getAncestry(o);
    }
    //player commands
    GTV.prototype.getPlayingSegment = function() {
        this.getEngine().getPlayingSegment();
    }
    GTV.prototype.getNextPlayingSegment = function() {
        this.getEngine().getNextPlayingSegment();
    }
    GTV.prototype.setPlayThrough = function(bool) {
        this.getEngine().setPlayThrough(bool);
    }
    GTV.prototype.usePlaylistTime = function(bool) {
        this.getEngine().setPlayThrough(bool);
    }
    GTV.prototype.playDeepLink = function(c,r,p,s,i) {
        this.getEngine().playDeepLink(c,r,p,s,i);
    }
    GTV.prototype.getRecentLevels = function(n) {
        this.getEngine().getRecentLevels(n);
    }
    GTV.prototype.getRecentSegments = function(n) {
        this.getEngine().getRecentSegments(n);
    }
    GTV.prototype.setCustomSegmentList = function(arr, n) {
        this.getEngine().setCustomSegmentList(arr, n);
    }
    GTV.prototype.removeCustomSegmentList = function() {
        this.getEngine().removeCustomSegmentList();
    }
    GTV.prototype.playItemByIndex = function(n) {
        this.getEngine().playItemByIndex(n);
    }
    GTV.prototype.addToPlayQueue = function(o) {
        this.getEngine().addToPlayQueue(o);
    }
    GTV.prototype.removeFromPlayQueue = function(o) {
        this.getEngine().removeFromPlayQueue(o);
    }
    GTV.prototype.setCustomAudio = function(o) {
        this.getEngine().setCustomAudio(o);
    }
    GTV.prototype.removeCustomAudio = function() {
        this.getEngine().removeCustomAudio();
    }
    GTV.prototype.dockControls = function(bool) {
        this.getEngine().dockControls(bool);
    }
    GTV.prototype.pause = function() {
        this.getEngine().pause();
    }
    GTV.prototype.unpause = function() {
        this.getEngine().unpause();
    }
    GTV.prototype.playDefault = function(s) {
        this.getEngine().playDefault(s);
    }
    GTV.prototype.setDefaultFLVChannel = function(s) {
        this.getEngine().setDefaultFLVChannel(s);
    }
    //favorites commands
    GTV.prototype.addToFavorites = function(o) {
        this.getEngine().addToFavorites(o);
    }
    GTV.prototype.removeFromFavorites = function(o) {
        this.getEngine().removeFromFavorites(o);
    }
    GTV.prototype.moveFavoritesSegment = function(o,n) {
        this.getEngine().moveFavoritesSegment(o,n);
    }
    GTV.prototype.addToFavorites = function(id) {
        this.getEngine().addToFavorites(id);
    }
    //login commands
    GTV.prototype.userLogin = function(id,pw) {
        this.getEngine().userLogin(id,pw);
    }
    GTV.prototype.userLogout = function(id,pw) {
        this.getEngine().userLogout();
    }
    GTV.prototype.userGetStatus = function(id,pw) {
        this.getEngine().userGetStatus();
    }
    
    //---------------------------------------
    // ExternalInterface Function Calls
    //---------------------------------------
    target.onClientInitialized__GTV = function(o) {
        scope.timeoutRelay(scope.onClientInitialized,[o]);
    }
    target.onChannels__GTV = function(o) {
        scope.timeoutRelay(scope.onChannels,[o]);
    }
    target.setAdBanner__GTV = function(url,href) {
        scope.timeoutRelay(scope.setAdBanner,[url,href]);
    }
    target.clearAdBanner__GTV = function(url,href) {
        scope.timeoutRelay(scope.clearAdBanner,[url,href]);
    }
    target.onAdPlaying__GTV = function() {
        scope.timeoutRelay(scope.onAdPlaying);
    }
    target.onAdEnd__GTV = function() {
        scope.timeoutRelay(scope.onAdEnd);
    }
    target.onDefaultPlaying__GTV = function() {
        scope.timeoutRelay(scope.onAdEnd);
    }
    target.onContentLoaded__GTV = function(arr) {
        scope.timeoutRelay(scope.onContentLoaded,[arr]);
    }
    target.onPlayStarted__GTV = function(o) {
        scope.timeoutRelay(scope.onPlayStarted,[o]);
    }
    target.resize__GTV = function() {
        scope.timeoutRelay(scope.resize);
    }
    target.onRecentLevels__GTV = function(arr) {
        scope.timeoutRelay(scope.onRecentLevels,[arr]);
    }
    target.onRecentSegments__GTV = function(arr) {
        scope.timeoutRelay(scope.onRecentSegments,[arr]);
    }
    target.onUserStatus__GTV = function(b,id,pw) {
        scope.timeoutRelay(scope.onUserStatus,[b,id,pw]);
    }
    target.onLoginError__GTV = function(id,pw) {
        scope.timeoutRelay(scope.onLoginError,[id,pw]);
    }
    target.onSegmentEnd__GTV = function(o) {
        scope.timeoutRelay(scope.onSegmentEnd,[o]);
    }
    target.onPlayingSegment__GTV = function(o) {
        scope.timeoutRelay(scope.onPlayingSegment,[o]);
    }
    target.onNextPlayingSegment__GTV = function(o) {
        scope.timeoutRelay(scope.onNextPlayingSegment,[o]);
    }
    target.onAncestry__GTV = function(arr) {
        scope.timeoutRelay(scope.onAncestry,[arr]);
    }
    //----- errors ------------------------------
    target.onStreamTimeout__GTV = function(o) {
        scope.timeoutRelay(scope.onStreamTimeout,[o]);
    }
    target.onStreamNotFound__GTV = function(o) {
        scope.timeoutRelay(scope.onStreamNotFound,[o]);
    }
    target.onContentLoadError__GTV = function(o) {
        scope.timeoutRelay(scope.onContentLoadError,[o]);
    }
    target.onChannelLoadError__GTV = function(c) {
        scope.timeoutRelay(scope.onChannelLoadError,[c]);
    }
    target.onDeepLinkError__GTV = function() {
        scope.timeoutRelay(scope.onDeepLinkError);
    }
    
    //---------------------------------------
    // Debugging
    //---------------------------------------
    
    //----- get build ------
    target.onShowVersion__GTV = function(str) {
        scope.timeoutRelay(scope.showVersion, [str]);
    }
    this.showVersion = function(str) {
        var v = 'VERSIONS:';
        v += '\n'+scope._version;
        v += '\n'+str;
        alert(v);
    }
    
    //---------------------------------------
    // Embedding SWFs
    //---------------------------------------

    this.embedDisplay = function(div,swf,cfg,wmd) {
        //todo: add smart cache killer
        var display = (typeof swf != 'undefined' && swf != '' && swf != null) ? swf : 'gdisplay.swf';
        var config = (typeof cfg != 'undefined' && cfg != '' && cfg != null) ? cfg : 'xml/displayconfig.xml';
        var wmode = (typeof wmd != 'undefined' && wmd != '' && wmd != null) ? wmd : 'window';
        var so = new SWFObject(display, 'gdisplay__GTV', '100%', '100%', '8', '#e0e0e0');
        so.addVariable('instance_stamp', instance_stamp);
        so.addVariable('altcfg', config);
        so.addParam('allowScriptAccess', 'always');
        so.addParam('wmode', wmode);
        so.addParam('align','TL');
        so.addParam('scale', 'noScale');
        so.write(div);
    }
    
    this.embedEngine = function(div,swf,cfg,wmd) {
        var engine = (typeof swf != 'undefined' && swf != '' && swf != null) ? swf : 'gengine.swf';
        var config = (typeof cfg != 'undefined' && cfg != '' && cfg != null) ? cfg : 'xml/config.xml';
        var wmode = (typeof wmd != 'undefined' && wmd != '' && wmd != null) ? wmd : 'window';
        var so = new SWFObject(engine, 'gengine__GTV', '2', '2', '8', '#000000');
        so.addVariable('instance_stamp', instance_stamp);
        so.addVariable('altcfg', config);
        so.addVariable('debuglevel', getQueryParamValue('debuglevel'));
        so.addParam('allowScriptAccess', 'always');
        so.addParam('wmode', wmode);
        so.addParam('align','t');
        so.addParam('scale', 'noborder');
        so.write(div);
    }
    
    this.getEngine = function() {
        if(navigator.appName.indexOf('Microsoft')!=-1) {
            return window['gengine__GTV'];
        } else {
            return document['gengine__GTV'];
        }
    }
    
    //---------------------------------------
    // Convenience Processing (syncornous returns)
    //---------------------------------------
    
    this.getDetailByName = function(o,id) {
        if(o.details) {
            var arr = o.details;
            id = id.toLowerCase();
            for(var i=0;i<arr.length;i++) {
                if(arr[i].name) {
                    if(arr[i].name.toLowerCase() == id) {
                        return arr[i].value;
                    }
                }
            }
        }
        return null;
    }
    
}