/*extern Pmx */

/**
 * Pelmorex JSON Script Request (JSR) class
 *
 * @fileoverview
 * This is the source file for the JSR class. This class is used for making
 * requests to web services using the dynamic script tag approach. This
 * technique is useful if you need to make requests that are cross-domain. In
 * Ajax, you'd have to set up a proxy. This technique is also cross-browser
 * compatible. However, JSR requires that the web service lets you specify a
 * JavaScript callback function.
 *
 * @author Jim Ing (jing@pelmorex.com)
 */

/**
 * JSON Script Request (JSR) class.
 *
 * @class
 * @constructor
 *
 * @param {string} srcUrl
 *
 * @example
 * url = 'http://webmapvector.weather.ca/RDW?X1=-79.66650868093356&Y1=43.82054307461604&X2=-79.1853075090585&Y2=43.66179689507885&ZL=12&AT=5&CB=Pmx.Map.drawRoads';
 * Pmx._jsr = new Pmx.JSR( url );
 * Pmx._jsr.buildScriptTag();
 * Pmx._jsr.addScriptTag();
 * Pmx.Map.drawRoads = function()
 * {
 *     // do something with the JSON result
 *     Pmx._jsr.removeScriptTag();
 * };
 */
Pmx.JSR = function( srcUrl )
{
    this.srcUrl      = srcUrl;
    this.noCache     = Pmx.Map.jsonReqSep + 'noCache=' + ( new Date() ).getTime() + Pmx.Map.jsonReqSep;
    this.headTagElem = document.getElementsByTagName( 'head' ).item( 0 );
    this.scriptId    = 'PmxMapId' + Pmx.JSR.scriptCounter++;
};

/**
 * @description
 * Add the script tag to the head tag.
 */
Pmx.JSR.prototype.addScriptTag = function()
{
    try
    {
        this.headTagElem.appendChild( this.scriptObj );
    }
    catch( e )
    {
        if( Pmx.debug === true && Pmx.console.error === true )
        {
            console.error( 'addScriptTag: ' + e.description );
        }
    }
};

/**
 * @description
 * Build the script tag.
 */
Pmx.JSR.prototype.buildScriptTag = function()
{
    try
    {
        this.scriptObj = document.createElement( 'script' );

        this.scriptObj.setAttribute( 'type', 'text/javascript' );
        this.scriptObj.setAttribute( 'src', this.srcUrl + this.noCache );
        //this.scriptObj.setAttribute( 'src', this.srcUrl );
        this.scriptObj.setAttribute( 'id', this.scriptId );
    }
    catch( e )
    {
        if( Pmx.debug === true && Pmx.console.error === true )
        {
            console.error( 'buildScriptTag: ' + e.description );
        }
    }
};

/**
 * @description
 * Remove the script tag from the head tag.
 */
Pmx.JSR.prototype.removeScriptTag = function()
{
    // TODO: add logic to remove any previous script tags that weren't removed
    try
    {
        if( this.scriptObj )
        {
            if( document.getElementById( this.scriptObj.id ) )
            {
                this.headTagElem.removeChild( this.scriptObj );
            }
        }
    }
    catch( e )
    {
        if( Pmx.debug === true && Pmx.console.warn === true )
        {
            console.warn( 'removeScriptTag: ' + e.description );
        }
    }
};
