/**
* The main class in Fluxo js engine.
* @class BasicWeb2
* @static
* @property {boolean} isIE true if the client is IE 
* @static
* @property {boolean} isFirefox true if the client is Firefox
* @static
* @property {boolean} isOpera true if the client is Opera
* @static
*/


BasicWeb2 = {};

// browse identification;
BasicWeb2.ie = 1;
BasicWeb2.opera = 2;
BasicWeb2.firefox = 4;

BasicWeb2.navigator = 0;
BasicWeb2.navigator |= (navigator.userAgent.indexOf("MSIE")!= -1) ? BasicWeb2.ie : 0;
BasicWeb2.navigator |= (navigator.userAgent.indexOf("pera")!= -1) ? BasicWeb2.opera : 0;
BasicWeb2.navigator |= (navigator.userAgent.indexOf("ecko")!= -1) ? BasicWeb2.firefox : 0;

BasicWeb2.isIE = (BasicWeb2.navigator & BasicWeb2.ie) == BasicWeb2.ie;
BasicWeb2.isOpera = (BasicWeb2.navigator & BasicWeb2.opera) == BasicWeb2.opera;
BasicWeb2.isFirefox = (BasicWeb2.navigator & BasicWeb2.firefox) ==  BasicWeb2.firefox;


/**
* @property {Object} UrlQuery store the query string in properties
* @static
*/
BasicWeb2.GetQuery = function()
{
	if(!document.location.search) return {};
  var e;
	var query = document.location.search.replace(/\'/g, "\\\'").substr(1);
	query = query.replace(/(\w+)(=(.*?)?(&|$)+)/g, "'$1': '$3',").replace(/,$/, '');;
	try{eval("query = {" + query.replace(/\"/g, "\\\"") + "}")}catch(e){return [];}
	return query;
}
BasicWeb2.UrlQuery = BasicWeb2.GetQuery();


/**
* Extentds a class
* @method extend
* @static
* @param class subclass the class to be extend
* @param class superclass the class to inherite from
*/

BasicWeb2.extend = function(subclass, superclass)
{
	var f = function() {};
  f.prototype = superclass.prototype;
  subclass.prototype = new f();
  subclass.prototype.constructor = subclass;
  subclass.superclass = superclass.prototype;
  if (superclass.prototype.constructor == Object.prototype.constructor)
     superclass.prototype.constructor = superclass;
}


BasicWeb2.nameSpaces = [];
/**
* @method loadNamespace
* @static
* @param {string} namespace
* @return {Object} the namespace class
*/
BasicWeb2.loadNamespace = function (namespace)
{
   if(BasicWeb2.nameSpaces[namespace]) return BasicWeb2.nameSpaces[namespace];

   var url = "/Fluxo/BasicWeb2/" + namespace.replace(/\./g, '/') + '.js?' + new Date().getTime(); 
     
   var xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
   xmlhttp.open("GET", url, false);
   xmlhttp.send(null);
   eval( "try{" +xmlhttp.responseText + "\n} catch(error) {alert(url +'\\n'+ error.message);}");
   
   eval('BasicWeb2.nameSpaces[namespace]= BasicWeb2.' + namespace);
   return BasicWeb2.nameSpaces[namespace];
}

/**
* Register fluxo class
* @method registerClasses
* @static
* @param {nodeList} nodelist
* @optional
*/
BasicWeb2.registerClasses = function()
{
  
  if(arguments[1] == undefined)
  {
    var a = document.getElementsByTagName('a');
    for(var i=0; i <a.length; i++)
    {
      a[i].href = a[i].href.replace(/&apos;/g, "'");
      if(!a[i].href.match(/^container:(\((\w+)\))?(.*)/))continue;
			a[i].href = RegExp.$3;
			var id = RegExp.$2;
			BasicWeb2.loadNamespace("Layout.gui");
			new BasicWeb2.Layout.gui.ContainerLink(a[i], {'container': id })
    }
    var nodes = BasicWeb2.NodeTree.getElementsByClass('fluxo');
  }
  else
  {
    nodes = BasicWeb2.NodeTree.getElementsByClass('fluxo', arguments[1]);
  }
	for( i = 0; i < nodes.length; i++)
	{
		var params = BasicWeb2.getParams(nodes[i]);
    if(params.length == undefined){alert(params); continue;}
    for(var j=0; j < params.length; j++)
		{
			var ns = params[j].code.replace(/(.*)\..*/, '$1') ;
			var code = params[j].code.replace(/.*\.(.*)/, '$1');
			if(ns = BasicWeb2.loadNamespace(ns))
				new ns[code](nodes[i], params[j]);
		}
	}
  
  
}

/**
* @method getParams  get params inside the next comment tag
* @static
* @param {Element} html object
* @return {Array} params array
*/
BasicWeb2.getParams = function (element)
{
	for (var i = element.nextSibling; i && i.nodeName == '#text'; i = i.nextSibling);
	if(!i) return;
	if(i.nodeName != '#comment')return;
	var params, e;
  try{eval("params = " + i.nodeValue + ";");}
  catch(e){alert(e.description + " "+ i.nodeValue);return;}
  i.parentNode.removeChild(i);
  return params;
  
};

/**
* @method arrayIndexOf
* @param array ar
* @param mixed item
* @return int index of de element or -1
*/
BasicWeb2.arrayIndexOf = function(ar, item)
{
	for(var i in ar)
		if(ar[i] == item) return i;
	return -1;
}

/**
* @method keepDefaults  mix 2 objects
* @static
* @param {Class} defaultArgs class constructor ref
* @param {Object} args object with the new values
* @return {Object} the class with the default values overwritten
*/
BasicWeb2.keepDefaults = function (defaultArgs, args)
{
	var r = new defaultArgs();
	for(var i in args)
	{
		r[i] = args[i];
	}
	return r;
};


/**
* @method dump
* @static
* @param {Mixed} obj object to dump 
* @param {Mixed} to function or htmlelement
* @param {boolean} htmlelement append content
* @optional
*/
BasicWeb2.dump = function(obj, to)
{
	if(to == undefined)return; 
	if((typeof obj).match(/string|number/)) {typeof to == 'function'  ? to(obj) : to.innerHTML = obj; return;}
	var s = "";
	for(var p in obj)
		s +=  "<div>" + p + ": " + obj[p] + "</div>\n";
	if(arguments[2])
	{
		to.innerHTML += s;
		return;
	}
	typeof to == 'function' ? to(s) : to.innerHTML = s;
};

/**
* @method bind make method reference
* @static
* @param {Object} obj the object
* @param {Function} fn method ref
* @return {Function} method reference
*/
BasicWeb2.bind = function(obj, fn){return function(){fn.apply(obj, arguments)};};

BasicWeb2.putFlash = function(mid, movie, width, height, fvars)
{
  var el = "<embed allowScriptAccess='SameDomain' id='"+ mid + "' src='" + movie + "' width='" + width+ "' height='" + height +"' flashvars='"+fvars+"' />";
  document.write(el.replace(/\"/g, '\\\"'));
}

BasicWeb2.money = function(n)
{
  n *= 100;
  n = Math.round(n);
  return (arguments[1] ? '': 'R$ ') + (n + "").replace(/(\d{2})$/, ",$1");
}
BasicWeb2.unMoney = function(e)
{
  var n = e.innerHTML.replace("R$ ", '');
  n = n.replace(',', '.'); 
  return parseFloat(n);
}

/**
 *@namespace BasicWeb2
 */

/**
* Provide some handy ways to access nodes
* @class NodeTree
* @static
*/
BasicWeb2.NodeTree = {};

/**
* @method getElementByClass 
* @static
* @param {string} classname  
* @param {nodelist} nodelist 
* @optional
* @return {array} nodelist
*/
BasicWeb2.NodeTree.getElementsByClass = function (classname) 
{
	var nodelist = arguments[1] != undefined ? arguments[1] : document.body.getElementsByTagName('*');
	var nodes = new Array();
	var pattern = new RegExp("(^|\\s)" + classname + "(\\s|$)");
	for (var i = 0; i < nodelist.length; i++) 
 		if (nodelist[i].className.match(pattern)) nodes.push(nodelist[i]);
  return nodes;
};


/**
* navigate through element parents until one matches the classname
* @method getParentWhereClass 
* @static
* @param {element} element  
* @param {string} classname
* @return {element} 
*/
BasicWeb2.NodeTree.getParentWhereClass = function (element, classname)
{
	var pattern = new RegExp("(^|\\s)" + classname + "(\\s|$)");
	for (var i = element.parentNode; i || i.className.match(pattern); i = i.parentNode);
	return i; 
};

/**
* navigate through element parents until one matches the tagname
* @method getParentWhereClass 
* @static
* @param {element} element  
* @param {string} tagname
* @return {element} 
*/
BasicWeb2.NodeTree.getParentWhereTag = function (element, tagname)
{
	var pattern = new RegExp(tagname);
	for (var i = element.parentNode; i || i.nodeName.match(pattern); i = i.parentNode);
	return i; 
};

/**
* Jump text nodes
* @method jumpText
* @static
* @param {element} element  
* @return {element} 
*/
BasicWeb2.NodeTree.jumpText = function (element)
{
	for (var i = element; i && i.nodeName == '#text'; i = i.nextSibling);
	return i; 
};

/**
* skip until the element has the nodeName
* @method jumpUntil
* @static
* @param {element} element
* @param {string} nodename
* @return {element} 
*/
BasicWeb2.NodeTree.jumpUntil = function (element, nodename)
{
	for (var i = element; i && i.nodeName != nodename; i = i.nextSibling);
	return i; 
};

BasicWeb2.loadNamespace('Event');
BasicWeb2.loadNamespace('Data');
BasicWeb2.loadNamespace('Layout');

