var app = {};
$.ajaxSetup({type:'POST', async:true});

$(function()
{
	/*
	*/
	$.ajax({url:'/application/request/load_configs',data:''
				,success:
				function(data)
				{
					if(!data) return app.configs = {};
					app.configs = $.php.json_decode(data);
					$(window).triggerHandler("CONFIG_LOADED");
				}});
	
	$(".clickclear").clickclear().trigger("blur");
});

function lang(term)
{
	if(!app.configs.lang || !app.configs.lang[term]) return '';	
	return app.configs.lang[term];
}

function config(config)
{
	if(!app.configs[config]) return '';	 
	return app.configs[config];
}


$.bind = 
{
	 vars:{'load':[],'resize':[],'scroll':[],'config':[]}
	,load:function(f)
	{
		this.vars['load'].push(f);
		return this;
	}
	,resize:function(f)
	{
		this.vars['resize'].push(f);
		return this;
	}
	,scroll:function(f)
	{
		this.vars['scroll'].push(f);
		return this;
	}
	,config:function(f)
	{
		this.vars['config'].push(f);
		return this;
	}
	,handlers:function()
	{
		window.onload 	= function(){$.bind.execute("load");};	
		window.onresize = function(){$.bind.execute("resize");};	
		window.onscroll = function(){$.bind.execute("scroll");};
		$(window).bind("CONFIG_LOADED",function(){$.bind.execute("config");})
	}
	,execute:function(t)
	{
		for(var i in $.bind.vars[t])
		{
			$.bind.vars[t][i]();	
		}
	}
};

$.bind.handlers();




$.msg = function(txt,type,timeout,container)
{
	var is_body = (!container || container.length==0),
		appendto = is_body?$("body"):container;
	$("#messages").remove();
	var box = $('<div id="messages" class="'+type+'-message" />').html(nl2br(txt)).append('<div class="corners corners-trans-fff"><div class="c rb"/><div class="c rt"/><div class="c lt"/><div class="c lb"/></div>').appendTo(appendto).css({opacity:0.1});
	var but = $("<a href='' class='close'></a>")
				.bind('click',function(){box.fadeTo(300,0.1,function(){$(this).hide();});return false;}).appendTo(box);
	
	if(is_body)
	box.css({marginTop:'-'+Math.floor(box.outerHeight()/2)+'px'}).addClass("messages-fly").fadeTo(200,1);
	else
	box.parent().show().end().fadeTo(200,1);
	
	if(appendto.offset().top < $(window).scrollTop()) scrollTo(0,appendto.offset().top-50);	
	
	if(timeout)
	{
		setTimeout(function()
		{
			box.fadeTo(300,0.1,function(){$(this).remove();});
		},2000);
	}
	return;

};

$.msg.close = function()
{
	$("#messages").fadeTo(300,0.1,function(){$(this).remove();});
};




$.php =
{
	json_decode:function(str_json){var json=window.JSON;if(typeof json==='object'&&typeof json.parse==='function'){try{return json.parse(str_json);}catch(err){if(!(err instanceof SyntaxError)){throw new Error('Unexpected error type in json_decode()');}
this.php_js=this.php_js||{};this.php_js.last_error_json=4;return null;}}
var cx=/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;var j;var text=str_json;cx.lastIndex=0;if(cx.test(text)){text=text.replace(cx,function(a){return'\\u'+
('0000'+a.charCodeAt(0).toString(16)).slice(-4);});}
if((/^[\],:{}\s]*$/).test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,'@').replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,']').replace(/(?:^|:|,)(?:\s*\[)+/g,''))){j=eval('('+text+')');return j;}
this.php_js=this.php_js||{};this.php_js.last_error_json=4;return null;}
	,nl2br:function(str, is_xhtml) {
		var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br />' : '<br>';
		return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1'+ breakTag +'$2');
	}
	,empty:function(mixed_var){var key;if(mixed_var===""||mixed_var===0||mixed_var==="0"||mixed_var===null||mixed_var===false||typeof mixed_var==='undefined'){return true;}
if(typeof mixed_var=='object'){for(key in mixed_var){return false;}
return true;}
return false;}
	
};

