/**
* @classDescription Script object for animating IVJ
* @param input_script string script
* @param input_settings object settings
*/
function ivj_script(input_script,input_settings)
{
    try
    {
        this.settings = {
            'default_delay':5000,
            'repeat':1
        }
        this.locations = [];
        this.pointer = 0;
        this.repeats = 0;
        this.paused = false;

        //setup
        if(input_script) this.locations = input_script.split('|');
        if(input_settings) this.settings = $.extend(this.settings,input_settings);
    }
    catch(err)
    {
        alert(err);
    }
}

/**
 *@method
 *Adds a location to the script queue
 *
 *@param location string location (semicolon delimited)
 *@returns void
 */
ivj_script.prototype.add_location = function(location)
{
    this.locations.push(location);
};

/**
 *@method
 *Removes all items from the script queue
 *
 *@returns void;
 */
ivj_script.prototype.clear = function()
{
    this.locations = [];
};

/**
 *@method
 *Resets the script counters (start the script over)
 *
 *@returns void
 */
ivj_script.prototype.reset = function()
{
    this.pointer = 0;
    this.repeats = 0;
}

/**
 *@method
 *Plays the current script
 *
 *@param instance_id string id of the inteleview instance to play on
 *@param resume boolean indicator to override "paused" state
 *@returns void
 */
ivj_script.prototype.play = function(instance_id,resume)
{
    $.log("Play Script");
    if(resume) this.paused = false;
    try
    {
        if(!instance_id) throw("Script must play on an instance!");
        var next = this.get_next(instance_id);
        if(next!==false)
        {
            //perform actions
            if(typeof next.location != 'undefined')
            {
                ivj.instances[instance_id].go_to(next.location);
            }
            if(typeof next.overlay != 'undefined')
            {
                //[id];[name];[url];[screen-x];[screen-y];[opacity]
                //screen_overlay = function(id,name,description,url,x,y,opacity,corner_radius)
                var p = next.overlay.split(';');
                if(typeof p[0]!='undefined'&&typeof p[1]!='undefined'&&typeof p[2]!='undefined')
                {
                    if(typeof p[5]!='undefined')
                        ivj.instances[instance_id].screen_overlay(p[0],p[1],'',p[2],p[3],p[4],p[5]);
                    else if(typeof p[4]!='undefined')
                        ivj.instances[instance_id].screen_overlay(p[0],p[1],'',p[2],p[3],p[4]);
                    else if(typeof p[3]!='undefined')
                        ivj.instances[instance_id].screen_overlay(p[0],p[1],'',p[2],p[3]);
                    else
                        ivj.instances[instance_id].screen_overlay(p[0],p[1],'',p[2]);
                }
            }
            if(typeof next.enableLayer != 'undefined')
            {
                ivj.instances[instance_id].set_layer_visibility(next.enableLayer,true);
            }
            if(typeof next.disableLayer != 'undefined')
            {
                ivj.instances[instance_id].set_layer_visibility(next.disableLayer,false);
            }
            if(typeof next.fullscreen != 'undefined')
            {
                $.log('fullscreen!');
                ivj.instances[instance_id].appletFullscreen(next.fullscreen);
            }


            //setup next call
            thisObj = this;
            setTimeout(function() {
                thisObj.play.call(thisObj,instance_id);
            }, parseInt(next.delay));
        }
    }
    catch(err)
    {
        alert(err);
    }
};

/**
 *@method
 *Get the next location from the script
 *
 *@caller play()
 *@param instance_id string id of the inteleview instance the script is being played on
 *@returns object
 */
ivj_script.prototype.get_next = function(instance_id)
{
    if(!this.paused)
    {
        if(this.locations.length > 0)
        {
            //if we have run out our welcome, leave.
            if(this.pointer>this.settings.repeat*this.locations.length) return false;

            //otherwise, set our location
            var loc = this.locations[this.pointer%this.locations.length];
            var retval = {};
            var start = 0;

            //special case for old style scripts
            if(loc.indexOf('Coordinates;')===0)
            {
                retval['location'] = this.locations[this.pointer];
                retval['delay'] = this.settings.default_delay;
                this.pointer++;
            }
            else
            {
                //we loop now because we may have multiple instructions / lines
                for (var i=0;i<=10;i++)
                {
                    loc = this.locations[this.pointer%this.locations.length].split('::');
                    $.log(this.pointer+' - '+loc);
                    if(loc.length==3)
                    {
                        //numbers, people
                        loc[0] = parseFloat(loc[0]);

                        //handle timing
                        if(i == 0)
                        {
                            start = loc[0];
                        }
                        else if (loc[0] != start)
                        {
                            if(loc[0] > start)
                            {
                                retval['delay'] = (loc[0]-start)*1000;
                            }
                            else if(loc[0] < start)
                            {
                                retval['delay'] = loc[0]*1000;
                            }
                            break;
                        }

                        //add the item to the return value
                        retval[loc[1]] = loc[2];
                    }
                    //increment the pointer
                    this.pointer++;
                }
            }
            return retval;
        }
        else return false;
    }
    else return false;
}

/**
 *@method
 *Pause the script immediately.
 *
 *@returns void
 */
ivj_script.prototype.pause = function(instance_id)
{
    ivj.instances[instance_id].go_to(ivj.instances[instance_id].get_coordinates());
    this.paused = true;
};

/**
 *@static propetry
 *Default scripts
 */
ivj_script.default_scripts = {
    'orbit':new ivj_script(),
    'timeline':new ivj_script()
};

/**
 *@Method Override
 *Change the get_next method to calculate the next position instead of fetching
 *it from the queue
 *
 *@param instance_id
 *@returns object
 */
ivj_script.default_scripts.orbit.get_next = function(instance_id)
{
    if(!this.paused)
    {
        var params = ivj.instances[instance_id].get_coordinates().split(';');
        params[1]=parseFloat(params[1]);
        params[2]=parseFloat(params[2]);
        params[2] += 36;
        params[1] = params[1]/1.2;

        return {
            'location':params.join(';'),
            'delay':5000
        };
    }
    else return false;
}
