// store variables to control where the popup will appear relative to the cursor position
// positive numbers are below and to the right of the cursor, negative numbers are above and to the left

// codigo retirado da AGE

var xOffset = 0;
var yOffset = -30;
var light_foto		  		// código de la foto
var light_foto_web			// código web de la foto
var firstPopup = true 		// ajuda pel comportament quan es polsa el general

function open_windows_mts(openfile,openwindow) {
  nw = window.open(openfile,openwindow,'directories=no,resizable=yes,scrollbars=no,width=430,height=230,hotkeys=yes,top=0,left=0');
}

function addToLightbox(nlight) {
  light_url = "http://www.grupokeystone.com.br/lightbox/adicionar/adicionar.php?cromoid="+light_foto+"&referencia="+light_foto_web+"&lightboxid="+nlight;
  open_windows_mts(light_url, '' + light_foto)
}

function showPopup (targetObjectId, foto, foto_web, eventObj, numfondo) {
  	if(eventObj) {
    
	light_foto = foto
	light_foto_web = foto_web
    firstPopup = true

		hideCurrentPopup();  			// hide any currently-visible popups
		eventObj.cancelBubble = true; 	// stop event from bubbling up any farther
		
		// ajustar les coordenades
		var newXCoordinate = (eventObj.pageX)?eventObj.pageX + xOffset:eventObj.x + xOffset + ((document.body.scrollLeft)?document.body.scrollLeft:0);
		newXCoordinate = 22+Math.floor(newXCoordinate/152)*152 	// <<= snap to grid (marge esquerre, ample del template)
		var newYCoordinate = getTop('im'+foto)-26				// <= id da imagem thumb setada na mesadeluz
		
		moveObject(targetObjectId, newXCoordinate, newYCoordinate);
		changeBgcolor(targetObjectId, numfondo);

		if (changeObjectVisibility(targetObjectId, 'visible') ) {
			window.currentlyVisiblePopup = targetObjectId; // store its Id on a globally-accessible object
			return true;
		} else {
			return false; // we couldn't show the popup, boo hoo!
		}
		} else {
		return false; // there was no event object, so we won't be able to position anything, so give up
	}
}

function getTop(imgElem) {
	var imgMenu = document.images[imgElem];
	if(document.layers)	{yPos = imgMenu.y;}
	else
	{
		if (document.getElementById) //W3C DOM
		{
			yPos = eval(imgMenu).offsetTop;
			tempEl = eval(imgMenu).offsetParent;
			while (tempEl != null) {
				yPos += tempEl.offsetTop;
				tempEl = tempEl.offsetParent;
			}
		}
		else //navigator.appVersion.substr(0,3)=="4.0") //IE4 MAC
		{
      yPos=0;
      obj=imgMenu;
      while (obj.tagName != 'HTML') {
        if (obj.parentNode) obj = obj.parentNode;
        else if (obj.parentElement) obj = obj.parentElement;
        if (obj.tagName.indexOf('T') == 0) {
          yPos += obj.offsetTop
        }
      }
      yPos=yPos+4;
	  }
  }
	return yPos;
}

function hideCurrentPopupOld() {
  if(window.currentlyVisiblePopup) {
    changeObjectVisibility(window.currentlyVisiblePopup, 'hidden');
    window.currentlyVisiblePopup = false;
  }
}

function hideCurrentPopup(evt) {
  if(window.currentlyVisiblePopup) {
    evt = (evt) ? evt : ((window.event) ? window.event : "")
    var amagar = true
    if (evt) {
      var elem
      if (evt.target) {
        elem = (evt.target.nodeType == 3) ? evt.target.parentNode : evt.target
      } else {
        elem = evt.srcElement
      }
      if (elem) {
        if (elem.name){
          if (elem.name.substring(0,2)=='im' && evt.type == 'click') {
            if (!firstPopup) {invertObjectVisibility(window.currentlyVisiblePopup);}
            firstPopup = false;
            amagar = false;
          }
        }
      }
    }
    if (amagar){
      changeObjectVisibility(window.currentlyVisiblePopup, 'hidden');
      window.currentlyVisiblePopup = false;
    }
  }
  return true
}

function chVisibilityCurrentPopup(eventObj) {
  if(window.currentlyVisiblePopup) {
    eventObj.cancelBubble = true;
    if (!firstPopup) {invertObjectVisibility(window.currentlyVisiblePopup);}
    firstPopup = false;
  }
}

// ***********************
// hacks and workarounds *
// ***********************

// initialize hacks whenever the page loads
window.onload = initializeHacks;

// setup an event handler to hide popups for generic clicks on the document
document.onclick = hideCurrentPopup;

function initializeHacks() {
    // this ugly little hack resizes a blank div to make sure you can click
    // anywhere in the window for Mac MSIE 5
    if ((navigator.appVersion.indexOf('MSIE 5') != -1) 
	&& (navigator.platform.indexOf('Mac') != -1)
	&& getStyleObject('blankDiv')) {
	window.onresize = explorerMacResizeFix;
    }
    resizeBlankDiv();
    // this next function creates a placeholder object for older browsers
    createFakeEventObj();
}

function createFakeEventObj() {
    // create a fake event object for older browsers to avoid errors in function call
    // when we need to pass the event object to functions
    if (!window.event) {
	window.event = false;
    }
} // createFakeEventObj

function resizeBlankDiv() {
    // resize blank placeholder div so IE 5 on mac will get all clicks in window
    if ((navigator.appVersion.indexOf('MSIE 5') != -1) 
	&& (navigator.platform.indexOf('Mac') != -1)
	&& getStyleObject('blankDiv')) {
	getStyleObject('blankDiv').width = document.body.clientWidth - 20;
	getStyleObject('blankDiv').height = document.body.clientHeight - 20;
    }
}

function explorerMacResizeFix () {
    location.reload(false);
}

function getStyleObject(objectId) {
    // cross-browser function to get an object's style object given its id
    if(document.getElementById && document.getElementById(objectId)) {
	// W3C DOM
	return document.getElementById(objectId).style;
    } else if (document.all && document.all(objectId)) {
	// MSIE 4 DOM
	return document.all(objectId).style;
    } else if (document.layers && document.layers[objectId]) {
	// NN 4 DOM.. note: this won't find nested layers
	return document.layers[objectId];
    } else {
	return false;
    }
} // getStyleObject

function changeObjectVisibility(objectId, newVisibility) {
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
	styleObject.visibility = newVisibility;
	return true;
    } else {
	// we couldn't find the object, so we can't change its visibility
	return false;
    }
} // changeObjectVisibility

function invertObjectVisibility(objectId) {
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
    	if (styleObject.visibility=='visible') {styleObject.visibility = 'hidden';}
      else {styleObject.visibility = 'visible';}
    	return true;
    } 
    else { // we couldn't find the object, so we can't change its visibility
	    return false;
    }
}

function moveObject(objectId, newXCoordinate, newYCoordinate) {
  // get a reference to the cross-browser style object and make sure the object exists
  var styleObject = getStyleObject(objectId);
  if(styleObject) {
    styleObject.left = newXCoordinate;
    styleObject.top = newYCoordinate;
    return true;
  } else {
  	// we couldn't find the object, so we can't very well move it
  	return false;
  }
} // moveObject

function changeBgcolor(objectId, numfondo) {
  // get a reference to the cross-browser style object and make sure the object exists
  var bgcol = '#F1F9FA';
  var styleObject = getStyleObject(objectId);

  if (numfondo=='2') {
    bgcol = '#E1A585';
  } else if (numfondo=='3') {
    bgcol = '#A9ADB4';
  } else if (numfondo=='4') {
    bgcol = '#F3D4C5';
  }
  if (styleObject){
    if(document.layers){	//thisbrowser="NN4";             
      styleObject.bgColor=bgcol;
    }         
    else{//thisbrowser="ie" OR W3CDOM
      styleObject.backgroundColor=bgcol;  
    }        
    return true
  }
  else {// we couldn't find the object, so we can't very well move it
  	return false;
  }
} // changeBgcolor

