/*
 * JavaScript file created by Rockstarapps Concatenation
*/

/*
 * START OF FILE - /Ext Touch/memory/js/Memory.js
 */
/**
 * Ext Touch Memory
 *
 * @author Nils Dehl - mail@nils-dehl.de - http://www.nils-dehl.de
 * @version 1.0.3
 */

Ext.ns('Memory');

Memory = Ext.apply(new Ext.util.Observable,{

	app: {},
	cards: [4,2,8,6,4,2,1,7,3,1,7,3,5,6,5,8],

	/**
	 * open cards
	 * @type array
	 */
	openCards: [],

	/**
	 * trys
	 */
	trys: 0,

	/**
	 * bootstrap
	 */
	bootstrap: function() {
		this.shuffleCards();
		this.initUi();
		this.initEventListener();
	},

	/**
	 * init the application ui
	 */
	initUi: function() {
		this.app = new Memory.Ui();
	},

	/**
	 * Init Event Listener
	 */
	initEventListener: function() {
		Memory.on('openCard', this.onOpenCard, this);
	},


	/**
	 * on open card event
	 * @param {Memory.Ui.Card} card
	 */
	onOpenCard: function(card) {
		card.removeTabEventListener();

		if (this.openCards.length < 2) {
			this.openCards.push(card);
		} else {
			var tempCard = card;
		}

		// if two cards open
		if (typeof tempCard == 'object') {

			// close if not equal
			if (this.openCards[0].cardId != this.openCards[1].cardId) {
				this.openCards[0].setCard(0,'flip');
				this.openCards[1].setCard(0,'flip');
				this.openCards[0].addTabEventListener();
				this.openCards[1].addTabEventListener();
			}

			delete this.openCards;
			this.openCards = Array();
			this.openCards.push(tempCard);
			delete tempCard;
		}

		this.incrementTrys();
	},

	/**
	 * increment trys counter and fire event
	 */
	incrementTrys: function() {
		this.trys = this.trys + 1;
		Memory.fireEvent('trysCount', this.trys);
	},


	/**
	 * Shuffle the card array
	 */
	shuffleCards: function() {
		var rand, tmp;

		for(var i = 0; i < this.cards.length; i++){
			rand = Math.floor(Math.random() * this.cards.length);
			tmp = this.cards[i];
			this.cards[i] = this.cards[rand];
			this.cards[rand] =tmp;
		}
	}


});
/*
 * END OF FILE - /Ext Touch/memory/js/Memory.js
 */

/*
 * START OF FILE - /Ext Touch/memory/js/Memory.Ui.js
 */
Ext.ns('Memory.Ui');
Memory.Ui = Ext.extend(Ext.TabPanel, {
	id: 'tabpanel',
	initComponent: function() {
		var config = {

			fullscreen: true,
			cls: 'table',
			ui: 'light',
			animation: {
				type: 'slide',
				cover: true
			},
			tabBar: {
				dock: 'bottom',
				layout: {
					pack: 'center'
				}
			},
			items: [
				new Memory.Ui.Game(),
				//new Memory.Ui.Highscore(),
				new Memory.Ui.Info()
			],
			dockedItems: [
				new Memory.Ui.Topbar()
			]
		};
		Ext.apply(this, config);
		Memory.Ui.superclass.initComponent.call(this);
	}
});
/*
 * END OF FILE - /Ext Touch/memory/js/Memory.Ui.js
 */

/*
 * START OF FILE - /Ext Touch/memory/js/Memory.Ui.Game.js
 */
Ext.ns('Memory.Ui');
Memory.Ui.Game = Ext.extend(Ext.Panel, {
	id: 'game',
	initComponent: function() {
		var config = {
			title: 'Game',
			cls: 'table',
			iconCls: 'favorites',
			layout: {
				type: 'vbox',
				align: 'stretch'
			}
		};
		Ext.apply(this, config);
		Memory.Ui.Game.superclass.initComponent.call(this);
	},

	// @private
	onRender : function(ct, position) {
		Memory.Ui.Game.superclass.onRender.call(this, ct, position);
		this.renderPlayfield();
	},

	/**
	 *
	 */
	renderPlayfield: function() {
		var c = 0;
		for (var i = 0; i < 4; i++) {
			var row = this.add({
				xtype: 'panel',
				flex: 1,
				layout: {
					type: 'hbox',
					align: 'stretch'
				}
			});
			for (var j = 0; j < 4; j++) {
				row.add({
					xtype: 'Memory.Ui.Card',
					className: 'card-' + Memory.cards[c],
					cardId: Memory.cards[c]
				});
				c++;
			}
		}
	}
});

/*
 * END OF FILE - /Ext Touch/memory/js/Memory.Ui.Game.js
 */

/*
 * START OF FILE - /Ext Touch/memory/js/Memory.Ui.Info.js
 */
Ext.ns('Memory.Ui');
Memory.Ui.Info = Ext.extend(Ext.Panel, {
	initComponent: function() {
		var config = {
			title: 'Info',
			cls: 'table info',
			iconCls: 'info',
			layout: {
				type: 'vbox',
				align: 'stretch'
			},
			html: 'Nils Dehl<br />' +
					'mail: <a href="mailto:mail@nils-dehl.de">mail@nils-dehl.de</a><br />' +
					'www: <a href="http://nils-dehl.de">http://nils-dehl.de</a><br />' +
					'<br />' +
					'&copy; Nils Dehl<br />' +
					'photos: Nils Dehl<br />' +
					''
		};
		Ext.apply(this, config);
		Memory.Ui.Info.superclass.initComponent.call(this);
	},

	// @private
	onRender : function(ct, position) {
		Memory.Ui.Info.superclass.onRender.call(this, ct, position);
	}
});

/*
 * END OF FILE - /Ext Touch/memory/js/Memory.Ui.Info.js
 */

/*
 * START OF FILE - /Ext Touch/memory/js/Memory.Ui.Highscore.js
 */
Ext.ns('Memory.Ui');
Memory.Ui.Highscore = Ext.extend(Ext.Panel, {
	initComponent: function() {
		var config = {
			title: 'Highscore',
			cls: 'table',
			iconCls: 'bookmarks',
			layout: {
				type: 'vbox',
				align: 'stretch'
			},
			items: [
				{
					xtype: 'button',
					handler: function() {
						var tabpanel = Ext.getCmp('tabpanel');
						console.log(tabpanel.getComponent(0));
						tabpanel.tabBar.onTabTap(tabpanel.getComponent(0));//Ext.getCmp('game'));
					}
				}
			]
		};
		Ext.apply(this, config);
		Memory.Ui.Highscore.superclass.initComponent.call(this);
	},

	// @private
	onRender : function(ct, position) {
		Memory.Ui.Highscore.superclass.onRender.call(this, ct, position);

	}
});

/*
 * END OF FILE - /Ext Touch/memory/js/Memory.Ui.Highscore.js
 */

/*
 * START OF FILE - /Ext Touch/memory/js/Memory.Ui.Bottombar.js
 */
Memory.Ui.Bottombar = Ext.extend(Ext.Toolbar, {
	initComponent: function() {
		var config = {
			dock: 'bottom',
			ui: 'light',
			items: [

				{
					xtype: 'button',
					text: 'new game',
					handler: this.onNewGameButtonClick,
					scope: this
				},
				{
					xtype: 'button',
					iconCls: 'info',
					text: 'trys',
					ref: 'trys'
				}
			]
		};
		Ext.apply(this, config);
		Memory.Ui.Bottombar.superclass.initComponent.call(this);

		this.on('afterrender', this.onAfterrenderAction, this);
	},

	/**
	 *
	 */
	onAfterrenderAction: function() {
		Memory.on('trysCount', this.updateTryBadge, this);

	},

	/**
	 *
	 */
	updateTryBadge: function(text) {
		this.items.items[1].setBadge(text);
	},

	onNewGameButtonClick: function() {
		document.location.reload();
	}


});
/*
 * END OF FILE - /Ext Touch/memory/js/Memory.Ui.Bottombar.js
 */

/*
 * START OF FILE - /Ext Touch/memory/js/Memory.Ui.Topbar.js
 */
Memory.Ui.Topbar = Ext.extend(Ext.Toolbar, {
	initComponent: function() {
		var config = {
			title: 'remember the card',
			dock: 'top',
			defaults: {
				ui: 'mask'
			},
			layout: 'hbox',
			items: [
				{
					text: 'try'
				},
				{
					xtype:'spacer',
					flex:1
				},
				{
					iconCls: 'refresh',
					handler: this.onNewGameButtonClick,
					scope: this
				}
			]
		};
		Ext.apply(this, config);
		Memory.Ui.Topbar.superclass.initComponent.call(this);
		this.on('afterrender', this.onAfterrenderAction, this);
	},

	/**
	 *
	 */
	onAfterrenderAction: function() {
		Memory.on('trysCount', this.updateTryBadge, this);

	},

	/**
	 *
	 */
	updateTryBadge: function(text) {
		this.getComponent(0).setBadge(text);
	},


	onNewGameButtonClick: function() {
		document.location.reload();
	}
});
/*
 * END OF FILE - /Ext Touch/memory/js/Memory.Ui.Topbar.js
 */

/*
 * START OF FILE - /Ext Touch/memory/js/Memory.Ui.Card.js
 */
Memory.Ui.Card = Ext.extend(Ext.Panel, {

	initComponent: function() {
		var config = {
			layout: 'card',
			cls: 'card',
			flex: 1,
			activeItem: 0,
			items: [
				{
					cls: 'cover'
				},
				{
					cls: this.initialConfig.className
				}
			]
		};
		Ext.apply(this, config, Ext.apply(this, this.initialConfig));
		Memory.Ui.Card.superclass.initComponent.call(this);

		this.on('afterrender', this.onAfterrenderAction, this);
	},

	/**
	 *
	 */
	onAfterrenderAction: function() {
		this.addTabEventListener();
	},

	/**
	 *
	 */
	addTabEventListener: function() {
		this.mon(
			this.el,
			{
				tap: this.onTab,
				scope: this
			}
		);
	},

	/**
	 *
	 */
	removeTabEventListener: function() {
		this.mun(
			this.el,
			{
				tap: this.onTab,
				scope: this
			}
		);
	},

	/**
	 * On tab flip the card
	 */
	onTab: function(){
		Memory.fireEvent('openCard', this);
		this.setCard(1,'flip');
	}
});

Ext.reg('Memory.Ui.Card', Memory.Ui.Card);
/*
 * END OF FILE - /Ext Touch/memory/js/Memory.Ui.Card.js
 */

/*
 * START OF FILE - /Ext Touch/memory/js/index.js
 */
Ext.setup(
	{
		tabletStartupScreen: 'tablet_startup.png',
		phoneStartupScreen: 'phone_startup.png',
		icon: 'icon.png',
		addGlossToIcon: false,
		onReady: Memory.bootstrap,
		scope: Memory
	}
);


/*
 * END OF FILE - /Ext Touch/memory/js/index.js
 */

/*
 * JavaScript file created by Rockstarapps Concatenation
*/

