// EVO Framework - Image navigation 1.0
// Author: EVO Studios: www.evostudios.it
// Licence (IT): 
// Lo script è liberamente utilizzabile, modificabile, distribuibile, insomma... potete farci tutto quello che volete.
// --------------------------------------

en_containerID = new Array();
en_panelClass = new Array();
en_type = new Array();
en_current = -1;
en_posDestX = new Array();
en_posDestY = new Array();
en_timer = new Array();
en_timerTemp = new Array();
en_posTop = new Array();
en_posLeft = new Array();
en_friction = new Array();
en_timing = new Array();
en_isOver = new Array();
en_margins = new Array();
en_activeMargins = new Array();
en_panelWidth = new Array();
en_startPos = new Array();

var evonavigator = {
	
	// Setup the slider
	init:function(config) {
		
		// Setup the variables array
		en_current++;
		en_containerID.push(config["containerID"]);
		en_panelClass.push(config["panelClass"]);
		en_type.push(config["type"]);
		en_friction.push(config["friction"]);
		en_timing.push(config["timing"]);
		en_margins.push(config["margins"]);
		en_activeMargins.push(config["ctiveMargins"]);
		en_isOver.push(false);
		
		evonavigator.init2(en_current);
		
		
	},
	
	init2:function(id) {
		
		if (en_type[id] == "vertical") {
			
			en_posTop[id] = 0 + en_margins[id];
						
			$(en_containerID[id]).mousemove(function(e) {			
				
				evonavigator.checkMouseMove(id,e);
				
			});
			
		}
		
		
		
		if (en_type[id] == "horizontal") {
			
			en_posLeft[id] = 0 + en_margins[id];
			
			// Set belt width
			en_panelWidth[id] = $(en_containerID[id] + " " + en_panelClass[id]).outerWidth(true);
						
			$(en_containerID[id]).mousemove(function(e) {			
				
				evonavigator.checkMouseMove(id,e);
				
			});
			
		}
		
		/*
		$(en_containerID[id]).mouseenter(function() {
			//debug("ENTER");
			if (!en_isOver[id]) { en_isOver[id] = true; evonavigator.startTimer(id); }
		});
		
		$(en_containerID[id]).mouseleave(function() {
			//evonavigator.stopTimer(id);
		});
		*/
	},
	
	tweenTo:function(id,destination) {
		
		element = en_containerID[id];

		if (en_type[id] == "vertical") {
			var difference = (en_posTop[id] - $(element).children('div:first-child').position().top) * en_friction[id];
			$(element).children('div:first-child').css("top",en_posTop[id] - difference);			
		}
		
		if (en_type[id] == "horizontal") {
			var difference2 = (en_posLeft[id] - $(element).children('.belt').position().left) * en_friction[id];
			$(element).children('.belt').css("left",en_posLeft[id] - difference2);
			//$(element).children('div:first-child').css("left",en_posLeft[id]);			
		}
		
		
		
				
	},
	
	checkMouseMove:function(id,e) {
		
		if (en_type[id] == "vertical") {

			var posY = e.pageY - $(en_containerID[id]).offset().top;
			var maxEscursion = $(en_containerID[id]).children('div:first-child').height() - $(en_containerID[id]).height();
			var perc = posY / $(en_containerID[id]).height() * 100;
			en_posTop[id] = Math.floor(0 - perc / 100 * maxEscursion);
		}
		
		if (en_type[id] == "horizontal") {
			
			var posX = e.pageX - $(en_containerID[id]).offset().left;
			
			var maxEscursion = 0;
			$('.panel').each(function() {
			
			maxEscursion = maxEscursion + $(this).outerWidth(true);	
				
			});
			
			maxEscursion = maxEscursion - $(en_containerID[id]).width() + 200;
			
			//debug2(maxEscursion);
			//alert($('.panel').length * $('.panel').outerWidth(true));
			//var maxEscursion = ($(en_containerID[id]).children('div:first-child').outerWidth(true)) - $(en_containerID[id]).width();
			
			var perc = (posX) / ($(en_containerID[id]).outerWidth(false)) * 100;

			//var margins = ($(en_containerID[id]).children('div:first-child').outerWidth(true) - $(en_containerID[id]).children('div:first-child').width())/2;
			en_posLeft[id] = (0 - perc / 100 * maxEscursion);
			//debug2(perc);
			//$(en_containerID[id]).children('div:first-child').css("left",en_posLeft[id]);
			
		}
		
		
	},
	
	startTimer:function(id) {
		//debug("Timer started for ID " + id);
		//debug("OK" + id);
		
		en_timerTemp[id] = setInterval("evonavigator.tweenTo('" + id + "')",en_timing[id]);				
		
	},
	
	stopTimer:function(id) {
		clearInterval(en_timerTemp[id]);	
	}
	
}

