
/* classes/smartbar.js */

 Ajax.Responders.register({
  onException: function(requester, exception) {
    console.log("received exception: " + exception.message);
  }
});

var Smartbar = Class.create();
		Smartbar.prototype = {
		  initialize: function(){
		    this.loadCategories();
		      if($("items_section") != null && $("items_section").visible()){
		        $("items_section").hide();
		      }
		      this.attachActions();

		  },

    setupLightbox: function() {
      if($('get_started') != null && $('get_started').visible()){
        $$('.guide_me').each(
  			      function(element){
  			        element.show();
  			        }
  			    );
        $('home_profile_lightbox').setStyle({visibility : 'visible'});
      }
    },

		closeSmartbar: function() {
			new Effect.BlindUp('pane'),{ duration: 0.625 };
		    $('home_profile_lightbox').setStyle({visibility : 'hidden'});
		},

		hideSmartbar: function() {
		
			new Effect.BlindUp('pane', {
			 duration: 0.625,
  			afterFinish : function(effect){
  			    $$('.guide_me').each(
  			      function(element){
  			        element.show();
  			        }
  			    );
			    }
			  });
			$('home_profile_lightbox').setStyle({visibility : 'hidden'});
      if(this.buttonSelected === '.view_categories'){
        setTimeout("$('choose_category').show()", 1000);
        setTimeout("$('items_section').hide()", 1000);
      }
  	  if ($('smartbar_selection_save_flag').innerHTML=='true') {
        window.location.replace('/mmh/home_profile/inventory_items');
  	  };
		},

		showSmartbar: function(){
			new Effect.BlindDown('pane', {
			 duration: 0.625,
  			afterFinish : function(effect){
            $('pane').setStyle("height: 311px");
  			    $$('.guide_me').each(
  			      function(element){
  			        element.hide();
  			        }
  			    );
			    }
			  });
			$('home_profile_lightbox').setStyle({visibility : 'visible'});
		},

		loadCategories: function(){

					 new Ajax.Request( '/mmh/api/taxonomies/products.json',
			    {
			      method:'get',
				 	  onSuccess: function(transport){
                  //var taxonomy=transport.responseJSON;
                  var JSONtext=transport.responseText;
                  if (JSONtext.isJSON){
						   // Show Guide Me flyout of Product Categories see guide_me_box.js
						   GuideMeBox.build(eval('(' + JSONtext + ")"));
                  } else {
                     console.log('JSON went wrong...');
                  }
				   },
			      onFailure: function(){ console.log('Something went wrong...') }
			    });
		},

		showCategories: function(){
				if($('get_started').visible()){
				  $('get_started').hide();
				  }
				$('choose_category').removeClassName('choose_category_off');
				//new Effect.Appear('choose_category',{
					//duration: 0.625 });
		},

		showProducts: function(){
				if($('get_started').visible()){
				  $('get_started').hide();
				  }
				new Effect.Appear('items_section',{
					duration: 0.625 });

				$('specialty_id').show();
		},

		bindSmartBarEvent: function(buttonSelector, action, event) {
      var self = this;
			event = event || 'click';
			$$(buttonSelector).each(function(e){
				e.observe(event, function(event) {
          if(self.buttonSelected === 'undefined' ||  self.buttonSelected !== '.view_categories'){
            self.buttonSelected = buttonSelector;
          }
					event.preventDefault();
					action();
				}.bindAsEventListener(this))
			});
		},

		attachActions: function(){
      var self = this;
		  this.setupLightbox();
			this.bindSmartBarEvent('.view_categories', function(){ self.showCategories() });
			this.bindSmartBarEvent('.view_products', function(){ self.showProducts() });
			this.bindSmartBarEvent('.guide_me', function(){ self.showSmartbar() });
			//this.bindSmartBarEvent('.hide_button', this.hideSmartbar);
			this.bindSmartBarEvent('.hide_guide_me', function(){ self.hideSmartbar() });
			this.bindSmartBarEvent('#smartbar_search', this.hideSmartbar, 'keyup');
			//this.showSmartbar();
		}
}

Event.observe(document, "dom:loaded", function(){
  var smartbar = new Smartbar;
});

(function($) {
  $(document).ready(function() {
    $.getJSON("/mmh/api/taxonomies/products/flat.json", function(data) {
      var mapped = data.map(function(e) { return {text: e} });
      $('input.autocomplete').jsonSuggest(mapped);
    });
    var smartbar = new Smartbar();
    $('input.autocomplete').focus(function(){
      smartbar.hideSmartbar();
    });
    //JQuery wzTooltip
    $(".tag_tooltip").each(function(){
      if($(this).text()) {
        var title = $(this).attr('title');
        $(this).wzTooltip( $( "<p/>" ).text( title ) );
      }
    });
    
    // Smartbar char counter
    $("#smartbar_search").charCounter(160, {
      container: "#smartbar_search_limit",
      format: "%1 characters remaining"
    });

  });
})(jQuery);

function disableEnterKey(e) {
  var key = window.event ? window.event.keyCode : e.which; //+ve for IE
  if(key == 13) {
    if(e.target.value.strip().length == 0) return false;
    if(document.getElementsByClassName('jsonSuggestResults')[0].style.display!="none") return false;
    $("dummy").focus(); // this dummy element receives the focus preventing multiple submits in case CR is presses several times(before page completes reloding)
  }
  return true;
}

function disableOnSubmit(){
  $('add_item_button').removeClassName('add_it').addClassName('disabled add_it_off');
  return true;
}
