/**
 * Clase para manejar el listado de favoritos
 */

var Favoritos = {
    idUsuario: null,
    lang: 0,
    
    requestUrl: '/rpc/favoritos.php',
    sending: false,
    idFicha: null,

    link: null,
    
    addText: null,
    delText: null,
    linkClass: ' list_selected',
        
    advise: null,
    
    interval: null,
    iCount: 0,
    
    ieTop: 0
}


/* Definición de métodos \______________________
\_____________________________________________*/

// Añadir textos de reemplazo para los enlaces
Favoritos.setAddText = function( text ) {
    this.addText = text;
}

Favoritos.setDelText = function( text ) {
    this.delText = text;
}

Favoritos.setLang = function( lang ) {
    this.lang = lang;
}

// Envío de la petición
Favoritos.send = function( elem, idFicha, option ) {
    if( this.sending ) {
        return false;
    }

    this.link  = elem;
    
    var callback = 'Favoritos.getResponse';
    if( option == 'delete' ) {
        callback = 'Favoritos.deleteRow';
        this.idFicha = idFicha;
    } else if( option == 'delete_map' ) {
        callback = 'Favoritos.deleteMapRow';
        this.idFicha = idFicha;
    }
    $.ajax({
            type: "GET",
            url: this.requestUrl,
            data: { idFicha: idFicha },
            success: function (data){
                eval(callback + "( data )");
                }
            });

    this.sending = true;
}


// Respuesta en el listado de favoritos para eliminar la línea
Favoritos.deleteRow = function() {
    var house = document.getElementById( 'file' + this.idFicha );
    var list = house.parentNode;
    
    list.removeChild( house );
    
    var headText = document.getElementById('list_link').firstChild;
    headText.data = headText.data.replace(/\d+/, this.subCount );
    
    var headerText = document.getElementById('fav_header').firstChild;
    headerText.data = headerText.data.replace(/\d+/, this.subCount );
    
    if( list.getElementsByTagName('li').length == 0 ) {
        list.parentNode.removeChild( list );
    }
    
    this.idFicha = null;
    this.sending = false;
}

// Respuesta en el mapa de favoritos para eliminar la casa
Favoritos.deleteMapRow = function() {
    //map.closeInfoWindow();

    var link = document.getElementById( 'file' + this.idFicha );
    
    link.className = 'resultlink add_to_list';
    
    var headText = document.getElementById('list_link').firstChild;
    headText.data = headText.data.replace(/\d+/, this.subCount );
    
    var headerText = document.getElementById('fav_header').firstChild;
    headerText.data = headerText.data.replace(/\d+/, this.subCount );
    
    this.idFicha = null;
    this.sending = false;
}

// Creación de la caja de aviso de nuevo elemento en la lista
Favoritos.buildAdvise = function()  {
    var container = document.createElement('div');
    
    var layer = document.createElement('div');
    layer.id = 'aviso_lista';
    layer.className= 'aviso_l' + this.lang;
    
    layer.style.opacity = 0;
    layer.style.filter = 'alpha(opacity=0)';
    
    container.appendChild( layer );
    
    container.style.cssFloat = 'right';
    container.style.width = '303px';
        
    if( navigator.appName == 'Microsoft Internet Explorer' ) {
        container.style.position = 'absolute';
        container.style.right = 0;
        layer.style.right = 0;
        layer.style.position = 'absolute';
        
        onscroll = function() {
            if( document.getElementById('aviso_lista') ) {
                document.getElementById('aviso_lista').style.top = document.documentElement.scrollTop + Favoritos.ieTop;
            }
        }
    }
    
    this.advise = container;
}


// Establece el intervalo necesario para mostrar el aviso con efecto fade
Favoritos.showAdvise = function() {
    if( this.iCount > 0 ) {
        if( this.iCount > 40 ) {
            this.iCount = 80 - this.iCount;
        }
        return false;
    }
    if( !this.advise ) {
        this.buildAdvise();
    }
    
    $('.area_user').append( this.advise );
    
    if( navigator.appName == 'Microsoft Internet Explorer' ) {
        var header = document.getElementById('header');
        var aviso = document.getElementById('aviso_lista');
        this.ieTop = aviso.parentNode.offsetTop;
        
        if( document.documentElement.scrollTop ) {
            aviso.style.top = document.documentElement.scrollTop + this.ieTop;
        }
    }
    
    this.interval = setInterval( 'Favoritos.fade()', 50 );
    
}

// Métodos de respuesta para el reemplazo del número de alojamientos
Favoritos.addCount = function( num ) {
    return ++num;
}

Favoritos.subCount = function( num ) {
    return --num;
}


// Ejecuta cada intervalo de desvanecimiento del aviso
Favoritos.fade = function() {
    elem = document.getElementById('aviso_lista');
    
    var opacity = 15 - Math.abs( this.iCount - 15 );
    if( opacity >= 0 && opacity <= 10 ) {
    
        elem.style.opacity = opacity/10;
        elem.style.filter = 'alpha(opacity=' + opacity*10 + ')';
    }
    
    this.iCount++;
    if( opacity < 0 ) {
        clearInterval( this.interval );
        elem.parentNode.parentNode.removeChild(elem.parentNode);
        this.iCount = 0;
    }
}

