/**
 * @license 
 * jQuery Tools 1.2.5 Scrollable - New wave UI design
 * 
 * NO COPYRIGHTS OR LICENSES. DO WHAT YOU LIKE.
 * 
 * http://flowplayer.org/tools/scrollable.html
 *
 * Since: March 2008
 * Date:    Wed Sep 22 06:02:10 2010 +0000 
 */
(function($) { 

	// static constructs
	$.tools = $.tools || {version: '1.2.5'};
	
	$.tools.scrollable = {
		
		conf: {	
			keyboard: true,
			mousewheel: false,
			speed: 400,
			touch: true,
			wheelSpeed: 0
		} 
	};
					
	// get hidden element's width or height even though it's hidden
	function dim(el, key) {
		var v = parseInt(el.css(key), 10);
		if (v) { return v; }
		var s = el[0].currentStyle; 
		return s && s.width && parseInt(s.width, 10);	
	}

	function find(root, query) { 
		var el = $(query);
		return el.length < 2 ? el : root.parent().find(query);
	}
	
	var current;		
	
	// constructor
	function Scrollable(root, conf) {   
		
		// current instance
		var self = this, 
			 fire = root.add(self),
			 itemWrap = root.children(),
			 index = 0,
			 vertical = conf.vertical;
				
		if (!current) { current = self; } 
		if (itemWrap.length > 1) { itemWrap = $(conf.items, root); }
		
		// methods
		$.extend(self, {
							
			move: function(offset, time) {
				return self.seekTo(offset, time);
			},
			
			/* all seeking functions depend on this */		
			seekTo: function(i, time, fn) {	
				var curPos = itemWrap.position().top;
				var height = itemWrap.height();
				if (height == 0) {height = itemWrap.outerHeight(); } // ie7 fix
				if (height > 300) {
					height -= 300;
				} else {
					height = 0;	
				}
				if (time === undefined) { time = conf.wheelSpeed; } 
				//alert('curPos='+curPos+' height='+height+' time='+time+' wrap:'+itemWrap.attr('id')); 
				var nextPos = curPos - i*time;
				if (nextPos >= 0) { nextPos = 0 }
				if (nextPos <= -height ) { nextPos = -height; }
				var props = {top: nextPos};  
				itemWrap.animate(props, conf.speed, conf.easing);	 
				return self; 
			}					
			
		});
			
		// mousewheel support
		if (conf.mousewheel && $.fn.mousewheel) {
			root.mousewheel(function(e, delta)  {
				if (conf.mousewheel) {
					self.move(delta < 0 ? 1 : -1, conf.wheelSpeed || 50);
					return false;
				}
			});			
		}
		
		// touch event
		if (conf.touch) {
			var touch = {};
			
			itemWrap[0].ontouchstart = function(e) {
				var t = e.touches[0];
				touch.x = t.clientX;
				touch.y = t.clientY;
			};
			
			itemWrap[0].ontouchmove = function(e) {
				
				// only deal with one finger
				if (e.touches.length == 1 && !itemWrap.is(":animated")) {			
					var t = e.touches[0],
						 deltaX = touch.x - t.clientX,
						 deltaY = touch.y - t.clientY;
	
					self[vertical && deltaY > 0 || !vertical && deltaX > 0 ? 'next' : 'prev']();				
					e.preventDefault();
				}
			};
		}
		
		if (conf.keyboard)  {
			
			$(document).bind("keydown.scrollable", function(evt) {

				// skip certain conditions
				if (!conf.keyboard || evt.altKey || evt.ctrlKey || $(evt.target).is(":input")) { return; }
				
				// does this instance have focus?
				if (conf.keyboard != 'static' && current != self) { return; }
					
				var key = evt.keyCode;
			
				if (vertical && (key == 38 || key == 40)) {
					self.move(key == 38 ? -1 : 1);
					return evt.preventDefault();
				}
				
				if (!vertical && (key == 37 || key == 39)) {					
					self.move(key == 37 ? -1 : 1);
					return evt.preventDefault();
				}	  
				
			});  
		}
		
	} 

		
	// jQuery plugin implementation
	$.fn.scrollable = function(conf) { 
			
		// already constructed --> return API
		var el = this.data("scrollable");
		if (el) { return el; }		 

		conf = $.extend({}, $.tools.scrollable.conf, conf); 
		
		this.each(function() {			
			el = new Scrollable($(this), conf);
			$(this).data("scrollable", el);	
		});
		
		return conf.api ? el: this; 
		
	};
	
	var h1El = $('#middle').find("h1");	
	var pageName = window.location.href;
	if (h1El.length > 0) {
		pageName = h1El.first().html();
		var h3El = $("#middle").children().find("h3");
		if (h3El.lenght > 0) {
			pageName += h3El.first().html();
		}
	}
	var sourceLocation = readCookie('sourceLocation');
	var targetLocation = pageName;
	
	if (sourceLocation == null || sourceLocation == targetLocation) {
		loadP(pageName);
	}
	
	$(window).unload(function() {
		unloadP(pageName);
	});
	
})(jQuery);


function getScrollXY() {
    var x = 0, y = 0;
    if( typeof( window.pageYOffset ) == 'number' ) {
        // Netscape
        x = window.pageXOffset;
        y = window.pageYOffset;
    } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
        // DOM
        x = document.body.scrollLeft;
        y = document.body.scrollTop;
    } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
        // IE6 standards compliant mode
        x = document.documentElement.scrollLeft;
        y = document.documentElement.scrollTop;
    }
    return [x, y];
}
function GetColumnY(id) {
    var y = document.getElementById(id).style.top;
    return y;
}
       
function setScrollXY(x, y) {
    window.scrollTo(x, y);
}
function setColumnY(id,y) {
	if (y!=null){
	    document.getElementById(id).style.top = y;
	}
}
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 loadP(pageref){
	x=readCookie(pageref+'x');
	y=readCookie(pageref+'y');
	setScrollXY(x,y);
	middleY=readCookie(pageref+'middleY');
	rightY=readCookie(pageref+'rightY');
	setColumnY("middle",middleY);
	//setColumnY("right", rightY);
}
function unloadP(pageref){
	s=getScrollXY()
	createCookie(pageref+'x',s[0],0.1);
	createCookie(pageref+'y',s[1],0.1);
	// middle column
	createCookie(pageref+'middleY',GetColumnY("middle"),0.1);
	// right column
	//createCookie(pageref+'rightY',GetColumnY("right"),0.1);
	// url
	createCookie('sourceLocation',pageref,0.1);
}

