/*  Yerbabuena Software SCA, 2008  |  www.yerbabuena.es
 * ----------------------------------------------------
 *
 * The DHTML Calendar, version 1.2
 *
 * This script is developed by YSENGINEERS
 *
 * This script is distributed under the GNU Lesser General Public License.
 * Read the entire license text here: http://www.gnu.org/licenses/lgpl.html
 */

// $Id: planning_macro.js 3823 2008-09-09 15:28:26Z davidmacias $

/** The Planning object constructor. */
// Global Variables
var pulsed;
var day_after;
var div_top;
var div_left;
var day_now;
var can_move=false;
var div_up;
var id_event;
var days_week = new Array();
var open_options;
var nav = navigator.appName;
var permission=false;

// Impide que se seleccione texto en IE4+
document.getElementById('window_scroll_div').onselectstart=new Function ("return false")

function pulsa_sombrea(id, hourStart, dayYear, dayMonth, dayDay, days){
    pulsed = true;
    day_year = dayYear;
    day_month = dayMonth;
    day_day = dayDay;
    hour_start = hourStart;
    //Borra el sombreado y cierra la ventana de creacion de evento si ya estaba abierta antes
    clearPlanning();
    cancelar_evento();
    document.getElementById(id).style.backgroundColor='#EAEAEA';
    //#FDF8BE
}// End pulsa_sombrea()

function pasa_sombrea(id){

    // Obtengo la hora de la celda donde tengo el ratón
    hour_now = id.replace("div","");
    hour_now = hour_now.split('.');
    hour_now = hour_now[0] + '.' + hour_now[1].charAt(0);
    
    // Obtengo el día de la celda donde tengo el ratón
    day_now = id.replace("div","");
    day_now = day_now.split('.');
    if (day_now[1].charAt(0) == '0'){
    	day_now = day_now[1].replace("0","");
    }else{
    	day_now = day_now[1].replace("5","");
    }    
    
        if (can_move){
                div_up = id;
                
                clearPlanning();

                // Sombrea la celda sobre la que estoy posicionado cuando muevo el evento
                id_div = 'div' + hour_now  + day_now;
                document.getElementById(id_div).style.backgroundColor='#EAEAEA';
        }

        if (pulsed && !can_move && !open_options){
                start = parseFloat(hour_start);
                now = parseFloat(hour_now);

                if (day_now == day_day){

                        clearPlanning();

		                //Sombrea las celdas cuando muevo el raton desde la hora que he hecho clic hacia arriba
		                if (start > now){
		                        //document.getElementById('view').innerHTML = 'START: ' + start + ' NOW: ' + now;
		                        for (var i = start; i >= now; i-=0.5){
		                        	 	if (i % 1 == 0) {
						                   id_div = 'div' + i + '.0' + day_day;
						                } else {
						                   id_div = 'div' + i + day_day;    
						                }
		                                document.getElementById(id_div).style.backgroundColor='#EAEAEA';
		                        }
		                }
		
		                //Sombrea las celdas cuando muevo el raton desde la hora que he hecho clic hacia abajo
		                if (start < now){
		                        //document.getElementById('show_cell').innerHTML = 'START: ' + float_start + ' NOW: ' + float_now;
		                        for (var i = start; i <= now; i+=0.5){
		                        		if (i % 1 == 0) {
						                   id_div = 'div' + i + '.0' + day_day;
						                } else {
						                   id_div = 'div' + i + day_day;    
						                }
						                document.getElementById(id_div).style.backgroundColor='#EAEAEA';
		                        }
		                }
		
		                        //Sombrea la celda en la que hago clic
		                if (hour_start == hour_now){
		                    document.getElementById(id).style.backgroundColor='#EAEAEA';
		                }
		        }
        }
}// End pasa_sombrea()

function up_mouse(hourEnd, absolute_url, calendar_url, divWidth, divHeight, divTop, dayDay, dayMonth, dayYear){

        if (can_move){
                // Asigno el mes y el año de la celda en la que levanto el ratón
                if (div_up){
                	day_month = dayMonth;
	                day_year = dayYear;
	                document.location.href = document.location + '/moveEvent?id=' + id_event + '&div=' + div_up + '&year=' + day_year + '&month=' + day_month;
                }
               	//can_move = false;
        }

        if (pulsed && !can_move && !open_options){
        	
                pulsed = false;
                hour_end = hourEnd;
                absoluteUrl = absolute_url;
            	calendarUrl = calendar_url;
            	day_after = dayDay;

                day_top = document.getElementById('day_'+dayDay).offsetTop;
                popup_top = ((divTop-60)+day_top).toString()+'px';
                popup_left = div_left - document.getElementById('window_scroll_div').offsetLeft - 40;
                
            if (day_now == day_day && permission){
                // Asigno posicion del div de creacion y lo muestro
                	if (popup_left > 600){
                	popup_left = popup_left - 290;
                	popup_left = popup_left.valueOf() + 'px';	
                    document.getElementById('event_title_div').style.top=popup_top;
                    document.getElementById('event_title_div').style.left=popup_left;
                    document.getElementById('create_event_corner1').style.display = "none";
         			document.getElementById('create_event_corner2').style.display = "inline";
                	}else{
                	popup_left = popup_left.valueOf() + 'px';	
                    document.getElementById('event_title_div').style.top=popup_top;
                    document.getElementById('event_title_div').style.left=popup_left;
                    document.getElementById('create_event_corner2').style.display = "none";
         			document.getElementById('create_event_corner1').style.display = "inline";
                	}
                    document.getElementById('event_title_div').style.display = 'inline';
                    document.form_event_create.event_title.focus();

                    // Muestra la fecha en el div de creacion de evento
                    document.getElementById('date').innerHTML = formatDate(day_year, day_month, day_day);
             
              }else{
                clearPlanning();
            }
        }

        pulsed = false;
        open_options = false;

}// End up_mouse()

function clearPlanning(){

        // Elimino el sombreado
        for (var i = 0; i < days_week.length; i++){
                for (j = 8; j<20; j++){
            id_div = 'div' + j + '.0' + days_week[i];
            document.getElementById(id_div).style.backgroundColor='';
            id_div = 'div' + j + '.5' + days_week[i];
            document.getElementById(id_div).style.backgroundColor='';
        }
        }

}

function mouseDown(id){

        // Asigno el id del evento que quiero mover y asigno true a can_move.

        id_event = id;
        can_move = true;
}

function formatDate(year, mes, dia){

    //Devuelve la fecha en formato, Ej: Lunes 1 de Enero

    int_mes = mes.valueOf() - 1;
    mydate = new Date(year,int_mes,dia);
    dia_semana = mydate.getDay();

    if (dia_semana == '0'){
        dia_semana = 'Domingo';
    }else if(dia_semana == '1'){
        dia_semana = 'Lunes';
    }else if(dia_semana == '2'){
        dia_semana = 'Martes';
    }else if(dia_semana == '3'){
        dia_semana = 'Mi&eacute;rcoles';
    }else if(dia_semana == '4'){
        dia_semana = 'Jueves';
    }else if(dia_semana == '5'){
        dia_semana = 'Viernes';
    }else if(dia_semana == '6'){
        dia_semana = 'S&aacute;bado';
    }

    if (mes == '1'){
        mes = 'Enero';
    }else if(mes == '2'){
        mes = 'Febrero';
    }else if(mes == '3'){
        mes = 'Marzo';
    }else if(mes == '4'){
        mes = 'Abril';
    }else if(mes == '5'){
        mes = 'Mayo';
    }else if(mes == '6'){
        mes = 'Junio';
    }else if(mes == '7'){
        mes = 'Julio';
    }else if(mes == '8'){
        mes = 'Agosto';
    }else if(mes == '9'){
        mes = 'Septiembre';
    }else if(mes == '10'){
        mes = 'Octubre';
    }else if(mes == '11'){
        mes = 'Noviembre';
    }else if(mes == '12'){
        mes = 'Diciembre';
    }
    
    return dia_semana + ' ' + dia + ' de ' + mes;
}// End formatDate()

function crear_evento(){
    // Cambio las horas por si empiezo a seleccionar celdas desde abajo
        if (parseFloat(hour_end)<parseFloat(hour_start)){
                var aux = hour_start;
                hour_start = parseFloat(hour_end) - 1.0;
                hour_end = parseFloat(aux) + 1.0;
        }

    title = document.getElementById('event_title').value;
    elements = document.getElementById('form_event_create').elements;
    var categories = new Array();
    for (var i = 0; i < elements.length; i++){
    	if (elements[i].checked){
    		categories.push(elements[i].value);
    	}
    }
    pagina = calendarUrl + '/eventMethod?title=' + title + '&hour_start=' + hour_start + '&hour_end=' + hour_end + '&day_year='+ day_year + '&day_month=' + day_month + '&day_day=' + day_day + '&calendar_url=' + calendarUrl + '&categories=' + categories;
        
    if (trim(title) != ''){
        document.location.href=pagina;
    }

}// End crear_evento()

function cancelar_evento(){
	clearPlanning();
    document.getElementById('event_categories_div').style.display = 'none';
    document.getElementById('event_title_div').style.display = 'none';
    
}// End cancelar_evento()

function trim(cadena){
    //Quita espacios en blanco al principio y al final de la cadena
    
    for(i=0; i<cadena.length; ){
        if(cadena.charAt(i)==" "){
            cadena=cadena.substring(i+1, cadena.length);
        }else{
            break;
        }
    }

    for(i=cadena.length-1; i>=0; i=cadena.length-1){

        if(cadena.charAt(i)==" ")
                    cadena=cadena.substring(0,i);
        else
            break;
    }

    return cadena
}// End trim()

function mostrar_types(id){

    open_options = true;
    div_id = 'types_' + id;
    visible = document.getElementById(div_id).style.display;
    options_id = 'options_' + id;
    visible_options = document.getElementById(options_id).style.display;
       
    if (visible_options == 'inline'){
       	document.getElementById(options_id).style.display = 'none';
    }

    if (visible == 'none'){
    	document.getElementById(div_id).style.display = 'inline';
    }else{
    	document.getElementById(div_id).style.display = 'none';
    }

}// End mostrar_types()

function mostrar_opciones(id){

        open_options = true;
        div_id = 'options_' + id 
        visible = document.getElementById(div_id).style.display;
        types_id = 'types_' + id
        visible_types = document.getElementById(types_id).style.display;
        
        if (visible_types == 'inline'){
        	document.getElementById(types_id).style.display = 'none';
        }

        if (visible == 'none'){
        		document.getElementById(div_id).style.display = 'inline';
        }else{
        		document.getElementById(div_id).style.display = 'none';
        }

}// End mostrar_opciones()

function mostrar_categorias_popup(){

	div = document.getElementById('event_categories_div');
	visible = div.style.display;
	
	if (visible == 'none'){
		div.style.display = 'inline';
	}else{
		div.style.display = 'none';
	}

}// End mostrar_categorias_popup

function mostrar_dia(id){

        visible = document.getElementById(id).style.display;

        if (visible == 'none'){
        		document.getElementById(id).style.display = 'inline';
        }else{
        		document.getElementById(id).style.display = 'none';
        }

}// End mostrar_dia()

function dayOver(id){
        if (can_move){
        		document.getElementById(id).style.display = 'inline';
        }
}

function setXYMouse(ev){
	if (nav == 'Netscape'){
		div_left = ev.clientX;
		div_top = ev.clientY;
	}else{
		div_left = event.clientX;
		div_top = event.clientY;
	}

}

document.getElementById('window_scroll_div').onmousemove = setXYMouse;