var glow;
var dom;
var anim;
var events;
var $;
var localisation = {scrollLeft:"Scroll Left", scrollRight:"Scroll Right"};

gloader.load(['glow','1.0','glow.anim','glow.dom','glow.events','glow.net','glow.tweens','glow.widgets.Overlay'], {
	onLoad: function(glowLocal) {
		glowLocal.ready(function() {
			glow = glowLocal;
			$ = glow.dom.get;
			events = glow.events;
			dom = glow.dom;
			anim = glow.anim;
			tweens = glow.tweens;

			var myCarousel = new carousel($("#artists_carousel").item(0));

			events.addListener($("#artists_carousel li"),"mouseover",function()
			{
				$(this).addClass("show");
				return false;
			});
			events.addListener($("#artists_carousel li"),"mouseout",function()
			{
				$(this).removeClass("show");
				return false;
			});
			events.addListener($("#artists_carousel li"),"click",function(e)
			{
				e.preventDefault();
				anchor=$(this).get("div > a");
				window.location.href=anchor[0].href;
			});
		});
	}
});

carousel=function(A)
{
	this.container=$(A);
	this.window=this.container.get("div.carousel-window");
	this.pageWidth=this.window.width();
	this.currentPos=0;
	this.newPos=0;
	this.currentPane=1;
	this.content=this.window.get("ul.carousel-content");
	this.items=$(this.window).get("> ul > li");
	this.items.each(function(B){
		this.num=B;
	});
	this.panesLoaded=[1];
	this.seeking=false;
	this.animate=true;//=!environment.browser.wii;
	if(this.items.length>0)
	{
		this.itemsPerPane=this.pageWidth/this.items.width();
		this.numPanes=Math.ceil(this.items.length/this.itemsPerPane);
	}
	if(this.items.length>this.itemsPerPane)
	{
		this.init();
	}
};

carousel.prototype=
{
	addControls:function()
	{
		this.prevControlHolder=this.container.get(".prev");
		this.prevControl=dom.create('<a href="#" class="control"><span>'+localisation.scrollLeft+"</span></a>");

		this.nextControlHolder=this.container.get(".next");
		this.nextControl=dom.create('<a href="#" class="control"><span>'+localisation.scrollRight+"</span></a>")
	},
	bindControls:function()
	{
		var A=this;
		events.addListener(this.prevControl,"click",function()
		{
			A.seekPrev();
			return false
		});
		events.addListener(this.nextControl,"click",function()
		{
			A.seekNext();
			return false
		});
		this.nextControl.appendTo(this.nextControlHolder)
	},
	updateControls:function()
	{
		if(this.currentPane==1)
		{
			this.prevControl.remove();
			if($(this.nextControlHolder).children().length===0)
			{
				this.nextControl.appendTo(this.nextControlHolder)
			}
		}
		else
		{
			if(this.currentPane==this.numPanes)
			{
				this.nextControl.remove();
				if($(this.prevControlHolder).children().length===0)
				{
					this.prevControl.appendTo(this.prevControlHolder)
				}
			}
			else
			{
				if($(this.prevControlHolder).children().length===0)
				{
					this.prevControl.appendTo(this.prevControlHolder)
				}
				if($(this.nextControlHolder).children().length===0)
				{
					this.nextControl.appendTo(this.nextControlHolder)
				}
			}
		}
	},
	move:function(B,C)
	{
		var A=this;
		function D()
		{
			A.seeking=false;
			A.updateControls();
			if(C)
			{
				A.preload(A.currentPane+1)
			}
		}
		if(this.animate)
		{
			var E=anim.css(this.content,0.75,{left:{to:this.newPos}},{tween:tweens.easeBoth()});
			events.addListener(E,"complete",D);
			E.start()
		}
		else
		{
			$(this.content).css("left",this.newPos+"px");
			D()
		}
	},
	seekNext:function()
	{
		if(this.seeking||(this.currentPane==this.numPanes))
		{
			return false
		}
		this.seeking=true;
		this.currentPane++;
		this.currentPos=this.newPos=this.currentPos-this.pageWidth;
		this.move(this.newPos,true)
	},
	seekPrev:function()
	{
		if((this.seeking)||(this.currentPane==1))
		{
			return false
		}
		this.seeking=true;
		this.currentPane--;
		this.currentPos=this.newPos=this.currentPos+this.pageWidth;
		this.move(this.newPos)
	},
	preload:function(C)
		{
		for(var D=0;D<this.panesLoaded.length;D++)
		{
			if(this.panesLoaded[D]==C)
			{
				return false
			}
		}
		if(C>this.numPanes)
		{
			return false
		}
		var B=this.itemsPerPane*(C-1);
		var A=(this.itemsPerPane*C);
		if(A>this.items.length)
		{
			A=this.items.length
		}
		this.items.slice(B,A).each(function()
		{
			//var E=$(this).get(".image img");
			//E.attr("src", '')//config.episodeImgUrl+E.attr("id"))
		});
		this.panesLoaded.push(C)
	},
	addFocusEvents:function()
	{
		var A=this;
		events.addListener(this.window,"keyup",function(D)
		{
			var C=$(D.source);
			C=A.getParent(C);
			var B=Math.floor(C[0].num/A.itemsPerPane)+1;
			if(B>A.currentPane)
			{
				A.seekNext()
			}
			else
			{
				if(B<A.currentPane)
				{
					A.seekPrev()
				}
			}
		})
	},
	getParent:function(A)
	{
		while(A.parent()&&!A.parent().hasClass("carousel-content"))
		{
			A=$(A).parent()
		}
		return A
	},
	init:function()
	{
		this.addFocusEvents();
		this.addControls();
		this.bindControls();
		this.preload(2)
	}
};

