var swfOptimizer = function() 
{
      /**
       * 
       * 
       */
      var _getWindow = function() 
      {
          try 
          {
              top.document;
              return top;
          } 
          catch (e) 
          {
              return window;
          }
      };
      
      /**
       * 
       * 
       */
      
      this.toString = function() 
      {
          return '[class swfOptimizer]';
      };
     
      /**
       * 
       * 
       */
      var _checkFlash = function(version)
      {
          var rv = version.toString().split('.');
          for (var i = 0; i < 3; i++)
              rv[i] = typeof rv[i] != UNDEFINED ? parseInt(rv[i]) : 0;
          var pv = [0,0,0];
          var d = null;
          if (typeof _navigator.plugins != UNDEFINED && typeof _navigator.plugins[SHOCKWAVE_FLASH] == OBJECT) 
          {
              d = _navigator.plugins[SHOCKWAVE_FLASH].description;
              if (d && !(typeof _navigator.mimeTypes != UNDEFINED && _navigator.mimeTypes[SHOCKWAVE_FLASH_MIME_TYPE] && !_navigator.mimeTypes[SHOCKWAVE_FLASH_MIME_TYPE].enabledPlugin)) 
              {
                  d = d.replace(/^.*\s+(\S+\s+\S+$)/, '$1');
                  pv[0] = parseInt(d.replace(/^(.*)\..*$/, '$1'), 10);
                  pv[1] = parseInt(d.replace(/^.*\.(.*)\s.*$/, '$1'), 10);
                  pv[2] = /r/.test(d) ? parseInt(d.replace(/^.*r(.*)$/, '$1'), 10) : 0;
              }
          } 
          else if (typeof window.ActiveXObject != UNDEFINED) 
          {
              var a = null;
              var fp6Crash = false;
              try 
              {
                  a = new ActiveXObject(SHOCKWAVE_FLASH_AX + '.7');
              } 
              catch(e) 
              {
                  try 
                  { 
                      a = new ActiveXObject(SHOCKWAVE_FLASH_AX + '.6');
                      pv = [6,0,21];
                      a.AllowScriptAccess = 'always';
                  } 
                  catch(e) 
                  {
                      if (pv[0] == 6)
                          fp6Crash = true;
                  }
                  if (!fp6Crash) 
                  {
                      try 
                      {
                          a = new ActiveXObject(SHOCKWAVE_FLASH_AX);
                      } 
                      catch(e) 
                      {
                      	
                      }
                  }
              }
              if (!fp6Crash && typeof a == OBJECT) 
              {
                  try 
                  {
                      d = a.GetVariable('$version');
                      if (d) 
                      {
                          d = d.split(' ')[1].split(',');
                          pv = [parseInt(d[0], 10), parseInt(d[1], 10), parseInt(d[2], 10)];
                      }
                  } 
                  catch(e) 
                  {
                  	
                  }
              }
          }    
          return (pv[0] > rv[0] || (pv[0] == rv[0] && pv[1] > rv[1]) || 
              (pv[0] == rv[0] && pv[1] == rv[1] && pv[2] >= rv[2])) ? true : false;
      };
	  var OBJECT = 'object';
      var SHOCKWAVE_FLASH = 'Shockwave Flash';
      var SHOCKWAVE_FLASH_AX = 'ShockwaveFlash.ShockwaveFlash';
      var SHOCKWAVE_FLASH_MIME_TYPE = "application/x-shockwave-flash"; 
      var UNDEFINED = 'undefined'; 
      var _window = _getWindow();
      var _document = _window.document;
      var _location = _window.location;
      var _navigator = navigator;
      var _hash_index = _location.href.indexOf('#');
      var _has_hash = (_hash_index != -1);
      var _quotation_index = _location.href.indexOf('?');
      var _has_quotation = (_quotation_index != -1);
      
      
      /**
      * @param {String} flashVersion minimum flash version eg:9.0.124
      * @param {string} scriptName   coldfusion script name eg:index.cfm
      * @param {String} baseAddress  eg:/GoldenLight_fx3_6-debug
      * @param {string} queryString  eg:/cat/10/subcat/10.050/
      *  
      *  example: address  http://localhost/GoldenLight_fx3_6-debug/cat/10/subcat/10.050 redirects from IIS rewrite module to 
      * 			http://localhost/GoldenLight_fx3_6-debug/index.cfm?cat=10&subcat=10.050
      * 			the web browser address bar has this string http://localhost/GoldenLight_fx3_6-debug/cat/10/subcat/10.050 
      * 			internal the coldfusion knows the actual address  http://localhost/GoldenLight_fx3_6-debug/index.cfm?cat=10&subcat=10.050
      */

      this.optimize =function (flashVersion,scriptName,baseAddress,queryAddress)
      {
	      var _isflash = _checkFlash(flashVersion);
    	  
    	  if (_isflash) 
	      {
	      	
	    	  // flash player exist and actual address is like :http://localhost/GoldenLight_fx3_6-debug/#/cat/10/subcat/10.050
	    	  if (_has_hash)
	      		{
	      		
	      		}
	      		// flash player exist and actual address is like :http://localhost/GoldenLight_fx3_6-debug/cat/10/subcat/10.050
	    	  else
	      		{
	    		  _location.replace(_location.protocol + '//' +_location.hostname + baseAddress + '/#' + queryAddress);
	      		}
	      	}
	      		// flash player don't exist 
	      else
	    	  {
	    	  //actual address is like :http://localhost/GoldenLight_fx3_6-debug/#/cat/10/subcat/10.050
	    	  	if (_has_hash) 
	    	  	{
	    		  _location.replace(_location.href.replace(/#\/?/, ''));
	    	  	}
	    	  	// flash player don't exist and actual address is like :http://localhost/GoldenLight_fx3_6-debug/?/cat/10/subcat/10.050
	    	  	else if ( _has_quotation)
	    	  	{
	    	  		// two cases 
	    	  		//1:http://localhost/GoldenLight_fx3_6-debug/?/cat/10/subcat/10.050
	    	  		//2:http://localhost/GoldenLight_fx3_6-debug/?cat=10&subcat=10.050 	
	    	  	}
	    	  
	    	  	else
	    	  	{
	    	  		// do nothing	
	    	  	}
	    	  } 
    	  return _isflash;
      };
      this.checkFlash = function( flashVersion)
      {
    	return _checkFlash(flashVersion);  
      };
}
