if (!window.jn) {
	window.jn = {}
}

jn.prevx = -1;
jn.active = "";

jn.handleThumbMouseMove = function (e) {
	var thumbs = document.getElementById("thumbnails");
	if (jn.prevx == -1) {
		jn.prevx = e.clientX;
	}
	
	// Add this padding such that we do not need to scroll to the absolute edges to be able to
	// see the entire edge thumbnails.
	var pad = 50;
	
	// Current x-coord of mouse pointer within the thumbnail container
	var opos = findPos(this);
	var pos = e.clientX - opos[0];

	// Amount of pixels the mouse moved in x direction with respect to previous state
	var diff = e.clientX - jn.prevx;
	


	if (diff > 0) {
		// Current amount of pixels we can scroll to the right
		var scrollright = thumbs.offsetWidth - (this.scrollLeft + this.offsetWidth) + pad;
			
		this.scrollLeft = Math.min(thumbs.offsetWidth - this.offsetWidth, this.scrollLeft + diff * scrollright/(this.offsetWidth - pos));
	}
	else if (diff < 0) {
		// Current amount of pixels we can scroll to the left
		var scrollleft = this.scrollLeft + pad;
		
		this.scrollLeft = Math.max(0, this.scrollLeft + diff * scrollleft/pos);
	}
	// else nothing to do
	
	
	jn.prevx = e.clientX;
}


addEvent(window, "load", function () {
	
	var thumb = document.getElementById("thumbwrapper");
	
	if (thumb) {
		addEvent(thumb, "mousemove", jn.hitch(thumb, jn.handleThumbMouseMove));
		addEvent(thumb, "mouseout", jn.hitch(thumb, function () {
			jn.prevx = -1;
		}));
		
		var offset = readCookie(jn.active + "-scroll");
		
		if (offset != null) {
			thumb.scrollLeft = offset;
		}
	}
	
});

addEvent(window, "unload", function () {
	var thumb = document.getElementById("thumbwrapper");
	
	if (thumb) {
		createCookie(jn.active + "-scroll", thumb.scrollLeft);
	}
});


/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: Caleb Duke | http://www.askapache.com/ */

//------------------------------------
// heavily based on the Quirksmode addEvent contest winner, John Resig
// addEvent
function addEvent(obj,type,fn){
    if(obj.addEventListener) obj.addEventListener(type,fn,false);
    else if(obj.attachEvent){
        obj["e"+type+fn]=fn;
        obj[type+fn]=function(){obj["e"+type+fn](window.event);}
        obj.attachEvent("on"+type,obj[type+fn]);
    }
}

//------------------------------------
// removeEvent
function removeEvent(obj,type,fn){
  if(obj.removeEventListener) obj.removeEventListener(type,fn,false);
  else if(obj.detachEvent){
    obj.detachEvent("on"+type,obj[type+fn]);
    obj[type+fn]=null;
    obj["e"+type+fn]=null;
  }
}

// Courtesy of http://stackoverflow.com/questions/2035236/does-jquery-have-an-equivalent-of-dojo-hitch

// The .bind method from Prototype.js 
if (!Function.prototype.bind) { // check if native implementation available
  Function.prototype.bind = function(){ 
    var fn = this, args = Array.prototype.slice.call(arguments),
        object = args.shift(); 
    return function(){ 
      return fn.apply(object, 
        args.concat(Array.prototype.slice.call(arguments))); 
    }; 
  };
}

jn.hitch = function(scope, fn) {
  if (typeof fn == "string") fn = scope[fn];
  if (fn && fn.bind) return fn.bind(scope);
};

// Courtesy of http://www.quirksmode.org/js/cookies.html


function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function findPos(obj) {
	var curleft = curtop = 0;

	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	
	return [curleft,curtop];
}

