/*
 * 	PhotoNavigation
 * 	A Javascript Module by Gaya Kessler
 * 	Version 1.1
 * 	Date: 14-01-09
 * 
 */
 
/**
 * Adicionada classe Container para permitir o uso
 * mais de container de fotos por pagina.
 * 
 * @author rogeriolino
 * @date 2009-06-23
 */
var Container = function(id, width, width2) {
	
	this.object = document.getElementById(id);
	this.width = width;
	this.photo = "";
	this.photoWidth = 0;
	
	var divs = this.object.getElementsByTagName("DIV");
		
	for (var i = 0; i < divs.length; i++) {
		if (divs[i].className.match('fixed')) {
			this.photo = divs[i];
			this.photoWidth = width2;
			this.photo.style.width = width2 + "px";
		}
	}
	
}
 
 
var PhotoNav = {
	
	statusArr: new Array(),
	debugging: false,
	smoothing: false,
	smoothingLevel: 0.1,
	tempX: 0,
	tempY: 0,
	winH: 0,
	winW: 0,
	IE: "",
	containers: new Array(),
	currentEffect: "",
	
	init: function(id, width, width2, smoothing, smoothingLevel, debugging) {
		
		//catch undefined vars
		if (typeof(smoothing) != 'undefined') {
			PhotoNav.smoothing = smoothing;
		} else {
			PhotoNav.smoothing = false;
		}
		
		if (typeof(smoothingLevel) != 'undefined') {
			PhotoNav.smoothingLevel = smoothingLevel;
		} else {
			PhotoNav.smoothingLevel = 0.1;
		}
		
		if (typeof(debugging) != 'undefined') {
			PhotoNav.debugging = debugging;
		} else {
			PhotoNav.debugging = false;
		}
		
		//make room in status array
		PhotoNav.statusArr[0] = "";
		PhotoNav.statusArr[1] = "";
		
		//let PhotoNav determine mouse position
		PhotoNav.IE = document.all?true:false
		if (!PhotoNav.IE) document.captureEvents(Event.MOUSEMOVE)
		document.onmousemove = PhotoNav.getMouseXY;
		
		//set some object vars
		var container = new Container(id, width, width2);
		PhotoNav.containers[PhotoNav.containers.length] = container;
				
	},
	
	addStatus: function(str) {
		if (PhotoNav.debugging == true) {
			var stats = document.getElementById("status");
			
			PhotoNav.statusArr.unshift(str);
			PhotoNav.statusArr.splice(3, 10);
			
			stats.innerHTML = "";
			
			for (var i = 2; i > 0; i = i - 1) {
				stats.innerHTML = PhotoNav.statusArr[i] + "<br />" + stats.innerHTML;	
			}
			
			if (PhotoNav.statusArr.length > 5) {
				stats.innerHTML = 'oi';
			}
		}
	},
	
	getMouseXY: function (e) {
		//determine mouse position
		if (PhotoNav.IE) { // grab the x-y pos.s if browser is IE
			PhotoNav.tempX = event.clientX + document.body.scrollLeft
			PhotoNav.tempY = event.clientY + document.body.scrollTop
		} else {  // grab the x-y pos.s if browser is NS
			PhotoNav.tempX = e.pageX
			PhotoNav.tempY = e.pageY
		}  
		// catch possible negative values in NS4
		if (PhotoNav.tempX < 0) {
			PhotoNav.tempX = 0;
		}
		
		if (PhotoNav.tempY < 0) {
			PhotoNav.tempY = 0;
		}
		
		if (parseInt(navigator.appVersion)>3) {
			if (navigator.appName=="Netscape") {
				PhotoNav.winW = window.innerWidth;
				PhotoNav.winH = window.innerHeight;
			}
			if (navigator.appName.indexOf("Microsoft")!=-1) {
				PhotoNav.winW = document.body.offsetWidth;
				PhotoNav.winH = document.body.offsetHeight;
			}
		}
		
		for (var i = 0; i < PhotoNav.containers.length; i++) {
			
			var container = PhotoNav.containers[i];
					
			var containerY = PhotoNav.findPosY(container.object);
			var containerH = container.object.offsetHeight;
			
			//first check if cursor is height enough
			if (PhotoNav.tempY > containerY && PhotoNav.tempY < (containerY + containerH)) {			
				//determine % horizontal
				var perc = (100 / (container.width / (PhotoNav.tempX - PhotoNav.findPosX(container.object)))) ;
				
				if (perc < 0) {
					perc = 0;
					PhotoNav.addStatus("Not in element.");
				} else if (perc > 100) {
					perc = 100;
					PhotoNav.addStatus("Not in element.");
				} else {
					PhotoNav.addStatus("Mouse X: " + PhotoNav.tempX + " / " + PhotoNav.winW + " = " + perc + "%");
					
					PhotoNav.posPicture(container, perc);
				}
			} else {
				PhotoNav.addStatus("Not in element.");
			}
			
		}
		
		return true;
	},
	
	posPicture: function(container, x) {
			var full = container.photoWidth;
			full = full - container.width;
			var curX = full * (x / 100);
			
			if (curX < 0) {
				curX = 0;
			}
			
			PhotoNav.addStatus("Mouse X container: " + curX);
			
			if (PhotoNav.smoothing == true) {
				if (typeof PhotoNav.currentEffect.cancel == 'function') {
					PhotoNav.currentEffect.cancel;
				}
				PhotoNav.currentEffect = new Effect.Morph(container.photo, {
				  style: 'margin-left:-' + curX + 'px;',
				  duration: PhotoNav.smoothingLevel
				});	
			} else {
				container.photo.style.marginLeft = "-" + curX + "px";
			}

	},
	
	findPosX: function (obj) {
		var curleft = 0;
		if (obj.offsetParent) {
			while(1) {
				curleft += obj.offsetLeft;
				if(!obj.offsetParent)
					break;
				obj = obj.offsetParent;
			}
		} else if(obj.x) {
			curleft += obj.x;
		}
		return curleft;
	},
	
	findPosY: function(obj) {
		var curtop = 0;
		if (obj.offsetParent) {
			while(1) {
				curtop += obj.offsetTop;
				if(!obj.offsetParent)
					break;
				obj = obj.offsetParent;
			}
		} else if(obj.y) {
			curtop += obj.y;
		}
		return curtop;
	}
	
};

	
