var EventSpotlight = {
	hoverSpeed : 200,
	fadeInSpeed : 600,
	switching:false,
	dimmed:false,
	selectedId:0,
	selectedIdx:0,
	autoRotate:true,
	cutoff:0,
	fullMode:true,
	nextCounter:1,
	load : function(items) {
		var $this = this;
		this.items = items;

		$('#firstpage-spotlight .spotlight-item-data').css('display', 'none');

		if (this.fullMode) {
			$('#events-spotlight li a').click(function(){return false;});
		}

		for (var i=0; i<this.items.length;i++) {
			var item = this.items[i];

			var icon = new Image();
			icon.id = item.id;
			icon.onload = function() {
				/*
				if (Modernizr.canvas) {
					var cnv_id = 'spotlight-cnv-' + this.id;
					var cnv = $('<canvas />', {id:cnv_id});
					$('#spotlight-item-' + this.id+ ' .img').append(cnv);

					var ctxSource = cnv[0].getContext('2d');
					ctxSource.drawImage(this, 0, 0);
					stackBlurCanvasRGBA(cnv_id, 0, 0, this.width, this.height, 10);
				}
				*/
			};
			icon.src = item.icon;

			var artwork_wrap = $('#artwork');

			if (this.fullMode) {
				var large = new Image();
				large.id = item.id;
				large.onload = function() {
					var img = $('<img/>', {id:'spotlight-large-'+this.id}).css('display','none');
					img.attr('src', this.src);
					artwork_wrap.append(img);
				}
				large.src = item.large;
			}

			var item_h = $('#spotlight-item-' + item.id);

			/*
			item_h.hover(function(){
				$(this).find('canvas').stop(true, true).fadeIn($this.hoverSpeed);
			}, function(){
				if (!$(this).hasClass('active')) {
					$(this).find('canvas').stop(true, true).fadeOut($this.hoverSpeed);
				}
			});
			*/

			item_h.find('.img a').attr('sid', item.id).click(function(){
				var sid = $(this).attr('sid');
				$this.autoRotate = false;

				setTimeout(function(){
					$this.autoRotate = true;
				}, 10000);

				if (sid != $this.selectedId && !$this.switching) {
					$this.switchTo(sid);
				}
			});
		}

		if (this.fullMode) {
			this.rotater = setInterval(function(){

				if ($this.autoRotate) {
					$this.nextItem();
				} else {
					//window.clearInterval($this.rotater);
				}
			}, 7000);
		}

		$(document).keyup(function(e) {
			if (e.keyCode == 27 && $this.dimmed) {
				$this.toggleVideo(0);
			}
		});

	},
	complete : function(s) {
		var $this = this;
		this.selectedId = s;

		if (this.fullMode) {
			$('#spotlight-item-' + s).addClass('active');
		}

		$('#spotlight-large-' + s).fadeIn(this.fadeInSpeed);
		$('#sp-data-' + s).fadeIn(this.fadeInSpeed);

		if (this.items.length <= this.cutoff) {
			$('#next-arr').remove();
		} else {
			$('#next-arr').click(function(){
				$this.moveRight();
			});
		}
	},
	moveRight : function() {
		var f = $('#events-spotlight li:first');
		var w = f.outerWidth(true);
		var l = $('#events-spotlight ul');

		l.animate({left:'-'+w}, 400, function(){
			var itm = $('#events-spotlight li:hidden:first');
			itm.fadeIn(200);
			l.append(f);
			f.css('display','none');
			l.css({left:0});
		});
	},
	switchTo : function(id) {
		var $this = this;
		if (this.dimmed) return;
		this.switching = true;

		var c_img = $('#spotlight-large-'+this.selectedId);
		var n_img = $('#spotlight-large-'+id);

		$('#events-spotlight li').removeClass('active');
		$('#spotlight-item-' + id).addClass('active');


		$('#sp-data-' + this.selectedId).fadeOut(this.fadeInSpeed);
		$('#sp-data-' + id).fadeIn(this.fadeInSpeed);

		c_img.fadeOut(this.fadeInSpeed, function() {

		});
		n_img.fadeIn(this.fadeInSpeed, function() {
			$this.switching = false;
			$this.selectedId = id;
			$this.selectedIdx = $this.getItemIdx(id);
		});

	},
	getItemIdx : function(id) {
		var idx = 0;
		for (var i=0; i < this.items.length; i++) {
			if (this.items[i].id == id) { idx = i; }
		}
		return idx;
	},
	nextItem : function() {
		var itemC = this.items.length;
		var nextIdx = this.selectedIdx + 1;
		if (this.selectedIdx + 1 >= itemC) {
			nextIdx = 0;
		}
		this.switchTo(this.items[nextIdx].id);

		if (this.items.length > this.cutoff && this.nextCounter >= this.cutoff) {
			this.moveRight();
		}
		this.nextCounter++;
	},
	toggleVideo : function(id) {
		var c_img = $('#spotlight-large-'+this.selectedId);
		var c_data = $('#sp-data-'+this.selectedId);

		if (this.dimmed) {
			var o = 1;
			var d = 'block';
			var state = 'closed';
		} else {
			var o = 0.3;
			var d = 'none';
			var state = 'open';
		}

		var v = c_data.find('.video');

		if (state == 'open') {
			v.addClass('loading');
		}

		if (!this.dimmed) {
			c_data.find('.cta-buttons').css('display',d);
		}

		c_img.fadeTo(200, o, function() {
			var v = c_data.find('.video');

			if (state == 'open') {
				v.fadeIn('fast');
				$.get('?rt=events&action=getvideo', {itemid:id}, function(data){
					v.removeClass('loading');
					v.append('<div class="video-output">'+data+'</div>');
					v.find('.close').css('display','block');
				});
			} else {
				v.fadeOut('fast');
				v.find('.video-output').remove();
				v.find('.close').css('display','none');
				c_data.find('.cta-buttons').css('display', 'block');
			}
		});
		this.dimmed = !this.dimmed;
	}
};
