var fileTypes = {
	acrobat : ['pdf'],
	application : ['exe','bat'],
	compress : ['zip','7z','rar','ace','cab','gz','gzip','tar','tar.z','tar.gz','tz','taz','tgz','jar','bz','bz2'],
	excel : ['ods','xlsx','xls','csv'],
	image : ['jpg','gif','bmp','png','tif','jpeg'],
	powerpoint : ['odp','pptx','ppsx','ppt','pps'],
	text : ['txt','inf','log'],
	word : ['odt','doc','docx']
};

function fileExt(str){
	var ext = str.match(/\.(.+)$/);
	return ext ? ext[1].toLowerCase() : false;
}

function fileType(str){
	var type = 'unknown';
	var ext = fileExt(str);
	if(ext){
		for(var _type in fileTypes){
			if(fileTypes[_type].contains(ext)){
				type = _type;
				break;
			}
		}
	}
	return type;
}

function empty(obj){
	return obj===undefined || obj==null || obj===false || obj==='';
}

function alt(a,b,c){
	if(empty(c)){
		return empty(a) ? b : a;
	}else{
		return empty(a) ? c : b;
	}
}

function $count(obj){
	var count = 0;
	for(var i in obj){
		count++;
	}
	return count;
}

function redirect(url){
	url = url.substr(0,4)=='http' ? url : BASE.web + url;
	window.location = url;
}

function reload(){
	window.location = window.location.href+'';
}

function parseSearchString(str){
	return str.replace(/[^\w\d\s\-\.@ñáéíóúñ]/g,'').replace(/^\s+|\s+$/g,'').replace(/\s+/g,'-');
}

function moneda(n,symbol){
	n = n.toFixed(2)+'';
	var x = n.split('.');
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x[0])){x[0] = x[0].replace(rgx,'$1'+' '+'$2');}
	return (!$defined(symbol) || symbol!==false ? msim+' ' : '')+x[0]+'.'+x[1];
}

function noSelects(mode){
	if(Browser.Engine.trident4){
		if(mode){
			$('body').addClass('noSelects');
		}else{
			$('body').removeClass('noSelects');
		}
	}
}

// iRules
var iRules = {
	name: {msg: _lng.iRules.name, regx: /^.{3,128}$/},
	email: {msg: _lng.iRules.email, regx: /^([\w\d_\.\-])+\@(([\w\dáéíóúñ\-])+\.)+([\w\d]{2,4})+$/},
	password: {msg: _lng.iRules.password, regx: /^[\w\d]{4,16}$/},
	phone: {msg: _lng.iRules.phone, regx: /^[\d()+\- ]{7,24}$/},
	number: {msg: _lng.iRules.number, regx: /^\d+$/},
	content: {msg: _lng.iRules.content, regx: /^.{3,}$/m},
	date: {msg: _lng.iRules.date, regx: /^[\d]{2}\/[\d]{2}\/[\d]{4}$/},
	hour: {msg: _lng.iRules.hour, regx: /^([0-9]|[0-1][0-9]|2[0-3]):[0-5]0$/},
	bool: {msg: _lng.iRules.bool, regx: /^0|1$/},
	color: {msg: _lng.iRules.color, regx: /^#([\w\d]{3}|[\w\d]{6})|([\d]{1,3},[\d]{1,3},[\d]{1,3})$/},
	image: {msg: _lng.iRules.image, regx: /\.(jpg|jpeg|gif|png)$/i},
	jpg: {msg: _lng.iRules.jpg, regx: /\.jpg$/i},
	select: {msg: _lng.iRules.bool, regx: /^.+$/},
	price: {msg: _lng.iRules.price, regx: /^[\d]{1,5}(\.[\d]{1,2})?$/},
	dni: {msg: _lng.iRules.dni, regx: /^[\d]{8}$/},
	ruc: {msg: _lng.iRules.ruc, regx: /^[\d]{11}$/},
	code: {msg: _lng.iRules.code, regx: /^[\w\d][\w\d\-]{0,22}[\w\d]$/},
	docs: {regx: /\.(pdf|doc|docx)$/i}
};

// loading
var Loading = {
	init: function(){
		if(!this.initialized){
			this.initialized = true;
			this.overlay = new Element('div',{'id':'loadingOverlay','class':'hidden'}).setOpacity(0.7).inject(document.body);
			this.modal = new Element('div',{'id':'loadingModal','class':'hidden'}).inject(document.body);
			this.body = document.getElement('body');
		}
	},
	set: function(message,klass,full){
		this.modal.className = klass || 'wait';
		this.modal.innerHTML = full ? (message || _lng.wait) : '<p>'+(message || _lng.wait)+'</p>';
		return this;
	},
	show: function(){
		if(!this.open){
			this.open = true;
			this.init();
			this.body.focus();
			if(Browser.Engine.trident4){
				noSelects(true);
				this.body.setStyle('overflow','hidden');
				this.overlay.setStyle('top',document.body.scrollTop+'px');
				this.modal.setStyle('top',(document.body.scrollTop+(document.body.offsetHeight/2))+'px');
			}
			this.overlay.className = '';
			this.body.setStyle('cursor','progress');
			this.set();
		}
	},
	hide: function(){
		if(this.open){
			this.open = false;
			this.overlay.className = 'hidden';
			this.modal.className = 'hidden';
			if(Browser.Engine.trident4){
				this.body.setStyle('overflow','auto');
				noSelects(false);
			}
			this.body.setStyle('cursor','');
		}
	}
};

var Overlay = {
	init: function(){
		if(!this.initialized){
			this.initialized = true;
			this.overlay = new Element('div',{'class':'overlay hidden'}).inject(document.body);
		}
	},
	show: function(ref,opacity,zindex){
		this.init();
		var overlay = (ref||$('body')).getCoordinates();
		var height = Browser.Engine.trident4 ? overlay.height+document.body.scrollTop : overlay.height;
		this.overlay.setStyles({
			'width': overlay.width+'px',
			'height': height+'px',
			'left': overlay.left+'px',
			'top': overlay.top+'px',
			'z-index': zindex || 21,
			'opacity': opacity || 0.5
		});
		this.overlay.removeClass('hidden');
	},
	hide: function(){
		this.overlay.addClass('hidden');
	}
}

// request
var onError = function(){
	alert('Error: '+this.status+'\nPor favor, comunique al administrador.\nGracias');
	Loading.hide();
}

// thumb previews
var ImagesPreview = new Class({
	Extends: Tips,
	attach: function(elements){
		$$(elements).each(function(element){
			var src = element.get('src');
			if(!src.test(/none\.jpg$/)){
				if(this.options.replace){
					src = src.replace(this.options.replace[0],this.options.replace[1]);
				}
				var title = element.retrieve('tip:title', '<img src="'+src+'" />');
				var text = element.retrieve('tip:text', '');
				var enter = element.retrieve('tip:enter', this.elementEnter.bindWithEvent(this, element));
				var leave = element.retrieve('tip:leave', this.elementLeave.bindWithEvent(this, element));
				element.addEvents({mouseenter: enter, mouseleave: leave});
				if (!this.options.fixed){
					var move = element.retrieve('tip:move', this.elementMove.bindWithEvent(this, element));
					element.addEvent('mousemove', move);
				}
				element.store('tip:native', element.get('src'));
				element.erase('title');
			}
		}, this);
		return this;
	}
});

// misc
function utf8d(a){var b=[],i=ac=c1=c2=c3=0;a+='';while(i<a.length){c1=a.charCodeAt(i);if(c1<128){b[ac++]=String.fromCharCode(c1);i++}else if((c1>191)&&(c1<224)){c2=a.charCodeAt(i+1);b[ac++]=String.fromCharCode(((c1&31)<<6)|(c2&63));i+=2}else{c2=a.charCodeAt(i+1);c3=a.charCodeAt(i+2);b[ac++]=String.fromCharCode(((c1&15)<<12)|((c2&63)<<6)|(c3&63));i+=3}}return b.join('')}function b64d(a){var b="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";var c,o2,o3,h1,h2,h3,h4,bits,i=ac=0,dec="",tmp_arr=[];a+='';do{h1=b.indexOf(a.charAt(i++));h2=b.indexOf(a.charAt(i++));h3=b.indexOf(a.charAt(i++));h4=b.indexOf(a.charAt(i++));bits=h1<<18|h2<<12|h3<<6|h4;c=bits>>16&0xff;o2=bits>>8&0xff;o3=bits&0xff;if(h3==64){tmp_arr[ac++]=String.fromCharCode(c)}else if(h4==64){tmp_arr[ac++]=String.fromCharCode(c,o2)}else{tmp_arr[ac++]=String.fromCharCode(c,o2,o3)}}while(i<a.length);dec=tmp_arr.join('');dec=utf8d(dec);return dec}
