﻿var TEST = false;
// Utils
Function.prototype.bind = function(obj) {
	var method = this;
	return function() { return method.apply(obj, arguments); };
}

function GTag(tag) { return document.getElementsByTagName(tag); }
function GId(id) { return document.getElementById(id); }
function T() { var s="";for (i=0;i<arguments.length;i++)s+=arguments[i]+",";alert(s.substring(0,s.length-1));} // simple trace  function
function D() // displays in an element met id=Trace
{	var s="";
	for (var i=0;i<arguments.length;i++)  {s += arguments[i] + "<br />"; }  ; 
	var ID = GId('Trace') ; 
	if(!ID) {alert("<DIV id='Trace' not found"); return; } ;
	ID.innerHTML=s;
	ID.onmousedown = function() {this.style.display='none';} ;
	ID.style.display='block';
}

function ShowLoad(Img)
{
	if(Img && Img.src) 
	{	GId('Thumb').src = Img.src ;
		Img.onload=null; // only once (IE keeps triggering otherwise)
	}
}

function MouseXY(e) // To find the mouse coordinates from within an event
{	var x,y, ev = (e) ? e : window.event;

	if (ev.pageX || ev.pageY) 
	{	x = ev.pageX;
		y = ev.pageY;
	}
	else if (ev.clientX || ev.clientY)
	{	x = ev.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		y = ev.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	return [x,y] ;
}

// Constants
var SHOW_TEXT = "Ver informações",
	SHOW_IMG = "Ver imagens" ;

var Plantas = { 'piso0' : "Images/Piso_0.jpg", 'piso1': "Images/Piso_1.jpg" } ; // big images of floor plans

var THUMB_TOP_OFFSET = 16,
	THUMB_LEFT_OFFSET = 20,
	THUMB_LARGE_SIDE = 160,
	BORDER_WIDTH = 1;

var thumbsize = 600; // smallest dimension
var ZOOM_FACTOR = 1.025; // growth factor
var interval = 40; // resize rate
var DefWidth = 1200, DefHeight = 1000 ;
var TimEvent;
var CurrentZoom, ZoomElement ;

// Global variables
var NO_TITLE = "" ;
var Cinema; // Holds the current image in the main display
var Thumb; // To display small image
var OnScreen ; // holds the object that holds the information about the image in the main display
var PrevItem = null; // to build the chain
var Oc = 0; // test
/*  werkt  niet in IE 
Image.prototype.Scale = function(MaxSize)
{	var ScaleFactor = Math.max(this.height,this.width) / MaxSize ;
	this.style.height = Math.round(this.height / ScaleFactor) + 'px' ;
	this.style.width =  Math.round(this.width / ScaleFactor) + 'px' ;
}
*/
function InitThumb()
{	Thumb = GId('Thumb') ;
	Thumb.style.display = 'none' ;
	Thumb.style.border = "2px solid maroon" ;
}

function SetThumb( Img ) 
{	var ScaleFactor, h, w, MaxSize = THUMB_LARGE_SIDE ;

	var I=Img;
	if (I.width == 0)
	{ // IE hack
		I = new Image ; 
		I.src = Img.src ; 
	}
	ScaleFactor = Math.max(h=I.height,w=I.width) / MaxSize ;

	if (ScaleFactor) {
		Thumb.style.height = Math.round(h / ScaleFactor) + 'px' ;
		Thumb.style.width =  Math.round(w / ScaleFactor) + 'px' ;
	} else {
		Thumb.style.height = MaxSize + 'px' ;
		Thumb.style.width =  MaxSize + 'px' ;
	}
	Thumb.src = Img.src ;
	Thumb.style.display = 'inline' ;
}

function FirstImage( E ) { while (E && E.nodeName.toUpperCase() != 'IMG') E = E.nextSibling; return E ; }
function NextImage( Img ) { return FirstImage(Img.nextSibling) ; }

function AlbumItem(Img, Class) // Object initialiser
{ // Initialise properties
	var i = 0 ;
	this.images = new Array ;
	this.index = 0 ;
	while (Img) // build image array from  HTML
	{	if (Img.src) 
		{	this.images[i++] = Img ;
			if( Img.id == 'default' )
			{	this.index = i-1 ;
				ShowItem.apply(this) ;
		}	}
		Img = NextImage(Img) ;
	}
	this.slide = Class != 'noSlide'  && i > 1; // Swap images or not
	this.isPlan = Class == 'planta' ;

	if (!PrevItem) { PrevItem = this ; this.next = this ; } ;
	this.prev = PrevItem ;
	this.next = PrevItem.next ; // keep the chain, last item points to first	
	PrevItem.next = this ;
	PrevItem = this ;
	this.next.prev = this ; // keep the circle, let the first one point to me
	this.no = Oc++;		// Only for administration and testing ;
	
// Global methods
	this.Show = function() 
	{ // Display image on the big screen, next image in the thumb
		if (this.slide) 
			NextThumb.apply(this);
		else
			this.index=this.images.length-1;
		ShowItem.apply(this) ;
		ShowInDisplay('Display');
	}

	this.ShowNext = function(e)
	{	var A=this, rightclick ;
		if (!e) var e = window.event;

		if (e.button != 2)
		{	do {
				if( ++A.index >= A.images.length ) 
				{	A = A.next ;
					A.index=0;
				}
			} while (A.images[A.index].className == 'skip') ;
		} else {
			do {
				if( --A.index < 0 ) 
				{	A = A.prev ;
					A.index = A.images.length-1 ;
				}
			} while (A.images[A.index].className == 'skip') ;
		}
		ShowItem.apply(A) ;
	}

	this.ShowThumb = function(Ev)
	{	var Coord = MouseXY(Ev), 
			Img = this.images[(this.slide) ? (this.index+1) % this.images.length : 0];
		Thumb.style.top  = (Coord[1]+THUMB_TOP_OFFSET) +'px' ;
		Thumb.style.left = (Coord[0]+THUMB_LEFT_OFFSET)+'px' ;
		SetThumb( Img ) ;
	}

	this.HideThumb = function() { Thumb.style.display='none'; }

// local methods
	function ShowItem()
	{ // Display image on the big screen, next image in the thumb
//		if (!this || !this.images || !this.images.length) return;
		var image = this.images[this.index];
		
		Cinema.onload = function() { 
			var Div = Cinema.parentNode, BorderLine = Div.clientWidth < Div.scrollWidth ;
			Div.style.borderWidth = (BorderLine ? BORDER_WIDTH : 0) + 'px' ;
			Cinema.style.borderWidth = (BorderLine ? 0 : BORDER_WIDTH) + 'px' ;
		}
		Cinema.src = image.src ;
		Cinema.alt = image.alt ;
		Cinema.id = image.id ;
		OnScreen = this ;
		GId("ampliar").style.visibility = this.isPlan ? 'visible' : 'hidden' ;
	}

	function NextThumb() // Scales the new images and replaces the old by the new
	{	
		var Img = this.images[(++this.index+1) % this.images.length] ;
		this.index %= this.images.length ;
		SetThumb( Img ) ;
	}
}

var DisplayItems = { 
	'address': [],
	'Display': [],
	'Map' : [],
	'descriptor' : [],
	'Plantas' : [],
	'Userform' :[],
	'container' : [],
	'desclink' : []
	}
	
function ShowInDisplay(div)
{	
	GId('address').style.visibility='hidden';
	GId('Display').style.display='none';
	GId('Map').style.display='none';
	GId('descriptor').style.display='none';
	GId('Plantas').style.display='none';
	GId('Userform').style.display='none';
	GId('container').style.display='block';
	GId("desclink").style.display="block";
	GId(div).style.display='block';
}

function ZoomImage( file ) { // class initialiazer
	this.img = new Image() ;
	this.src = file ; // trace

	this.InitZoom = function(e) {
	var imag = this.img ;
	var w = imag.width;
	var h = imag.height;
	var max = h;
	if (w>h) max=w;
	this.maxwidth = w ;
	this.maxheight = h ;
	this.minwidth = w * thumbsize/max ;
	this.minheight = h * thumbsize/max ;
	if ( h > DefHeight ) { w *= DefHeight / h ; h = DefHeight; }
	if ( w > DefWidth ) { h *= DefWidth / w ; w = DefWidth ; }
	this.Hdef = h ;
	this.Wdef = w ;
	this.width = w ;
	this.height = h ;
	imag.width = w ;
	imag.height = h ;
	}
	
	this.img.onload = this.InitZoom.bind(this) ;
	this.img.src = file ;
}

function Shrink() {
	var pic = CurrentZoomImage ;
	var w = pic.width / ZOOM_FACTOR, h = pic.height / ZOOM_FACTOR ;
	
	if (w <= CurrentZoom.minwidth || h <= CurrentZoom.minheight ) {
			w = CurrentZoom.minwidth ;
			h = CurrentZoom.minheight ;
	}
	pic.width = w ;
	pic.height = h ;	
	TimEvent = setTimeout("Shrink()",interval) ;
}

function Grow() {
	var pic = CurrentZoomImage ;
	var w = pic.width * ZOOM_FACTOR, h = pic.height * ZOOM_FACTOR ;
	
	if (w >= CurrentZoom.maxwidth || h >= CurrentZoom.maxheight ) {
			w = CurrentZoom.maxwidth ;
			h = CurrentZoom.maxheight ;
	}
	pic.width = w ;
	pic.height = h ;	
	TimEvent = setTimeout("Grow()",interval) ;
}

function StopGrow() {	
	if (TimEvent) clearTimeout(TimEvent);
}

function ShowPlanta()
{
	CurrentZoom = Plantas[Cinema.id];
	CurrentZoomImage.src = CurrentZoom.img.src ;
	CurrentZoomImage.width = CurrentZoom.Wdef ;
	CurrentZoomImage.height = CurrentZoom.Hdef ;
	ShowInDisplay('Plantas') ;
	GId("container").style.display="none";
	GId("desclink").style.display="none";
}

onload = function() // Initializes the whole Slide Show
{	var Spans = GTag('span'), Span, Img, i ; // Images are all siblings of <span>
	GId('OnLoadTextBox').style.display = 'none' ;
	InitThumb();
	Cinema = FirstImage( GId('Display').firstChild ) ;
	if (!Cinema) {T("No show today!"); return ;} ;
	Cinema.oncontextmenu= function() {return false;} ;
	Cinema.onselectstart = function() {return false;} ; // ie
//	document.onselectstart = function() { return false; } ; // Safari
	
	for (i=0; i<Spans.length; i++)
	{
		Span=Spans[i];
		if ( Span.className != 'skip' && (Img = FirstImage(Span)) ) // This item contains 1 or more images
		{	var Page = new AlbumItem( Img, Span.className ) ;
			if (Span.innerHTML.toString().length > 1 )
			{	Span.className = 'Album' ;
				if (!Span.onmousedown && !Span.onclick)	Span.onclick = Page.Show.bind(Page);
				if (!Span.onmouseover)
				{	Span.onmouseover = Page.ShowThumb.bind(Page);
					Span.onmouseout = Page.HideThumb.bind(Page);
				}
			}
		}
	}
	Cinema.onmousedown = function(event) { OnScreen.ShowNext(event); return false; } ;
	CurrentZoomImage = GId('piso');
	
	for( i in Plantas)	{	Plantas[i] = new ZoomImage( Plantas[i] ) ;	}
}


