// -*-java-*-
//  [13 Oct 01]  --- caching --> the code must be here, and not separated in 3 files ...
//  We have to consult the cookie at the client side.


// Exact as plain  HTML

// This is our window reserved for fotos, Global
// var fwin=0;


function getCookie(Name) {                                                               
    var search = Name + "=";
    if (document.cookie.length > 0) { // if there are any cookies
	offset = document.cookie.indexOf(search);
	if (offset != -1) { // if cookie exists
          offset += search.length
          // set index of beginning of value
          end = document.cookie.indexOf(";", offset);
          // set index of end of cookie value
          if (end == -1)
	  end = document.cookie.length;
          return unescape(document.cookie.substring(offset, end));
       }
    }
};




function init_foto(Id,N){
//	document.write ("foto.js");
    Cookie=getCookie("foto");
    // document.write (Cookie);    
    if (getCookie("foto") == "foto.js"){
	change_foto_in_foto(Id,N);
	// document.write ("foto.js");
    }
}


// This is my first web-client computation, i think. Here the burden to generate this simple HTML
// which just says height=100%,    is on the client !!!
function change_foto_in_foto(Id,N){

    //    var fwin=window.fwin;
    //      if (!fwin || (fwin==0)  || (fwin.closed)){
    //  	open_the_fwin();
    //      }
    //    change_here_foto(id,n);

    LeftPosition =  10;
    TopPosition = 0; // screen.height
    Width=500; // screen.width -20;
    Height=500;
    var Settings = 'height='+ Height +',width='+ Width +
            // ',top='+TopPosition+',left='+LeftPosition+
         ',scrollbars=yes,location=no';
    // Settings = 'scrollbars=yes,location=no';
    var Fwin = window.open('','foto',Settings);

    //    var fwin = window.open('http://www.ruska.it/jscript/foto.js','foto');
    // re-init the IMG, so we have to re-init all the HTML (document) ???
    //  alert(e.width+" " +e.height);
    Fwin.document.open("text/html");
    Fwin.document.write("<img name=\"foto\" src=\"/f/" +Id +"/" +N +"\"  height=\"100%\">");
    Fwin.document.close();
    Fwin.focus();
    //	fwin.document.onmousedown=resize_foto;
}


// This is my first web-client computation, i think. Here the burden to generate this simple HTML
// which just says 100%,    is on the client !!!
function change_foto(id,n){
    // re-init the IMG, so we have to re-init all the HTML (document) ???
    document.open("text/html");
    document.write("<img name=\"foto\" src=\"/f/" +Id +"/" +N +"\"  height=\"100%\" >");
    document.close();
    //	fwin.document.onmousedown=resize_foto;
}




function link_to_foto (Id,N,W,H){
    link_to_foto_a (Id,N,W,H,N);
    }

// get HTML to start the A-tag which points to the function ... change_foto
function link_to_foto_a (Id,N,W,H,AltN){
    Cookie=getCookie("foto");

    if (Cookie == "foto.js"){
	// get HTML to start the A-tag which points to the function ... change_foto
	document.write("<a href=\"javascript:change_foto_in_foto("+Id+","+AltN+")\">");
    } else {
	if (Cookie == "foto_same.js"){
	    document.write("<a  href=\"javascript:change_foto_in_foto("+Id+","+AltN+")\">");
	} else {
	    // plain HTML:
            // document.write("<a  href=\"javascript:change_foto_in_foto("+Id+","+AltN+")\">");
                
           // `Good':
           // document.write("<a href=\"/f/" +Id +"/" +AltN +"\" height=\"100%\" >");

          document.write("<a href=\"/ep/foto?/f/" +Id +"/" +AltN +"\" height=\"100%\" >");
                
	}	
    }
    // Common
    //document.write("<IMG src=\"/f/"+Id +"/" +N +"/t\" alt=\"fotografia "+N+"\" width=\""+W+"\" height=\""+H+"\">");

    document.write("<IMG id=\"trigger" + N + "\" src=\"/f/"+Id +"/" +N +"/t/2\" alt=\"fotografia "+N+"\" width=\""+W+"\" height=\""+H+"\">");
    
    document.write("</A>");
}

