var randomizer = {
	spinTime : 2400,
	init : function () {
		this.container = Dom.get('randomizer');
		this.list = Dom.get('randomizer-list');
		this.listItems = this.list.getElementsByTagName('li');
		
		Dom.removeClass(this.container, 'hidden');
		
		var listRegion = Dom.getRegion(this.list);
		this.listHeight = listRegion.bottom - listRegion.top;
		this.listItemHeight = Dom.getStyle(this.listItems[0],'height');
		this.listItemHeight = Number(this.listItemHeight.slice(0, -2));



		// determine the Y position of the second-last item, which we always scroll to
		this.scrollToPX = this.listHeight - (6 * this.listItemHeight) + (this.listItemHeight / 2) - 4; // 4 = correction
		// Dom.addClass(this.container, 'hidden');

		Dom.addClass(this.container, 'hidden');
		
		// click this
		// events.enableAddButton();
		Event.on('randomSuggestion', 'click', this.showRandomizer, this, true);
		// console.log(this.showRandomizer)
	},


	showRandomizer : function (event) {
		Event.preventDefault(event);

		var hideAutosuggestorAnim = new YAHOO.util.Anim("autosuggestor", { opacity: {from: 1, to: 0} }, .6, YAHOO.util.Easing.easeOutStrong);
		
		hideAutosuggestorAnim.onComplete.subscribe(function() {
			Dom.removeClass(this.container, 'hidden');
			
			var showRandomizerAnim = new YAHOO.util.Anim(this.container, { opacity: {from: 0, to: 1} }, 1, YAHOO.util.Easing.easeOutStrong);
			showRandomizerAnim.animate();
			
			this.onClick();

		}, this, true);

		hideAutosuggestorAnim.animate();
		quickHideSuggestionBox();
	},

	hideRandomizer : function (callback) {
		var hideRandomizerAnim = new YAHOO.util.Anim(this.container, { opacity: {from: 1, to: 0} }, .6, YAHOO.util.Easing.easeOutStrong);
		
		hideRandomizerAnim.onComplete.subscribe(function() {
			Dom.addClass(this.container, 'hidden');

			var showAutosuggestorAnim = new YAHOO.util.Anim("autosuggestor", { opacity: {from: 0, to: 1} }, .6, YAHOO.util.Easing.easeOutStrong);
			showAutosuggestorAnim.animate();

			callback();
		}, this, true);

		hideRandomizerAnim.animate();

	},

	shuffle : function (listItems) {
		// !! - Fake shuffle: finds 10 random items and moves them to the end of the list
		for (var i = 0; i < 10; i++) {
			var itemToMove = listItems[Math.floor(Math.random() * (listItems.length - 10))];

			// check if item already has been added to the list. If so, don't show this as possible final record
			var addedBool = checkIfAdded("stringMode", itemToMove.className.toLowerCase());

			if(addedBool) {
				i--;
				continue;
			}
			// add random item to end of list (scrolling animation will animate from top to bottom)
			itemToMove.parentNode.appendChild(itemToMove);
		}
	},

	getActiveItem : function () {
		return this.listItems[this.listItems.length - 4];
	},

	onClick : function (e) {
		if(e) {
			Event.preventDefault(e);
		}
		
		// shuffle list items
		this.shuffle(this.listItems);

		// spin the wheel
		this.spinWheel(this.scrollToPX);
	},
	

	onSpinReady : function (e, o) {
		// first stop other anims and jump to their end (true)
		if(this.spinWheel.myFontColorAnim) this.spinWheel.myFontColorAnim.stop();


		var activeItem = this.getActiveItem();
		var surroundingItems = [activeItem.previousSibling, activeItem.nextSibling];

		// set font-color of active item
		this.onSpinReady.myFontColorAnim2 = new YAHOO.util.ColorAnim(activeItem, {
			color: {
				to: "#000000"
			} 
		}, .8, YAHOO.util.Easing.easeOut);
		this.onSpinReady.myFontColorAnim2.animate();

		// left pos fix
		this.onSpinReady.myAnim = new YAHOO.util.Anim(activeItem, {
			left: {
				from: 30,
				to: 20
			} 
		}, .8, YAHOO.util.Easing.easeOut);
		this.onSpinReady.myAnim.animate();

		// left pos fix
		this.onSpinReady.myAnim2 = new YAHOO.util.Anim(surroundingItems, {
			left: {
				from: 30,
				to: 25
			} 
		}, .8, YAHOO.util.Easing.easeOut);
		this.onSpinReady.myAnim2.animate();

		//preset button info
		this.setAddButtonInfo();

		activeItem.tickerWidth = this.getTickerWidth(activeItem);
		if(activeItem.tickerWidth > this.maxVisibleWidth) {
			// add mouseover ticker
			this.enableTicker(activeItem);
		} else {
			// remove mouseover ticker
			this.disableTicker(activeItem);
		}
	},

	spinWheel : function (scrollTo) {
		// first stop other anims and jump to their end (true)
		if(this.onSpinReady.myFontColorAnim2) this.onSpinReady.myFontColorAnim2.stop();
		if(this.onSpinReady.myAnim) this.onSpinReady.myAnim.stop();
		if(this.onSpinReady.myAnim2) this.onSpinReady.myAnim2.stop();

		// reset the font color and left of li's
		Dom.setStyle(this.listItems,'color', '#999999');
		Dom.setStyle(this.listItems,'left', '30px');

		// first font color animation:
		this.spinWheel.myFontColorAnim = new YAHOO.util.ColorAnim(this.list, {
			color: {
				to: "#999999"
			} 
		}, ((this.spinTime/1000)/5), YAHOO.util.Easing.easeOutStrong);

		this.spinWheel.myFontColorAnim.animate();

		
		this.spinWheel.myAnim = new YAHOO.util.Anim(this.list, {
			top: {
				from: 0,
				to: -scrollTo
			} 
		}, (this.spinTime/1000), YAHOO.util.Easing.easeOutStrong);

		var myFontColorAnim2Started = false;
		this.spinWheel.myAnim.onTween.subscribe(function(e, o){
			if(!myFontColorAnim2Started && o[0].currentFrame > this.spinTime * .4) {
				myFontColorAnim2Started = true;
				this.onSpinReady();
			}
		}, this, true);

		this.spinWheel.myAnim.onComplete.subscribe(function(e, o){
			
			var randomTalent = stripHTML(this.getActiveItem().innerHTML);

			this.hideRandomizer(function() {
				initWithSuggestion(randomTalent);
			});

		}, this, true);
		

		this.spinWheel.myAnim.animate();
	}
}

Event.onDOMReady(randomizer.init, randomizer, true);
