/**
* gestió de l'ampliació de les fotillos de la fitxa d'empresa
*/

/**
* afeixim listener al onLoad de la pàgina
*/
addLoadListener(initImatges);


/**
* atacham event d'entrada i sortida de ratolí a tots els elements de la classe hastolltip
*/
function initImatges() {


	//assignam el callback showTip quant el ratolí passi per damunt una imatge
	//de clase azar_empresa_fotillo
	attachEventToElements( "class", "hastooltip", //"azar_empresa_fotillo",
							"mousedown", showImatge, false );

	//attachEventToElements( "class", "hastooltip", "mouseout", hideImatge, false );

	//attacham el callback per els articles i ofertes que no tenen imatge
	attachEventToElements( "class", "hastooltipBuida", //"azar_empresa_fotillo",
							"mousedown", showImatgeBuida, false );

	//attachEventToElements( "class", "hastooltipBuida", "mouseout", hideImatge, false );


	return true;
}


/**
* crea el div de l'imatge (si ho existeix) i la fà visible
*/
function showImatge(event) {


  if (typeof event == "undefined")
  {
    event = window.event;
  }

  var target = getEventTarget(event);

  while (target.className == null || !/(^| )hastooltip( |$)/.test(target.className))
  {
    target = target.parentNode;
  }

  //numero d'imatge
  var content = target.getAttribute("id");
  //alert("contingut id "+"<img src=\"/img_socis/"+content+".jpg\" width=\"320\" height=\"200\"/>");
  //target.setAttribute("title", "");

  var capsa=getRefToDiv("tooltip"+content);
  //si la capsa de la foto no existeix
  if (!capsa) {

	  //cream l'element
	  var tip = document.createElement("div");

	  tip.id="tooltip"+content;
	  tip.className = "tooltip";
	  tip.style.position = "absolute";
	  //agafam coordenades de la fotillo
	  var c=getElementPosition(target);
	  tip.style.left = c[0]+"px";
	  tip.style.top = c[1]+"px";
	  tip.style.visibility = "hidden";

	  //ficam el div com  a fill de oficines perque si no els flash overlapen la finestra
//	  var oficines=getRefToDiv("oficines");
//	  var pare=oficines.parentNode;

	  document.getElementsByTagName("body")[0].appendChild(tip);
//	  pare.insertBefore(tip, oficines);

	  var tamany=getElementPosition(target);

//	  tip.innerHTML="<img src=\"/img_socis/"+content+".jpg\" width=\"320\" height=\"200\"/>";
	  tip.innerHTML= "<div class=\"marc_foto\">"+
	  				 "<div class=\"intra_foto\">"+
					 "<img src=\"/img_socis/"+content+".jpg\" width=\"320\" height=\"200\" onClick=\"javascript:hideDiv(\'"+tip.id+"\'); return false;\" />"+
					 "<div class=\"cerrar_foto\"><a href=\"\" onClick=\"javascript:hideDiv(\'"+tip.id+"\'); return false;\">Close</a></div>"+
//					 "x1 "+tamany[0]+" y1 "+tamany[1]+"x2 "+(tamany[0]+65)+" y2 "+(tamany[1]+47)+
					 "</div></div>";

	  tip.style.visibility = "visible";
  } else  {
  	//si la capsa ja existeix miram si està visible, si ja hi està no feim res per evitar el flicker
  	if (capsa.style.visibility!="visible") 	capsa.style.visibility="visible";
  }

  return true;

}

/**
* amaga l'imatge
*/
function hideImatge(event) {

  if (typeof event == "undefined")
  {
    event = window.event;
  }

  //taget de l'envent
  var target = getEventTarget(event);

  while (target.className == null || !target.className.match(/(^| )hastooltip( |$)/))
  {
    target = target.parentNode;
  }

  var content = target.getAttribute("id");

  var id="tooltip"+content;


  hideDiv(id);

return true;
  //return false;

}

/**
* Mostra una imatge
*/
function muestra_imagen(archivo,ancho,alto){
   setInnerHTML('c1', '<img src=\"' + archivo + '\" width=\"' + ancho + '\" height=\"' + alto + '\" border=\"0\">');
   showDiv('ampliacion');
}

/**
* oculta l'imatge anterior
*/
function oculta_imagen(id_div){
   hideDiv('ampliacion');
}


function treuImatge(event) {

  if (typeof event == "undefined")
  {
    event = window.event;
  }

  var target = getEventTarget(event);

  while (target.className == null || !/(^| )hastooltip( |$)/.test(target.className))
  {
    target = target.parentNode;
  }

  //numero d'imatge
  var content = target.getAttribute("title");
  //target.setAttribute("title", "");

}



/**
* crea i mostra una finestra de popup amb l'imatge de
*/
function makePopup(referencia)
{

  width=320;
  height=240;
  url="/afoto.html";
  overflow="";

  if (overflow == '' || !/^(scroll|resize|both)$/.test(overflow))
  {
    overflow = 'both';
  }

  var win = window.open('', '',
      'width=' + width + ',height=' + height
      + ',scrollbars=' + (/^(scroll|both)$/.test(overflow) ? 'yes' : 'no')
      + ',resizable=' + (/^(resize|both)$/.test(overflow) ? 'yes' : 'no')
      + ',status=yes,toolbar=no,menubar=no,location=no'
  );


  win.document.write="hola que tal";

  /*afoto=win.document.body.afoto;
  if (afoto) {
  	afoto.innerHTML=referencia;
	win.document.write(referencia);
  } else alert("no hi haafoto");
  */
  return win;
}


function ventana1(titol, referencia) {
var newWindow = window.open("","","toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=no,resizable=no,copyhistory=yes,height=260,width=340")
if (newWindow != null) {
// assemble content for new window
var newContent = "<HTML><HEAD><TITLE>"+titol+"</TITLE></HEAD>"
newContent += "<BODY>"
newContent += "<img src=\""+referencia+"\" width=\"320\" height=\"200\"/>";
newContent += "<br/><CENTER><FORM><INPUT TYPE='button' VALUE='Close' onClick='self.close()'></FORM></CENTER>"
newContent += "</BODY></HTML>"
// write HTML to new window document
newWindow.document.write(newContent)
newWindow.document.close() // close layout stream
}
}


/**
*
*/
function showTip(event)
{
  if (typeof event == "undefined")
  {
    event = window.event;
  }

  var target = getEventTarget(event);

  while (target.className == null || !/(^| )hastooltip( |$)/.test(target.className))
  {
    target = target.parentNode;
  }

  var tip = document.createElement("div");
  var content = target.getAttribute("title");

  target.tooltip = tip;
  target.setAttribute("title", "");

  if (target.getAttribute("id") != "")
  {
    tip.setAttribute("id", target.getAttribute("id") + "tooltip");
  }

  tip.className = "tooltip";
  tip.appendChild(document.createTextNode(content));


  var scrollingPosition = getScrollingPosition();
  var cursorPosition = [0, 0];

  if (typeof event.pageX != "undefined" && typeof event.x != "undefined")
  {
    cursorPosition[0] = event.pageX;
    cursorPosition[1] = event.pageY;
  }
  else
  {
    cursorPosition[0] = event.clientX + scrollingPosition[0];
    cursorPosition[1] = event.clientY + scrollingPosition[1];
  }

  tip.style.position = "absolute";
  tip.style.left = cursorPosition[0] + 10 + "px";
  tip.style.top = cursorPosition[1] + 10 + "px";
  tip.style.visibility = "hidden";

  document.getElementsByTagName("body")[0].appendChild(tip);

  var viewportSize = getViewportSize();

  if (cursorPosition[0] - scrollingPosition[0] + 10 + tip.offsetWidth > viewportSize[0] - 25)
  {
    tip.style.left = scrollingPosition[0] + viewportSize[0] - 25 - tip.offsetWidth + "px";
  }
  else
  {
    tip.style.left = cursorPosition[0] + 10 + "px";
  }

  if (cursorPosition[1] - scrollingPosition[1] + 10 + tip.offsetHeight > viewportSize[1] - 25)
  {
    if (event.clientX > (viewportSize[0] - 25 - tip.offsetWidth))
    {
      tip.style.top = cursorPosition[1] - tip.offsetHeight - 10 + "px";
    }
    else
    {
      tip.style.top = scrollingPosition[1] + viewportSize[1] - 25 - tip.offsetHeight + "px";
    }
  }
  else
  {
    tip.style.top = cursorPosition[1] + 10 + "px";
  }

  tip.style.visibility = "visible";

  return true;
}



/**
* crea el div de l'imatge (si ho existeix) i la fà visible
*/
function showImatgeBuida(event) {


  if (typeof event == "undefined")
  {
    event = window.event;
  }

  var target = getEventTarget(event);

  while (target.className == null || !/(^| )hastooltipBuida( |$)/.test(target.className))
  {
    target = target.parentNode;
  }

  //numero d'imatge
  var content = target.getAttribute("id");
  //alert("contingut id "+"<img src=\"/img_socis/"+content+".jpg\" width=\"320\" height=\"200\"/>");
  //target.setAttribute("title", "");

  var capsa=getRefToDiv("tooltip"+content);
  //si la capsa de la foto no existeix
  if (!capsa) {

	  //cream l'element
	  var tip = document.createElement("div");

	  tip.id="tooltip"+content;
	  tip.className = "tooltip";
	  tip.style.position = "absolute";
	  var c=getElementPosition(target);
	  tip.style.left = c[0]+"px";
	  tip.style.top = c[1]+"px";

	  document.getElementsByTagName("body")[0].appendChild(tip);

	  var tamany=getElementPosition(target);

	  tip.innerHTML= "<div class=\"marc_foto\">"+
	  				 "<div class=\"intra_foto\">"+
					 "<img src=\"/img_socis/no_product.jpg\" width=\"320\" height=\"200\" onClick=\"javascript:hideDiv(\'"+tip.id+"\'); return false;\" alt=\"Click to close\" />"+
					 "<div class=\"cerrar_foto\"><a href=\"\" onClick=\"javascript:hideDiv(\'"+tip.id+"\'); return false;\">Close</a></div>"+
					 "</div>"+tip.id+"</div>";

	  tip.style.visibility = "visible";



  } else  {
  	//si la capsa ja existeix miram si està visible, si ja hi està no feim res per evitar el flicker
  	if (capsa.style.visibility!="visible") 	capsa.style.visibility="visible";
  }

  return true;

}




