(function($){
	$.fn.tstoolbar = function (options) {
		// override default options
		var options = $.extend({}, $.fn.tstoolbar.options, this.options);
		
		var self = this;
		
		return this.each(function(){
			
			var toolbar = self;
			var search = $('#search', toolbar);
			var login = $('#login', toolbar);  
			var language = $('#language', toolbar);
			
			var showLanguage = $("#show_language");
			var closeLanguage = $("#close_language", toolbar);
			var showLogin = $("#show_login");
			var closeLogin = $("#close_login", toolbar);
			var showSearch = $("#fake_search");
			var searchField = $("#site_search");
			var universalClose = $(".close", toolbar);
			
			function tsToolbar(openButton, closeButton, element, toolbarHeight) {
				// if toolbar is closed, open it with current element
				if(!toolbar.data('element')) {
					toolbar.data('element', element.attr('id'));
					toolbar.data('button', closeButton.attr('id'));
					openButton.hide();
					element.show();
					toolbar.css("height","0");
					closeButton.show();
					toolbar.stop().animate({
						height: toolbarHeight
					}, 500, function () {
//						element.fadeIn();
					});	
				// if an element is open
				} else {
					// if it's the one associated with the button, close it
					if(toolbar.data('element') == element.attr('id')) {
						toolbar.removeData('element');
						toolbar.removeData('button');
						toolbar.stop().animate({
							height: 0
						}, 500, function (){
							element.hide();
							showLanguage.show();
							showLogin.show();
							showSearch.show();
						});
						
						// if it's another one, hide the old one, show the new one
					} else {
						$('#' +toolbar.data('element')).hide();
						if(toolbarHeight != $('#' +toolbar.data('element')).outerHeight(true)) {
							toolbar.animate({
								height: toolbarHeight
							});
						}
						toolbar.data('element', element.attr('id'));
						showLanguage.show();
						showLogin.show();
						showSearch.show();
						$('#search_form_small button[name="search_btnSubmit"]').show();
						element.fadeIn();
						$('#' +toolbar.data('button')).fadeIn();
						openButton.fadeOut();
					}
				}
			};

			showLanguage.click(function() {
				tsToolbar($(this), closeLanguage, language, options.language.height);
			});
			
			showLogin.click(function() {
				tsToolbar($(this), closeLogin, login, options.login.height);
			});
				
			closeLanguage.click(function() {
				tsToolbar(showLanguage, $(this), language);
			});
			
			closeLogin.click(function() {
				tsToolbar(showLogin, $(this), login);
			});
			// TODO 
			showSearch.click(function() {
				if(toolbar.data('element')) {
					$('#' + toolbar.data('element')).hide();
					if(170 != $('#' +toolbar.data('element')).outerHeight(true)) {
						toolbar.animate({
							height: options.search.height
						});
					}
					toolbar.data('element', search.attr('id'));
					toolbar.data('button', showSearch.attr('id'));
					showSearch.hide();
					$('#search_form_small button[name="search_btnSubmit"]').hide();
					showLanguage.show();
					showLogin.show();
					search.fadeIn();
					searchField.css({
						width: 520,
						height: 35
					});
					searchField.trigger('focus');
				} else {
					toolbar.data('element', search.attr('id')).data('button', showSearch.attr('id'));
					$(this).hide();
					$('#search_form_small button[name="search_btnSubmit"]').hide();
					search.show();
					toolbar.css("height","0").stop().animate({
						height: options.search.height
					}, function() {	
					});
					searchField.animate({
						width: 520,
						height: 35
					}, 500, function () {
						searchField.trigger('focus');
					});
				}
			});
			
			$('.close_search').click(function () {
				toolbar.removeData('element');
				toolbar.removeData('button');
				searchField.stop().animate({
					width: 155,
					height: 16
				});
				toolbar.stop().animate({
					height: 0
				}, 500, function () {
					search.hide();
					showSearch.show();
					$('#search_form_small button[name="search_btnSubmit"]').show();
					showLanguage.show();
					showLogin.show();
				});
			});
			
			universalClose.click(function () {
				element = toolbar.data('element');
				button = toolbar.data('button');
				tsToolbar(null, $("#" +button), $("#" +element));
			});
		});
	};
	
	$.fn.tstoolbar.options = {
		search: { height: 170 },
		language: { height: 320 },
		login: { height: 170 }
	};
	
})(jQuery);
