
/* classes/home_profile/guide_me_list.js */

 
function oc(a)
{
  var o = {};
  for(var i=0;i<a.length;i++)
  {
    o[a[i]]='';
  }
  return o;
}

/* classes/home_profile/guide_me_list.js */

var ChildPlaceholder = Class.create({
  initialize : function(categoryId, childData, guideMeBox) {
    this.categoryId = categoryId;
    this.guideMeBox = guideMeBox;
    this.childData = childData;
  },
  
  show : function() {
    var childBox = this.guideMeBox.childBoxen.get(this.categoryId);
    //if(childBox == null || childBox == undefined){
    childBox = ChildGuideMeBox.build(this.guideMeBox, this.childData, this.categoryId);
    this.guideMeBox.childBoxen.set(this.categoryId, childBox);
    //}
    childBox.show();
  }
});

/*------------------------ Parent Box Start ---------------*/
var GuideMeBox = Class.create({
	initialize: function(taxonomy) {
		this.list = $('guide_me_box');
		this.container = $('categories_container');
		this.select = $('choose_product_category').down('select');
		this.taxonomy = taxonomy;
		this.childBoxen = new Hash();
    this.total_categories = 0;
		this.buildParentBox();
		var slide_by_cols = 1;
		var smartbar_columns = ($("mini_smartbar") != null) || ($("mini_smartbar") != undefined) ? 2 : 3;
		setup_carousel("smartbar_categories", smartbar_columns, slide_by_cols,this.categories.length);
	},

	show : function() {
		this.list.show();
	},

	hide : function() {
		this.list.hide();
	},

	buildParentBox: function(){
		// build categories
    this.categories = $A();
		var category_list = this.taxonomy.children.reject(function(node) { return node.name == "Other" } );

		if($('browse_products_link')){
      var browse_products_link=$('browse_products_link');
      browse_products_link.observe('click', function(event) {
        event.preventDefault();
        $$('.child_guide_me_box').each(
          function(item){
	          item.hide();
        });
        GuideMeBox.instance.categories.first().show();
      }.bindAsEventListener(this));
    };
    if($('browse_products_section')){
      var browse_products_section=$('browse_products_section');
      browse_products_section.observe('click', function(event) {
        event.preventDefault();
        $$('.child_guide_me_box').each(
          function(item){
	          item.hide();
        });
        GuideMeBox.instance.categories.first().show();
      }.bindAsEventListener(this));
    };

    this.select.observe('change', function(event) {
      event.preventDefault();
      var element = event.element();
      if(element.selectedIndex == 0){
          $('choose_category').show();
          $('items_section').hide();
          $('choose_category').removeClassName('choose_category_off');
          return
      }
      $$('.child_guide_me_box').each(
        function(item){
            item.hide();
      });
      var option = element.options[element.selectedIndex];
      if (option.category) option.category.show();
    }.bindAsEventListener(this));

    category_list.withColumns(6, function(column_category_list, column_start_index, column_end_index) {

      var column_list_item_element = new Element('li');
      column_list_item_element.addClassName('carousel_item');
      this.list.insert({bottom:column_list_item_element});
      var column_list_element = new Element('ul');
      column_list_element.addClassName('product_category_list');
      column_list_item_element.insert({bottom:column_list_element});
      var first_parent = category_list[0];
	  
      column_category_list.each(function(child) {
        var categoryId = "category" + child.id;
        var childPlaceholder = new ChildPlaceholder(categoryId, child, this);

        var category_option = new Element( 'option', {
          'id' : 'id' + categoryId}).update(child.name);
        category_option.category = childPlaceholder;
        category_option.childTaxonomyNodes = child.children;

        var anchor = new Element( 'a', {
          'href' 	: "#" + categoryId,
          'id' 		: categoryId }).update(child.name.length>23 ? child.name.substring(0,23)+"..." : child.name);

        anchor.wzTooltip(child.name);

        anchor.childTaxonomyNodes = child.children;
        anchor.observe('click', function(event) {
          event.preventDefault();
          $$('.child_guide_me_box').each(
            function(item){
	            item.hide();
          });
          childPlaceholder.show();
        }.bindAsEventListener(this));

        column_list_element.insert( {
          bottom:	new Element( 'li', { 'id': child.id } ).update(anchor)
        });

        this.select.insert( {
          bottom:	category_option
        });

        this.categories.push(childPlaceholder); 
        
      }.bind(this));
    }.bind(this));    
    //this.categories.first().show();
    $("items_section").hide();
	}
});
GuideMeBox.instance = null;
GuideMeBox.build = function(taxonomy) {
  if (GuideMeBox.instance == null)
    GuideMeBox.instance = new GuideMeBox(taxonomy);
}

setup_carousel = function(local_carousel_id, smartbar_columns, slide_by_cols, total_items){
    var carousel_id = local_carousel_id;
    params = {
      slide_by: slide_by_cols,
      window_size: smartbar_columns,
      total_per_items: total_items,
      per_item: 6
    };
    callbacks = {};
    smartbarCarousel = new BaseCarousel(carousel_id, params, callbacks);
    smartbarCarousel.setUp();
}

Array.prototype.withColumns = function(rows_per_column, column_action) {
  var column_start_index = 0;
  var next_column_start_index = column_start_index + rows_per_column;
  var column_end_index = Math.min(next_column_start_index - 1, this.length);
  while (column_start_index < this.length) {
    var sub_a = this.slice(column_start_index, column_end_index + 1);
    column_action(sub_a, column_start_index, column_end_index);
    column_start_index = next_column_start_index;
    next_column_start_index = column_start_index + rows_per_column;
    column_end_index = Math.min(next_column_start_index - 1, this.length);
  }
}

var selected_items=[];

/*------------------------ Child Box Start ---------------*/
var ChildGuideMeBox = Class.create({
	initialize : function(parentBox, category, categoryId) {
		this.parentBox = parentBox;
		this.category = category;
    this.categoryId = categoryId;
		this.buildUL();
		var slide_by_cols = 1;
		var smartbar_columns = ($("mini_smartbar") != null) || ($("mini_smartbar") != undefined) ? 2 : 3;
		setup_carousel("smartbar_items", smartbar_columns, slide_by_cols,this.category.children.length);
		this.addInventoryItem();
	  this.select = $('choose_product_category').down('select');
		this.select.show();
	},

	buildUL: function() {
	  this.list = new Element('ul');
	  this.list.addClassName('child_guide_me_box carousel_items product_category_block clearfix');
	  $('child_container').update(this.list);
	  //$('child_container').insert({top:this.list});
	  //$('child_container').appendChild(this.list);
    
    selected_items=eval("["+ $('smartbar_selection').innerHTML + "]");


    var category_header = $('current_category');
    category_header.update(this.category.name);

		var items_list = this.category.children;
    items_list.withColumns(6, function(column_items_list, column_start_index, column_end_index) {

    var column_list_item_element = new Element('li');
    column_list_item_element.addClassName('carousel_item');
    this.list.insert({bottom:column_list_item_element});

    var column_list_element = new Element('ul');
    column_list_element.addClassName('product_category_list');
    column_list_item_element.insert({bottom:column_list_element});

		column_items_list.each(function(child) {
		  var element = new Element( 'li', { 'id': child.id });

		  element.update('<a href="/mmh/home_profile/inventory_items" class="item_link" id="'+child.id+'">'+ (child.name.length>23 ? child.name.substring(0,23)+"..." : child.name) +"</a>");

      element.wzTooltip(child.name);
      
      column_list_element.insert({	bottom: element });

      item_link=element.firstDescendant();
      if (child.name in oc(selected_items)) {
        selected_items[selected_items.length]=child.name;
        item_link.style.overflow="hidden";
        //item_link.style.paddingLeft="4px";
        item_link.addClassName("item_selected");
        //item_link.insert({before:'<img id="list_check" src="/mmh/images/task_list/blue_chk.gif" style="display:block;position:relative;top:6px;*top:21px;float:left;" border="0"/>'});       
      }

			}.bind(this));
		}.bind(this));
	},

	addInventoryItem : function(){
	  $$('.item_link').each(

	    function(item){
        item.observe('click', function(event){
	        var element = event.element();
	        if (element.hasClassName('item_link')){
	          event.stop();
            
            /*
            var form = new Element('form', {
              'action': '/mmh/home_profile/inventory_items',
              'method': 'post'
            });
            
            var product_type_id_input = new Element('input');
            product_type_id_input.type = 'hidden';
            product_type_id_input.name = 'home_profile_inventory_item[product_type_id]';
            product_type_id_input.value = item.id;


            var name_input = new Element('input');
            name_input.type = 'hidden';
            name_input.name = 'home_profile_inventory_item[name]';
            name_input.value = item.innerHTML;

            form.insert({bottom:product_type_id_input});
            form.insert({bottom:name_input});
            $(document.body).insert({bottom: form});
            */
            //Omniture
            s.events='event15'; 
            category = $('specialty_id').options[$('specialty_id').selectedIndex].innerHTML;
            s.eVar17= "Guide : Inventory: " + category + ": " + item.innerHTML;
            void(s.t());

            if(logged_in) {
              if (!element.hasClassName('item_selected')) {
              
                // let's set the flag first, so that page can be redirected on close
                $('smartbar_selection_save_flag').innerHTML="true";
                
                selected_items[selected_items.length]=item.id;
                
                /* show message */
                element.hide();
                element.insert({before:"<span id='added_message_"+item.id+"' class='added_message'>You’ve added this item.</span>"});
                
                element.style.overflow="hidden";
                //element.style.paddingLeft="4px";
                element.addClassName("item_selected");
                new Ajax.Request('/mmh/home_profile/inventory_items',{
                                onSuccess:function(response) {
                                  //alert('yeah!');
                                },
                                parameters:{
                                  "home_profile_inventory_item[product_type_id]":item.id,
                                  "home_profile_inventory_item[name]":item.innerHTML
                                  }
                                });

                setTimeout(function(){
                  $('added_message_'+item.id).remove();
                  //element.insert({before:'<img src="/mmh/images/task_list/blue_chk.gif" style="display:block;position:relative;top:6px;*top:21px;float:left;" border="0"/>'});
                  element.show();
                },1500);
                
              } else {
                /* commented out because it takes forever for alert to show */
                //alert('item already selected!');
              }
            } else {
              var modal = new MMH.LoginModal(item);
              modal.form_element = form;
              modal.open();
            }
				  }
			  });

		  });
	},
 
	show : function() {
    this.select.down("option#id" + this.categoryId).selected = true;
    this.list.show();
    $("items_section").show();
    $("choose_category").hide();
	}
});
ChildGuideMeBox.build = function(parentBox, category, categoryId) {
	return new ChildGuideMeBox(parentBox, category, categoryId);
}

