
/* FUNCIONES DE CARGA DE IMAGENES */
var isWorking = false;
var urlArchivos = "/ms/archivos/";
function respuestaLoadIMG(xmlDocument) {
	 //var request = obj.request;
	 //if (obj.response.text.indexOf('invalid') == -1) {
	   // Use the XML DOM 
	  // var xmlDocument = obj.response.xml; 
	   var mostrados = xmlDocument.getElementsByTagName('mostrados').item(0).firstChild.data;
	   //alert(mostrados);
	   for (i=0; i<ROWS_X_PAGE; i++) {
	    if (i<mostrados) {
	     el('caja'+i).style.display = '';
	     
	     var id = xmlDocument.getElementsByTagName('id').item(i).firstChild.data;
	     var imagen = xmlDocument.getElementsByTagName('imagen').item(i).firstChild.data;
	     //var descripcion = xmlDocument.getElementsByTagName('descripcion').item(i).firstChild.data;
	     var visitas = xmlDocument.getElementsByTagName('visitas').item(i).firstChild.data;
	     var puntuacion = xmlDocument.getElementsByTagName('puntuacion').item(i).firstChild.data;
	     setImagen(i,id,imagen,'',puntuacion,visitas);
	 } else {
	     el('caja'+i).style.display = 'none';
	 }
	   }
	 if (mostrados == 0) {
		el('mensaje').style.display = '';
	 }
	   isWorking = false;
	   
	 //}
	 loadNewTips();


	// Analytics	
	pageTracker._trackPageview("/archivos" );	
}

function setImagen(i, id, imagen, descripcion, puntuacion, visitas) {
	elementos['opciones'+i]['id'] = id;
	elementos['opciones'+i]['imagen'] = new Image();
	elementos['opciones'+i]['imagen'].src = imagen;
	el('a'+i).href = '/ms/ver/'+id+'/';
	el('p'+i).innerHTML = puntuacion;
	el('v'+i).innerHTML = visitas;
	el('i'+i).title = MSG['home.resultados.visitas']+': '+visitas+'::'+MSG['home.resultados.puntuacion']+': '+puntuacion;
	el('i'+i).className = 'Tips';
	
	cargandoImagen(i);
}

function setImagenLigth(i, id, imagen) {
	elementos['opciones'+i]['id'] = id;
	elementos['opciones'+i]['imagen'] = new Image();
	elementos['opciones'+i]['imagen'].src = imagen;
	cargandoImagen(i);
}

function cargandoImagen(i) {
	if (elementos['opciones'+i]['imagen'].complete) cargadaImagen(i);
	else setTimeout("cargandoImagen("+i+")", 100);
}

// Una vez cargada la imagen, la substituye
function cargadaImagen(i) {
	el('i'+i).src = elementos['opciones'+i]['imagen'].src;
    fadeIn('i'+i,0,30);
}


// Buscador de imagenes
function loadIMG() {
  if (action != 'home') {
  	alert(MSG['js.utilizarbuscador']);
  	return;
  }
  if (!isWorking) {
  
	// Oculto Capas  	
  	el('mensaje').style.display = 'none';
	for (i=0; i<ROWS_X_PAGE; i++) {
      	fadeOut('i'+i,100,1);
		var id = 'opciones'+i;
		if (elementos[id]['abierto']) {
			setTimeout("moveDown('"+i+"',20,20)",10);
			elementos[id]['abierto'] = !elementos[id]['abierto'];
		    el('tablaPuntos'+i).style.display = '';
		}
	}
  
    // Construyo url
  	var urlLoad = urlArchivos;
  	//var idcat = getForm('buscador').categorias.value;
  	//if (idcat != '') {
  		//urlLoad += '&idcat='+idcat;
  	//}
  	var idact = getForm('buscador').actrices.value;
  	if (idact != '') {
  		urlLoad += '&idact='+idact;
  	}
  	order = el('order').value;
	urlLoad += '&order='+order;
  	
  	// Realizo peticion

	$.ajax({
		url: urlLoad, 
		type: 'get',
		success: function(data) {
			respuestaLoadIMG(data);
		}
	});
	/*
		var miAjax = new Ajax(urlLoad, 
		{
		method: 'get',
		onComplete: function() {
			
		}
		}
	)
	miAjax.request();
	*/
  }
}

/* MISCELANEA DE FUNCIONES */

// Formulario para enviar enlaces
function envioEnlace() {
	checkForm('enlace',true);
}

// Formulario para enviar contactos
function envioContacto() {
	checkForm('contacto',true);
}

// Formulario para registro de usuario
function envioUsuario() {
	checkForm('usuario',true);
}

// Agregar pagina a Favoritos
function agregarFavoritos() {
	if (document.all) { window.external.AddFavorite("http://www.materialsexual.com", "MaterialSexual.com"); }
	if (window.sidebar) { window.sidebar.addPanel("MaterialSexual.com", "http://www.materialsexual.com", ""); }
}

function showXvideo(id_video) {
	showThickboxUrl('/ms/xvideoid/&id_video='+id_video);

	// Analytics	
	pageTracker._trackPageview("/xvideoid" );	
}


function showThickbox(msg,width,height) {
	if (width == undefined && height == undefined) {
		auto = true;
	} else {
		auto = false;
		if (width == undefined) {
			width = 'auto';
		} else {
			width = width+'';
		}
		if (height == undefined) {
			height = 'auto';
		} else {
			height = height+'';
		}
	}
	
	jQuery(document).ready(function() {
		$.fancybox(
			msg,
			{
				'scrolling'			: 'no',
	        	'autoDimensions'	: auto,
				'width'         	: width,
				'height'        	: height,
				'transitionIn': 'none'
			}
		);
	});

}


function showThickboxUrl(url,width,height) {
		
	$.ajax({
		url: url, 
		type: 'get',
		success: function(data) {
			showThickbox(data,width,height);
		}
	});

}

function cerrarThickbox() {
	$.fancybox.close();
}

/*
function resize() {
	//alert(el('container').offsetHeight);
	var v = getPageSizeWithScroll();
	el('barra').style.height = (v[1]+0)+'px';
	el('container').style.height = (100+(v[1]+0))+'px';
}
*/

$(document).ready(function() {
	loadTips();
});


function loadTips() {
	//Tooltips
	$(".Tips").hover(function(){
		//tip = $(this).find('.tip');
		tip = $('.tip');
		tip.html($(this).attr('title'));
		tip.show(); //Show tooltip
	}, function() {
		tip.hide(); //Hide tooltip		  
	}).mousemove(function(e) {
		var mousex = e.pageX + 20; //Get X coodrinates
		var mousey = e.pageY + 20; //Get Y coordinates
		var tipWidth = tip.width(); //Find width of tooltip
		var tipHeight = tip.height(); //Find height of tooltip
		
		//Distance of element from the right edge of viewport
		var tipVisX = $(window).width() - (mousex + tipWidth);
		//Distance of element from the bottom of viewport
		var tipVisY = $(window).height() - (mousey + tipHeight);
		  
		if ( tipVisX < 20 ) { //If tooltip exceeds the X coordinate of viewport
			mousex = e.pageX - tipWidth - 20;
		} if ( tipVisY < 20 ) { //If tooltip exceeds the Y coordinate of viewport
			mousey = e.pageY - tipHeight - 20;
		} 
		tip.css({  top: mousey, left: mousex });
	});
}

/*function loadNewTips() {
	var Tips22 = new Tips($$('.Tips'), {
		className: 'custom',
		offsets: {'x': 10, 'y': 0}
	});
}*/
