﻿// Archivo JScript

var offsetfrommouse=[15,15]; //image x,y offsets from cursor position in pixels. Enter 0,0 for no offset
var displayduration=0; //duration in seconds image should remain visible. 0 for always.
var imgWidth = 400  // maximum image size.
var imgHeight = 400;
var divPadding = 30; 
var mouseX = 0;
var mouseY = 0;
var loadingImage = "Img/loadingTiny.gif";

document.onmousemove=followmouse;

function showPreview(imgSrc, objSender){
    
    // TODO cambiar valores fijos de ancho y largo
    var diffY = 0;
    var diffX = 0;        
    var divWidth = imgWidth + divPadding;
    var divHeight = imgHeight + divPadding;
           
    // genero miniatura
	gettrailimgobj().src = "Thumb.ashx?SrcPath="+imgSrc+"&Width="+imgWidth+"&Height="+imgHeight+"&Q=90&WaterMark=1";
	
	// busco la posicion adecuada para mostrar		    
    /*if ( parseInt(findPosY(objSender)) + divHeight > document.body.clientHeight ){	
	    diffY = parseInt(findPosY(objSender)) + divHeight - document.body.clientHeight;
	} 
	
	gettrailobj().top = parseInt(findPosY(objSender)) - diffY + "px";
	
	 	
	if ( parseInt(findPosX(objSender)) + divWidth > document.body.clientWidth ){
	    diffX = parseInt(findPosX(objSender)) + divWidth - document.body.clientWidth;
	} 
	
	gettrailobj().left = parseInt(findPosX(objSender)) - diffX + "px";	*/
	
	// muestro div
	/*gettrailobj().display = 'block';*/
	waitFor(gettrailimgobj());
	
	//$('#DIVPreview').BlindDown(500);
}

function waitFor(img){

    if( !isImageOk(img) ){
	    setTimeout('waitFor("'+img+'")', 500);
    } else {	
	    // Code to execute on completion of download
	    //$(".imgPr").fadeIn("slow");
	    SetPosition(mouseX, mouseY);
	    //$(img).fadeTo("slow", 1);

	    if (gettrailimgobj().src.match(loadingImage)){
	        gettrailobj().display = 'none';	
	    } else {
	        gettrailobj().display = 'block';    
	    }
    }
}

function hidePreview(objSender){

	gettrailobj().display = 'none';	
	gettrailimgobj().src = loadingImage;	
	
    //document.onmousemove=""
    gettrailobj().left = "-1000px"
    if (typeof gettrailimgobj().naturalWidth == "undefined"){
        mouseX = -1000
        mouseY = -1000
    }
	//$('#DIVPreview').FadeOut(500); 
}

function findPosX(obj)
{
    var curleft = 0;

    if(obj.offsetParent)
	    while(1) 
	    {
	      curleft += obj.offsetLeft;
	      if(!obj.offsetParent)
		    break;
	      obj = obj.offsetParent;
	    }
    else if(obj.x)
	    curleft += obj.x;
    return curleft + 15;
}

function findPosY(obj)
{
    var curtop = 0;

    if(obj.offsetParent)
	    while(1)
	    {
	      curtop += obj.offsetTop;
	      if(!obj.offsetParent)
		    break;
	      obj = obj.offsetParent;
	    }
    else if(obj.y)
	    curtop += obj.y;
    return curtop + 15;
}
	  
/* ================================================================================================ */
/* ================================================================================================ */
/* ================================================================================================ */
/* ================================================================================================ */

/*
Simple Image Trail script- By JavaScriptKit.com
Visit http://www.javascriptkit.com for this script and more
This notice must stay intact
*/

/*if (document.getElementById || document.all){
	document.write('<div id="trailimageid">');
	document.write('</div>');
}*/

function gettrailobj(){
if (document.getElementById)
return document.getElementById("DIVPreview").style
else if (document.all)
return document.all.DIVPreview.style
}

function gettrailimgobj(){
if (document.getElementById)
return document.getElementById("IMGPreview")
else if (document.all)
return document.all.DIVPreview
}

function gettrailobjnostyle(){
if (document.getElementById)
return document.getElementById("DIVPreview")
else if (document.all)
return document.all.DIVPreview
}


function truebody(){
return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

/*function showtrail(imagename,title,description,ratingaverage,ratingnumber,showthumb,height){

	if (height > 0){
		currentimageheight = height;
	}

	document.onmousemove=followmouse;

	cameraHTML = '';

	if (ratingnumber == 0){
		ratingaverage = 0;
	}

	for(x = 1; x <= 5; x++){

		if (ratingaverage >= 1){
			cameraHTML = cameraHTML + '<img src="/images/camera_1.gif">';
		} else if (ratingaverage >= 0.5){
			cameraHTML = cameraHTML + '<img src="/images/camera_05.gif">';
		} else {
			cameraHTML = cameraHTML + '<img src="/images/camera_0.gif">';
		}
	
		ratingaverage = ratingaverage - 1;

	}

	cameraHTML = cameraHTML + ' (' + ratingnumber + ' Review';
	if ( ratingnumber != 1 ) cameraHTML += 's';
	cameraHTML = cameraHTML + ')';

	newHTML = '<div style="padding: 5px; background-color: #FFF; border: 1px solid #888;">';
	newHTML = newHTML + '<span class="largetitle">' + title + '</span><div class="borderbot"></div>';
	newHTML = newHTML + 'Rating: ' + cameraHTML + '<br/>';
	newHTML = newHTML + description + '<br/>';

	if (showthumb > 0){
		newHTML = newHTML + '<div align="center" style="padding: 8px 2px 2px 2px;"><img src="' + imagename + '" border="0"></div>';
	}

	newHTML = newHTML + '</div>';

	gettrailobjnostyle().innerHTML = newHTML;

	gettrailobj().visibility="visible";

}


function hidetrail(){
	gettrailobj().visibility="hidden"
	document.onmousemove=""
	gettrailobj().left="-500px"

}*/

function followmouse(e){

    var pageX = 0
    var pageY = 0
    
    if (typeof e != "undefined"){
		pageX = e.pageX
		pageY = e.pageY 
	} else if (typeof window.event != "undefined"){
	    pageX = event.clientX
		pageY = event.clientY +	truebody().scrollTop		
	}

    mouseX = pageX;
    mouseY = pageY;
    
    SetPosition(pageX, pageY)
    
}

function SetPosition(pageX, pageY){

    var naturalHeight = 0;
    var naturalWidth = 0;
	var xcoord=offsetfrommouse[0]
	var ycoord=offsetfrommouse[1]
    
    
	var docwidth=document.all? truebody().scrollLeft+truebody().clientWidth : pageXOffset+window.innerWidth-15
	var docheight=document.all? Math.min(truebody().scrollHeight, truebody().clientHeight) : Math.min(document.body.offsetHeight, window.innerHeight)

    /* dimensiones de la imagen */
    if (typeof gettrailimgobj().naturalWidth != "undefined")
        naturalWidth = gettrailimgobj().naturalWidth;
    else
        naturalWidth = gettrailimgobj().clientWidth;
    
    if (typeof gettrailimgobj().naturalHeight != "undefined")
        naturalHeight = gettrailimgobj().naturalHeight;
    else
        naturalHeight = gettrailimgobj().clientHeight;
   
    /* calculos de posicion */        
	if (docwidth - pageX < naturalWidth ){
		xcoord = pageX - xcoord - naturalWidth - 30; // Move to the left side of the cursor
	} else {
		xcoord += pageX;
    }
       
	if (docheight - pageY < naturalHeight){
		ycoord += pageY - Math.max(0,(naturalHeight + 60 + pageY - docheight - truebody().scrollTop));
	} else {
		ycoord += pageY;
	}

    var docwidth=document.all? truebody().scrollLeft+truebody().clientWidth : pageXOffset+window.innerWidth-15
	var docheight=document.all? Math.max(truebody().scrollHeight, truebody().clientHeight) : Math.max(document.body.offsetHeight, window.innerHeight)

    /* asigno posicion */
	gettrailobj().left=xcoord+"px"
	gettrailobj().top=ycoord+"px"
    

}