
var foto_id = "";
function OnMouseDown(e)
{
	
	
	// IE is retarded and doesn't pass the event object
	if (e == null) 
		e = window.event; 
	
	// IE uses srcElement, others use target
	var target = e.target != null ? e.target : e.srcElement;
	
	//_debug.innerHTML = (target.className == 'drag')? 'draggable element clicked' : 'NON-draggable element clicked';

	// for IE, left click == 1
	// for Firefox, left click == 0



	if ((e.button == 1 && window.event != null || e.button == 0) && target.className == 'drag'){
		// grab the mouse position
		
		
		_startX = e.clientX;
		_startY = e.clientY;
		
		// grab the clicked element's position
	
		
		_offsetX = ExtractNumber(target.style.left);
		_offsetY = ExtractNumber(target.style.top);
		
		foto_id = target.id.substr(3,target.id.length);
		
		// bring the clicked element to the front while it is being dragged
		_oldZIndex = target.style.zIndex;
		target.style.zIndex = 10000;
		
		// we need to access the element in OnMouseMove
		_dragElement = target;

		// tell our code to start moving the element with the mouse
		document.onmousemove = OnMouseMove;
		
		// cancel out any text selections
		document.body.focus();
		
		
		// prevent text selection in IE
		document.onselectstart = function () { return false; };
		// prevent IE from trying to drag an image
		target.ondragstart = function() { return false; };
		
		// prevent text selection (except IE)
		return false;
	}
}

function ExtractNumber(value)
{
	var n = parseInt(value);
	
	return n == null || isNaN(n) ? 0 : n;
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function getCoor(image) {
    image = document.getElementById(image);  
	var x = (document.layers) ? image.x : image.offsetLeft;
	var y = (document.layers) ? image.y : image.offsetTop;

	return [x , y];
}


function OnMouseMove(e)
{
    
	if (e == null) 
		var e = window.event; 

	// this is the actual "drag code"
	
	_scroll = getScrollXY();
	
	_ref = document.getElementById("img"+foto_id);
	_foto_pos = getCoor("ref"+foto_id);
	
	desp = (navigator.appName=="Netscape")?0:(_ref.width/2);
    _x1 = _foto_pos[0]-desp;
	_x2 = _foto_pos[0]+_ref.width-desp;
	
	_y1 = _foto_pos[1];
	_y2 = _foto_pos[1]+_ref.height;
	
		
    _px = e.clientX;
    _py = e.clientY + _scroll[1];
    
    if(_x1<_px && _px<_x2)
	    _dragElement.style.left = (_px-20) + 'px';
	if(_y1<_py && _py<_y2)
	    _dragElement.style.top = (_py-9) + 'px';
	    

	    
	//_debug.innerHTML = '(' + _py + ')';	
	
}

function ubicarMiras(){
    for(i=0;i<ubicarMiraArr.length;i++){
        px=ubicarMiraArr[i][0];
        py=ubicarMiraArr[i][1];
        ida=ubicarMiraArr[i][2];
        
        _mira = document.getElementById("dr_"+ida);
        _ref = document.getElementById("img"+ida);
    	_foto_pos = getCoor("ref"+ida);
    	
    	desp = (navigator.appName=="Netscape")?0:(_ref.width/2);
    	_x1 = _foto_pos[0]-desp;
    	_x2 = _foto_pos[0]+_ref.width-desp;
        
           	
    	
    	_y1 = _foto_pos[1];
    	_y2 = _foto_pos[1]+_ref.height;
    	
    	_px = (px * (_x2-_x1) / 100)+_x1;
    	_py = (py * (_y2-_y1) / 100)+_y1;
    	
    //if(i==1) alert(_px) 
    	
        _mira.style.left = (_px-20) + 'px';
    	_mira.style.top = (_py-9) + 'px';
    	_mira.style.visibility  = "visible";
    }
    
}

function OnMouseUp(e)
{
	if (_dragElement != null)
	{
		

		_dragElement.style.zIndex = _oldZIndex;

		// we're done with these events until the next OnMouseDown
		document.onmousemove = null;
		document.onselectstart = null;
		_dragElement.ondragstart = null;

		// this is how we know we're not dragging
		_dragElement = null;
		
		_ref = document.getElementById("img"+foto_id);
    	_foto_pos = getCoor("ref"+foto_id);
    	
    	_x1 = _foto_pos[0];
    	_x2 = _foto_pos[0]+_ref.width;
    	
    	_y1 = _foto_pos[1];
    	_y2 = _foto_pos[1]+_ref.height;
	    
	    _mira = document.getElementById("dr_"+foto_id);
	    _mx = ExtractNumber(_mira.style.left)+20;
	    _my = ExtractNumber(_mira.style.top)+9;
	    
	
		_px = (_mx-_x1)*100/(_x2-_x1);
		_py = (_my-_y1)*100/(_y2-_y1);
		
		desp = (navigator.appName=="Netscape")?0:((_ref.width/2)-30);
		actualizar('_archivo_centro.php?id='+foto_id+'&px='+parseInt(_px+desp)+'&py='+parseInt(_py));
		//_debug.innerHTML = '_px: '+parseInt(_px)+' _py: '+parseInt(_py);
	}
}





function actualizar(url){
    
    oldsc=document.getElementById("old_sc");
    if(oldsc)
        document.getElementsByTagName('body')[0].removeChild(oldsc);
    sc=document.createElement('script');
    sc.id="old_sc";
    sc.src=url+'&'+Math.random();
    document.getElementsByTagName('body')[0].appendChild(sc);
}

function getGroupOrder() {
	var elementos = document.getElementsByClassName('section');
	var alerttext = '';
	for(var i=0;i<elementos.length;i++){
        if(alerttext!='') alerttext +=',';
		if((elementos[i].id != "") && (elementos[i].className == "section") ){
			var item = elementos[i].id;
			item = item.replace("item_","");
		    alerttext +=  item;
		}    						
	}						
    
	return alerttext;
}

function moverABandeja(e){                        
    if(e.substr(0,8)=="item_rec") 
        //if(confirm("¿Está seguro que desea MOVER este contenido a la Bandeja de entrada?")){
            actualizar("_mover_a_bandeja.php?ide="+e+"&idc="+idc+"&eer="+eer+"&lim="+lim+"&p="+p);
            //Effect.SlideUp(e,{duration:1.0});
        //}                            
}

function duplicar(e){                        
    if(confirm("¿Está seguro que desea DUPLICAR este contenido?")){
        actualizar("_duplicar.php?ide="+e+"&idc="+idc+"&eer="+eer+"&lim="+lim+"&p="+p);
    }                            
}

function sin_permisos(){                        
    alert("No tienes los permisos necesarios para utilizar esta funcionalidad");
    void(0);                          
}


function relacionarContenidos(e){ 
    
    if(e.substr(0,8)=="item_rel") 
        //if(confirm("¿Está seguro que desea MOVER este contenido a la Bandeja de entrada?")){
            actualizar("_relacionar_contenidos.php?ide="+e+"&idc="+idc+"&eer="+eer);
            //Effect.SlideUp(e,{duration:1.0});
        //}                            
}

function moverElemento(e){                        
    //if(confirm("¿Está seguro que desea MOVER este contenido a la carpeta de reciclaje?")){
        actualizar("_move.php?ide="+e);
        Effect.SlideUp(e,{duration:1.0});
    //}
}


function relacionarArchivos(idh,idp){ 
                            

        actualizar("_relacionar_archivos.php?idp="+idp+"&idh="+idh);
    }

abierto=0;
function abrirCerrarCargador(c){
    
    if(c==1){
        if(!abierto){
            Effect.SlideDown('wrapper',{duration:0.5});
            document.getElementById("abrir_cargador").style.display='none';
            document.getElementById("cerrar_cargador").style.display='';
            
            
            var id = setInterval('so.write("cargador")',1000);
            setTimeout("clearInterval("+id+"); document.getElementById('cargador').style.display='';",1100);
            abierto = 1;
        }
    }else{
        if(abierto){
            document.getElementById("cargador").style.display='none';
            Effect.SlideUp('wrapper',{duration:0.5});
            document.getElementById("abrir_cargador").style.display='';
            document.getElementById("cerrar_cargador").style.display='none';
            abierto = 0;
        }
    }
}

borrar_sin_confirmar=false;
function borrarElemento(e){
         

    var es_reciclado = e.indexOf("_rec_");
    var elemento = (es_archivo=="1")?"archivo":"contenido";
    var mensaje = "¿Está seguro que desea BORRAR este "+elemento+"?";
    
    
    // si es archivo
    if(es_archivo){
        if(!borrar_sin_confirmar){
            if(confirm(mensaje)){
                actualizar("_del.php?ide="+e+"&a="+es_archivo+"&m=0&idcp="+idcp+"&lim="+lim+"&p="+p);
                Effect.SlideUp(e,{duration:1.0});
            }                        
        }else{
            actualizar("_del.php?ide="+e+"&a="+es_archivo+"&m=0&idcp="+idcp+"&lim="+lim+"&p="+p);
            Effect.SlideUp(e,{duration:0.1});
        }    
    }else{
        if(es_relacionado){        
            actualizar("_quitar_relacion_contenidos.php?ide="+e+"&idc="+idcp+"&lim="+lim+"&p="+p);
            Effect.SlideUp(e,{duration:1.0});
                                        
        }else{
        
            // si es contenido // o estamos en usuarios FF con idcp==0
            if(borrar_sin_confirmar || idcp==0){
                actualizar("_del.php?ide="+e+"&a="+es_archivo+"&m=0&idcp="+idcp+"&lim="+lim+"&p="+p);
                Effect.SlideUp(e,{duration:0.1});
            }else{
                if(idc_nav!=-2 && es_reciclado==-1){
                    
                    if(confirm(mensaje)){                                
                            if(confirm("Este contenido puede que tenga contenidos relacionado.\n\n¿Desea mover estos contenidos a la Carpeta de reciclaje?")){
                                actualizar("_del.php?ide="+e+"&a="+es_archivo+"&m=1&idcp="+idcp+"&lim="+lim+"&p="+p);
                                Effect.SlideUp(e,{duration:1.0});
                                
                            }else{                                    
                                if(confirm("Este contenido, junto con todos sus archivos y contenidos relacionados, serán borrados definitivamente.\n\n¿Desea continuar?")){
                                    actualizar("_del.php?ide="+e+"&a="+es_archivo+"&m=0&idcp="+idcp+"&lim="+lim+"&p="+p);
                                    Effect.SlideUp(e,{duration:1.0});
                                    
                                } 
                            }
                         
                    }      
                }else{
                    if(confirm("Este contenido, junto con todos sus archivos y contenidos relacionados, serán borrados definitivamente.\n\n¿Desea continuar?")){
                        actualizar("_del.php?ide="+e+"&a="+es_archivo+"&m=0&idcp="+idcp+"&lim="+lim+"&p="+p);
                        Effect.SlideUp(e,{duration:1.0});
                        
                    }
               }
            }
                                  
        }
    }

        
    
        
}
