/* Greybox Redux
 * Required: http://jquery.com/
 * Written by: John Resig
 * Based on code by: 4mir Salihefendic (http://amix.dk)
 * License: LGPL (read more in LGPL.txt)
 */

var GB_DONE = false;
var GB_HEIGHT = 400;
var GB_WIDTH = 400;
var GB_CONFIRM = false;


function GB_show(behaviour, caption, url,posizione) {
	
   GB_HEIGHT =  400;
   GB_WIDTH = 750;
   GB_CONFIRM =  false;

   var parameterString = url.replace(/.*\?(.*)/, "$1");
   var parameterTokens = parameterString.split("&");
   
   for (i = 0; i < parameterTokens.length; i++)
	{
	var parameterName = parameterTokens[i].replace(/(.*)=.*/, "$1");
	var parameterValue = parameterTokens[i].replace(/.*=(.*)/, "$1");
	if  ( parameterName == 'height' ) { GB_HEIGHT =  parameterValue } 
	else if ( parameterName == 'width' ) { GB_WIDTH =  parameterValue }
	else if ( parameterName == 'confirm' ) { GB_CONFIRM =  parameterValue }	
	// parameterList[parameterName] = parameterValue;
	}


  if (GB_CONFIRM) {
	  if (!(confirm("Vuoi andare alla pagina " + caption + "?")))  { return false} 
   }
	  
  if (behaviour == "confirm") {
  	window.location = url;
	return false;	  
  }

  $.get(url,function(xml){
  var pattern = /class\="redirect"/g;
  if (pattern.test(xml)) {
	var aPos = xml.indexOf('href='); 
	var GB_REDIRECT = xml.substr(aPos+6); 
	aPos = GB_REDIRECT.indexOf('"'); 
	GB_REDIRECT = GB_REDIRECT.substr(0,aPos);
        window.location.replace(GB_REDIRECT);
	return false;
  }
  else {
     var aPos = xml.indexOf('<body>'); 
     var GB_NOHEAD = xml.substr(aPos+6); 
     aPos = GB_NOHEAD.indexOf('</body>'); 
     GB_NOHEAD = GB_NOHEAD.substr(0,aPos);

    $(document.body)
      .append("<div id='GB_overlay'></div><div id='GB_window'><div id='GB_caption'></div>"
        + "<img src='/images/closeNotify.gif' alt='Close window'/><div id='GB_frame'>"+GB_NOHEAD+"</div></div>");
    $("#GB_window img").click(GB_hide);
    $("#GB_overlay").click(GB_hide);
    $(window).resize(GB_position);
    $("#GB_caption").html(caption);
        
    
    var altezza = document.body.clientHeight; // firefox
    	
    $("#GB_overlay").show(); 
    $("#GB_overlay").css({height:altezza+"px"});
    GB_position(posizione);
    
    //greybox originale
    //GB_position();
    //$("#GB_window").show();  
  }
});

  

}

function GB_hide() {
  $("#GB_window,#GB_overlay").hide();
  //window.location = location;  
    window.location.replace(location);
}

function GB_position(posizione) {
  //greybox originale
  //var de = document.documentElement;
  //var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;  
  //$("#GB_window").css({width:GB_WIDTH+"px",height:GB_HEIGHT+"px",left: ((w - GB_WIDTH)/2)+"px" });
  //$("#GB_frame").css("height",GB_HEIGHT - 32 +"px");
  
  	var w = document.body.clientWidth;  
  	var h = document.body.clientHeight;    	
  	var content = document.getElementById('content');	
	var posContent = findPos(content);	
	
	var uno = ((w - GB_WIDTH)/2);
	var due = ((h - posizione[1])/2);

	var due = (posizione[1]-GB_HEIGHT);
	if(due < posContent[1]){
		var due = posizione[1] + posContent[1];
	}
		
	$("#GB_window").css({ width:GB_WIDTH+"px",height:GB_HEIGHT+"px",left: uno, top: due });
	$("#GB_frame").css("height",GB_HEIGHT - 32 +"px");
}