var country;

isNS = (window.innerWidth)?true:false;

var specialcase = ((navigator.userAgent.indexOf('Mac') != -1) || document.all)
var flag = 0;
var msg = "";

var x,y,x1,y1,copyAttempt;

var YScroll=0; //stores scroll state value

var bigImages = new Array();
var queryStrings = new Array();
var windowCoords = new Array();
var aspParams;


function init()
{
   if (!(document.getElementById || document.all || document.layers)) return;
   
   initImages();
   
   if (specialcase && document.layers)
   {
      document.captureEvents(Event.MOUSEMOVE);
      document.onmousemove = special;
   }
   for (i=0;i<document.images.length;i++)
   {
            document.images[i].onmousedown = checkIt;
            document.images[i].onmouseup = function() {return false};
            if (specialcase)
            {
               document.images[i].onmousemove = special;
               document.images[i].onclick = clearIt;
            }
      
   }
}

function initImages(){
  
         msg = "Esta imagem está protegida por direitos de autor.\n\n© 2004 LuzHenriques.com.pt. Todos os Direitos reservados.";
  
   
    
      var aspParamsEl =  document.getElementsByName("aspParam");
      
      if( aspParamsEl.length ) {
		  aspParams="";
         for(j=0 ; j<aspParamsEl.length ; j++) {
            aspParams += "&" + aspParamsEl[j].getAttribute("param") ;
         }
      }
   
   var thumbs = document.getElementsByName("thumb");
   
   for(i=0; i<thumbs.length ;i++){
      
      if( thumbs[i].src ){
         var parent = thumbs[i].parentNode;
         var img = new Image();
         var path  = getImgPath( thumbs[i].src, "_small");
         img.src = path;  //loads the image path 
         bigImages[i] = img;
         
        
         var onClickFunBody = "openBigImage('"+i+"');" ;
         
         thumbs[i].title="Clique para ampliar a imagem";
        
         var link ;
         //--
         if( parent.nodeName.toLowerCase() == "a") {
            link = parent;
         } else {
            link = document.createElement("a");
            
            var removedNode = parent.removeChild( thumbs[i] ); //remode(new node , old node )
            link.appendChild( removedNode );
            parent.appendChild(link);
         }
           
         link.style.cursor = "pointer";
         
         link.onclick = new Function( onClickFunBody );
            
        
         thumbs[i].style.visibility = "visible";
         
         } else {
          thumbs[i].style.visibility = "hidden";  
         }
      }
}

//--

//prepares popup window for displaying big image
function openBigImage( imgNumber ) {

   updateScroll();
   
   var win;
   
   if(queryStrings[imgNumber]) {
      win = window.open(  queryStrings[imgNumber] ,'','top='+windowCoords[imgNumber][0]+',left='+windowCoords[imgNumber][1]+',width='+windowCoords[imgNumber][2]+',height='+windowCoords[imgNumber][3]+',location=no,status=no,resizable=yes,scrollbars=yes,menubar=no,titlebar=no',false);
   } else {
   var img = bigImages[imgNumber];
  
   // Array[ top , left , width , height, imgWidth, imgHeight ]
   var coords = getWindowCoords(imgNumber);
   var query = "/Scripts/show.asp?";
   query += "file="+img.src;
   query += "&width="+coords[4];
   query += "&height="+coords[5];
   
   query += aspParams;
   
   queryStrings[imgNumber]  = query;
   windowCoords[imgNumber]  = coords;
   
   win = window.open(  query ,'','top='+coords[0]+',left='+coords[1]+',width='+coords[2]+',height='+coords[3]+',location=no,status=no,resizable=yes,scrollbars=yes,menubar=no,titlebar=no',false);
   
   }
   
   win.onUnload = "this.opener.restoreScroll()";

}


//prepares popup window for displaying image information
function openImageDescription(filename) {
 //centers window 
 
 //--
  updateScroll();
   
   var win;
   var query = "/Scripts/showdetails.aspx?";
   query+= "filename="+filename;
   
   //resize and center
   
   var screenWidth = screen.availWidth;
   var screenHeight = screen.availHeight;
  
   var newWindowWidth = 420;
   var newWindowHeight = 210;
     
    //center
    newPosX = Math.floor((screenWidth-newWindowWidth)/2);
    newPosY = Math.floor((screenHeight-newWindowHeight)/2);
    
   //	
   win = window.open(  query ,'','top='+newPosY+',left='+newPosX+',width='+newWindowWidth+',height='+newWindowHeight+',location=no,status=no,resizable=no,scrollbars=yes,menubar=no,titlebar=no',false);;
   
}

function updateScroll()
{
   if (window.Event) //is NN4
      YScroll = window.pageYOffset;
   else //is IE
      YScroll = document.body.scrollTop;
}

function restoreScroll() {
   window.scrollTo(0,YScroll);   
}


//--  returns Array[ 0_top , 1_left , 2_width , 3_height , 4_imgWidth , 5_imgHeight ]
function getWindowCoords(imgNumber){
  
   var coords = new Array();
   var img = bigImages[imgNumber];
   var imgratio = img.width / img.height;
   
   var factor  = 0.9;
   
   var windowXMargin = 100;
   var windowYMargin = 120;
   
   var maxImgWidth = screen.availWidth  - windowXMargin;
   var maxImgHeight = screen.availHeight - windowYMargin ; 
   
   var exceeds = ( img.width >= maxImgWidth || img.height >= maxImgHeight );
   
   if(exceeds){
      if(imgratio <= 1 ) { //w<=h 
       //by height...
       coords[5] = maxImgHeight;    
       coords[4] = Math.floor(coords[5]*imgratio) ; //w=h*ratio
       
       coords[3] = (maxImgHeight + windowYMargin);
       coords[2] = Math.floor(maxImgWidth*imgratio) + windowXMargin;
         
      } else { //w>h
        //by width...
         coords[4] = maxImgWidth;
         coords[5] = Math.floor(coords[4] / imgratio); //h=w/ratio
         
         coords[2] = (maxImgWidth + windowXMargin);
         coords[3] = Math.floor(maxImgHeight / imgratio)+windowYMargin;
      }
      
   } else { //does not exceed maxImg dimensions
        coords[2] = img.width + windowXMargin;
        coords[3] = img.height + windowYMargin;
        coords[4] = img.width;
        coords[5] = img.height;
   }
   
   //window left pos
   coords[1] = Math.floor( (screen.availWidth - coords[2]) /2);
   
   //window top pos
   coords[0] = Math.floor( (screen.availHeight - coords[3]) /2 )  ;
  
   return coords;
}



function getImgPath(pathSrc, suffix) { 
   var s = pathSrc.split(suffix);
   s = s[0] + s[1];
   
   return s;
}

//--?
function printTitle() {
   document.write( document.getElementById("TituloExposicao").getAttribute("param").split("exhibition=")[1]);
}



function checkIt(e)
{
   
   copyAttempt = 0;
   if (window.Event)
   {
      x = e.screenX;
      y = e.screenY;
      theButt = (e.which == 3);
   }
   else
   {
      x = window.event.clientX;
      y = window.event.clientY;
      theButt = (window.event.button == 2);
   }
   if (theButt)
   {
      copyAttempt = 1;
      flag = 0;
      alert(msg);
      document.location.reload();
      return false; // NN4 only
   }
   if (specialcase) flag = 1;
   return false;
   
}



function special(e)
{
   theObj = '';
   if (window.Event) //NN4
   {
      x1 = e.screenX;
      y1 = e.screenY;
      if (e.target.parentNode) theObj = e.target.parentNode.tagName;
   }
   else //IE
   {
      x1 = window.event.clientX;
      y1 = window.event.clientY;
      theObj = window.event.srcElement.parentElement.tagName;
   }
   var isLink = (theObj == 'A');
   if (flag && (!isLink || ((Math.abs(x-x1) > 10) || (Math.abs(y-y1) > 10))))
   {
      copyAttempt = 1;
      flag = 0;
      alert(msg);
      document.location.reload();
      return false;
   }
}

function clearIt()
{
   flag = 0;
   if (copyAttempt)
   {
      copyAttempt = 0;
      return false;
   }
}

//força o reload da página , após o alertbox
function rld() {
document.location.reload()
}

