(function(){
	var util = window.util = {
		init: function() {
			this.asegurar();
			setTimeout("util.auto_hide_elements()", 4500);
		},
		go_to: function(url) {
			window.location = url;
		},
		get_relpath: function() {
			return _relpath;
		},
		clear_number: function(num) {
			if (undefined == num) return;
			return parseFloat((num.toString().replace(/[^0-9\.\-]/, '') || 0));
		},
		number_format: function(number, decimals, dec_point, thousands_sep) {
		    var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
		    var d = dec_point == undefined ? "." : dec_point;
		    var t = thousands_sep == undefined ? "," : thousands_sep, s = n < 0 ? "-" : "";
		    var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
		    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
		},
		round: function(val, precision) {
		    return parseFloat(parseFloat(val).toFixed(precision));
		},
		round_and_format: function(number, precision) {
			var p = precision == undefined ? 2 : precision;
			return util.number_format(util.round(number, p));
		},
		currency_format: function(num) {
			num = util.clear_number(num);
			return '$' + util.round_and_format(num);
		},
		ucwords: function(str) {
		    return (str + '').replace(/^(.)|\s(.)/g, function($1) { return $1.toUpperCase(); });
		},
		range: function(value, ranges) {
			for (var i in ranges) {
				var r = ranges[i];
				if (value >= r.min && (value <= r.max || r.max == -1)) return r;
			}
			return null;
		},
		asegurar: function() {
			$('input.warning').click(function() {
				var text_confirm = '¿Seguro?';
				if (this.value != text_confirm) {
					$(this).attr('rel', 'unlocked');
					this.value = text_confirm;
					return false;
				}
			});

			$('a.warning').click(function() {
				var text_confirm = '¿Seguro?';
				if (this.firstChild.nodeValue != text_confirm) {
					$(this).attr('rel', 'locked');
					this.firstChild.nodeValue = text_confirm;
					return false;
				}
			});
		},
		auto_hide_elements: function() {
			$('div.auto_hide').hide();
		},
		is_in: function(haystack, needle) {
			for (key in haystack) if (needle == haystack[key]) return key;
		},
		get_option_selected: function(select) {
			var option = $(select).find('option:selected')[0];
			if (option) return {'value': option.value, 'text': option.text};
		},
		get_options_selected: function(select) {
			var options = [];
			$(select).find('option:selected').each(function(i, option) {
				options.push({'value': option.value, 'text': option.text});
			});
			return options;
		},
		select_options: function(select, values, unselect_all_before) {
			$(select).find('option').each(function(i, option) {
				if (unselect_all_before) option.selected = false;
				if (util.is_in(values, option.value)) option.selected = true;
			});
		},
		unselect_options: function(select, values) {
			$(select).find('option').each(function(i, option) {
				if (util.is_in(values, option.value)) option.selected = false;
			});
		},
		place_holder: function(element, text) {
			var e = $(element);

			e.focus(function() {
				if (e.attr('value') == text) {
					e.attr('value', '');
					e.removeClass('inactive_field');
				}
			});

			e.blur(function() {
				if (e.attr('value') == text || e.attr('value') == '') {
					e.attr('value', text);
					e.addClass('inactive_field');
				}
			});

			e.blur();
		},
		populate_select: function(select, items) {
			select = $(select);
			this.reset_select(select);
			$.each(items, function(i,item) {
				var option = document.createElement('option');
				option.setAttribute('value', item.value);
				if (item.text) option.appendChild(document.createTextNode(item.text));
				select.append(option);
			});
		},
		remove_all_childs: function(element) {
			while ($(element).firstChild) {
				$(element).removeChild($(element).firstChild);
			}
		},
		reset_select: function(select) {
			$(select).empty();
		},
		popup: function(url, options) {
			var pos_left = (screen.width - options.width) / 2;
			var pos_top = (screen.height - options.height) / 2;
			var win_properties = 'width='+options.width+',height='+options.height+',left='+pos_left+',top='+pos_top+'toolbar=0,location=0,directories=0,menubar=0,resizable=0,scrollbars=auto';
			Win = window.open(url, (options.name || ''), win_properties);
			Win.window.focus();
		},
		mt_rand: function(min, max) {
		    var argc = arguments.length;
		    if (argc === 0) {
		        min = 0;
		        max = 2147483647;
		    } else if (argc === 1) {
		        throw new Error('Warning: mt_rand() expects exactly 2 parameters, 1 given');
		    }
		    return Math.floor(Math.random() * (max - min + 1)) + min;
		},
		array_unique: function(array) {
		   var tem_arr = new Array();
		    for (i=0; i<array.length; i++) {
		        if (!util.in_array(array[i], tem_arr)) tem_arr.push(array[i]);
		    }
		    return tem_arr;
		},
		in_array: function(needle, haystack) {
		    var key = '';
	        for (key in haystack) if (haystack[key] == needle) return true;
		    return false;
		},
		push: function(element, array) {
			if (!util.in_array(element, array)) array.push(element);
			return array;
		}
	};
	
	// jQuery.jQueryRandom = 0;
	// jQuery.extend(jQuery.expr[":"], {
	//     random: function(a, i, m, r) {
	//         if (i == 0) {
	//             jQuery.jQueryRandom = Math.floor(Math.random() * (r.length-1)) + 1;
	// 			console.debug(jQuery.jQueryRandom);
	//         };
	//         return i == jQuery.jQueryRandom;
	//     }
	// });

	$(function(){
		util.init();
	});
})();

jQuery.url=function(){var segments={};var parsed={};var options={url:window.location,strictMode:false,key:["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],q:{name:"queryKey",parser:/(?:^|&)([^&=]*)=?([^&]*)/g},parser:{strict:/^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,loose:/^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/}};var parseUri=function(){str=decodeURI(options.url);var m=options.parser[options.strictMode?"strict":"loose"].exec(str);var uri={};var i=14;while(i--){uri[options.key[i]]=m[i]||""}uri[options.q.name]={};uri[options.key[12]].replace(options.q.parser,function($0,$1,$2){if($1){uri[options.q.name][$1]=$2}});return uri};var key=function(key){if(!parsed.length){setUp()}if(key=="base"){if(parsed.port!==null&&parsed.port!==""){return parsed.protocol+"://"+parsed.host+":"+parsed.port+"/"}else{return parsed.protocol+"://"+parsed.host+"/"}}return(parsed[key]==="")?null:parsed[key]};var param=function(item){if(!parsed.length){setUp()}return(parsed.queryKey[item]===null)?null:parsed.queryKey[item]};var setUp=function(){parsed=parseUri();getSegments()};var getSegments=function(){var p=parsed.path;segments=[];segments=parsed.path.length==1?{}:(p.charAt(p.length-1)=="/"?p.substring(1,p.length-1):path=p.substring(1)).split("/")};return{setMode:function(mode){strictMode=mode=="strict"?true:false;return this},setUrl:function(newUri){options.url=newUri===undefined?window.location:newUri;setUp();return this},segment:function(pos){if(!parsed.length){setUp()}if(pos===undefined){return segments.length}return(segments[pos]===""||segments[pos]===undefined)?null:segments[pos]},attr:key,param:param}}();
