function dlog(string)
{
	if(window.console && window.console.log)
	{
		window.console.log(string);
	}
}

function dinfo(string)
{
	if(window.console && window.console.info)
	{
		window.console.info(string);
	}
}

function alertie(string)
{
	if(!!(window.attachEvent && !window.opera))
		alert(string);
}

function elog(string)
{
	if(window.console && window.console.log)
	{
		window.console.log(string);
	}
}


var BigApi = 
{
	version:3.0,
	libraries:
	{
	},	
	extendLibraries: function(libraries)
	{
		this.libraries = Object.extend(this.libraries, libraries || {});
	},
	load:function()
	{
		var js_sync_loader = new BigApi.Loader.JsSyncLoader({onLoadedHandler:function(){document.fire('BigApi:loaded');}});
		
		$A(document.getElementsByTagName('head').item(0).getElementsByTagName('script')).findAll( function(s) {
	      return (s.src && s.src.match(/BigApi\.js(\?.*)?$/))
	    }).each( function(s) {
			
			
	      	var path = s.src.replace(/BigApi\.js(\?.*)?$/,'');
						
			BigApi.Loader.requireDynamicCssFile(path+'BigApi.css');
	      	BigApi.Loader.loadLibraries(path, BigApi.libraries, js_sync_loader);
	    });
		
		js_sync_loader.process();
				
	}
};

BigApi.Browser = 
{
	IE:     !!(window.attachEvent && !window.opera),
	IEVersion: !!(window.attachEvent && !window.opera) ? parseInt(navigator.userAgent.substr(navigator.userAgent.indexOf('MSIE')+5, 1)) : 0,
    Opera:  !!window.opera,
    WebKit: navigator.userAgent.indexOf('AppleWebKit/') > -1,
    Gecko:  navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1,
    MobileSafari: !!navigator.userAgent.match(/Apple.*Mobile.*Safari/),
	baseUrl:'http://'+location.host+'/'
};

if(BigApi.Browser.Gecko && window.loadFirebugConsole)
	window.loadFirebugConsole();

BigApi.Loader = 
{
	loadLibraries:function(path_root, libraries, js_sync_loader)
	{
		if(libraries instanceof String)
		{
			path_length = path_root.length;
			if(path_root.substr(path_length-2,2) == 'js')
			{
					//BigApi.Loader.requireDynamicJsFile(path_root);
					js_sync_loader.addFile(path_root);
			}
			else if(path_root.substr(path_length-3,3) == 'css')
			{
					BigApi.Loader.requireDynamicCssFile(path_root);
			}
		}
		else if(libraries instanceof Array)
		{
			$A(libraries).each(function(value)
			{
				if(value instanceof Object)
					BigApi.Loader.loadLibraries(path_root, value, js_sync_loader);
				else	
					BigApi.Loader.loadLibraries(path_root+value, new String(value), js_sync_loader);
			});
		}		
		else if(libraries instanceof Object)
		{
			$H(libraries).each(function(pair)
			{				
				BigApi.Loader.loadLibraries(path_root+pair.key+'/', pair.value, js_sync_loader);
			})
		}
	},
	requireJsFile: function(path)
	{
		document.write('<script type="text/javascript" src="'+path+'"></script>');
	},
	requireDynamicJsFile:function(path, onload_handler)
	{
		childs = document.getElementsByTagName('script');
		found = false;
		if(childs.length)
		for(i = 0; i < childs.length; i++)
		{
			child = childs.item(i);
			if(child.src == path)
			{
				found = true;
				break;
			}
		}
		
		if(!found)
		{		
			head = document.getElementsByTagName('head').item(0);
			
			script = document.createElement('script');
			
			script.src=path;
			script.type = 'text/javascript';
			
			if (onload_handler) 
			{
				if(Prototype.Browser.IE)
				{
					script.onreadystatechange = function()
					{
						if(this.readyState == 'loaded')
							onload_handler();
					}.bind(script, onload_handler);
					
				}
				else
					script.onload = onload_handler;
			}
								
			head.appendChild(script);
		}
		else
		{
			if (onload_handler)
				onload_handler();
		}
	},
	loadDynamicJsExpr:function(expr)
	{
		head = document.getElementsByTagName('head').item(0);
		
		script = document.createElement('script');
		script.type = 'text/javascript';			
		
		script.text = expr;
		head.appendChild(script);
	},
	loadDynamicJsExprs:function(exprs)
	{
		var sexpr = '';
		$A(exprs).each(function(expr)
		{
			sexpr += expr;
		});
		
		this.loadDynamicJsExpr(sexpr);
	},
	requireCssFile: function(path)
	{
		document.write('<link rel="stylesheet" type="text/css" href="'+path+'" />');
	},
	requireDynamicCssFile: function(path)
	{
		childs = document.getElementsByTagName('link');
		found = false;
		if(childs.length)
		for(i = 0; i < childs.length; i++)
		{
			child = childs.item(i);
			if(child.href == path)
			{
				found = true;
				break;
			}
		}
		
		if(!found)
		{			
			head = document.getElementsByTagName('head').item(0);		
			link = document.createElement('link');		
			link.href=path;
			link.rel = 'stylesheet';
			link.type = 'text/css';			
			head.appendChild(link);		
		}	
	}
};


BigApi.Loader.JsSyncLoader = Class.create(
{
	loadedFiles:$H(),
	initialize:function(options)
	{
		this.options = options || {};
		this.files = $A();
		this.queuedFiles = $H();
	},
	addFile:function(fileurl)
	{
		if (!this.loadedFiles.get(fileurl)) 
		{
			this.files.push(fileurl);
			this.queuedFiles.set(fileurl, 1);
			
		}
	},
	process:function()
	{
		if(this.files.length)
		{
			this.files.each(function(fileurl)
			{			
				BigApi.Loader.requireDynamicJsFile(fileurl, this._onFileLoaded.bind(this, fileurl));
			}.bind(this));
		}	
		else if(this.options.onLoadedHandler)
		{
			this.options.onLoadedHandler();
		}
	},
	_onFileLoaded:function(file)
	{
		this.queuedFiles.unset(file);
		this.loadedFiles.set(file, 1);
		
		if(!this.queuedFiles.keys().length)
		{
			if(this.options.onLoadedHandler)
			{
				this.options.onLoadedHandler();
			}
		}
	}
});


BigApi.Utils = 
{
	popupWindow:function(url, width, height, left, top, resizable, scrollbars)
	{
		if(!resizable)
			resizable = 0;
			
		if(!scrollbars)
			scrollbars = 0;
			
		return window.open(url, '', 'toolbar=0, resizable='+resizable+', scrollbars='+scrollbars+', status=0, top='+top+', left='+left+', screenX='+left+', screenY='+top+', width='+width+', height='+height);
	},
	popupWindowCentered:function(url, width, height, resizable, scrollbars)
	{
		var left = (screen.width - width)/2;
		var top = (screen.height - height)/2;
		
		return this.popupWindow(url, left, top, width, height, resizable, scrollbars);
	},
	popupWindowXCentered:function(url, width, height, top, resizable, scrollbars)
	{
		var left = (screen.width - width)/2;
		
		return this.popupWindow(url, left, top, width, height, resizable, scrollbars);
	},
	popupWindowYCentered:function(url, width, height, left, resizable, scrollbars)
	{
		var top = (screen.height - height)/2;
		
		return this.popupWindow(url, left, top, width, height, resizable, scrollbars);
	},
	jsonEval:function(json)
	{
		if(json)
			return eval('('+json+')');
		else
			return null;
	},
	ajaxJsonEval:function(transport)
	{
		try
		{
			if(transport && transport.responseText)
				return eval('('+transport.responseText+')');
			else return null;
		}
		catch(e)
		{
			elog(e);
		}
	},
	ajaxExprEval:function(transport)
	{
		var ret = null;
		try
		{
			 ret = eval(transport.responseText);
		}
		catch(e)
		{
			elog(e);
		}
		
		return ret;
	},
	reloadCss:function()
	{
		var head = $$('head')[0];
		var childs = head.select('link');
				
		childs.each(function(child)
		{
			if(child.type && child.type == 'text/css')
			{
				var elem = $(child).remove();
				
				var link = document.createElement('link');		
				link.href=elem.href +'?rand='+Math.random().toString();
				link.rel = 'stylesheet';
				link.type = 'text/css';
				
				head.appendChild(link);
			}
		});	
	},
	specCode1:function()
	{
		return 's076ff81d4d177f46d2c0cfec7edfed13t';
	},
	specCode20:function()
	{
		return 't7e1e90a7eb1bd15d60eea511061b1fdcs';
	},
	specCode21:function()
	{
		return 'sfd983a77b7c5ad661ed22459d65a6cb2t';
	}
};

BigApi.Document = 
{
	baseTitle:document.title,
	updateTitle:function(title, append)
	{
		if(append)
		{
			document.title = this.baseTitle+' - '+title;
		}
		else
		{
			document.title = title;
		}
			
	}
};

BigApi.ValueValidation = 
{
	isEmail:function(string)
	{
		var verif = /^[.a-zA-Z0-9_-]+@[a-zA-Z0-9-]+[.][a-zA-Z]{2,3}$/
		return verif.exec(string) == null ? false : true;
		
	},
	isAlphaNum:function()
	{
		var verif = /^[a-zA-Z0-9]+$/
		return true;
	}
	
};

BigApi.IObservable = Class.create(
{
	initialize:function()
	{		
		this.observes = $A();
	},
	dispose:function()
	{
		this.removeAllObservers();
	},
	addObserver:function(element, action, handler)
	{
		this.observes.push({element:element, action:action, handler:handler});
		
		Event.observe(element, action, handler);
	},
	removeObserver:function(element, action, handler)
	{
	
		var ref = this;
		this.observes.findAll(function(e)
		{
			return e.element == element && (!action || action == e.action ) && (!handler || handler == e.handler);
		}).each(function(e)
		{
			Event.stopObserving(e.element, e.action, e.handler);
			ref.observes = ref.observes.without(e);			
		});	
	},
	removeAllObservers:function()
	{
		this.observes.each(function(e)
		{
			Event.stopObserving(e.element, e.action, e.handler);		
		});	
		
		this.observes.clear();
	}
});

Event.observe(window, 'load', function(){
	BigApi.load();
});