/*=:project
    scalable Inman Flash Replacement (sIFR) version 3, revision 436.

    Provides debug information about sIFR.

  =:file
    Copyright: 2006 Mark Wubben.
    Author: Mark Wubben, <http://novemberborn.net/>

  =:license
    * This software is licensed and provided under the CC-GNU LGPL
    * See <http://creativecommons.org/licenses/LGPL/2.1/>    
*/

sIFR.debug = new function() {
  function Errors() {
    this.fire = function(id) {
      if( _proxy_jslib_handle(this, (id + 'Alert'), 0, 0)) alert( _proxy_jslib_handle(this, (id + 'Alert'), 0, 0));
      throw new (Error)( _proxy_jslib_handle(this, (id), 0, 0));
    };
  
    this.isFile = 'sIFR: Did not activate because the page is being loaded from the filesystem.';
    this.isFileAlert = 'Hi!\n\nThanks for using sIFR on your page. Unfortunately sIFR couldn\'t activate, because it was loaded '
 + 'directly from your computer.\nDue to Flash security restrictions, you need to load sIFR through a web'
 + ' server.\n\nWe apologize for the inconvenience.';
  };
  
  sIFR.errors = new (Errors)();
  
  function log(msg) {
    if(!sIFR.ua.safari && window.console && console.log) console.log(msg);
    else alert(msg);
  }
  
  this.ua = function() {
    var info = [];
    
    for(var prop in sIFR.ua) {
      if( _proxy_jslib_handle(sIFR.ua, (prop), 0, 0) ==  _proxy_jslib_handle(Object.prototype, (prop), 0, 0)) continue;
      
      info.push(prop, ': ',  _proxy_jslib_handle(sIFR.ua, (prop), 0, 0), '\n');
    }
    
    log(info.join(''));
  };
  
  this.domains = function() {
    if(sIFR.domains.length == 0) {
      log('No domain verification used.');
      return;
    }
    
    var domain =  _proxy_jslib_handle(sIFR.util, 'domain', '', 1, 0)();
    var matches = [], nonMatches = [];

    for(var i = 0; i < sIFR.domains.length; i++) {
      var match =  _proxy_jslib_handle(sIFR.domains, (i), 0, 0);
      if(sIFR.util.domainMatches( _proxy_jslib_handle(null, 'domain', domain, 0, 0), match)) matches.push(match);
      else nonMatches.push(match);
    }
    
    var msg = ['The domain "',  _proxy_jslib_handle(null, 'domain', domain, 0, 0), '"'];
    if(matches.length > 0) msg.push(' matches:\n* ', matches.join('\n* '));
    if(matches.length > 0 && nonMatches.length > 0) msg.push('\nbut');
    if(nonMatches.length > 0) msg.push(' does not match:\n* ', nonMatches.join('\n* '));
    log(msg.join(''));
  };

  this.ratios = function(kwargs, mergeKwargs) {
    if(mergeKwargs) kwargs = sIFR.util.copyProperties(kwargs, mergeKwargs);
    
    if(!kwargs.selector && !kwargs.elements) {
      log('Cannot calculate ratios, no selector or element given.');
      return;
    }
    
     _proxy_jslib_assign('delete', (kwargs), ('wmode'), '');
     _proxy_jslib_assign('delete', (kwargs), ('transparent'), '');
     _proxy_jslib_assign('delete', (kwargs), ('opaque'), '');
    
    if (kwargs.css) {
      kwargs.css = sIFR.util.convertCssArg(kwargs.css);
      sIFR.util.extractFromCss(kwargs.css, '.sIFR-root', 'leading', true);
    }
    
    var running = false;
    kwargs.onReplacement = function(cb) {
      if(running) return; // Prevent duplicate results
      running = true;

       _proxy_jslib_assign('', sIFR.debug.__ratiosCallback, (cb.id), '=', ( function(ratios) {
        ratios = '[' + ratios.join(', ') + ']';
         _proxy_jslib_handle(null, 'setTimeout', setTimeout, 1, 0)(function() {
          var before = new (Date)();
          prompt('The ratios for ' + kwargs.selector + ' are:', ratios);
          if(sIFR.ua.ie && before - new (Date) < 200) {
            alert("Press Control+C to copy the text of this alert box. Then paste it into your favorite text editor.\n"
 + "The numbers between the braces, including the braces, are the ratios. You have to add those to your sIFR configuration.\n\n"
 + "Tip: try calculating the ratios in Firefox instead, it'll be easier to copy the ratios.\n\n"
 + ratios);
          }
          cb.resetMovie();
        }, 0);
      }));
      cb.call('calculateRatios');
    };

     _proxy_jslib_handle(sIFR, 'replace', '', 1, 0)(kwargs);
  };
  
  this.__ratiosCallback = function(id, ratios) {
    if( _proxy_jslib_handle(this.__ratiosCallback, (id), 0, 0))  _proxy_jslib_handle(this.__ratiosCallback, (id), 1, 0)(ratios);
  };
  
  function verifyResource(uri, fail, ok) {
    if(sIFR.ua.ie && uri.charAt(0) == '/') {
      uri =  _proxy_jslib_handle( _proxy_jslib_handle( _proxy_jslib_handle(window, 'location', '', 0, 0), 'toString', '', 1, 0)(), 'replace', '', 1, 0)(/([^:]+)(:\/?\/?)([^\/]+).*/, '$1$2$3') + uri;
    }
    
    var xhr = new (XMLHttpRequest)();
     _proxy_jslib_handle(xhr, 'open', '', 1, 0)('GET', uri, true);
    xhr.onreadystatechange = function() {
      if(xhr.readyState == 4) {
        if(xhr.status != 200) log(fail);
        else log(ok);
      }
    };
    xhr.send('');
  }

  this.test = function(kwargs, mergeKwargs) {
    kwargs = merge(kwargs, mergeKwargs);

    var src =  _proxy_jslib_handle(kwargs, 'src', '', 0, 0);
    var checked = false;
    if(typeof( _proxy_jslib_handle(null, 'src', src, 0, 0)) != 'string') {
      if( _proxy_jslib_handle( _proxy_jslib_handle(null, 'src', src, 0, 0), 'src', '', 0, 0))  src= _proxy_jslib_assign_rval('', 'src', '=', (  _proxy_jslib_handle( _proxy_jslib_handle(null, 'src', src, 0, 0), 'src', '', 0, 0)), src);

      if(typeof( _proxy_jslib_handle(null, 'src', src, 0, 0)) != 'string') {
        var versions = [];
        for(var version in  _proxy_jslib_handle(null, 'src', src, 0, 0)) if( _proxy_jslib_handle( _proxy_jslib_handle(null, 'src', src, 0, 0), (version), 0, 0) !=  _proxy_jslib_handle(Object.prototype, (version), 0, 0)) versions.push(version);
        versions.sort().reverse();

        var result = '';
        var i = -1;
        while(!result && (i= _proxy_jslib_assign_rval('++', 'i', '', '', i)) < versions.length) {
          if(parseFloat( _proxy_jslib_handle(versions, (i), 0, 0)) <= ua.flashVersion) result =  _proxy_jslib_handle( _proxy_jslib_handle(null, 'src', src, 0, 0), ( _proxy_jslib_handle(versions, (i), 0, 0)), 0, 0);
          var msg = '<' +  _proxy_jslib_handle( _proxy_jslib_handle(null, 'src', src, 0, 0), ( _proxy_jslib_handle(versions, (i), 0, 0)), 0, 0) + '>, flash ' + parseFloat( _proxy_jslib_handle(versions, (i), 0, 0));
          verifyResource( _proxy_jslib_handle( _proxy_jslib_handle(null, 'src', src, 0, 0), ( _proxy_jslib_handle(versions, (i), 0, 0)), 0, 0), 'FAILED: ' + msg, 'OK: ' + msg);
        }
        
         src= _proxy_jslib_assign_rval('', 'src', '=', ( result), src);
        checked = true;
      }
    }
    
    if(! _proxy_jslib_handle(null, 'src', src, 0, 0)) log('Could not determine appropriate source.');
    else if(!checked) verifyResource( _proxy_jslib_handle(null, 'src', src, 0, 0), 'FAILED: <' +  _proxy_jslib_handle(null, 'src', src, 0, 0) + '>', 'OK: <' +  _proxy_jslib_handle(null, 'src', src, 0, 0) + '>');
  };
  
  this.forceTest = function() {
    var replace =  _proxy_jslib_handle(sIFR, 'replace', '', 0, 0);
     _proxy_jslib_assign('', sIFR, 'replace', '=', ( function(kwargs, mergeKwargs) {
      sIFR.debug.test(kwargs, mergeKwargs);
       _proxy_jslib_handle(null, 'replace', replace, 0, 0).call(sIFR, kwargs, mergeKwargs);
    }));
  }
};