//CODE DEVELOPED BY OPENDOC 2005 - WWW.OPENDOC.IT//
var EXP_item_cat;  //name of the Image Catalog 
var EXP_item_name; //name of the image to view
var URL_info_image; //url to get the image information (width,height,numpages)
var URL_image_start; //address of the Express Server to retrive the image width catalog and item
var num_pages; 		//Number of page of the document
var dimIMGOrigx;	//Original dimension of the image
var dimIMGOrigy;
var dimXPreview;	// Dimension (width/Height) of the preview image
var dimYPreview;
var document_page=0; //Actual document page 
var use_xml=true;
var ARRAY_dimension_image=undefined; //Array of the images dimension if used with JIESViewer
var obj_ante;
var img_ante_vuota;
var objpreview;
// Detect if the browser is IE or not.
// If it is not IE, we assume that the browser is NS.
var IE = document.all?true:false

// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) document.captureEvents(Event.MOUSEMOVE)


var viewUserPreview=true; //indicate if user wanto to see the preview (true,false);



function get(obj){ return document.getElementById(obj);}



/*******************************************************************************************************
*	Generate the Jies Viewer 
*	width the item_name and teh catalog (cat_name)
*	arrayDim is an option parameter, that indicates the pages width/height 
********************************************************************************************************/
function JIESViewer(item_name,cat_name,arrayDim){
	EXP_item_cat=cat_name;
	EXP_item_name=item_name;
	URL_info_image=adr_exp_server_info+"?style=none&cat="+EXP_item_cat+"&item="+EXP_item_name+"&page="+document_page;
	URL_image_start=adr_exp_server+"?cat="+EXP_item_cat+"&item="+EXP_item_name+type_output_image;
	if (arrayDim!=undefined){
		ARRAY_dimension_image=arrayDim;
	}
	window.onload=load_information;
	
}


/*******************************************************************************************************
*	Load the start information ...
*******************************************************************************************************/
function load_information(){
	initDHTMLAPI();
	create_html_start("container_viewmap");
	if (ARRAY_dimension_image!=undefined){
		num_pages=ARRAY_dimension_image.length;
		dimIMGOrigx=ARRAY_dimension_image[document_page][0];
		dimIMGOrigy=ARRAY_dimension_image[document_page][1];
		dimOrigx=dimIMGOrigx;
		dimOrigy=dimIMGOrigy;
		initSystemPan();
	}else{
		makeRequest(URL_info_image,readInitContents);
	}
}
/*
END load_information
*******************************************************************************************************/

/*******************************************************************************************************
*	Init JiesViewer:
*	1. Calculate the preview dimension
*	2. Create the preview
*	3. Create the Map (image)
*	4.Create the thumbnails list
*******************************************************************************************************/
function initSystemPan(){
	//Calculate the dimension of preview
	var factor;
	if ((dimIMGOrigx/dimIMGOrigy)>(200/125)) {
		factor=dimIMGOrigx/200;
	}
	else{
		factor=dimIMGOrigy/125;
	}
    if (factor!=0){
        dimXPreview=parseInt(dimIMGOrigx/factor);
	    dimYPreview=parseInt(dimIMGOrigy/factor);
    }
    //Create the preview
    //Load the object
    new dragElement('preview_rect');	
	obj_ante=get("preview_rect");
	img_ante_vuota=get("img_ante_vuota");
	objpreview=get("preview");
	//Show the principal object
	objpreview.style.display="inline";
	//Load the image preview
	img_preview=get("preview_img");
	img_preview.src=adr_exp_server+"?cat="+EXP_item_cat+"&item="+EXP_item_name+type_output_image+"&method=scale&wid="+dimXPreview+"&hei="+dimYPreview+"&page="+document_page;
	objpreview.style.height=dimYPreview+"px";
	objpreview.style.width=dimXPreview+"px";
	
	get("iconPreview").src=path_libraries+"images/button_up.gif";
	get("linkIconPreview").title="Show preview";
	get("iconPreview").alt="Show preview";
	get("linkIconPreview").onclick=HidePreview;
	setOpacity('preview',80);
	setStartPositionPreview();
	get("toolbar_preview").style.width=dimXPreview+"px";

	//Create the select to change the page
	var OBJselect_pages=document.getElementById("selectpage");
	if (OBJselect_pages!=null){
		for (var conta=0;conta<num_pages;conta++) {
		    var OBJoption=document.createElement("option");
		    OBJoption.setAttribute('value',conta);
		    if (conta==document_page) {OBJoption.setAttribute('selected',"selected");}
		    var OBJtext=document.createTextNode(conta+1);
		    OBJoption.appendChild(OBJtext);
		    OBJselect_pages.appendChild(OBJoption);
		}
	}
	//END Create the select to change the page
	
	/*Create all the tile of the image*/
	initMap();
	
	/*Create Thumbnails list...*/
	create_thumbnails("thumbnails",num_pages);
	get("thumbnails").style.height=get("viewmap").offsetHeight;
	ShowHide("thumbnails","show");	
	setPosition("thumbnails","viewmap","outtopright");
	ShowHide("thumbnails","hide");
	/*END Create the Thumbnails...*/
}
/*
END initSystemPan
*******************************************************************************************************/

/*******************************************************************************************************
Create the thumbnails:
create 'num_pages' image html (TAG IMG) in teh object 'id_thumbnails'
width the src blank
Then call function loadOneThumbnail
*/
function create_thumbnails(id_thumbnails,num_pages){
	var scorri; 		//for index
	var link_temp; 		//temporary Link for the single thumbnail
	var new_page;		//actual page
	var ris="";			//result
	var adr_image_tmp; //Nome dell'immagine da caricare senza la pagina
	objThumb=getRawObject(id_thumbnails); //Oggetto contenitore delle thumbnails

	adr_image_tmp=adr_exp_server_thumb+"?cat="+EXP_item_cat+"&item="+EXP_item_name+"&thumbspec="+name_thumbspec+type_output_image;
	for(scorri=0;scorri<num_pages;scorri++){
		new_page=scorri+1;
		link_temp='<a title="Go to page: '+new_page+'" href="#" onclick="moveToPage('+scorri+");return false\"><img id=\"thumby"+scorri+"\" src=\"\" alt=\"Go to page: "+new_page+"\" title=\"Go to page:"+new_page+"\">page "+new_page+"</a>";
		
		ris+="<div id=\"thumb"+scorri+"\" class=\"singolathumb\">"+link_temp+"</div>";
	}
	objThumb.innerHTML =ris;
	loadOneThumbnail(adr_image_tmp,num_pages,0);
}
/*
END Create the thumbnails 
*******************************************************************************************************/

/*******************************************************************************************************
*
* 	Function that load one at time the thumnail images previously  created blank
*	by the function : create_thumbnails
*	
*
*/
function loadOneThumbnail(temp_image_name,number_image,start){
	//temp_image_name: nome immagine djvu 
	get("thumby"+start).src=temp_image_name+'&page='+ start;
	//richiamo la funzione
	start=start+1;
	if (start<number_image){
		setTimeout('loadOneThumbnail("'+temp_image_name+'",'+(number_image)+','+start+')',500);
	}
}
/*
END loadOneThumbnail
*******************************************************************************************************/


/*******************************************************************************************************
* 	Set the position of the first object 
*	near the second in the position 
*	pos : topleft,topright,bottomleft,bottomright,outtopright,outtop
*******************************************************************************************************/
function setPosition(firstObj,secondObj,pos){
	topSecondObj=parseInt(findPosY(get(secondObj)));
	leftSecondObj=parseInt(findPosX(get(secondObj)));
	bottomSecondObj=topSecondObj+get(secondObj).offsetHeight-get(firstObj).offsetHeight;
	rightSecondObj=leftSecondObj+get(secondObj).offsetWidth-get(firstObj).offsetWidth;
	topSecondObj=topSecondObj+"px";
	rightSecondObj=rightSecondObj+"px";
	bottomSecondObj=bottomSecondObj+"px";
	switch(pos){
		case "topright":
			setPositionXY(firstObj,rightSecondObj,topSecondObj)
		break
		case "bottomleft":
			setPositionXY(firstObj,leftSecondObj,bottomSecondObj);
		break
		case "bottomright":
			setPositionXY(firstObj,rightSecondObj,bottomSecondObj);
		break
		case "outtopright":
			rightSecondObj=leftSecondObj+get(secondObj).offsetWidth;
			rightSecondObj=rightSecondObj+"px";
			setPositionXY(firstObj,rightSecondObj,topSecondObj);
		break
		case "outtop":
			topSecondObj=parseInt(topSecondObj)-get(firstObj).offsetHeight;
			topSecondObj=topSecondObj+"px";
			setPositionXY(firstObj,rightSecondObj,topSecondObj);
		break
		default:
			setPositionXY(firstObj,leftSecondObj,topSecondObj);
	}
}

function setPositionXY(obj,xNew,yNew){
	get(obj).style.top=yNew;
	get(obj).style.left=xNew;
}
/*
END setPosition
*******************************************************************************************************/



/*******************************************************************************************************
* 	
*	manage the Show7Hide about the preview
*
*******************************************************************************************************/
function ShowPreview(e){
	ShowHidePreview('show');
}

function HidePreview(e){
	ShowHidePreview('hide');
}

function ShowUserPreview(e) {
	viewUserPreview=true;
	ShowHidePreview('show');
	refreshSquareRedPreview();
	return false;
}

function HideUserPreview(e) {
	viewUserPreview=false;
	ShowHidePreview('hide');
	refreshSquareRedPreview();
	return false;
}
function ShowHidePreview(type){
	//var txt_showhide=get('showhide_preview');
	if (type=="hide"){

		ShowHide('preview',type);
		get("linkIconPreview").onclick=ShowUserPreview;
		get("iconPreview").src=path_libraries+"images/button_up.gif";
		get("linkIconPreview").title="Show preview";
		get("iconPreview").alt="Show preview";
	}
	else{
		ShowHide('preview',type);
		get("linkIconPreview").onclick=HideUserPreview;
		get("iconPreview").src=path_libraries+"images/button_down.gif";
		get("linkIconPreview").title="Hide preview";
		get("iconPreview").alt="Hide preview";
		setStartPositionPreview();
	}
	
}






/*******************************************************************************************************
*	showThumbnails: 
*	Show or Hide the thumbnails list
*******************************************************************************************************/
function showThumbnails() {
	if (get("thumbnails").style.display=="none"){
		ShowHide("thumbnails","show");
		setPosition("thumbnails","viewmap","outtopright");	
	}
	else{
		ShowHide("thumbnails","hide");
	}
}

/*******************************************************************************************************
*	setStartPositionPreview: Set the start position of the preview
*******************************************************************************************************/
function setStartPositionPreview(){
	setPosition('preview','toolbar_preview','outtop');
}
/*******************************************************************************************************
* 	Functions about moving to pages
*	
*	moveToPage
*	nextPage
*	lastPage
*	firstPage
*	prevPage
*******************************************************************************************************/
var previousPage=-1;
function moveToPage(newPage){
	if (previousPage!=-1){
		if (get("thumb"+previousPage)){
			get("thumb"+previousPage).className="singolathumb";
		}
	}
	if (get("thumb"+newPage)){
		get("thumb"+newPage).className = "thumbattuale";
	}
	document_page=newPage;
	get_data_new_page(document_page);
	get('selectpage').options[newPage].selected=true;
	previousPage=newPage;
}

function nextPage(){
	last=parseInt(get('selectpage').length)-1;
	page_act=parseInt(document_page);
	if (page_act<last){
		moveToPage(page_act+1);
	}
}

function lastPage(){
	last=parseInt(get('selectpage').length)-1;
	moveToPage(last);
}

function firstPage(){
	moveToPage(0);
}

function prevPage(){
	page_act=parseInt(document_page);
	if (page_act>0){
		moveToPage(page_act-1);
	}
}

function changePage(theElement){
	moveToPage(parseInt(theElement.value));
	return false;
}
/*
END Moving Page
*******************************************************************************************************/

/*******************************************************************************************************
* get information (dimension) about new page
*	
*******************************************************************************************************/
function get_data_new_page(newpage){
	//new address
   	URL_info_image=adr_exp_server_info+"?style=none&cat="+EXP_item_cat+"&item="+EXP_item_name+"&page="+newpage;
   	if (ARRAY_dimension_image!=undefined){
   		dimIMGOrigx=ARRAY_dimension_image[newpage][0];
		dimIMGOrigy=ARRAY_dimension_image[newpage][1];
		change_page()   		
   	}else{
    	makeRequest(URL_info_image,changePageContents);
   	}
}


/*******************************************************************************************************
* 	After have get the dimension of new page, 
*	load the new page
*******************************************************************************************************/
function change_page(){
    // found new dimension preview
    var factor;
    if ((dimIMGOrigx/dimIMGOrigy)>(200/125)) {
		factor=dimIMGOrigx/200;
	}
	else{
		factor=dimIMGOrigy/125;
	}
	if (factor!=0){
         dimXPreview=parseInt(dimIMGOrigx/factor);
	     dimYPreview=parseInt(dimIMGOrigy/factor);
	}
	//load new preview
    img_preview=document.getElementById("preview_img");
	img_preview.src=adr_exp_server+"?cat="+EXP_item_cat+"&item="+EXP_item_name+"&method=scale&wid="+dimXPreview+"&hei="+dimYPreview+"&page="+document_page;
	img_preview.style.width=dimXPreview+"px";
	img_preview.style.height=dimYPreview+"px";
	get("preview").style.width=dimXPreview+"px";
	get("toolbar_preview").style.width=dimXPreview+"px"
	srcWidth =dimIMGOrigx;
   	srcHeight =dimIMGOrigy;
   	change_zoom(0);
}
/* END change_page
*******************************************************************************************************/

/******************************************************************************************************
*	refreshPreview:
*	refresh the content of the preview (often at the zoom change)
*	Hide/show the preview 
*******************************************************************************************************/
function refreshPreview(){
	var lev = zoom_level;
	var cpY=posCenterY/dimOrigy;
	var cpX=posCenterX/dimOrigx;
	rect_red_x=parseInt(width_viewmap*dimXPreview/(dimOrigx));
	rect_red_y=parseInt(height_viewmap*dimYPreview/(dimOrigy));

	if ((rect_red_x<dimXPreview) || (rect_red_y<dimYPreview)){
		obj_ante.style.display="inline";
		img_ante_vuota.style.display="";
		if (viewUserPreview) ShowHidePreview('show');
	}else{	
		obj_ante.style.display="none";
		img_ante_vuota.style.display="none";
		ShowHidePreview('hide');
	}

	if (rect_red_x>dimXPreview) {rect_red_x=dimXPreview;}
	if (rect_red_y>dimYPreview) {rect_red_y=dimYPreview;}
	var newTop=parseInt(cpY*dimYPreview-parseInt(rect_red_y/2));
	
	if (newTop<0){newTop=0;}
	var newLeft= parseInt(cpX*dimXPreview-parseInt(rect_red_x/2));
	if (newLeft<0){newLeft=0;}
	obj_ante.style.width=rect_red_x;
	obj_ante.style.height=rect_red_y;
	img_ante_vuota.width=rect_red_x;
	img_ante_vuota.height=rect_red_y;
	obj_ante.style.top=newTop;
	obj_ante.style.left=newLeft;
}

/******************************************************************************************************
*	Check if the square red is bigger then the previw 
*	if is true hide the square
*******************************************************************************************************/
function refreshSquareRedPreview(){
	rect_red_x=parseInt(width_viewmap*dimXPreview/(dimOrigx));
	rect_red_y=parseInt(height_viewmap*dimYPreview/(dimOrigy));

	if ((rect_red_x<dimXPreview) || (rect_red_y<dimYPreview)){
		obj_ante.style.display="inline";
		img_ante_vuota.style.display="";
	}else{
		img_ante_vuota.style.display="none";
		obj_ante.style.display="none";
	}
}


/*******************************************************************************************************
*	show_value_zoom:
*	Show in the toolbar teh zoom level
*******************************************************************************************************/
function show_value_zoom(act_level_zoom){
	if (get("view_zoom_value")){
		var percent_zoom=100/Math.pow(2,act_level_zoom);
		get("view_zoom_value").value=percent_zoom+"%";
	}
}




/*******************************************************************************************************
*	find the X postion of th object
*******************************************************************************************************/
function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
		
	if (obj.offsetLeft){
		curleft+=obj.offsetLeft;
	}
	return curleft;
}

/*******************************************************************************************************
*	find the X postion of th object
*******************************************************************************************************/
function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{	
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
		
	if (obj.offsetTop){
		curtop+=obj.offsetTop;
	}
	return curtop;
}

/*******************************************************************************************************
*	Set th Opacity of an object
*	opacity is an integer from 0 to 100
*******************************************************************************************************/
function setOpacity(obj,opacity){
	var non_IE_opacity=parseInt(opacity)/100;
	// Internet Explorer opacity
	get(obj).style.filter="alpha(opacity="+opacity+")";
	// Mozilla/Firefox/Netscape
	get(obj).style.MozOpacity=non_IE_opacity;
	// Safari>1.2
	get(obj).style.opacity =non_IE_opacity;
	// Safari<1.2, Konqueror
	get(obj).style.KHTMLOpacity = non_IE_opacity;
}

/*******************************************************************************************************
*	ShowHide(name_id,type):
*	Show or hide an object 
*	name_id: object id
*	type: (string) "hide" or "show"
*******************************************************************************************************/
function ShowHide(name_id,type){
	var obj_sh=get(name_id);
	if (type=="hide"){
		obj_sh.style.display = "none";
	}
	else{
		
		obj_sh.style.display = "inline";
	}
}
