//////////////////////////////////////////////////Ã
//  SlideShow.js javascript 
//
// gup( name )
// getScreenSize()
// getBrowserWindowSize()
// zoomIn()
// zoomOut()
// show(id)
// hide(id)
// replaceButtonText(buttonId, text)
// get_slidenames()
// CacheImage(ImageSource) 
// ShowSlide(Direction)
// PlaySlideShow(sw)
// PlaySlideShowRecursive(sw)
// DownloadRecursive() 
// Download() 
// CacheNextSlide() 
// StartSlideShow() 

var screenW = 640, screenH = 480;
var CurrentSlide = -1;
			
str=location.href;
re= /Full/;
if (str.search(re) == -1)
	imageMode="small";
else
	imageMode="large";

////////////////////////////////////////////////////////
function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

//var n_slides=gup('n_slides');


/////////////////////////////////////////////////////////
function getScreenSize()
{
	if (parseInt(navigator.appVersion)>3)
	{
  	screenW = screen.width;
  	screenH = screen.height;
  }
	else if (navigator.appName == "Netscape"
    && parseInt(navigator.appVersion)==3
    && navigator.javaEnabled()
   )
	{
 		var jToolkit = java.awt.Toolkit.getDefaultToolkit();
 		var jScreenSize = jToolkit.getScreenSize();
 		screenW = jScreenSize.width;
 		screenH = jScreenSize.height;
	}
}



////////////////////////////////////////////////////////
function getBrowserWindowSize() {
    var winW = 630, winH = 460;

    if (parseInt(navigator.appVersion)>3) {
        if (navigator.appName=="Netscape") {
            winW = window.innerWidth;
            winH = window.innerHeight;
        }
        if (navigator.appName.indexOf("Microsoft")!=-1) {
            winW = document.body.offsetWidth;
            winH = document.body.offsetHeight;
        }
    }

    var rval = {
        width: winW,
        height: winH
    };

    return rval;
}


////////////////////////////////////////////////////////
function zoomIn()
{
oldLocation=window.location;
getScreenSize();


//alert("W="+screenW);
//alert("H="+screenH);
//window.personalbar.visible=false;
//window.resizeTo(screenW,screenH);

//alert("Resized !");
//window.location="http://sanpolino.it/CachedSlideShowFull.php?sw="
//                +screenW+"&sh="+screenH+"&oldLocation="+oldLocation;


window.open("http://sanpolino.it/CachedSlideShowFull.php?sw="
                +screenW+"&sh="+screenH
								+"&oldLocation="+oldLocation
								+"&CurrentSlide="+CurrentSlide
								+"&n_slides="+n_slides
								, "FullScreenWindow","width="+screenW+",height="+screenH+",fullscreen=1,toolbar=0,status=0,personalbar=0,menubar=0,location=0,directories=0,dependent=1");
							//	, "FullScreenWindow","width="+screenW+",height="+screenH+",fullscreen=1,toolbar=1,status=0,personalbar=0,menubar=1,location=0,directories=0,dependent=1");

//window.open("http://sanpolino.it/CachedSlideShowFull.php?sw="
//                +screenW+"&sh="+screenH+"&oldLocation="+oldLocation, "FullScreenWindow");
}
////////////////////////////////////////////////////////
function zoomOut()
{
	self.close();
  //window.location=gup('oldLocation');
}

//	#Next {position:absolute; left:20px; top:350px;}
//	#Previous {position:absolute; left:120px; top:350px;}
//Preloaded slideshow script- By Jason Moon (jasonmoon@usa.net)
//For this script and more
//Visit http://www.dynamicdrive.com
var slideShowMode='Manual';
var interval =2000;

var ns4 = (document.layers);
var ie4 = (document.all && !document.getElementById);
var ie5 = (document.all && document.getElementById);
var ns6 = (!document.all && document.getElementById);

////////////////////////////////////////////////////////
function show(id){
	// Netscape 4
	if(ns4){
		document.layers[id].visibility = "show";
	}
	// Explorer 4
	else if(ie4){
		document.all[id].style.visibility = "visible";
	}
	// W3C - Explorer 5+ and Netscape 6+
	else if(ie5 || ns6){
		document.getElementById(id).style.visibility = "visible";
	}
}

//////////////////////////////////////////////////////
function hide(id){
	// Netscape 4
	if(ns4){
		document.layers[id].visibility = "hide";
	}
	// Explorer 4
	else if(ie4){
		document.all[id].style.visibility = "hidden";
	}
	// W3C - Explorer 5+ and Netscape 6+
	else if(ie5 || ns6){
		document.getElementById(id).style.visibility = "hidden";
	}
}

//////////////////////////////////////////////////////
function replaceButtonText(buttonId, text)
{
  if (document.getElementById)
  {
    var button=document.getElementById(buttonId);
    if (button)
    {
      if (button.childNodes[0])
      {
        button.childNodes[0].nodeValue=text;
      }
      else if (button.value)
      {
        button.value=text;
      }
      else //if (button.innerHTML)
      {
        button.innerHTML=text;
      }
    }
  }
}

function parseUri(sourceUri){
	var uriPartNames = ["source","protocol","authority","domain","port","path","directoryPath","fileName","query","anchor"],
		uriParts = new RegExp("^(?:([^:/?#.]+):)?(?://)?(([^:/?#]*)(?::(\\d*))?)((/(?:[^?#](?![^?#/]*\\.[^?#/.]+(?:[\\?#]|$)))*/?)?([^?#/]*))?(?:\\?([^#]*))?(?:#(.*))?").exec(sourceUri),
		uri = {};
	
	for(var i = 0; i < 10; i++){
		uri[uriPartNames[i]] = (uriParts[i] ? uriParts[i] : "");
	}
	
	/* Always end directoryPath with a trailing backslash if a path was present in the source URI
	Note that a trailing backslash is NOT automatically inserted within or appended to the "path" key */
	if(uri.directoryPath.length > 0){
		uri.directoryPath = uri.directoryPath.replace(/\/?$/, "/");
	}
	
	return uri;
}


function get_slidenames()
{
		//alert("OLDPATH="+window.location.pathname);
		if (imageMode == "large")
		{
			//alert("imageMode 1="+imageMode);
			//alert("imageMode 2="+imageMode);
			//alert("testing 0");
			//alert("oldLocation="+oldLocation);
			//alert("testing 1");
			//alert("uri path="+parseUri(oldLocation).directoryPath);
			//alert("testing 2");
		}
		//Slides=new Array();
		//alert("(1) n_slides="+n_slides);

		if (imageMode == "small")
    {
			newPathName="";
			pathArray=window.location.pathname.split('/');
			for (i=0;i<pathArray.length-1;++i) 
			{
				newPathName += pathArray[i];
  			newPathName +='/';
				//alert("PathName="+newPathName);
			}
		}

		if (imageMode == "large")
		{
			newPathName= parseUri(oldLocation).directoryPath;
		}

		/*
		if (imageMode == "large")
		{
			//alert("oldLocation="+oldLocation);
			pathArray=oldLocation.split('/');
		}
		for (i=0;i<pathArray.length-1;++i) 
		{
			newPathName += pathArray[i];
  		newPathName +='/';
  		if (imageMode == "large")
  		{
			//alert ("i="+i+" pathArray[i]="+pathArray[i]+ " newPathName="+newPathName);
			}
		}
		*/
		//if (newPathName == '/') {slash="";} else {slash="/";}
		//alert("PATHNAME="+newPathName+ " slash="+slash+"*");
	
		//alert("n_slides="+n_slides);
		for(i=1;i<=n_slides;++i)
		{
  		if (imageMode == "small")
  		{
    		//Slides[i-1]=newPathName+slash+'SlideShow/medium/img'+i+'.jpg';
    		Slides[i-1]=newPathName+'SlideShow/medium/img'+i+'.jpg';
  		}
 			else
  		{
    		//Slides[i-1]=newPathName+slash+'SlideShow/large/img'+i+'.jpg';
    		Slides[i-1]=newPathName+'SlideShow/large/img'+i+'.jpg';
  		}
			//alert("SLIDE="+Slides[i-1]);
		}
		//alert("Slide="+Slides[0]);
	}
	//get_slidenames();


// DO NOT EDIT BELOW THIS LINE!

//////////////////////////////////////////////////////
function CacheImage(ImageSource) 
{ // TURNS THE STRING INTO AN IMAGE OBJECT

//alert ("Caching..");
	var ImageObject = new Image();
	ImageObject.src = ImageSource;
	if (imageMode =="small")
		ImageObject.style.width=200;
	else
		ImageObject.style.width=800;
	return ImageObject;
}


//////////////////////////////////////////////////////
function ShowSlide(Direction)
{
//alert("Testing...");
	if (SlideReady) 
	{
//alert("Current Slide="+CurrentSlide);
		NextSlide = CurrentSlide + Direction;

		// THIS WILL DISABLE THE BUTTONS (IE-ONLY)
		//document.SlideShow.Previous.disabled = (NextSlide == 0);
		//document.SlideShow.Next.disabled = (NextSlide == (Slides.length-1)); 

		if ((NextSlide >= 0) && (NextSlide < Slides.length)) 
		{ 
			if (imageMode == "small")
			{
//alert("Testing A...");
				document.images['Screen'].src = Slides[NextSlide].src;
//alert("Testing B...");
			}
			else
			{
		//alert("NextSlide="+NextSlide);
				document.images['Screen'].src = Slides[NextSlide].src;
			//	if (document.body)
			//	document.body.background=Slides[NextSlide].src;
			}
			CurrentSlide = NextSlide++;
			Message = 'Picture ' + (CurrentSlide+1) + ' of ' + Slides.length;
			self.defaultStatus = Message;
			if (Direction == 1) CacheNextSlide();
		}
		return true;
	}
}

//////////////////////////////////////////////////////
function PlaySlideShow(sw)
{
	if (slideShowMode == 'Manual')

	{ 
			hide("Previous");
			hide("Next");
			slideShowMode = "Auto";
			//replaceButtonText("PlayControl", "Stop");
			//replaceButtonText("SlideShow", "Stop");
			swap('SlideShow', 'ManualGlow_img');
			SlideShowIcon="Manual_img";
			SlideShowGlowingIcon="ManualGlow_img";
			PlaySlideShowRecursive(sw);
	}
	else
	{
			show("Previous");
			show("Next");
			slideShowMode = "Manual";
			//replaceButtonText("PlayControl", "Play");
			swap('SlideShow', 'SlideShowGlow_img');
			SlideShowIcon="SlideShow_img";
			SlideShowGlowingIcon="SlideShowGlow_img";
			clearTimeout(timerID);
	}
}


function PlaySlideShowRecursive(sw)
{

	Direction=1;
	if (SlideReady) 
	{
		NextSlide = CurrentSlide + Direction;
		if (NextSlide ==Slides.length) NextSlide=0;
		if (sw==0)
		{
			// THIS WILL DISABLE THE BUTTONS (IE-ONLY)
//			document.SlideShow.Previous.disabled = (NextSlide == 0);
//			document.SlideShow.Next.disabled = (NextSlide == (Slides.length-1)); 
			// hides Previous and Next buttons
		}

		if ((NextSlide >= 0) && (NextSlide < Slides.length)) 
		{
			document.images['Screen'].src = Slides[NextSlide].src;
			CurrentSlide = NextSlide++;
			Message = 'Picture ' + (CurrentSlide+1) + ' of ' + Slides.length;
			self.defaultStatus = Message;
			if (Direction == 1) CacheNextSlide();
		}
		//var recur_call="Download();PlaySlideShowRecursive(1)";
		var recur_call="DownloadRecursive()";
		timerID =setTimeout(recur_call,interval)
		//return true;
	}

}

//////////////////////////////////////////////////////
function DownloadRecursive() 
{
	if (Slides[NextSlide].complete) 
	{
		SlideReady = true;
		self.defaultStatus = Message;
    PlaySlideShowRecursive(1);
	}
	else setTimeout("DownloadRecursive()", 100); // CHECKS DOWNLOAD STATUS EVERY 100 MS
	return true;
}


//////////////////////////////////////////////////////
function Download() 
{
	if (Slides[NextSlide].complete) 
	{
		SlideReady = true;
		self.defaultStatus = Message;
	}
	else setTimeout("Download()", 100); // CHECKS DOWNLOAD STATUS EVERY 100 MS
	return true;
}

//////////////////////////////////////////////////////
function CacheNextSlide() 
{
	if ((NextSlide < Slides.length) && (typeof Slides[NextSlide] == 'string'))
	{ // ONLY CACHES THE IMAGES ONCE
		SlideReady = false;
		self.defaultStatus = 'Downloading next picture...';
		Slides[NextSlide] = CacheImage(Slides[NextSlide]);
		Download();
	}
	return true;
}


//////////////////////////////////////////////////////
function StartSlideShow() 
{
	//alert ("IM="+imageMode);
	if(imageMode =="large")
CurrentSlide=gup("CurrentSlide")-1;
	else
		CurrentSlide = -1;
//CurrentSlide = -1;

//alert("2 CurrentSlide="+CurrentSlide);
//alert("2 Slide[0]="+Slides[0]);
//alert("2 Slide[CurrentSlide]="+Slides[CurrentSlide]);

//alert("(2) CurrentSlide="+CurrentSlide+"---");
//alert("Slide="+Slides[0]);
// test initial slide	Slides[0] = CacheImage(Slides[0]);
	Slides[CurrentSlide+1] = CacheImage(Slides[CurrentSlide+1]);
// alert ("AT 1");
//Download();
	SlideReady = true;
	ShowSlide(1);
//alert ("Testing ...");
}


