/*
	Class:    	Selects
	Author:   	Cédric Pellevillain
	Website:    http://www.devintact.com
	Version:  	0.0.1
	Date:     	06/16/2010
	Built For:  MooTools 1.2
*/

(function(){

    var $ = document.id;

    this.Selects = new Class({
    	
    	//implements
    	Implements: [Elements,Options,Events],
    
    	//options
    	options: {
        j: 0,	
    		select_div: [],
    		select: [],
    		select_display_div: [],
    		select_display_choice: [],
    		select_display_span: [],
    		select_display_div: [],
    		select_ul_bot: []
    	},
    	
    	//initialization
    	initialize: function(element,options) {
    		
        if(Browser.Engine.trident4) return false;
        this.setOptions(options);
    		if($type(element) == 'array') {
          this.options.element = element; 
        } else {
          this.options.element = [element]
        }
        
        this.selectBuild();
    		
    	},
    
    	selectBuild: function() {
    	
        this.options.element.each(function(item,j) {
        
          if(item.id == 'sthemes') {
            this.options.select_div_pos = item.getCoordinates(item.getParent('p'));
          } else {
            this.options.select_div_pos = item.getCoordinates('bg_footer');
          }  
          item.setStyles({'opacity':0, 'visibility': 'hidden', 'left':'-9999px', 'position':'absolute'});
          this.options.select_div = new Element('div',{'class':'select_div_'+j+' select_div','styles':{'left':this.options.select_div_pos.left,'top':this.options.select_div_pos.top}});
          this.options.select = new Element('ul',{'class':'select_ul_'+j+' select_ul'}); 
          this.options.select_display_div = new Element('div',{'class':'select_display_div_'+j+' select_display_div'});
          this.options.select_display_choice = new Element('span',{'class':'select_display_choice'});
          this.options.select_display_span = new Element('span',{'class':'select_display_arrow_'+j+' select_display_arrow'});
    
          this.options.select_display_choice.set('html',item.getChildren('option')[0].innerHTML);
          this.options.select_display_choice.inject(this.options.select_display_div);
          this.options.select_display_span.inject(this.options.select_display_div);
          
          this.options.li = new Array();
          item.getChildren().each(function(option,i) {
            if(i!=0) {
              this.options.li[i] = new Element('li',{'class':option.value,'html':option.innerHTML});
              this.options.li[i].addEvent('click', function() {
                this.options.select.hide();
                this.options.select_display_span.removeClass('clicked');
                if(this.options.li[i].className != '') { window.location.href = 'http://'+this.options.li[i].className; }
              }.bind(this));
            }
          }.bind(this));
          
          this.options.li.each(function(li) {
            if($(li) != null) {
              $(li).inject(this.options.select,'bottom');
            }
          }.bind(this));
          this.options.select_ul_bot = new Element('li',{'class':'select_ul_bot'});
          this.options.select_ul_bot.inject(this.options.select,'bottom');
          
          this.options.select.inject(this.options.select_div);
          this.options.select_display_div.inject(this.options.select_div);
          this.options.select_div.inject(item.getParent());
        
          this.options.boundClickOutsideListener = this.clickOutsideListener.bind(this);
          this.options.footer_padding = $$('#bg_footer div.footer')[0].getStyle('padding-bottom'); 
        
          this.options.select_display_div.addEvents({
            'click': function(event) { 
              event.stop();            
              this.options.item = item.id;       
              this.options.z = j;   
              if($$('.select_display_arrow_'+j)[0].hasClass('clicked')) {
                if(this.options.item != 'sthemes') {
                  $$('#bg_footer div.footer')[0].setStyle('padding-bottom',this.options.footer_padding);
                }
                $$('.select_display_arrow_'+j)[0].removeClass('clicked');
                $$('.select_ul_'+j)[0].hide();
                this.window_Events('add');
              } else {
                
                $$('.select_ul').each(function(it,p) {            
                  if(p != j && $(it).getStyle('display') == 'block') {
                    $$('.select_ul_'+p)[0].hide();
                    if($$('.select_display_arrow_'+p)[0].hasClass('clicked')) {
                      $$('.select_display_arrow_'+p)[0].removeClass('clicked');
                    }
                    if(it.getParents('div#sthemes') != null) {                
                      $$('#bg_footer div.footer')[0].setStyle('padding-bottom',this.options.footer_padding);
                    }
                    this.window_Events('remove');
                  }
                }.bind(this));
                     
                if(item.id != 'sthemes') {            
                  $$('#bg_footer div.footer')[0].setStyle('padding-bottom',($$('#bg_footer ul.select_ul')[0].getDimensions().height-40)+'px');
                } 
                $$('.select_display_arrow_'+j)[0].addClass('clicked');
                $$('.select_ul_'+j)[0].show();
                this.window_Events('add');
              } 
            }.bind(this)
          }); 
        
        }.bind(this));
           
    	},
    	
    	window_Events: function(action) {
          if(Browser.Engine.trident) {
            var body_s = 'document.documentElement';
          } else {
            var body_s = 'window'; 
          }
          eval(body_s + '.' + action + 'Event("click", this.options.boundClickOutsideListener);');
      },
    	
    	clickOutsideListener: function(event) {
          $$('.select_ul_'+this.options.z)[0].hide();
          
          if($$('.select_display_arrow_'+this.options.z)[0].hasClass('clicked')) {
            $$('.select_display_arrow_'+this.options.z)[0].removeClass('clicked');
          }
          
          if(this.options.item != 'sthemes') {
            $$('#bg_footer div.footer')[0].setStyle('padding-bottom',this.options.footer_padding);
          }
          
          this.window_Events.bind(this,'remove');
      }
    	
    });

})();

