var vAlignScrollSidebars = {
	timer : null,

	init : function()
	{
		vAlignScrollSidebars.update();
		this.setUpdateInterval(5000);
	},
	
	setUpdateInterval : function(interval)
	{
		if ( this.timer )
			window.clearInterval(this.timer);
		this.timer = window.setInterval(vAlignScrollSidebars.update, isNaN(interval) ? 1000 : interval );
	},
	
	update : function()
	{
		vAlignScrollSidebars.align('sliderTallLeft');
		vAlignScrollSidebars.align('sliderTallRight');
	},
	
	align : function(className)
	{
		try
		{
			var els = document.getElementsByClassName(className);
			
			for ( var i=0; i < els.length; i++ )
			{
				var pNode = els[i].parentNode;
				while ( (pNode.tagName == 'undefined' || pNode.tagName == 'A') && pNode != document.body )
					pNode = pNode.parentNode;
					
				els[i].style.position	= 'relative';
				els[i].style.top		= ((pNode.offsetHeight - els[i].offsetHeight)/2) + 'px';
			}
		}
		catch(e) { /*alert(e);*/ }
	}
}
//following lines were removed so that there is no loop 
//it will only be called on page load and on a click of a slider.
//vAlignScrollSidebars.init();
//onLoad(vAlignScrollSidebars.setUpdateInterval);

onLoad(vAlignScrollSidebars.update);

var loadAndSlide = {
	stack : [],
	speed : 10,
	step  : 50,
	
	load : function(targetId, url, direction, maxPage, initPage, manualPagingID)
	{
		try
		{			
			var key = targetId.replace(/[^a-zA-Z0-9_-]/g,'');
			if ( !this.stack[key] )
			{
				var el = document.getElementById(targetId);
				if ( !el ) return;
				
				if ( url.indexOf('?') == -1 )
				{
					if ( url[url.length-1] != '/' )
						url = url + '/';
					
					url = url + '?';
				}
				else url = url + '&';
				
				this.stack[key] = {
						el:el,
						busy:false,
						container:null,
						infoDiv:null,
						
						url:url,						
						mPage:maxPage,
						cPage:initPage,
						manualPagingID:manualPagingID
				};
			}
			
			
			if ( this.stack[key].busy ) return;
			
			
			var page = direction >= 0 ? this.stack[key].cPage + 1 : this.stack[key].cPage - 1;
			
			// cycle round the pages
			if ( page > this.stack[key].mPage )
				page = 1;
			else if ( page < 1 )
				page = this.stack[key].mPage;
				
			url = this.stack[key].url + 'page=' + page;
			this.stack[key].cPage = page;
			var duplicate = this.prepareToSlide(this.stack[key], direction);
			
			var stackItem = this.stack[key];
			
			dojo.xhrGet({
				url: url,
				node : duplicate,
				load : function(data, xhr)
				{
					if(xhr.args.node)
					{
						xhr.args.node.innerHTML = data;
						removeElement(stackItem.infoDiv);
						setOpacity(stackItem.el,1);
						
						loadAndSlide.slide(direction, stackItem, duplicate);
						
						var manualPaging = document.getElementById(stackItem.manualPagingID);
						/*if ( manualPaging )
						{
							dojo.xhrGet({
								url: '/index/pagination/?ID=' + manualPaging.id + '&mPage=' + stackItem.mPage + '&page=' + stackItem.cPage + '&padding=' + manualPaging.getAttribute('padding') + '&baseURL=' + escape(manualPaging.getAttribute('baseURL')),
								node: manualPaging,
								load: function(data, xhr)
								{
									if(xhr.args.node)
										xhr.args.node.innerHTML = data;
								},
								error: function(e)
								{
									//alert(e);
								}
							});
						}*/
					}
				},
				error: function(e)
				{
					//alert(e);
				}
			});
		}
		catch(e) { /*alert(e);*/ }
	},
	
	slide : function(direction, stackItem, duplicate, offset)
	{
		try
		{			
			if ( typeof offset == 'undefined' ) offset = direction < 0 ? stackItem.container.offsetWidth : 0;
			
			if ( ( direction < 0 && offset > 0 ) || ( direction >= 0 && offset < stackItem.container.offsetWidth ) )
			{
				offset += direction < 0 ? -this.step : this.step ;
				if ( direction < 0 && offset < 0 ) offset = 0;
				else if ( direction >= 0 && offset > stackItem.container.offsetWidth ) offset = stackItem.container.offsetWidth;
				
				stackItem.innerContainer.style.marginLeft = (  0 - offset ) + 'px';
								
				window.setTimeout(function()
				{
					loadAndSlide.slide(direction, stackItem, duplicate, offset);
				}, this.speed);
			}
			else this.finishSlide(stackItem, duplicate);
		}
		catch(e) { /*alert(e);*/ }
	},
	
	prepareToSlide : function(stackItem, direction)
	{
		try
		{
			stackItem.busy = true;
			
			stackItem.container = stackItem.el.cloneNode(false);
			stackItem.container.style.width = stackItem.el.offsetWidth + 'px'; // lock the width
			stackItem.container.style.overflow = 'hidden';
			
			stackItem.innerContainer = document.createElement('div');
			stackItem.container.appendChild(stackItem.innerContainer);
			
			stackItem.el.parentNode.insertBefore(stackItem.container, stackItem.el);
			stackItem.innerContainer.appendChild(stackItem.el);
			
			stackItem.innerContainer.style.width = ( stackItem.el.offsetWidth * 2 ) + 'px';
			
			var duplicate = stackItem.el.cloneNode(false);
			duplicate.style.float = stackItem.el.style.float = 'left';
			stackItem.el.parentNode.insertBefore(duplicate, direction < 0 ? stackItem.el : stackItem.el.nextSibling);
			
			setOpacity(stackItem.el,0.1);
			
			stackItem.infoDiv = document.createElement('div');
			document.body.appendChild(stackItem.infoDiv);
			stackItem.infoDiv.innerHTML = '<blink>Loading...</blink><img src="http://i.4tube.com/images/loading.png" />';
			
			var pos = objPos(stackItem.el);
			stackItem.infoDiv.style.position = 'absolute';
			stackItem.infoDiv.style.left = pos[0] + 'px';
			stackItem.infoDiv.style.width = stackItem.el.offsetWidth + 'px';
			stackItem.infoDiv.style.top = ( pos[1] + (stackItem.el.offsetHeight - stackItem.infoDiv.offsetHeight)/2 ) + 'px';
			stackItem.infoDiv.className = 'infoDiv';
			
			return duplicate;
		}
		catch(e) { /*alert(e);*/ }
	},
	
	finishSlide : function (stackItem, newEl)
	{
		try
		{
			removeElement(stackItem.el);
			stackItem.el = newEl;
			stackItem.el.style.float = '';
			
			stackItem.container.parentNode.insertBefore(stackItem.el, stackItem.container);
			removeElement(stackItem.innerContainer);
			removeElement(stackItem.container);
					
			stackItem.busy = false;
			
			vAlignScrollSidebars.update();
		}
		catch(e) { /*alert(e);*/ }
	}
}

