//
// anim.scroll.js
// Deep (c) 2009
// Jocke Ling
//

// preload scroll images
var scrollUpActive		= new Image();
var scrollUpInactive	= new Image();
var scrollDownActive	= new Image();
var scrollDownInactive	= new Image();
var scrollLeftActive	= new Image();
var scrollLeftInactive	= new Image();
var scrollRightActive	= new Image();
var scrollRightInactive	= new Image();

scrollUpActive.src		= '/images/scroll_up_active.gif';
scrollUpInactive.src	= '/images/scroll_up_inactive.gif';
scrollDownActive.src	= '/images/scroll_down_active.gif';
scrollDownInactive.src	= '/images/scroll_down_inactive.gif';
scrollLeftActive.src	= '/images/scroll_left_active.gif';
scrollLeftInactive.src	= '/images/scroll_left_inactive.gif';
scrollRightActive.src	= '/images/scroll_right_active.gif';
scrollRightInactive.src	= '/images/scroll_right_inactive.gif';

// global, put all scroller obj here to avoid AJAX render issues
var WEscrollInit = new Array();

// WebsEdge anim scroll object
function WebsEdge() {

	this.num = new Array();
	this.eleId = false;
	this.disp_items = 0;
	this.scroll_begin = 0;
	this.scroll_end = 0;
	this.items = new Array();
	this.position = 0;
	this.speed = 1;
	this.mode = 0;

	// init
	// eleId = element to be initilized
	this.init = function(eleId) {
		var tagI = 0, xy;
		this.eleId = eleId;

		document.getElementById(this.eleId).scrollTop = 0;
		document.getElementById(this.eleId).scrollLeft = 0;

		// get elements position
		xy = YAHOO.util.Dom.getXY(this.eleId);
		var defaultX = xy[0];
		var defaultY = xy[1];

		// get all a tags within element
		div = typeof this.eleId === "string" ? document.getElementById(this.eleId) : this.eleId;
		var elms = div.getElementsByTagName('a');

		this.num = elms.length;
		for(var i = 0; i < this.num; ++i) {
			var elm = elms[i];
			if (elm.id != "") {
				xy = YAHOO.util.Dom.getXY(elm.id);

				// create object
	        	this.items[tagI] = new Object();

	        	// add id, to be able to search items
	        	this.items[tagI].id = elm.id;

	        	// add XY position - element X/Y position
	        	this.items[tagI].Y = xy[1]-defaultY;
	        	this.items[tagI++].X = xy[0]-defaultX;
			}
		}
	}

	// config
	// n = num items in list
	// items = items showed in one frame
	this.display = function(items) {
		this.disp_items = items;

		// if num is more then display items, active down arrow
		if (this.num > this.disp_items)
			if ( this.mode == 0 ) {
				document.getElementById('scrolldown_'+this.eleId).src = scrollDownActive.src;
				document.getElementById('scrolldownAtag_'+this.eleId).className = 'scroll';
			} else if ( this.mode == 1 )
				document.getElementById('scrollright_'+this.eleId).src = scrollRightActive.src;

		this.upd_info_bar();
	}

	// scroll to item in items array
	// itemId = tag id in HTML
	this.show_item = function(itemId) {
		var index = false;
		for (var i = 0; i < this.num ; ++i) {
			if (this.items[i].id == itemId) {
				index = i;
				break;
			}
		}

		if (index !== false) {
			// get first item on page
			this.position = Math.floor(index/this.disp_items) * this.disp_items;

			// if down to last page, check if numbers add up to paging setup
			if ((this.position + this.disp_items) > (this.num - 1))
				this.position = this.num - this.disp_items;

			this.scroll_end = this.items[this.position].Y;

			// set attributes, begin and end
		    var attributes = {
		        scroll: { to: [0, this.scroll_end] }
		    };
		    // exec yui anim tool
			var anim = new YAHOO.util.Scroll(this.eleId, attributes, 0);
			anim.animate();

			// save ground value in new frame
			this.scroll_begin = this.scroll_end;

			this.upd_info_bar();

			// image controll
			document.getElementById('scrollup_'+this.eleId).src = (this.position == 0) ? scrollUpInactive.src : scrollUpActive.src;
			document.getElementById('scrolldown_'+this.eleId).src = (this.position >= (this.num - this.disp_items - 1)) ? scrollDownInactive.src : scrollDownActive.src;
		}
	}

	// set speed of scroller
	// ms = miliseconds per pixel scroll, animation
	this.set_speed = function(ms) {
		this.speed = ms;
	}

	// set mode of scroller
	// 0 = vertical, 1 = horizontal
	this.set_mode = function(m) {
		this.mode = m;
	}

	// update info bar
	this.upd_info_bar = function() {
		// only display info bar if items displayed
		if (this.num > 0) {
			var max = (this.disp_items > this.num) ? this.num : (this.position+this.disp_items);
			document.getElementById(this.eleId + "_info").innerHTML = (this.position+1) + "-" + max + " of " + this.num;
		}
	}

	// make anim
	// direction = 'up', 'down', 'left' or 'right'
	this.anim_scroll = function(direction) {
		// if indexing of items not finished or display() not called, quit scrolling!
		if (this.num == 0 || this.disp_items == 0)
			return false;

		switch (direction) {
			case 'up':
				if (document.getElementById('scrollup_'+this.eleId).src == scrollUpInactive.src)
					break;

				// if scroll not at top, make end dist one frame page up (height * displaying items)
				this.position -= this.disp_items;

				if (this.position < 0)
					this.position = 0;

				this.scroll_end = this.items[this.position].Y;

				// image control
				document.getElementById('scrollup_'+this.eleId).src = (this.position == 0) ? scrollUpInactive.src : scrollUpActive.src;
				document.getElementById('scrollupAtag_'+this.eleId).className = (this.position == 0) ? 'scrollDisabled' : 'scroll';

				 // when we scroll up, down scroll will always be active, change if inactive
				 if (document.getElementById('scrolldown_'+this.eleId).src == scrollDownInactive.src && this.num > this.disp_items) {
					document.getElementById('scrolldown_'+this.eleId).src = scrollDownActive.src;
					document.getElementById('scrolldownAtag_'+this.eleId).className = 'scroll';
				 }

				break;
			case 'down':
				if (document.getElementById('scrolldown_'+this.eleId).src == scrollDownInactive.src)
					break;

				// move position down
				this.position += this.disp_items;

				// if lower then num items set to last item
				if (this.position > (this.num - this.disp_items))
					this.position = (this.num - this.disp_items);

				// get first item's position
				this.scroll_end = this.items[this.position].Y;

				// image controll
				
				// -1 removed from statement. 
				// document.getElementById('scrolldown_'+this.eleId).src = (this.position >= (this.num - this.disp_items)) ? scrollDownInactive.src : scrollDownActive.src;
				// document.getElementById('scrolldownAtag_'+this.eleId).className = (this.position >= (this.num - this.disp_items)) ? 'scrollDisabled' : 'scroll';
				document.getElementById('scrolldown_'+this.eleId).src = (this.position >= (this.num - this.disp_items)) ? scrollDownInactive.src : scrollDownActive.src;
				document.getElementById('scrolldownAtag_'+this.eleId).className = (this.position >= (this.num - this.disp_items)) ? 'scrollDisabled' : 'scroll';

				 // when we scroll down, up scroll will always be active, change if inactive
				 if (document.getElementById('scrollup_'+this.eleId).src == scrollUpInactive.src && this.num > this.disp_items) {
					document.getElementById('scrollup_'+this.eleId).src = scrollUpActive.src;
					document.getElementById('scrollupAtag_'+this.eleId).className = 'scroll'
				 }

				break;
			case 'left':
				if (document.getElementById('scrollleft_'+this.eleId).src == scrollLeftInactive.src)
					break;
				// if scroll not at left, make end dist one frame page left (height * displaying items)
				this.position -= this.disp_items;

				if (this.position < 0)
					this.position = 0;

				this.scroll_end = this.items[this.position].X;

				// image control
				 document.getElementById('scrollleft_'+this.eleId).src = (this.position == 0) ? scrollLeftInactive.src : scrollLeftActive.src;

				 // when we scroll right, left scroll will always be active, change if inactive
				 if (document.getElementById('scrollright_'+this.eleId).src == scrollRightInactive.src && this.num > this.disp_items)
					document.getElementById('scrollright_'+this.eleId).src = scrollRightActive.src;

				break;
			case 'right':
				if (document.getElementById('scrollright_'+this.eleId).src == scrollRightInactive.src)
					break;
				// move position down
				this.position += this.disp_items;

				// if lower then num items set to last item
				if (this.position > (this.num - this.disp_items))
					this.position = (this.num - this.disp_items);

				// get first item's position
				this.scroll_end = this.items[this.position].X;

				// image controll
				 document.getElementById('scrollright_'+this.eleId).src = (this.position >= (this.num - 1 - this.disp_items)) ? scrollRightInactive.src : scrollRightActive.src;

				// when we scroll right, left scroll will always be active, change if inactive
				 if (document.getElementById('scrollleft_'+this.eleId).src == scrollLeftInactive.src && this.num > this.disp_items)
					document.getElementById('scrollleft_'+this.eleId).src = scrollLeftActive.src;
				break;
		}

		this.upd_info_bar();
		// set attributes, begin and end
	    var attributes = {
	        scroll: { to: [this.scroll_begin, this.scroll_end] }
	    };

		// exec yui anim tool
		var anim = new YAHOO.util.Scroll(this.eleId, attributes, this.speed);
		anim.animate();

		// save ground value in new frame
		this.scroll_begin = this.scroll_end;
	}

}