 //agrega funcionalidad a los objetos tr
 function prototypeRow(tr){
 	tr.getNumCells = function(){
 		return this.cells.length;
 	}
 	tr.addCell = function(){
 		if(arguments.length == 0){
 		  var cell =  this.insertCell(-1);
 		}else{
 		  var cell = this.insertCell(arguments[0]);
 		}
 		return cell;
 	}
 	
 	tr.getCell = function(index){
 		return this.cells.item(index);
 	}
 	
 	tr.addCells = function(cantidad){
 	   for(var i = 0; i <  cantidad; i++){
 	   	  this.addCell();
 	   }
 	}
 	
 	tr.getIndex = function(){
 		return this.sectionRowIndex;
 	}
 	return tr;
 }
 
 var incx = 0;
 var incy = 0;
 var lista_ventanas = new Array();
 
 function prototypeDiv(div){
 	div.setLeft = function(left){
 		this.style.left = left + 'px';
 	}
 	div.setTop = function (top){
 		this.style.top = top + 'px';
 	}
 	
 	div.setPositionType = function (type){
 		this.style.position = type;
 	}
 	div.setZIndex = function(index){
 		this.style.zIndex = index;
 	}
 	
 	div.setBackgroundColor =  function(color){
 		this.style.backgroundColor = color;
 	}
 	
 	div.setBorder = function(values){
 		this.style.border = values;
 	}
 	
 	div.getLeft = function(){
 		return parseInt(this.style.left);
 	}
 	
 	div.getTop = function(){
 		return parseInt(this.style.top);
 	}
 	
 	div.setWidth = function(ancho){
 		this.style.width = ancho + 'px';
 	}
 	div.setHeight = function(alto){
 		this.style.height = alto + 'px';
 	}
 	div.getWidth = function(){
 		return parseInt(this.style.width);
 	}
 	div.getHeight = function(){
 		return parseInt(this.style.height);
 	}
 	
 	div.setVisible = function(visible){
 		 if(visible){
 		 	 this.style.display = 'block';
 		 }else{
 		 	this.style.display = 'none';
 		 }
 	}
 	
var drag = false; 	
 	
 	div.setDragable = function(){
 		 div.incx = 0;
 		 div.incy = 0;
 		 div.drag = false;
 		 div.onmousedown = function(e){
 		  try{	
 		  	var punto = obtenerPosicionMouse(e);
 		  	document.onmousedown =  function() { return false };
 		  	drag = true;
 		 	incx = punto.x - div.getLeft();
 		 	incy = punto.y - div.getTop();
 		 	div.style.MozOpacity = 0.2; //transparencia para mozilla
	  	    div.style.filter = 'Alpha(opacity=30)'; //transparencia para internet explorer
	  	    div.drag = true;
	  	    div_dragable = div;
	  	    var divs = document.getElementsByTagName('div');
	  	    var id = div.id
	  	    for(var i=0; i < divs.length; i++) {
	  	    		divs[i].style.zIndex = 1;
 		    }
 		    div.style.zIndex = 2;
 		  }catch(exception){
 		  	 //alert(exception);
 		  }
 		 }
 		 div.onmouseup = function(e){
 		 	div.style.MozOpacity = 1; //transparencia para mozilla
	  	    div.style.filter = 'Alpha(opacity=100)'; //transparencia para internet explorer
	  	    div.drag = false;
	  	    drag = false;
	  	    document.onmousedown = null;
 		 }
// 		 div.onmouseover = function(){ if(drag){ div.style.cursor = 'move' ; } else {  div.style.cursor = 'pointer'; }}  
 		 //agrego este div a la lista 
 		 lista_ventanas.push(div);
 	}
 	
 	
 	
 	div.setDragableY = function(){
 		 div.incx = 0;
 		 div.incy = 0;
 		 div.drag = false;
 		 
 		 addEvent(div,'mousedown',function(e){
 		  try{	
 		  	var punto = obtenerPosicionMouse(e);
 		 	incx = 0;
 		 	incy = punto.y - div.getTop();
 		 	div.style.MozOpacity = 0.5; //transparencia para mozilla
	  	    div.style.filter = 'Alpha(opacity=50)'; //transparencia para internet explorer
	  	    div.drag = true;
	  	    div.style.cursor = 'move';
	  	    div_dragable = div;
	  	    div.style.zIndex = 20;
	  	    var divs = document.getElementsByTagName('div');
	  	    var id = div.id
 		  }catch(exception){
 		  	 //alert(exception);
 		  }
 		 },false
 		 )

 		 addEvent(div, 'mouseup', function(e){ div.style.MozOpacity = 1; //transparencia para mozilla
	  	                          div.style.filter = 'Alpha(opacity=100)'; //transparencia para internet explorer
	  	                          div.drag = false;
	  	                          div.style.cursor = 'pointer';
	  	                          div_dragable = null;
	  	                          div.style.zIndex = 1;
 		 },false);
 		 
 		 addEvent(div,'mouseover',function(){  div.style.cursor = 'pointer';});
 		 //agrego este div a la lista 
 		 lista_ventanas.push(div);
 	}
 	
 	return div;
 }
 
 //agrega funcionalidad a los objetos table
 function prototypeTable(table){
 	
 	table.cantidad_filas = 0;
 	
 	table.addRow = function(){
 		if(arguments.length == 0){
 			var row = this.insertRow(-1);
 		}else{
 			var row = this.insertRow(arguments[0]);
 		}
 		row = prototypeRow(row);
 		this.cantidad_filas++;
 		return row;
 	}
 	table.getNumRows = function(){
 		return this.cantidad_filas;
 	}
 	table.getRow = function(index){
 		return prototypeRow(this.rows.item(index));
 	}
 	table.getLastRow = function(){
 		 return prototypeRow(this.rows.item(this.rows.length - 1));
 	}
 	table.addRows = function(cantidad){
 		for(var i=0; i < cantidad; i++){
 			this.addRow();
 		}
 	}
 	table.delRow = function(index){
 		this.deleteRow(index);
 		this.cantidad_filas--;
 	}
 	
 	table.remove = function(){
 		if(this.parentNode){
 			this.parentNode.removeChild(this);
 		}
 	}
 	return table;
 }
 
 
 function Table(){
 	var tabla =  document.createElement('table');
 	return prototypeTable(tabla);
 }
 
 function Row(){
 	var tr = document.createElement('tr');
 	tr = prototypeRow(tr);
 	return tr;
 }
 
 function Cell(){
 	return document.createElement('td');
 }
 
 function Text(valor){
 	return document.createTextNode(valor);
 }
 
 function prototypeLink(dom_link){
 	dom_link.setText = function(label){
 		if(this.firstChild) this.removeChild(this.firstChild);
 		this.appendChild(new Text(label));
 	}
 	dom_link.setHref = function(href){
 		this.href = href;
 	}
 	return dom_link;
 }
 
 function Link(href,text){
 	var a = prototypeLink(document.createElement('a'));
 	
 	if(typeof href == 'string'){
 		a.setHref(href);
 	}
 	if(typeof text == 'string'){
 		a.setText(text);
 	}
 	return a;
 }
 
 function Hidden (nombre,value,id){
 	
 	var h = document.createElement('input');
 	h.type = 'hidden';
 	
 	if(typeof nombre != 'undefined'){
 		h.name = nombre;
 	}
 	if(typeof value != 'undefined'){
 		h.value = value;
 	}
 	if(typeof id != 'undefined'){
 		h.id = id;
 	}
 	return h;
 }
 
 
 
 function Div(id, ancho, alto){
 	var div = prototypeDiv(document.createElement('div'));
 	if(typeof id != 'undefined') div.id = id;
 	if(typeof ancho != 'undefined') div.style.width = ancho + 'px';
 	if(typeof alto != 'undefined') div.style.height = alto + 'px'; 
 	return div;
 }
 
 function Input(name,value, id){
 	var input = document.createElement('input');
 	if(typeof name != 'undefined') input.name = name;
 	if(typeof value != 'undefined') input.value = value;
 	if(typeof id != 'undefined') input.id = id;
 	return input;
 }
 
function getWindow(){
 	window.getWidth = function(){
 		return window.innerWidth ? window.innerWidth : document.body.clientWidth;
 	}
 	window.getHeight = function() {
 		return window.innerHeight ? window.innerHeight : document.body.clientHeight;
 	}
 	return window;
 }
 
 function BR(){
 	return document.createElement('br');
 }
 
 function Form(){
 	return document.createElement('form');
 }
 
 function Center(){
 	return document.createElement('center');
 }
 
 function getDataServer(url, vars){
  	var xml = null;
  	try{
  		 xml = new ActiveXObject('Microsoft.XMLHTTP');
  	}catch(exception){
  		xml = new XMLHttpRequest();
  	}
  	
  	xml.open('GET',url + '?param=1&' + vars, false);
  	xml.send(null);
  	if(xml.status == 404) alert('Url no valida');
  	return xml.responseText;
  }
  
  function postDataServer(url, vars){
  	var xml = null;
  	try {
  		xml = new ActiveXObject('Microsoft.XMLHTTP');
  	}catch(exception){
  		xml = new XMLHttpRequest();
  	}
  	try {
	  	xml.open('POST',url,false);
	  	xml.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	  	xml.send (vars);
	  	if(xml.status == 404) alert('Url no valida');
	  	if(xml.status == 405) alert('Method not allowed!');
  	}catch(exception){//alert(exception);
}
  	
  	return xml.responseText;
  }
  
  
  function addEvent(elm, evType, fn, useCapture){
	
	if(elm.addEventListener){
		
		elm.addEventListener(evType, fn, useCapture);
		return true;
	
	}else if(elm.attachEvent){
		
		var r = elm.attachEvent('on' + evType, fn);
		
	}else{
	
		elm['on' + evType] = fn;
		
	}
 }
  
  function $(id){
  	return document.getElementById(id);
  }
  
function obtenerPosicionMouse(e){
 	 var e2 = e ? e : window.event;
 	 var punto = new Object();
 	 if(e2.pageX){
 	 	punto.x = e2.pageX;
 	 	punto.y = e2.pageY;
 	 }else{
 	 	punto.x = e2.clientX + document.body.scrollLeft;
 	 	punto.y = e2.clientY + document.body.scrollTop;
 	 }
 	 return punto;
}



var divs_dragables = new Array();
var color_over = '#ccbbcc';
var color_original = '#ddccdd';
var func_mouse_up = null;

function set_dragableY_divs(){
	var divs = getTags('div');
	var div = null;
	//recorro la lista de divs en busca de los dragables.
	for(var i in divs){
		div = divs[i];
		if(div && div.className &&  (' ' + div.className + ' ').indexOf(' mltr ') != -1){
			div = prototypeDiv(div);
			div.setDragableY();
			div.setBackgroundColor(color_original);
			div.original_pos = new Object();
			div.original_pos.left = div.getLeft();
			div.original_pos.top = div.getTop();
			div.style.ZIndex = 1;
			divs_dragables.push(div);
			addEvent(div, 'mouseup', function() { reordenar_divs(); if( typeof func_mouse_up == 'function' ) func_mouse_up(divs_d)},false);
			//Las siguientes lineas se comentan por mal funcionamiento en internet explorer.
//			addEvent(div, 'mouseover',function(){if( typeof color_over != 'undefined') this.setBackgroundColor(color_over)},false);
//			addEvent(div, 'mouseout', function(){if(typeof color_original != 'undefined') this.setBackgroundColor(color_original)},false);
		}
	}
}


function array_sort(array, cmp_func){
	for(var i = 0; i < array.length; i++){
		for(var j= i + 1; j < array.length; j++){
			if(cmp_func(array[i], array[j]) == -1){
				//es mayor el de la izquierda
				var temp_value = array[i];
				array[i] = array[j];
				array[j] = temp_value;
			}
		}
	}
	return array;
}

function comparar_divs(div1, div2){
	if(div1.getTop() > div2.getTop())
		return -1;
    if(div1.getTop() < div2.getTop())
    	return 1;
    return 0;
}
var inc = 0;
function reordenar_divs(){
	// Primero ordeno los divs como se encuentran visualmente.
	if(divs_d.length > 2 && inc == 0) {
		inc = divs_d[1].pos.top - divs_d[0].pos.top;
	}
	if( inc == 0)
	    inc = 22;
	    
	divs_dragables = array_sort(divs_d, comparar_divs);
	var j = 0;
	var top  = init_top;


	    
	for(var i in divs_dragables){
		divs_dragables[i].original_pos.top = top;
		divs_dragables[i].setTop(top);
		top += inc;
	}
	divs_d = divs_dragables;
}

function obtener_top_anterior(top){
	var divs = null;
	//ordeno los divs.
    divs = divs_dragables;
	var top_anterior = null;
	for(var i in divs){
		if(divs[i].getTop() < top){
			top_anterior = divs[i];
		}else{
			break;
		}
	}
	return top_anterior;
}

function obtener_top_siguiente(top){
	var divs = null;
	divs = divs_dragables;
	var top_siguiente = null;
	for(var i in divs){
		if(divs[i].getTop() > top){
			top_siguiente = divs[i];
			break;
		}
	}
	return top_siguiente;
}


function limpiar_lineas(){
  for(var i in divs_dragables){
  	 divs_dragables[i].style.borderBottom = '';
  	 divs_dragables[i].style.borderTop = '';
  }
}

function guardar_nuevo_orden(divs_ordenados){
	var accion = 'act=guardar_nuevo_orden';
	var id = 0;
	var split = '';
	for(var i in divs_ordenados){
		id = divs_ordenados[i].id.split('_')[1];
		accion += '&id_usuarios_peliculas[]=' + id ;
	}
	var response = getDataServer('milista.php',accion);
//    alert(response);
}


var mouse = null;
var clon = null;
var div_dragable = null;
// Utilizada para el modulo de ordenamiento de divs.
 document.onmousemove = function(e){
  try{
 	var mouse = obtenerPosicionMouse(e);
 	if(div_dragable){ //solo puede haber un elemento "arrastrable"
 		div_dragable.style.cursor = 'move';
 		if(incx != 0)	
 		     div_dragable.setLeft(mouse.x - incx);
 		  if(incy != 0)	
 			 div_dragable.setTop(mouse.y - incy);
 	}
 	
  }catch(e){
  	 //alert(e);
  }
 }
 
 function getTags(tag){
 	return document.getElementsByTagName(tag);
 }
 
 function open_popup(url,ancho, alto){
	var left = (screen.width - ancho) / 2 ;
	var top = (screen.height - alto) / 2 ;
	var ventana = open(url,'ventana','width=' + ancho +',height=' + alto + ', left=' + left + ',top=' + top + ',resizable=yes, scrollbars=yes');
	ventana.focus();
 }
 
 function $tag(tag){
 	return document.getElementsByTagName(tag);
 }
 
 function find_pos(obj){
 	var curleft = 0;
 	var curtop = 0 ;
 	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft;
			curtop  += obj.offsetTop;
			obj      = obj.offsetParent;
		}
	}
	else if (obj.x && obj.y){
		curleft += obj.x;
		curtop += obj.y;
	}
	var point = new Object();
	point.left = curleft;
	point.top  = curtop;
	return point;
 }
