
var yzoom = {};

yzoom.WEBSITE_ADRESS = "http://gui1/";
yzoom.JAVASCRIPT_FOLDER = "/_js";

yzoom.service = {};

yzoom.service.callback = null;

yzoom.lazyload = function () {

  // -- Private Variables ------------------------------------------------------

  // Shorthand reference to the browser's document object.
  var d = document,

  // Reference to the <head> element.
  head,

  // Requests currently in progress, if any.
  pending = {},

  // Queued requests.
  queue = {css: [], js: []},

  // User agent information.
  ua;

  // -- Private Methods --------------------------------------------------------

  /**
   * Creates and returns an HTML element with the specified name and attributes.
   *
   * @method createNode
   * @param {String} name element name
   * @param {Object} attrs name/value mapping of element attributes
   * @return {HTMLElement}
   * @private
   */
  function createNode(name, attrs) {
    var node = d.createElement(name), attr;

    for (attr in attrs) {
      if (attrs.hasOwnProperty(attr)) {
        node.setAttribute(attr, attrs[attr]);
      }
    }

    return node;
  }

  /**
   * Called when the current pending resource of the specified type has finished
   * loading. Executes the associated callback (if any) and loads the next
   * resource in the queue.
   *
   * @method finish
   * @param {String} type resource type ('css' or 'js')
   * @private
   */
  function finish(type) {
    var p = pending[type];

    if (!p) { return; }

    var callback = p.callback,
        urls     = p.urls;

    urls.shift();

    // If this is the last of the pending URLs, execute the callback and
    // start the next request in the queue (if any).
    if (!urls.length) {
      if (callback) {
        callback.call(p.scope || window, p.obj);
      }

      pending[type] = null;

      if (queue[type].length) {
        load(type);
      }
    }
  }

  /**
   * Populates the <code>ua</code> variable with useragent information. Uses a
   * paraphrased version of the YUI useragent detection code.
   *
   * @method getUserAgent
   * @private
   */
  function getUserAgent() {
    // No need to run again if ua is already populated.
    if (ua) { return; }

    var nua = navigator.userAgent,
        pF  = parseFloat,
        m;

    ua = {
      gecko : 0,
      ie    : 0,
      opera : 0,
      webkit: 0
    };

    m = nua.match(/AppleWebKit\/(\S*)/);

    if (m && m[1]) {
      ua.webkit = pF(m[1]);
    } else {
      m = nua.match(/MSIE\s([^;]*)/);

      if (m && m[1]) {
        ua.ie = pF(m[1]);
      } else if ((/Gecko\/(\S*)/).test(nua)) {
        ua.gecko = 1;

        m = nua.match(/rv:([^\s\)]*)/);

        if (m && m[1]) {
          ua.gecko = pF(m[1]);
        }
      } else if (m = nua.match(/Opera\/(\S*)/)) {
        ua.opera = pF(m[1]);
      }
    }
  }

  /**
   * Loads the specified resources, or the next resource of the specified type
   * in the queue if no resources are specified. If a resource of the specified
   * type is already being loaded, the new request will be queued until the
   * first request has been finished.
   *
   * When an array of resource URLs is specified, those URLs will be loaded in
   * parallel if it is possible to do so while preserving execution order. All
   * browsers support parallel loading of CSS, but only Firefox and Opera
   * support parallel loading of scripts. In browsers other than Firefox and
   * Opera, scripts will be queued and loaded one at a time to ensure correct
   * execution order.
   *
   * @method load
   * @param {String} type resource type ('css' or 'js')
   * @param {String|Array} urls (optional) URL or array of URLs to load
   * @param {Function} callback (optional) callback function to execute when the
   *   resource is loaded
   * @param {Object} obj (optional) object to pass to the callback function
   * @param {Object} scope (optional) if provided, the callback function will be
   *   executed in this object's scope
   * @private
   */
  function load(type, urls, callback, obj, scope) {
    var i, len, node, p, url;

    // Determine browser type and version.
    getUserAgent();

    if (urls) {
      // Cast urls to an Array.
      urls = urls.constructor === Array ? urls : [urls];

      // Create a request object for each URL. If multiple URLs are specified,
      // the callback will only be executed after all URLs have been loaded.
      //
      // Sadly, Firefox and Opera are the only browsers capable of loading
      // scripts in parallel while preserving execution order. In all other
      // browsers, scripts must be loaded sequentially.
      //
      // All browsers respect CSS specificity based on the order of the link
      // elements in the DOM, regardless of the order in which the stylesheets
      // are actually downloaded.
      if (type === 'css' || ua.gecko || ua.opera) {
        queue[type].push({
          urls    : [].concat(urls), // concat ensures copy by value
          callback: callback,
          obj     : obj,
          scope   : scope
        });
      } else {
        for (i = 0, len = urls.length; i < len; ++i) {
          queue[type].push({
            urls    : [urls[i]],
            callback: i === len - 1 ? callback : null, // callback is only added to the last URL
            obj     : obj,
            scope   : scope
          });
        }
      }
    }

    // If a previous load request of this type is currently in progress, we'll
    // wait our turn. Otherwise, grab the next item in the queue.
    if (pending[type] || !(p = pending[type] = queue[type].shift())) {
      return;
    }

    head = head || d.getElementsByTagName('head')[0];
    urls = p.urls;

    for (i = 0, len = urls.length; i < len; ++i) {
      url = urls[i];

      if (type === 'css') {
        node = createNode('link', {
          href : url,
          rel  : 'stylesheet',
          type : 'text/css'
        });
      } else {
        node = createNode('script', {src: url});
      }

      if (ua.ie) {
        node.onreadystatechange = function () {
          var readyState = this.readyState;

          if (readyState === 'loaded' || readyState === 'complete') {
            this.onreadystatechange = null;
            finish(type);
          }
        };
      } else if (type === 'css' && (ua.gecko || ua.webkit)) {
        // Gecko and WebKit don't support the onload event on link nodes, so we
        // just have to finish after a brief delay and hope for the best.
        setTimeout(function () { finish(type); }, 50 * len);
      } else {
        node.onload = node.onerror = function () { finish(type); };
      }

      head.appendChild(node);
    }
  }

  return {

    /**
     * Requests the specified CSS URL or URLs and executes the specified
     * callback (if any) when they have finished loading. If an array of URLs is
     * specified, the stylesheets will be loaded in parallel and the callback
     * will be executed after all stylesheets have finished loading.
     *
     * Currently, Firefox and WebKit don't provide any way to determine when a
     * stylesheet has finished loading. In those browsers, the callback will be
     * executed after a brief delay. For information on a manual technique you
     * can use to detect when CSS has actually finished loading in Firefox and
     * WebKit, see http://wonko.com/post/how-to-prevent-yui-get-race-conditions
     * (which applies to LazyLoad as well, despite being originally written in
     * in reference to the YUI Get utility).
     *
     * @method css
     * @param {String|Array} urls CSS URL or array of CSS URLs to load
     * @param {Function} callback (optional) callback function to execute when
     *   the specified stylesheets are loaded
     * @param {Object} obj (optional) object to pass to the callback function
     * @param {Object} scope (optional) if provided, the callback function will
     *   be executed in this object's scope
     * @static
     */
    css: function (urls, callback, obj, scope) {
      load('css', urls, callback, obj, scope);
    },

    /**
     * Requests the specified JavaScript URL or URLs and executes the specified
     * callback (if any) when they have finished loading. If an array of URLs is
     * specified and the browser supports it, the scripts will be loaded in
     * parallel and the callback will be executed after all scripts have
     * finished loading.
     *
     * Currently, only Firefox and Opera support parallel loading of scripts
     * while preserving execution order. In browsers other than Firefox and
     * Opera, scripts will be queued and loaded one at a time to ensure correct
     * execution order.
     *
     * @method js
     * @param {String|Array} urls JS URL or array of JS URLs to load
     * @param {Function} callback (optional) callback function to execute when
     *   the specified scripts are loaded
     * @param {Object} obj (optional) object to pass to the callback function
     * @param {Object} scope (optional) if provided, the callback function will
     *   be executed in this object's scope
     * @static
     */
    js: function (urls, callback, obj, scope) {
      load('js', urls, callback, obj, scope);
    },
	
	/**
     * Requests the specified JavaScript URL or URLs and executes the specified
     * callback (if any) when they have finished loading. If an array of URLs is
     * specified and the browser supports it, the scripts will be loaded in
     * parallel and the callback will be executed after all scripts have
     * finished loading.
     *
     * Currently, only Firefox and Opera support parallel loading of scripts
     * while preserving execution order. In browsers other than Firefox and
     * Opera, scripts will be queued and loaded one at a time to ensure correct
     * execution order.
     *
     * @method js
     * @param {String|Array} urls JS URL or array of JS URLs to load
     * @param {Function} callback (optional) callback function to execute when
     *   the specified scripts are loaded
     * @param {Object} obj (optional) object to pass to the callback function
     * @param {Object} scope (optional) if provided, the callback function will
     *   be executed in this object's scope
     * @static
     */
    loadScript: function (urls, callback, obj, scope) {
      load('js', urls, callback, obj, scope);
    },
	
	loadModule: function (modules, callback)
	{
		var mdls = modules.split(", ");
		
		if (mdls.length == 1)
			load('js', yzoom.JAVASCRIPT_FOLDER + "/" + modules + ".js", callback);
		else
		{
			var i;
			for (i=0; i<(mdls.length-1); i++)
			{
				mdls[i] = yzoom.JAVASCRIPT_FOLDER + "/" + mdls[i] + ".js";
			}
			mdls[i] = yzoom.JAVASCRIPT_FOLDER + "/" + mdls[i] + ".js";
			load('js', mdls, callback);
		}
	}

  };
}();


/**
 * @method yzoom.service.main();
 */
function main ()
{
	yzoom.service.ie6 = ($.browser.msie && $.browser.version.substr(0,1)<7);
	yzoom.service.ie7 = ($.browser.msie && $.browser.version.substr(0,1) == 7);
	
	$('.lb').lightBox();

	if (typeof ycallback != 'undefined') // se existe callback
	{
		if (typeof ycallback == "function") ycallback();
		else if (ycallback.constructor.toString().indexOf("Array")) // se há varios callbacks
		{
			var l = ycallback.length;
			for (var i=0; i<l; i++)
			{
				var fn = ycallback.pop();
				fn();
			}
		}
	}
}

yzoom.service.main = main;



yzoom.lazyload.loadModule("yzoom.jquery, yzoom.stdpack", yzoom.service.main);

