var msSelectExport = {  
    addElementCache: new Array(),
    ExportTypes: [ 'Microsoft Office Excel 2007', 'Microsoft Office Excel CSV', 'Open Office CSV', 'XML'],
    ExportCTypes: [ 'xlsx', 'csv', 'csv', 'xml'],
    container: '',
    whatFormatToExport: 'csv',
    
    getEl: function (container, input) {     

      this.container = container;
      this.input = input;
      
      p_text = 'Export type';
      var div_element = new Element('div');  
      
      var ul_element = new Element('ul');
      
      ul_element.addClassName('element-list');
      
      div_element.appendChild(ul_element);
      
        
      Obj = this;
      
	  this.ExportTypes.each(function(s, i) {
		  
	    var option_el = new Element('li');      
	    option_el.id = 'form-elem-' + i;
	 
	    option_el.addClassName('form-element');
	    
	   
	    
	    option_el.innerHTML = '<div class="list-item-container"><div class="list-item-inner-container">' + s + '</div></div><div class="list-item-footer-container"><div class="list-item-footer-left-container"></div></div>';
	   // Obj.addElementCache[i + 'click'] = function(e) { Obj.clickListner(e);};
	    Event.observe(option_el, 'click', Obj.clickListner.bindAsEventListener(this));      
	    ul_element.appendChild(option_el);
	  });
    
      
      return [p_text, div_element, this.input, this.container];
    },
  
  clickListner: function(e) {
    var data = $A(arguments);    
    data.shift(); 
    
    var node = Event.element(e);
    
    c = 0;
    
    while(!$(node).hasClassName('form-element') && c < 10) {
      node = node.parentNode;
      c++;
    }
    
    $(msSelectExport.container).hide();
    
    msSelectExport.selectExport(e, node.id.substr(10));
  },
  
  click: function (srcTarget) {
	  
	  
  },
  
  selectExport: function(e, k) {
	// default action is to add a input box, which is key 0
	if (k == undefined) k = 0;
	
	msSelectExport.exportTypeSelected(msSelectExport.ExportCTypes[k]);
	
	msSelectExport.exportData(window.location.href + '/export', 'list', 'filter-1');
	
	
  },
  
  exportData: function(url, listId, filterName) {
	  param = ''; 
	  param += '&list_name=' + listId;
	 // if (typeof($(filterName)) != 'undefined' && $(filterName).value != '') {
	 //   param += '&filterValue=' + $(filterName).value;
	//  }
	    
	  orderByJSON = Object.toJSON(orderBy);  
	  param += '&orderBy=' + orderByJSON;
	  
	  param += '&to=' + this.whatFormatToExport;
	  
	  window.location.href= url + '?' + param;  
  },

  exportTypeSelected: function(what) {
	this.whatFormatToExport = what;
  }
}

