/** Portfolio navigation **/

function PortfolioNav(columnDiv, elUp, elDown)
{
	this.arItems = new Array();
	this.divNav = document.getElementById( columnDiv );
	
	if( !this.divNav ) {
		return;
	}
	
	var items = this.divNav.getElementsByTagName( "DIV" );
	var maxHeight = 0;
	
	for( var j = 0; j < items.length; j ++ ) {
		var item = items[ j ];
		
		if( item.className !== undefined ) {
			if( (item.className != "portfolioItem") && (item.className != "portfolioItemSelected") )
				continue;
		} else if( item.getAttribute ) {
			if( (item.getAttribute( "class" ) != "portfolioItem") && (item.getAttribute( "class" ) != "portfolioItemSelected") )
				continue;
		}
		
		this.arItems[ this.arItems.length ] = item;
		maxHeight = (item.offsetHeight > maxHeight) ? item.offsetHeight : maxHeight;
	}
	
	this.itemHeight = maxHeight;
	this.totalHeight = this.itemHeight * this.arItems.length;
	
	this.isScrolling = false;
	this.scorllingDirection = "";
	this.scrollingInt = null;
	this.scrollRate = 0;
	
	this.areaUp = document.getElementById( elUp );
	
	if( this.areaUp !== undefined ) {
		setEventHandler( this.areaUp, "mouseover", function() { pnav.onScrollStart( "up" ); } );
		setEventHandler( this.areaUp, "mouseout", function() { pnav.onScrollEnd( "up" ); } );
	}
	
	this.areaDown = document.getElementById( elDown );
	
	if( this.areaDown !== undefined ) {
		setEventHandler( this.areaDown, "mouseover", function() { pnav.onScrollStart( "down" ); } );
		setEventHandler( this.areaDown, "mouseout", function() { pnav.onScrollEnd( "down" ); } );
	}
	
	this.loader = new Array();
	this.loader[0] = new Image();
	this.loader[0].src = "/layout/up_o.gif";
	this.loader[1] = new Image();
	this.loader[1].src = "/layout/down_o.gif";
}

PortfolioNav.prototype.scrollTick = function()
{
	if( !this.isScrolling ) return;
	
	if( this.scrollingDirection == "up" ) {
		if( this.divNav.scrollTop > 0 ) {
			this.divNav.scrollTop -= this.scrollRate;
		} else {
			this.areaUp.style.background = "";
		}
	} else if( this.scrollingDirection == "down" ) {
		var be = this.divNav.scrollTop;
		this.divNav.scrollTop += this.scrollRate;
		if( be == this.divNav.scrollTop ) {
			this.areaDown.style.background = "";
		}
	}
	
	if( this.scrollRate < 24 )
		this.scrollRate ++;
}

PortfolioNav.prototype.onScrollStart = function(direction)
{
	if( this.scrollingInt != null ) return;
	this.isScrolling = true;
	this.scrollingDirection = direction;
	this.scrollRate = 1;
	this.scrollingInt = setInterval( function() { pnav.scrollTick(); }, 50 );
	
	if( direction == "up" ) {
		this.areaUp.style.background = "url(/layout/up_o.gif) no-repeat top center";
	} else if( direction == "down" ) {
		this.areaDown.style.background = "url(/layout/down_o.gif) no-repeat bottom center";
	}
}

PortfolioNav.prototype.onScrollEnd = function(direction)
{
	if( this.scrollingInt == null ) return;
	clearInterval( this.scrollingInt );
	this.scrollingInt = null;
	this.isScrolling = false;
	this.scrollingDirection = "";

	if( direction == "up" ) {
		this.areaUp.style.background = "";
	} else if( direction == "down" ) {
		this.areaDown.style.background = "";
	}
}

PortfolioNav.prototype.choose = function(id)
{
	var fromItem = 0;
	var tillItem = 0;
	
	for( var j = 0; j < this.arItems.length; j ++ ) {
		var item = this.arItems[ j ];
		if( item.getAttribute === undefined ) continue;
		if( item.getAttribute( "oid" ) == id ) {
			this.arItems[ j - 1 ].scrollIntoView();
			window.scrollTo( 0, 0 );
			return;
		}
	}
	
	setTimeout( function() { pnav.choose( id ); }, 400 ); // fb
}