var last_popup = null;
var website = 'http://dev.davidfeldman.com/';

// Detect if the browser is IE or not.
// If it is not IE, we assume that the browser is NS.
var IE = document.all?true:false

// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) document.captureEvents(Event.MOUSEMOVE)

// Set-up to use getMouseXY function onMouseMove
document.onmousemove = getMouseXY;

// Temporary variables to hold mouse x-y pos.s
var tempX = 0
var tempY = 0
var finalX = 0
var finalY = 0

// Main function to retrieve mouse x-y pos.s

function getMouseXY(e) {
  if (IE) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.body.scrollLeft
    tempY = event.clientY + document.body.scrollTop
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX
    tempY = e.pageY
  }  
  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}  

  return true
}

function getAbsY(obj)
{
	var absY  = 0;	
	var node = obj;
	while (node)
	{
		absY  += node.offsetTop;
		node = node.offsetParent;
	}
	return absY ;
}


function getAbsX(obj)
{
	var absX  = 0;	
	var node = obj;
	while (node)
	{
		absX += node.offsetLeft;
		node = node.offsetParent;
	}
	return absX ;
}

// Calcul de la hauteur interne de la fenêtre (hors menu, barre d'état etc ...)
function getInnerHeight()
{
	var height=0;
	if (self.innerHeight) // all except Explorer
	{
		height = self.innerHeight;
	}
	else 
	{
		if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
		{
			height = document.documentElement.clientHeight;
		}
		else 
		{
			if (document.body) // other Explorers
			{
				height = document.body.clientHeight;
			}
		}
	}
	return height;
}

// Calcul de la largeur interne de la fenêtre (hors menu, barre d'état etc ...)
function getInnerWidth()
{
	var width=0;
	if (self.innerWidth) // all except Explorer
	{
		width = self.innerWidth;
	}
	else 
	{
		if (document.documentElement && document.documentElement.clientWidth)
		// Explorer 6 Strict Mode
		{
			width = document.documentElement.clientWidth;
		}
		else 
		{
			if (document.body) // other Explorers
			{
				width = document.body.clientWidth;
			}
		}
	}
	return width;
}

// Calcul de l'offset horizontal = de combien on a déplacé le curseur de l'ascenseur horizontal
function getOffsetX()
{
	var offsetX =0;
	if (self.pageXOffset) // all except Explorer
	{
		offsetX = self.pageXOffset;
	}
	else 
	{
		if (document.documentElement && document.documentElement.scrollLeft)
		// Explorer 6 Strict
		{
			offsetX = document.documentElement.scrollLeft;
		}
		else 
		{
			if (document.body) // all other Explorers
			{
				offsetX = document.body.scrollLeft;
			}
		}
	}
	return offsetX;
}

// Calcul de l'offset vertical = de combien on a déplacé le curseur de l'ascenseur vertical
function getOffsetY()
{
	var offsetY =0;
	if (self.pageYOffset) // all except Explorer
	{
		offsetY = self.pageYOffset;
	}
	else 
	{
		if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
		{
			offsetY = document.documentElement.scrollTop;
		}
		else 
		{
			if (document.body) // all other Explorers
			{
				offsetY = document.body.scrollTop;
			}
		}
	}
	return offsetY;
}

// Code pour centrer un calque noir d'une opacité de 65% sur toute la page
function showOpacityBackground(){
	document.body.style.overflow="hidden";
	var eltopac = document.getElementById("fullbkg");
	eltopac.style.left = "0px";
	eltopac.style.top = "0px";
}
// Code pour cacher le calque
function hideOpacityBackground(){
	document.body.style.overflow="auto";
	var eltopac = document.getElementById("fullbkg");
	eltopac.style.left = "-100000px";
	eltopac.style.top = "-100000px";
}

// Ecoute les entrées clavier et retourne close_popup si la touche ESC est enfoncée
function keyPressHandler(e) {
  var kC  = (window.event) ?    // MSIE or Firefox?
			 event.keyCode : e.keyCode;
  var Esc = (window.event) ?   
			27 : e.DOM_VK_ESCAPE // MSIE : Firefox
  if(kC==Esc) {
		// Close popup properly using close function
		hidePopup();
	 }
}

function loadThumbnails(lotid){
  url=website+'scripts/getlotthumbnails.php?lotid='+lotid;
  req_thumbs = false;
  if(window.XMLHttpRequest) {
	try {
	  req_thumbs = new XMLHttpRequest();
	} catch(e) {
	  req_thumbs = false;
	}
  }
  else if(window.ActiveXObject)
  {
	try {
	  req_thumbs = new ActiveXObject("Msxml2.XMLHTTP");
	} catch(e) {
	try {
	  req_thumbs = new ActiveXObject("Microsoft.XMLHTTP");
	} catch(e) {
	  req_thumbs = false;
	}
  }
  }
  if(req_thumbs) {
	req_thumbs.onreadystatechange = processSetThumbsSelector;
	req_thumbs.open("GET", url, true);
	req_thumbs.send("");
  }
}

function processSetThumbsSelector(){
 var THUMBFOLDER='/lots/thumbs/';
 var IMGFOLDER='/lots/photos/';
 var thumbSelector=document.getElementById("thumbSelector");
 if (req_thumbs.readyState == 4 && req_thumbs.status == 200 && req_thumbs.responseText != null)
 {
	var selector="";
	var thumbs=req_thumbs.responseText.split('||');
	if(thumbs.length>2){
		thumbSelector.style.width="100px";
		thumbSelector.style.borderRight="1px solid #043882";
		for(i=0;i<thumbs.length-1;i++){
			selector+='<img class="thumb" src="'+THUMBFOLDER+'/'+thumbs[i]+'" onClick="loadThumbImage(\''+IMGFOLDER+'/'+thumbs[i]+'\')" alt="Thumb '+i+'"><br><br>';
			image = new Image();
			image.src = IMGFOLDER+'/'+thumbs[i]
		}
	} else {
		thumbSelector.style.width="0";
		thumbSelector.style.borderRight="0";
	}
	thumbSelector.innerHTML=selector;
 }
}

function loadThumbImage(stamp)
{
	var image = document.getElementById("stamp");
	image.src = stamp;
}

function showFullSizeThumb(stamp,lotid)
{
	// affiche un voile noir bloquant le site
	showOpacityBackground();
	loadLotValues(lotid);
	loadThumbnails(lotid);
	var image = document.getElementById("stamp");
	//précharge l'image taille réele
	image.onLoad = setTimeout('showWindow()',500);
	image.src = stamp;
}

function str_replace(haystack, needle, replacement) {
    var temp = haystack.split(needle);
    return temp.join(replacement);
}

function loadLotValues(lotid)
{
	var catnum=document.getElementById("catnum");
	var title=document.getElementById("title");
	var desc=document.getElementById("desc");
	var icons=document.getElementById("icons");
	var estim=document.getElementById("estim");
	
	var lot_icons=document.getElementById("icons_"+lotid).innerHTML
	
	catnum.innerHTML='Cat. No. '+document.getElementById("catnum_"+lotid).innerHTML;
	title.innerHTML=document.getElementById("title_"+lotid).innerHTML;
	desc.innerHTML=document.getElementById("desc_"+lotid).innerHTML;
	icons.innerHTML=str_replace(lot_icons,'<br>','&nbsp;');
	estim.innerHTML='Estimate : '+document.getElementById("estim_"+lotid).innerHTML;
}

function showWindow()
{

	var elt = document.getElementById("full_thumb");
	// Code pour afficher le popup à côté de la position courante de la souris
	elt.style.left = ((getInnerWidth()-elt.offsetWidth)/2 + getOffsetX())+"px";
	elt.style.top = (((getInnerHeight()-elt.offsetHeight)/2) + getOffsetY())+"px";
}

function hidePopup()
{
	var elt = document.getElementById("full_thumb");
	// Code pour afficher le popup à côté de la position courante de la souris
	elt.style.left = "-10000px";
	elt.style.top = "-10000px";
	hideOpacityBackground();
}
