
package
import flash.external.*;
import flash.utils.*;
import mx.core.Application;

{
	public class QueryString
	{
		  private var _queryStringFromUrl:String;
    private var _paramsFromUrl:Object;
    private var _noOfParamsFromUrl:uint;
 
    private var _noOfParamsFromSwfObject:uint;
    private var _paramsFromSwfObject:Object;
 
    private var _params:Object;
    private var _noOfParams:uint;
 
    /**
     * getters and setters for the general array containing both
     * params from URL and from SWF Object
     **/
    public function get parameters():Object
    {
        return _params;
    }
    public function get noOfParams():uint
    {
        return _noOfParams;
    }	
 
    public function QueryString()
    {
        readQueryString();
        getParamsFromSwf();
        combineParams();
    }
 
    /**
     * method used to read parameters from SWF Object
     **/
    private function getParamsFromSwf():void
    {
        _paramsFromSwfObject = mx.core.Application.application.parameters;
    }
 
    /**
     * method used to read URL query string
     **/
    private function readQueryString():void
    {
        _paramsFromUrl = {};
        try
        {
            _queryStringFromUrl = ExternalInterface.call("window.location.search.substring", 1);
            if(_queryStringFromUrl)
            {
                var params:Array = _queryStringFromUrl.split('&');
                var length:uint = params.length;
                _noOfParamsFromUrl = length;
 
                for (var i:uint=0,index:int=-1; i<length; i++)
                {
                    var kvPair:String = params[i];
                    if ((index = kvPair.indexOf("=")) > 0)
                    {
                        var key:String = kvPair.substring(0,index);
                        var value:String = kvPair.substring(index+1);
                        _paramsFromUrl[key] = value;
                    }
                }
            }
        }
        catch(e:Error)
        {
            trace("Some error occured. ExternalInterface doesn't work in Standalone player.");
        }
    }
 
    /**
     * method used to combine the 2 associative arrays
     **/
    private function combineParams():void
    {
        /*
         * Remove the duplicates from url because the params from SWF Object has priority
         * Put the new associative array in the newParamsFromUrl
         */
        var newParamsFromUrl:Object = new Object();
 
        for (var tmpC1:* in _paramsFromUrl)
        {
            for (var tmpC2:* in _paramsFromSwfObject)
            {
                if(tmpC1 == tmpC2)
                {
                    // do nothing - pass over to eliminate the duplicate
                } else {
                    newParamsFromUrl[tmpC1] = _paramsFromUrl[tmpC1];
                }
            }
        }
 
        /*
         * Combine the _paramsFromSwfObject with newParamsFromUrl
         * No duplicates from this point on
         * The new array will be accesible throug _params variable and it's getter
         */
        _params = _paramsFromSwfObject;
        for (var tmpC3:* in newParamsFromUrl)
        {
            _params[tmpC3] = newParamsFromUrl[tmpC3];
        }
 
        /*
         * Count the number of parameters
         */
        _noOfParams=0;
 
        for (var tmpC4:* in _params)
        {
            _noOfParams++;
        }
    }

	}
}
