/**
 * @author gonzalo
 */
var read_permission = 0;
var write_permission = 1;
var admin_permission = 2;

var public_privacy = 0;
var friends_privacy = 1;
var private_privacy = 2;

var pending_notice_status = 0;
var read_notice_status = 1;
var sent_notice_status = 2;
var deleted_notice_status = 3;

var pending_diary_status = 0;
var rejected_diary_status = 1;
var accepted_diary_status = 2;
var finished_diary_status = 3;

var heartbeats_interval = 60000;
var long_interval = 60000;

hq_alert = function(text){
    if (parent != undefined) {
        var target = parent;
    }
    else {
        var target = window;
    };
    if (target.Mediabox != undefined) {
        var div = new Element('div', {
            'id': 'mb_alert'
        }).inject($(target.document.body));
		
        var text = new Element('div', {
            'id': 'mbText',
            'html': text,
            'styles': {
                'background': 'transparent url("/js/mediabox/images/logo.png") no-repeat 5px 5px',
				'padding-top': '50px',
				'width': '360px'
            }
        }).inject(div);
        
        target.Mediabox.open('#mb_alert', 'Mensaje de Hurriquest');
        
    }
    else {
        alert(text);
    }
}

hq_confirm = function(text, yes_function, no_function) {
	if (parent != undefined) {
        var target = parent;
    }
    else {
        var target = window;
    };
    if (target.Mediabox != undefined) {
        var div = new Element('div', {
            'id': 'mb_confirm'
        }).inject($(target.document.body));
		
        var body = new Element('div', {
            'id': 'mbBody',
            'styles': {
                'background': 'transparent url("/js/mediabox/images/logo.png") no-repeat 5px 5px',
				'padding-top': '50px',
				'width': '360px'
            }
        }).inject(div);
		
		var text = new Element('div', {
			'html': text
		}).inject(body);
		
		var controls = new Element('div', {
			'styles': {
				'width': '90%',
				'text-align': 'right'
			}
		}).inject(body);
		
        var yes_a = new Element('a', {
            'href': 'javascript:void(0);'
        });
		var yes_button = new Element('img', {
            'src': '/js/mediabox/images/SonicYes.gif'
        }).addEvent('click', function(event){
			yes_function();
			target.Mediabox.close();
		}).inject(yes_a);
		
		var no_a = new Element('a', {
            'href': 'javascript:void(0);'
        });
		var no_button = new Element('img', {
			'src': '/js/mediabox/images/SonicNo.gif'
		}).inject(no_a);
		no_button.addEvent('click', function(event){no_function(); target.Mediabox.close();});
        
        target.Mediabox.open('#mb_confirm', "Confirmación");
		var mbCloseLink = target.$('mbCloseLink');
		mbCloseLink.empty();
		mbCloseLink.adopt(yes_a, no_a);
    }
    else {
        confirm(text);
    }
}

get_selected_value = function(radio_group){
    for (i = 0; i < radio_group.length; i++) {
        if (radio_group[i].checked) {
            return radio_group[i].value;
        }
    }
}

set_selected_value = function(radio_group, value){
    for (i = 0; i < radio_group.length; i++) {
        if (radio_group[i].value == value) {
            radio_group[i].checked = true;
            break;
        }
    }
}

set_selected_option = function(select, value){
    for (var i = 0; i < select.options.length; i++) {
        if (select.options[i].value == value) {
            select.selectedIndex = i;
        }
    }
}

toggle_disabled = function(el){
    try {
        el.disabled = true;
    } 
    catch (E) {
    }
    
    if (el.childNodes && el.childNodes.length > 0) {
        for (var x = 0; x < el.childNodes.length; x++) {
            toggle_disabled(el.childNodes[x]);
        }
    }
}

check_integer = function(input){
    if (isNaN(input.value) || input.value == '') {
        input.value = "0";
        input.focus();
    }
}

random_range = function(min, max){
    return (min + Math.random() * (max - min));
}

random_string = function(string_length){
    if (string_length == null) {
        string_length = 8;
    }
    var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
    var randomstring = '';
    for (var i = 0; i < string_length; i++) {
        var rnum = Math.floor(Math.random() * chars.length);
        randomstring += chars.substring(rnum, rnum + 1);
    }
    return randomstring;
}

/* Funcion para formatear el tamano de los archivos */
format_file_size = function(size){
    var strReturn;
    size = size / 1000;
    strReturn = size.toFixed(1) + " KB";
    if (size > 1000) {
        size = size / 1000;
        strReturn = size.toFixed(1) + " MB";
        if (size > 1000) {
            size = size / 1000;
            strReturn = size.toFixed(1) + " GB";
        }
    }
    return strReturn;
}

format_date = function(seconds){
    if (isNaN(seconds)) {
        return seconds + " segundos";
    }
    else {
        var strReturn = Math.floor(seconds % 60) + " segundos";
    }
    var minutes = Math.floor(seconds / 60);
    if (minutes > 0) {
        strReturn = Math.floor(minutes % 60) + " minutos " + strReturn;
    }
    var hours = Math.floor(hours / 60);
    if (hours > 0) {
        strReturn = Math.floor(hours % 60) + " horas " + strReturn;
    }
    return strReturn;
}

Element.implement({
    ellipsis: function(enableUpdating){
        var s = this.getStyles();
        if (!('textOverflow' in s || 'OTextOverflow' in s)) {
            var el = this;
            if (el.getStyle("overflow") == "hidden") {
                var originalText = el.get('html');
                var w = el.get('width');
                
                var t = this.clone().setStyles({
                    'display': 'none',
                    'position': 'absolute',
                    'width': 'auto',
                    'overflow': 'visible',
                    'max-width': 'inherit'
                });
                t.inject(el, 'after');
                
                var text = originalText;
                while (text.length > 0 && t.getDimensions().x > el.getDimensions().x) {
                    text = text.substr(0, text.length - 1);
                    t.set('html', text + "...");
                }
                
                el.set('html', t.get('html'));
                
                t.dispose();
                
                if (enableUpdating == true) {
                    var oldW = el.getStyle('width');
                    setInterval(function(){
                        if (el.getStyle('width') != oldW) {
                            oldW = el.getStyle('width');
                            el.set('html', originalText);
                            el.ellipsis();
                        }
                    }, 200);
                }
            }
        }
        else 
            return this;
    },
    
    visible: function(){
        return this.style.display != 'none';
    },
    
    toggle: function(){
        this[this.visible() ? 'hide' : 'show']();
        return this;
    },
    
    hide: function(){
        this.style.display = 'none';
        return this;
    },
    
    show: function(){
        this.style.display = '';
        return this;
    }
});

function desvanecer(elemento){
	var efecto = new Fx.Tween($(elemento));
  efecto.start('opacity', '0').chain(function(){$(elemento).setStyle('display','none');});
}
