/*
 ===================================================
	Creates and draws a list of items to search
 ===================================================
 */
var kHUDLeftOffset 	= 160;
var kHUDRightOffset = 850;

var ItemsList = cc.Node.extend({
	_winSize:null,
	_level:null,
	_List:null,
	scrollView:null,
	contentWidth:0,
	itemsArray:null,
	items_count:null,

	ctor:function () {
		this._super();
		this.init();
	},

	init:function () {
		this.removeAllChildren();
		this._winSize = cc.director.getWinSize();
		this._level = GameManager.getInstance().getSelectedLevel();
		this.itemsArray = new Array();
		var TempList = new Array();
		for(var item in this._level.Items){
			TempList.push(this._level.Items[item]);			
		}

		this._List = new Array();
		var titleTTF = null;
		this.items_count = 0;

		if(GameManager.getInstance().getGameMode() == SS.GAME_MODE.RELAX){
			this.items_count = RELAX_MODE_ITEMS_COUNT;
		}else{
			this.items_count = GameManager.getInstance().getCurrentRound().ItemSearchNum;
		}

		for(var i = 0; i < this.items_count;i++){
			this._List.push(  TempList.splice(Math.floor(Math.random() * TempList.length), 1)[0]  );
		}
		this.initForDesktop();

		return true;
	},

	initForDesktop : function(){
		for(var i = 0; i < this.items_count;i++){
			titleTTF = new cc.LabelTTF(this._List[i].Title, "Consolas", 15);
			titleTTF.color = cc.color(253, 211,129);
			var posX;
			if(i<5) {	
				posX = this.getPositionXforTitleAndIndex(titleTTF, i);
				titleTTF.setPosition(cc.p(posX , 57));
			}		
			else if(i<10){			
				posX = this.getPositionXforTitleAndIndex(titleTTF, (i-5));
				titleTTF.setPosition(cc.p(posX , 35));
			}
			else if(i<15){
				posX = this.getPositionXforTitleAndIndex(titleTTF, (i-10));
				titleTTF.setPosition(cc.p(posX , 13));
			}
			this.addChild(titleTTF);
		}
	},

	getPositionXforTitleAndIndex: function(titleTTF, index){
		var posX;
		if( (this._winSize.width*0.2 + this._winSize.width*0.15*index - titleTTF.getBoundingBox().width/2) < kHUDLeftOffset )
		{
			posX = kHUDLeftOffset + titleTTF.getBoundingBox().width/2;
		}
		else if( (this._winSize.width*0.2 + this._winSize.width*0.15*index + titleTTF.getBoundingBox().width/2) > kHUDRightOffset)
		{			
			posX = kHUDRightOffset - titleTTF.getBoundingBox().width/2;
		}
		else
		{
			posX = this._winSize.width*0.2 + this._winSize.width*0.15*index;
		}
		return posX;
	},

	getItemsList : function(){
		return this._List;
	},

	getItems : function () {
			var labels = this.getChildren();
			return labels.length?labels:null
	},

	removeItem : function (item, bool) {
		for(var i = 0; i < this.itemsArray.length; i++){
			if( this.itemsArray[i].title == item.getString() ){

				this.scrollView.removeChild(item, bool);                
				this.itemsArray.splice(i, 1);

			}
		}
	},

	updateList : function (){		
		var labels = this.scrollView.getChildren();
		this.contentWidth = 0;
		var longTTF		= labels[0];
		var prevLongTTF = new cc.LabelTTF("", "Consolas", 30);
		prevLongTTF.setPosition(cc.p(0, 0));
		prevPosition = cc.p(0, 0);
		var offset = 50;
		var moveTo = null;

		for(var i = 0; i < labels.length;i++){
			if(i<2) offset = 0;
			else offset = 50;
			//current label

			//longest label
			if(i % 2 == 0) {
				if(i < this.itemsArray.length - 1 ){
					longestWordIdx = (this.itemsArray[i].width > this.itemsArray[i + 1].width)?i
							:(i + 1);
				}
				else{
					longestWordIdx = i;
				}
				longTTF = labels[longestWordIdx];
				var position = cc.p(prevPosition.x + longTTF.getBoundingBox().width/2 + prevLongTTF.getBoundingBox().width/2 + offset, 40);
				moveTo = new cc.MoveTo(0.3, position);
				labels[i].runAction(moveTo);
				if(i == longestWordIdx){
					longTTF = labels[i];
					this.contentWidth += labels[i].getBoundingBox().width + offset;
				}
			}			
			else {	  	   
				if(i == longestWordIdx){
					longTTF = labels[i];
					this.contentWidth += labels[i].getBoundingBox().width + offset;
				}
				var position = cc.p(prevPosition.x + longTTF.getBoundingBox().width/2 + prevLongTTF.getBoundingBox().width/2 + offset, 10);
				moveTo = new cc.MoveTo(0.3, position);
				labels[i].runAction(moveTo);
				prevLongTTF = longTTF;
				prevPosition = position;
			}
		}
		this.scrollView.setInnerContainerSize(cc.size(this.contentWidth,75));
	},

	getItemPositionAndMoveList: function(item){
		return item.getPositionX();
		this.scrollView.stopAllActions();
		this.scrollView.setTouchEnabled(false);
		var ContentX = 0;
		this.scrollView.runAction( cc.sequence( cc.delayTime(2), 
				cc.callFunc(
						function(b){
							this.scrollView.setTouchEnabled(b);
							this.updateList();
						}, this, false)
		));
		if(GameManager.getInstance().getGameState() != SS.GAME_STATE.FALLING_ITEM){
			var percentOffset = item.getPositionX() / this.scrollView.getInnerContainerSize().width * 100;
			this.scrollView.scrollToPercentHorizontal(percentOffset, 1, true);
			var w = this.scrollView.getInnerContainer().getContentSize().width - this.scrollView.getContentSize().width;
			ContentX = -(percentOffset * w / 100);
		}else{
			ContentX = this.scrollView.getInnerContainer().getPositionX();
		}	
		var resultX = item.getPositionX() + ContentX + this.scrollView.getPositionX();
		if(GameManager.getInstance().getGameState() == SS.GAME_STATE.FALLING_ITEM
				&& resultX > (this.scrollView.getPositionX() 
						+ this.scrollView.getContentSize().width) ){
			resultX = this.scrollView.getPositionX() + this.scrollView.getContentSize().width;
			this.animateExtraLabel(item,resultX);
		}
		if(GameManager.getInstance().getGameState() == SS.GAME_STATE.FALLING_ITEM
				&& resultX < this.scrollView.getPositionX() ){
			resultX = this.scrollView.getPositionX();
			this.animateExtraLabel(item,resultX);
		}
		return resultX;
	},

	animateExtraLabel: function(label, positionX){
		var extraLabel = new cc.LabelTTF(label.string, "Consolas", 30);
		extraLabel.setPosition(cc.p(positionX, 110));
		extraLabel.color = cc.color(253, 211,129);
		var fadeout = new cc.FadeOut(2);		
		var moveBy = new cc.MoveBy(2, cc.p( 0, 40));
		var callfunc = new cc.callFunc(function(){
			extraLabel.removeFromParent(true);
		} );
		var callfunc2 = new cc.callFunc(function(){
			extraLabel.setVisible(true);
		} );		
		this.addChild(extraLabel);
		extraLabel.setVisible(false);
		extraLabel.runAction(cc.sequence(cc.delayTime(2),callfunc2, moveBy, callfunc));
		extraLabel.runAction(cc.sequence(cc.delayTime(2),fadeout));
	}
});

