/**
 *	Navigation Script for Photogalleries
 *	Author:	Georg Tavonius <g.tavonius@gmail.com>
 */

if(Jaz == undefined)
	var Jaz = {};

Function.prototype.bind = function(obj) {
	var method = this,
		temp = function() {
		return method.apply(obj, arguments);
	};
	return temp;
} 

Jaz.Photo = {
	navUsed: false,
	
	/**
	 *	Allows you to click in the Picture to get to previous or next pictures
	 *	@param	photoid		id-parameter of img-tag
	 *	@param	imgpad		padding of this img-tag
	 *	@param	linkPrev	Link to Previous picture it there is one
	 *	@param	linkNext	Link to Next picture it there is one
	 *	@param	fullIfNo	If its the oldest or newest picture enlarge clicking area this only link?
	 */
	nav: function(photoid, imgpad, linkPrev, linkNext, fullIfNo) {
		fullIfNo = fullIfNo ? fullIfNo : false;
		var isPrev = ( linkPrev && linkPrev != "" );
		var isNext = ( linkNext && linkNext != "" );
		var img = document.getElementById(photoid);
		var top = img.offsetTop;
		var left = img.offsetLeft;
		var height = img.height;
		var width = img.width;
		
		var photoleftE = document.getElementById('photoleft');
		var photorightE = document.getElementById('photoright');
		if(isPrev) {
			document.getElementById('photoleft').firstChild.firstChild.width = width/2;
			document.getElementById('photoleft').firstChild.firstChild.height = height;
			document.getElementById('photoleft').firstChild.firstChild.firstChild.style.border = 'none';
			document.getElementById('photoleft').firstChild.href = linkPrev;
			document.getElementById('photoleft').firstChild.style.width = width/2+'px';
			document.getElementById('photoleft').firstChild.style.height = height+'px';
			document.getElementById('photoleft').firstChild.style.display = 'block';
			document.getElementById('photoleft').style.position = 'absolute';
			document.getElementById('photoleft').style.top = (top+imgpad)+'px';
			document.getElementById('photoleft').style.left = (left+imgpad)+'px';
			if(!isNext)
				document.getElementById('photoright').style.display = 'none';
			if(!isNext && fullIfNo)
				document.getElementById('photoleft').firstChild.firstChild.width = width;
		}
		if(isNext) {
			document.getElementById('photoright').firstChild.firstChild.width = width/2;
			document.getElementById('photoright').firstChild.firstChild.height = height;
			document.getElementById('photoright').firstChild.firstChild.firstChild.style.border = 'none';
			document.getElementById('photoright').firstChild.href = linkNext;
			document.getElementById('photoright').firstChild.style.width = width/2+'px';
			document.getElementById('photoright').firstChild.style.height = height+'px';
			document.getElementById('photoright').firstChild.style.display = 'block';
			document.getElementById('photoright').style.position = 'absolute';
			document.getElementById('photoright').style.top = (top+imgpad)+'px';
			document.getElementById('photoright').style.left = (width/2+left+imgpad)+'px';
			if(!isPrev)
				document.getElementById('photoleft').style.display = 'none';
			if(!isPrev && fullIfNo) {
				document.getElementById('photoright').firstChild.firstChild.width = width;
				document.getElementById('photoright').style.left = (left+imgpad)+'px';
			}
		}
		this.navUsed = true;
	},
	
	/**
	 *
	 *	@param	photoid			id of an image-tag
	 *	@param	toolTipElement	if of HTML-Element that should be displayed as tooltip
	 *	@param	imgContent		Content that should be displayed in this Title box. (Can be HTML-Markup of course)
	 *	@param	onPos			if set, it should be an array (x;y) of koordinates where the Title should appear, else it will follow the cursor
	 */
	showTooltipTitle: function(photoid, toolTipElement, onPos) {
		var img = document.getElementById(photoid);
		var obj = {
			img: 	document.getElementById(photoid)
		,	element:document.getElementById(toolTipElement)
		,	onPos:	onPos
		,	navUsed:this.navUsed
		};
		if(obj.element != null)
			document.onmousemove = this._showTitleEventL.bind(obj);
	},
	
	_showTitleEventL: function(event) {
		if(!event)
			event = window.event; // IE
		var top = this.img.offsetTop, left = this.img.offsetLeft,
			height = this.img.height, width = this.img.width;
		
		var mouseX = event.pageX ? event.pageX : event.clientX
		,	mouseY = event.pageY ? event.pageY : event.clientY;
		if(this.onPos == null) {
			onPos = [mouseX,mouseY]; //mockup TODO
		}
		else
			onPos = [
				this.onPos[0] + left
			,	this.onPos[1] + top
			];
		
		var tooltipElem = this.element;
		tooltipElem.style.position = 'absolute';
		tooltipElem.style.left = onPos[0]+'px';
		tooltipElem.style.top = onPos[1]+'px';
		tooltipElem.style.display = (mouseX > left && mouseX < left+width && mouseY > top && mouseY < top+height) ? 'block' : 'none';
	}
}

window.onload = function(e) {
	//Jaz.Photo.nav('photo', 0, document.getElementById('image-nav-previous').firstChild, document.getElementById('image-nav-next').firstChild, true);
	//Jaz.Photo.showTooltipTitle('photo', 'photoinfos');
}