/**
 * @author willy
 */

App.Toolbar = function() {
	var toolbar = null;
	var loginButton, logoutButton, basketButton;
	
	this.events = {
		'loginclick': true
		, 'logoutclick': true
		, 'basketload': true
	};
	
	return {
		query: null
		
		, newsletterMenu: null
		
		, profileMenu: null
		
		, searchMenu: null
 		
		, create: function() {
			this.newsletterMenu = new Ext.menu.Menu({ id: 'newsletterMenu' });
			this.newsletterMenu.add(
				new Ext.menu.Item({
					id: 'Subscription'
					, text: 'Newsletter abonnieren'
					, icon: '/lib/images/newspaper_go.png'
					, handler: App.Profile.subscribe
					, scope: App.Profile
				})
				
/**
 *  2010-04-08 Newsletter abbestellen ausgehängt
 */				
				
/*				, new Ext.menu.Item({
					id: 'Unsubscription'
					, text: 'Newsletter abbestellen'
					, icon: '/lib/images/newspaper_delete.png'
					, handler: App.Profile.unsubscribe
					, scope: App.Profile
				}) */
			);
			
			this.profileMenu = new Ext.menu.Menu({ id: 'profileMenu' });
			this.profileMenu.add(
				new Ext.menu.Item({
					id: 'Register'
					, text: 'Neu anmelden'
					, icon: '/lib/images/user_add.png'
					, handler: App.Profile.register
					, scope: App.Profile
				})
				, new Ext.menu.Item({
					id: 'Request'
					, text: 'Kennworterinnerung anfordern'
					, icon: '/lib/images/key_go.png'
					, handler: App.Profile.requestPassword
					, scope: App.Profile
				})
				, '-'
				, new Ext.menu.Item({
					id: 'Edit'
					, text: 'Kontaktdaten bearbeiten'
					, icon: '/lib/images/user_edit.png'
					, handler: function() {
						App.nextStep = null;
						App.Profile.edit();
					}
					, scope: App.Profile
				})
				, new Ext.menu.Item({
					id: 'Password'
					, text: 'Kennwort ändern'
					, icon: '/lib/images/textfield_key.png'
					, handler: App.Profile.changePassword
					, scope: App.Profile
				})
				, new Ext.menu.Item({
					id: 'Discounts'
					, text: 'Artikel mit Sonderpreisen'
					, icon: '/lib/images/award_star_gold_1.png'
					, handler: App.Search.listDiscounts
					, scope: App.Search
				})
				, new Ext.menu.Item({
					id: 'CoreArticles'
					, text: 'Mein Kernsortiment'
					, icon: '/lib/images/thumb_up.png'
					, handler: App.Search.listCoreArticles
					, scope: App.Search
				})
				/*, new Ext.menu.Item({
					id: 'History'
					, icon: '/lib/images/package.png'
					, text: 'Bestellhistorie'
				})*/
				, new Ext.menu.Item({
					id: 'Favourites'
					, icon: '/lib/images/asterisk_yellow.png'
					, text: 'Meistgekaufte Artikel'
					, handler: App.Search.listFavourites
					, scope: App.Search
				})
			);

			this.basketMenu = new Ext.menu.Menu({ id: 'basketMenu'});
			this.basketMenu.add(
				new Ext.menu.Item({
					id: 'ShowBasket'
					, text: 'Anzeigen und Bearbeiten'
					, icon: '/lib/images/basket_edit.png'
					, handler: App.Basket.show
					, scope: App.Basket
				})
				, new Ext.menu.Item({
					id: 'EmptyBasket'
					, text: 'Ausleeren'
					, icon: '/lib/images/basket_delete.png'
					, handler: App.Basket.dropAllArticles
					, scope: App.Basket
				})
				, new Ext.menu.Item({
					id: 'Checkout'
					, text: 'Zum Bestellvorgang'
					, icon: '/lib/images/basket_go.png'
					, handler: App.Checkout.step1
					, scope: App.Checkout
				})
 			);
 			
			this.searchMenu = new Ext.menu.Menu({ id: 'searchMenu' });
			this.searchMenu.add('<span class="menu-title">Wo soll gesucht werden?</span>'
				, new Ext.menu.CheckItem({
					id: 'Name'
					, text: 'Bezeichnung'
					, checked: true
					, hideOnClick: false
					, checkHandler: App.Search.onConstraintCheck
				})
				, new Ext.menu.CheckItem({
					id: 'Manufacturer'
					, text: 'Herstellername'
					, checked: true
					, hideOnClick: false
					, checkHandler: App.Search.onConstraintCheck
				})
				, new Ext.menu.CheckItem({
					id: 'Key'
					, text: 'Artikelnummer'
					, checked: true
					, hideOnClick: false
					, checkHandler: App.Search.onConstraintCheck
				})
				, new Ext.menu.CheckItem({
					id: 'Fulltext'
					, text: 'Volltext <i>(Vorsicht, zeitaufwändig!)</i>'
					, checked: true
					, hideOnClick: false
					, checkHandler: App.Search.onConstraintCheck
				})
				, new Ext.menu.CheckItem({
					id: 'ShowCoreArticlesOnly'
					, text: 'Nur Kernsortiment anzeigen'
					, checked: false
					, hideOnClick: false
					, checkHandler: App.Search.onConstraintCheck
				})
				, '-'
				, new Ext.menu.Item({ id: 'Help'
					, text: 'Hilfe zur Suchfunktion'
					, icon: '/lib/images/help.png'
					, cls: 'x-btn-text-icon bmenu search-help'
					, hideOnClick: true
					, handler: App.Toolbar.onHelpClick
				})
			);
			
			loginButton = new Ext.Toolbar.Button({
				text: 'Login'
				, cls: 'x-btn-text-icon'
				, icon: '/lib/images/door_open.png'
				, tooltip: 'Hier klicken zum Einloggen'
				, handler: App.Toolbar.onLoginClick
			});

			logoutButton = new Ext.Toolbar.Button({
				text: 'Logout'
				, cls: 'x-btn-text-icon'
				, icon: '/lib/images/door_out.png'
				, tooltip: 'Hier klicken zum Abmelden'
				, handler: App.Toolbar.onLogoutClick
			});
			
			toolbar = new Ext.Toolbar('toolbar');
			toolbar.add(loginButton, logoutButton, '-');

			newsletterButton = new Ext.Toolbar.Button({
				text:'Newsletter'
				, cls: 'x-btn-text-icon bmenu'
				, icon: '/lib/images/newspaper.png'
				, menu: this.newsletterMenu
			});
			newsletterSpacer = new Ext.Toolbar.Separator();
			toolbar.add(newsletterButton, newsletterSpacer);
			
			toolbar.add({ text:'Mein Konto'
				, cls: 'x-btn-text-icon bmenu'
				, icon: '/lib/images/user.png'
				, menu: this.profileMenu
			});
			toolbar.add('-');

			basketButton = new Ext.Toolbar.Button({
				text:'Warenkorb'
				, cls: 'x-btn-text-icon bmenu'
				, icon: '/lib/images/basket.png'
				, menu: this.basketMenu
			});
			toolbar.add(basketButton, '-');
			
			toolbar.add(new Ext.Toolbar.TextItem('Schnellsuche:'));

			var filter = new Ext.form.TextField({
				width: 100,
				selectOnFocus: true,
				qtip: "Zu wenig Ergebnisse?<br />Unter &quot;Suchoptionen&quot; finden Sie nützliche Hinweise<br />in der Hilfe zur Artikelsuche !"
			});
			toolbar.addField(filter);
			filter.el.on('keypress', function(e) { // setup an onkeypress event handler
				if(e.getKey() == e.ENTER) // listen for the ENTER key
					App.Search.search();
			});
			filter.el.on('keyup', function(e) { // setup an onkeyup event handler
				if(e.getKey() == e.BACKSPACE && this.getValue().length == 0) // listen for the BACKSPACE key and the field being empty
					App.home();
			});
			this.query = filter.el;
			
			var b = new Ext.Toolbar.Button({ text: 'Suchen'
				, cls: 'x-btn-text-icon'
				, icon: '/lib/images/magnifier.png'
				, tooltip: 'Suche starten (Ergebnis wird im Hauptfenster angezeigt)'
			});
			toolbar.add(' '
				, b
			);
			b.on('click', App.Search.search, App.Search);
			toolbar.add('-');
			toolbar.add({ text:'Optionen'
				, id: 'search-menu-button'
				, cls: 'x-btn-text-icon bmenu'
				, icon: '/lib/images/magnifier_zoom_in.png'
				, menu: this.searchMenu
			});
			
			this.hidePrivateItems();
			App.User.on('login', this.showPrivateItems, this);
			App.User.on('logout', this.hidePrivateItems, this);
		}

		, hidePrivateItems: function() {
			newsletterButton.show();
			newsletterSpacer.show();
			var menu = this.profileMenu;
			menu.items.get('Register').enable();
			menu.items.get('Request').enable();
			menu.items.get('Edit').disable();
			menu.items.get('Password').disable();
			menu.items.get('Favourites').disable();
			menu.items.get('Discounts').disable();	
			menu.items.get('CoreArticles').disable();	
			logoutButton.hide();
			loginButton.show();
		}		
		
		, onBasketLoaded: function(hasItems) {
			basketButton.setDisabled(! hasItems);
		}
		
		, showPrivateItems: function() {
			newsletterButton.hide();
			newsletterSpacer.hide();
			var menu = this.profileMenu;
    		menu.items.get('Register').disable();
    		menu.items.get('Request').disable();
    		menu.items.get('Edit').enable();
    		menu.items.get('Password').enable();
    		// menu.items.get('History').enable();
    		menu.items.get('Favourites').enable();
    		if (App.User.getData().PricingGroupsId > 1)
    			menu.items.get('Discounts').enable();	
    		if (App.User.getData().HasCoreArticles)
    			menu.items.get('CoreArticles').enable();
			loginButton.hide();
    		logoutButton.show();
		}
		
		, flashHelp: function() {
			var menu = this.searchMenu;
			menu.show('search-menu-button');
    		menu.items.get('Help').getEl().highlight().highlight().highlight();
		}
		
		, onLogoutClick: function() {
    		App.User.logout();
		}
		
		, onLoginClick: function() {
    		App.User.showLoginDialog();
		}
		
		, onHelpClick: function() {
			App.Viewer.load('templates/SearchHelp.html');
		}
	};
}();
Ext.extend(App.Toolbar, Ext.util.Observable);

