var ForzieriBarClass = new Class({
	Implements: [Options, Events],
	
	options: {
		element: 'forzieriBar',
		logged: false,
		actual_state: 'closed',
		actual_section: 'login_choice',
		cookieName: 'forzieriBarState'
	},
	
	// Default Values
	closed_state: 30,
	normal_state: 100,
	wishlist_state: 95,
	prive_state: 50,
	signup_state: 245,
	signin_state: 245,
	nologged_state: 80,

	// * SECTIONS *
	// - welcome
	// - login_choice
	// - login
	// - signup
	// - wishlist
	// - prive
	actual_state: '',		// Actual Bar State (Closed - Opened)
	actual_section: '',		// Actual Section (The Actual Section before the New Choice)
	selected_section: '',	// Selected Section (The New Selected Section)
	return_section: '',	 	// Return Section (A Pointer to the Actual Section, with this I can know where return with Close Buttons)

	bar_animation: null,
	
	// Bar Tween Animation Handler
	bar_content: null,
	bar_content_div: null,
	login_div: null,
	login_div_complete: null,
	login_choice: null,
	wishlist_button: null,
	prive_button: null,
	loader: null,
	menu: null,
	items_container: null,
	welcome: null,
	wishlist_div: null,
	prive_div: null,
	close_btn: null,
	wishlist_num_item: null,
	basket_action_div: null,

	toggle_button_up: '/forzieriBar/img/double_black_up.gif',
	toggle_button_down: '/forzieriBar/img/double_black_down.gif',

	logged: false,

	_animation1: null,	// Wishlist Items
	_animation2: null,	// Wishlist Info Items
	_animation3: null,	// Num Items Div
	_animation4: null,

	itemsObj: {},	// Wishlist Items
	itemsObj2: {},	// Wishlist Info Items
	
	itemsInWishlist: null,

	initialize: function(options) {
		this.setOptions(options);
		
		this.element = $(this.options.element) || new Element('div', {id: this.options.element}).inject(document.body);
		this.logged = this.options.logged;
		this.actual_state = this.options.actual_state;
		this.actual_section = this.options.actual_section;
		
		var self = this;
		if ( Tools.isMobile() ) {
			this.addEvents({
				initialize: function() { self.onInitializeMobile(); },
				toggle: function() { self.onToggleMobile(); }
			});
		}
		
		var self = this,
		request = new Request.HTML({
			url: Tools.baseUrlRel('/forzieriBar/forzieriBarHTML.asp'),
			update: this.options.element,
			onSuccess: function() {
				self.fireEvent('initialize');
				self.init();
			}
		}).send();
	},

	init: function () {
		this.bar_content = $('forzieriBar_content');
		this.bar_content_div = $('fzb_content_div');
		this.login_div = $('fzb_login_1');
		this.login_div_complete = $('fzb_login_2');
		this.login_choice = $('fzb_login_choice');
		this.wishlist_button = $('fzb_wishlist_tab');
		this.prive_button = $('fzb_prive_tab');
		this.menu = $('fzb_primary_menu');
		this.loader = $('fzb_loader');
		this.items_container = $$('.items_container');
		this.welcome = $('fzb_welcome');
		this.wishlist_div = $('fzb_wishlist');
		this.prive_div = $('fzb_prive');
		this.close_btn = $$('#fzb_close a');
		this.itemsInWishlistDiv = $('itemsInWishlist');
		this.basket_action_div = $('fzb_basket_action');

		this.setButtons();
		this.resetItemsButtons();

		// Items in Wishlist
		this._setItemsInWishlist();

		this._setBarOpacity(this.closed_state);
		
		var statusCookie = Cookie.read(this.options.cookieName);
		if ( statusCookie == null || statusCookie == 'closed' ) {
			this._setStatusCookie('closed');
		} else {
			switch (statusCookie) {
				case 'wishlist':
					this.showWishlistAction();
					break;
				case 'prive':
					this.showPriveAction();
					break;
			}
		}
	},

	// Set Default Button Actions
	setButtons: function () {
		var self = this;
		
		// Menu Buttons
		this.wishlist_button.addEvent('click', function (e) {
			e.preventDefault();
			self.showWishlistAction();
		});

		// Prive Button may not exist
		if (this.prive_button) {
			this.prive_button.addEvent('click', function (e) {
				e.preventDefault();
				self.showPriveAction();
			});
		}

		// Close Button
		this.close_btn.addEvent('click', function (e) {
			e.preventDefault();
			self.toggleBox();
		});
	},

	// Set Items Buttons
	resetItemsButtons: function () {
		var self = this;

		// Reset
		this._animation1 = null;
		this._animation2 = null;

		// Set Animation
		var elements = $$('.items_container .item');
		var infoElements = $$('.items_container .item .info');

		// Set Num Item Div
		this.wishlist_num_item = $('num_item');

		// Animations
		this._animation1 = new Fx.Elements(elements, {
			link: 'cancel',
			duration: 100,
			transition: Fx.Transitions.Sine.easeOut
		});
		this._animation2 = new Fx.Elements(infoElements, {
			link: 'cancel',
			duration: 100,
			transition: Fx.Transitions.Sine.easeOut
		});
		this._animation3 = new Fx.Morph(this.wishlist_num_item, {
			link: 'cancel',
			duration: 100,
			transition: Fx.Transitions.Sine.easeOut
		});

		// Reset Animation Objects
		this.itemsObj = {};
		this.itemsObj2 = {};

		// Items
		elements.forEach(function (t, i) {

			// Set Infos
			$('info_' + i).setStyles({
				'opacity': 0,
				'display': 'block'
			});

			// Events
			t.removeEvents('mouseover');
			t.removeEvents('mouseout');

			t.addEvent('mouseover', function () {
				self.moveWishItem(this);
			});
			t.addEvent('mouseout', function () {
				self.hideWishItem(this);
			});

		});
	},

	// Show Wishlist
	showWishlistAction: function () {
		var height;
		
		// If not Logged toggle Login
		if (!this.logged) {
			this.toggleLoginAction();
			return;
		}

		// Status Cookie
		if (this.actual_state == 'closed') {
			this._setStatusCookie('wishlist');
		} else {
			this._setStatusCookie('closed');
		}

		// Hide ever, the basket_action
		this.basket_action_div.setStyle('display', 'none');

		// For Sections
		if (this.actual_state == 'closed' || this.selected_section != 'wishlist') {
			// Set State
			this.return_section = 'welcome';
			this.selected_section = 'wishlist';

			height = this.wishlist_state;

			this._animateBar(height);
			this.actual_state = 'opened';
			this.actual_section = this.selected_section;

			this.welcome.setStyle('display', 'none');
			// Prive Button may not exist
			if (this.prive_button) {
				this.prive_div.setStyle('display', 'none');
			}
			this.wishlist_div.setStyle('display', 'block');
			this._setCloseBtn(false);

			this._selectButton(this.wishlist_button);

		} else {

			this.selected_section = this.return_section;
			
			height = this.closed_state;
			
			this._animateBar(height);
			
			this.actual_state = 'closed';
			this.actual_section = this.selected_section;
			this.return_section = '';
			
			this.welcome.setStyle('display', 'block');
			this.wishlist_div.setStyle('display', 'none');
			this._setCloseBtn(true);
			
			this._selectButton(false);
		}
	},

	// Show Wishlist
	showPriveAction: function () {
		var height;
		
		// Status Cookie
		if (this.actual_state == 'closed') {
			this._setStatusCookie('prive');
		} else {
			this._setStatusCookie('closed');
		}

		// Hide ever, the basket_action
		this.basket_action_div.setStyle('display', 'none');

		// For Sections
		if (this.actual_state == 'closed' || this.selected_section != 'prive') {
			// Set State
			this.return_section = 'welcome';
			this.selected_section = 'prive';

			height = this.prive_state;

			this._animateBar(height);
			this.actual_state = 'opened';
			this.actual_section = this.selected_section;

			this.welcome.setStyle('display', 'none');
			this.wishlist_div.setStyle('display', 'none');
			this.prive_div.setStyle('display', 'block');
			this._setCloseBtn(false);

			this._selectButton(this.prive_button);

		} else {

			this.selected_section = this.return_section;

			height = this.closed_state;

			this._animateBar(height);

			this.actual_state = 'closed';
			this.actual_section = this.selected_section;
			this.return_section = '';

			this.welcome.setStyle('display', 'block');
			this.prive_div.setStyle('display', 'none');
			this._setCloseBtn(true);

			this._selectButton(false);
		}
	},

	// Toggle Login Choice
	toggleLoginAction: function () {
		var height;
		
		// Set State
		this.selected_section = 'login_choice';
		
		// Set opened or closed. here is simple
		if (this.actual_state == 'closed') {
			this.actual_state = 'opened';
			this._setCloseBtn(false);
			height = this.nologged_state;
		} else {
			this.actual_state = 'closed';
			this._setCloseBtn(true);
			height = this.closed_state;
		}
		
		this._animateBar(height);
		
		// Animate the login alert
		var elm1, elm2;
		
		if (this.login_div.getStyle('opacity') > 0) {
			elm1 = this.login_div;
			elm2 = this.login_div_complete;
			
			elm2.setStyles({
				'opacity': 0,
				'display': 'block'
			});

		} else {
			elm1 = this.login_div_complete;
			elm2 = this.login_div;
		}

		elm1.set('tween', {duration: 'short'}).fade('out');
		elm2.set('tween', {duration: 'short'}).fade('in');

		// Actual Section
		this.actual_section = 'login_choice';

	},

	showLoginAction: function () {

		// Set Height
		var height = this.signin_state;

		// Set State
		this.selected_section = 'login';

		// Hide login_choice and Menu
		this.login_choice.hide();
		this.menu.hide();

		// Show Loader
		this.loader.show();

		// Create Login Div
		var login_div = new Element('div', {
			'id': 'fzb_login'
		});
		this.bar_content_div.adopt(login_div);

		// Load Login Content
		this._loadContentAndPutIn(Tools.baseUrlRel('/forzieriBar/login.asp'), login_div, true);

		// Animate Bar
		this._animateBar(height);

		// Actual Section
		this.return_section = this.actual_section;
		this.actual_section = 'login';
		this.actual_state = 'opened';
	},

	showRegisterAction: function () {

		// Set Height
		var height = this.signup_state;

		// Set State
		this.selected_section = 'signup';

		// Hide login_choice and Menu
		this.login_choice.setStyle('display', 'none');
		this.menu.setStyle('display', 'none');

		// Show Loader
		this.loader.setStyle('display', 'block');

		// Create Login Div
		var signup_div = new Element('div', {
			'id': 'fzb_signup'
		});
		this.bar_content_div.adopt(signup_div);

		// Load Login Content
		this._loadContentAndPutIn(Tools.baseUrlRel('/forzieriBar/register.asp'), signup_div, false);

		// Animate Bar
		this._animateBar(height);

		// Actual Section
		this.return_section = this.actual_section;
		this.actual_section = 'signup';
		this.actual_state = 'opened';
	},

	toggleBox: function () {

		// Close
		if (this.actual_state == 'opened') {

			// Only for the Login Section
			if (this.actual_section == 'login_choice') {
				this.toggleLoginAction();
				return;
			}

			// UI Elements
			$('fzb_' + this.actual_section).setStyle('display', 'none');
			if (this.return_section != '') {
				$('fzb_' + this.return_section).setStyle('display', 'block');
			}
			this._setCloseBtn(true);

			// Reset
			this.actual_section = this.return_section;
			this.return_section = '';
			this.selected_section = '';

			var height = this.closed_state;
			this._animateBar(height);
			this.actual_state = 'closed';
			this.basket_action_div.setStyle('display', 'none');

			// Reset Buttons
			this._selectButton(false);

			// Status Cookie
			this._setStatusCookie('closed');
		}

		// Open for default the Wishlist
		else {
			this.showWishlistAction();
		}
	},

	moveWishItem: function (elm) {
		var self = this,
		
		id = $(elm).getProperty('id'),
		num = id.replace('fzb_item_', '').toInt();

		$$('.items_container .item').each(function (t, i) {

			// Position for Images
			if (i > num) {
				self.itemsObj[i] = {
					'left': 105
				};
			} else {
				self.itemsObj[i] = {
					'left': 0
				};
			}

			//Opacity for Infos
			if (i == num) {
				self.itemsObj2[i] = {
					'width': 80,
					'opacity': 1
				};
			} else {
				self.itemsObj2[i] = {
					'width': 0,
					'opacity': 0
				};
			}

		});

		// Start Animation
		this._animation1.start(this.itemsObj);
		this._animation2.start(this.itemsObj2);

		// Num Items
		this._animation3.start({
			'left': 100
		});

		// Add To Basket Button
		var form = $(elm).getChildren();
		form.forEach(function (t, i) {

			// The info DIV
			if ($(t).getProperty('class') == 'info') {

				var c = $(t).getChildren();
				c.forEach(function (t, i) {

					// The Form
					if ($(t).getProperty('class') == 'addToBasketForm') {
						// The Anchor
						var a = $(t).getChildren();
						a.forEach(function (t, i) {
							if (t.getProperty('class') == 'addToBasketBtn') {

								// Show Item
								$(t).setStyle('display', 'block');

							}
						});
					}

				});
			}
		});

	},

	hideWishItem: function (elm) {
		var self = this,
		id = $(elm).getProperty('id'),
		num = id.replace('fzb_item_', '').toInt();

		// for each elements, set styles
		$$('.items_container .item').each(function (t, i) {
			self.itemsObj[i] = {
				'left': 0
			};
			self.itemsObj2[i] = {
				'width': 0,
				'opacity': 0
			};
		});

		// Start Animation
		this._animation1.start(this.itemsObj);
		this._animation2.start(this.itemsObj2);

		// Num Items
		this._animation3.start({
			'left': 0
		});

		// Add To Basket Button
		var form = $(elm).getChildren();
		form.forEach(function (t, i) {

			// The info DIV
			if ($(t).getProperty('class') == 'info') {

				var c = $(t).getChildren();
				c.forEach(function (t, i) {

					// The Form
					if ($(t).getProperty('class') == 'addToBasketForm') {
						// The Anchor
						var a = $(t).getChildren();
						a.forEach(function (t, i) {
							if (t.getProperty('class') == 'addToBasketBtn') {
								$(t).setStyle('display', 'none');
							}
						});
					}
				});
			}
		});
	},

	changeWish: function (select) {
		var self = this;
		
		// Wishlist ID
		var wishlist_id = $(select).options[$(select).selectedIndex].value;

		// Styles
		$('fzb_wishlist').setStyle('display', 'none');
		this.loader.setStyle('display', 'block');

		// Request
		var request = new Request({
			method: 'post',
			url: Tools.baseUrlRel('/forzieriBar/getForzieriBarSession.asp'),
			evalScripts: true
		});

		request.onSuccess = function (responseText) {

			$('fzb_wishlist').empty();
			$('fzb_wishlist').set('html', responseText);
			self.loader.setStyle('display', 'none');
			$('fzb_wishlist').setStyle('display', 'block');

			self.resetItemsButtons();

			// Items in Wishlist
			self._setItemsInWishlist();
		};
		request.onFailure = function (xhr) {
			alert('Failure Error! Try Again Later.');
		};
		request.onException = function (headerName, value) {
			alert('Exception Error! Try Again Later.');
		};

		request.send('wishlist_id=' + wishlist_id);

	},

	// *********************
	// Add Item to Wish List
	// *********************
	additemToWishlist: function (formName, url) {
		var self = this;
		
		// Only if Logged
		if (this.logged) {

			// Open the Bar
			if (this.actual_state == 'closed' || this.selected_section != 'wishlist') {
				this.showWishlistAction();
			}

			// Styles
			$('fzb_wishlist').setStyle('display', 'none');
			this.loader.setStyle('display', 'block');

			// Form Send Replacement
			$(formName).set('send', {
				url: url,
				onComplete: function (responseText) {
					
					$('fzb_wishlist').empty();
					$('fzb_wishlist').set('html', responseText);
					self.loader.setStyle('display', 'none');
					$('fzb_wishlist').setStyle('display', 'block');
					
					self.resetItemsButtons();
					
					// Items in Wishlist
					self._setItemsInWishlist();
				}
			});
			$(formName).send();
		}
		// If not Logged
		else {
			if (this.actual_state == 'closed') {
				this.toggleLoginAction();
			}
		}
	},

	additemToWishlistAjax: function (url) {
		var self = this;
		
		// Only if Logged
		if ( this.logged ) {
			var request = new Request({
				url: url,
				evalScripts: true
			});
			
			// Open the Bar
			if (this.actual_state == 'closed' || this.selected_section != 'wishlist') {
				this.showWishlistAction();
			}

			// Styles
			$('fzb_wishlist').setStyle('display', 'none');
			this.loader.setStyle('display', 'block');

			request.onSuccess = function (responseText) {

				$('fzb_wishlist').empty();
				$('fzb_wishlist').set('html', responseText);
				self.loader.setStyle('display', 'none');
				$('fzb_wishlist').setStyle('display', 'block');

				self.resetItemsButtons();

				// Items in Wishlist
				self._setItemsInWishlist();

			};
			request.onFailure = function (xhr) {
				alert('Failure Error! Try Again Later.');
			};
			request.onException = function (headerName, value) {
				alert('Exception Error! Try Again Later.');
			};

			request.send('qty=1');
		}
		// If not Logged
		else {
			if (this.actual_state == 'closed') {
				this.toggleLoginAction();
			}
		}

	},

	addToBasket: function (id) {
		var self = this,
		form = $('addToBasketForm_' + id),
		url = $(form).getProperty('action');

		// Open the Bar
		if (this.actual_state == 'closed' || this.selected_section != 'wishlist') {
			this.showWishlistAction();
		}

		// Styles
		$('fzb_wishlist').setStyle('display', 'none');
		this.loader.setStyle('display', 'block');

		// Form Send Replacement
		$(form).set('send', {
			url: url,
			onComplete: function (responseText) {
				if (responseText == 'ok') {
					self.basket_action_div.setStyle('display', 'block');
					self.loader.setStyle('display', 'none');

					// Call to the default funciton for the basket update
					addBasketItems();
				} else {
					alert('Error Occured!');
				}
			}
		});

		$(form).send();
	},

	returnToWishlist: function () {
		$('fzb_wishlist').setStyle('display', 'block');
		this.loader.setStyle('display', 'none');
		this.basket_action_div.setStyle('display', 'none');
	},

	_animateBar: function (height) {
		var self = this;
		
		this._setBarOpacity(height);
		this.bar_animation = new Fx.Morph(this.bar_content, {
			duration: 'short',
			transition: Fx.Transitions.Sine.easeOut,
			onComplete: function() {
				self.fireEvent('toggle');
			}
		}).start({
			'height': height
		});
	},

	_setBarOpacity: function (height) {
		if (height == this.closed_state) {
			$$('#forzieriBar_table_ui .content').setStyles({
				'opacity': 0.9
			});
		} else {
			$$('#forzieriBar_table_ui .content').setStyles({
				'opacity': 1
			});
		}
	},

	_setStatusCookie: function (cookie) {
		Cookie.dispose(this.options.cookieName);
		Cookie.write(this.options.cookieName, cookie, {duration: 1, domain: Tools.cookieDomain, path: '/'});
	},

	_loadContentAndPutIn: function (url, element, evalScripts) {
		var self = this,
		
		request = new Request.HTML({
			method: 'post',
			url: url,
			evalScripts: evalScripts,
			update: element,
			onSuccess: function() {
				self.loader.hide();
				self.fireEvent('content');
			}
		}).send();
	},

	_destroyAndClose: function () {
		var el = $('fzb_' + this.actual_section);
		
		var _animation1 = new Fx.Tween(el, {
			duration: 300,
			transition: Fx.Transitions.Sine.easeOut,
			onComplete: function () {
				el.destroy();
			}
		}).start('opacity', 0);
		
		// Close Box and Reset States
		this.toggleBox();
		this._resetStates();
	},

	_resetStates: function () {

		// For Actual Section
		switch (this.actual_section) {

			// Login State
		case 'login_choice':
			this.login_div.setStyles({
				'opacity': 1,
				'display': 'block'
			});
			this.login_div_complete.setStyles({
				'opacity': 0,
				'display': 'none'
			});
			this.login_choice.setStyle('display', 'block');
			this.menu.setStyle('display', 'block');
			break;
		}

	},

	_selectButton: function (btn) {

		// Remove Selected to al button
		this.wishlist_button.removeClass('selected');

		// Prive Button may not exist
		if (this.prive_button) {
			this.prive_button.removeClass('selected');
		}

		// If I'm in a section
		if (false != btn) {
			// Add selected to the current button
			btn.addClass('selected');
		}
	},

	_setItemsInWishlist: function () {
		if (this.logged && (this.itemsInWishlist != null) && (this.itemsInWishlistDiv)) {
			this.itemsInWishlistDiv.set('text', this.itemsInWishlist);
		}
	},

	_setCloseBtn: function (close) {
		if (close) {
			this.close_btn.setStyle('background', "transparent url('" + this.toggle_button_up + "') center center no-repeat");
		} else {
			this.close_btn.setStyle('background', "transparent url('" + this.toggle_button_down + "') center center no-repeat");
		}
	},
	
	onInitializeMobile: function() {
		this.element.setStyles({
			position: 'absolute',
			bottom: 'auto'
		});
		this.followAlong = new followAlong(this.options.element);
	},
	
	onToggleMobile: function() {
		this.followAlong.follow();
	}
});


domReady( function() {
	var request = new Request({
		method: 'post',
		url: Tools.baseUrlRel('/forzieriBar/getLogged.asp'),
		onSuccess: function(responseText) {
			var options = {
				element: 'forzieriBar'
			};
			if ( responseText == 'YES' ) {
				options.logged = true;
				options.actual_section = 'welcome';
			}
			ForzieriBar = new ForzieriBarClass(options);
			ForzieriBar.addEvent('content', function() {
				$script.ready('privilege-club', function() {
					if ( typeof(privilegeClubPopup) != 'undefined' ) {
						privilegeClubPopup.attachFormEvents('#forzieriBar');
					}
				});
			});
		}
	}).send();
});
