/*
Class: Rako Showcase [Built on MooTools framework: www.mootools.net]
Author: Martin Jezek 2011
Version: 1.3
*/  
var RakoShowcase = new Class ({
  // Options
  Implements : Options,
  options : { 
    showcase_id : "",
    wheeler_id : "",
    heading_id : "",
    left_id : "",
    right_id : "",
    width : 0,
    item_width : 141,
    transition : "quad:out",
    duration : "normal",
    heading_transition : "elastic:out",
    heading_duration : "normal",
    delay: 4000,
    stop_wheeling : false,
    full_showcase : true,
    preset_product_index : false,
    title_left_offset : -40
  },
  
  // Initialize
  initialize : function(options){
    this.setOptions(options);
    if($(this.options.showcase_id)){ 
      this.initShowcase();
      this.initWheeler();
      if(this.options.full_showcase){
        this.initHeading();
      }
      if(this.options.preset_product_index){
        this.presetProductIndex();
      }
      this.initEvents();
      if(this.options.full_showcase){
        if(this.wheeler_count > 1){
          this.looper = this.wheeling.periodical(this.options.delay, this);
        }
      }
    }
  },

  now : 0,
  busy : false,
  clickWay : "none",
    
  // Methods
  initShowcase : function(){
    this.showcase = $(this.options.showcase_id);
    this.items = this.showcase.getElements(".item");
    this.items_count = this.items.length;
    this.item_width = this.options.item_width;
    
    if(this.items_count > 7){
      
      /* copy 7 items to left */
      var temp = new Element("div");
      for(var x = this.items_count-1; x >= this.items_count-7; x--){
        this.items[x].clone().inject(temp, "bottom");
      }
      var temp_items = temp.getElements(".item");
      for(var y = 0; y < 7; y++){
        temp_items[y].inject(this.showcase, "top");
      }
      
      /* resize showcase */
      this.items_copy = this.showcase.getElements(".item");
      this.items_copy_count = this.items_copy.length;
      this.showcase.setStyle("width", this.items_copy_count*this.item_width);
      
      /* set first to center */
      this.now = 4;
      this.showcase.setStyle("left", -this.now*this.item_width);      
      this.items_copy[this.now+3].addClass("act");
    } else {
      $(this.options.left_id).setStyle("display", "none");
      $(this.options.right_id).setStyle("display", "none");
      this.items[this.now].addClass("act");
    }
    
    this.showcase_tween = new Fx.Tween(this.showcase,{
      transition : this.options.transition,
      property : "left",
      duration : this.options.duration
    });

  },

  initWheeler : function(){
    this.wheeler = $(this.options.wheeler_id);
    this.wheeler_items = this.wheeler.getElements(".item");
    this.wheeler_count = this.wheeler_items.length;
    this.wheeler.setStyle("width", this.wheeler_count * 100 + "%");
    this.wheeler_items.setStyle("width", 100 / this.wheeler_count + "%");
    if(Browser.opera){
			this.wheeler_items.setStyle("width", this.wheeler.getWidth() / this.wheeler_count);
		}
    if(this.options.stop_wheeling){
      this.stop_wheeling();
    }
  },

  initHeading : function(){
    this.heading = $(this.options.heading_id);
    this.heading_items = this.heading.getElements("h2");
    this.heading_tween = new Array();
    for(var x = 0; x < this.heading_items.length; x++){
      this.heading_tween[x] = new Fx.Tween(this.heading_items[x],{
        transition : this.options.heading_transition,
        property : "bottom",
        duration : this.options.heading_duration
      });
    }
  },

  initEvents : function(){
    if($(this.options.left_id)){
      $(this.options.left_id).addEvent("click", this.leftClick.bindWithEvent(this));
    }
    if($(this.options.right_id)){
      $(this.options.right_id).addEvent("click", this.rightClick.bindWithEvent(this));
    }
    if($("showcase_title")){
      this.items_copy.each(function(item, index){
        item.addEvent("mouseenter", this.showTitle.bind(this, {"item": item, "index" : index}));
        item.addEvent("mouseleave", this.hideTitle.bind(this, {"item": item, "index" : index}));
      }, this);      
    }
    this.addKeyDownEvent();
  },

  addKeyDownEvent : function(){
    if(Browser.Engine.trident){
      $$("body")[0].addEvent("keydown", this.addKeyControl.bindWithEvent(this));
    } else {
      window.addEvent("keydown", this.addKeyControl.bindWithEvent(this));
    }
  },

  addKeyControl : function(event){
    if($("xbox_wall")){
      if($("xbox_wall").getStyle("display") == "none"){
        if (event.key == "right"){
          this.rightClick();
        } else if (event.key == "left"){
          this.leftClick();
        }
      }
    } else {
      if (event.key == "right"){
        this.rightClick();
      } else if (event.key == "left"){
        this.leftClick();
      }
    }
  },

  leftClick : function(){
    this.clickWay = "left";
    this.wheeling();
    this.clickWay = "none";
    this.wheeler.addClass("stop_wheeling");
    $(this.options.left_id).blur();
    return false; 
  },

  rightClick : function(){
    this.clickWay = "right";
    this.wheeling();
    this.clickWay = "none";
    this.wheeler.addClass("stop_wheeling");
    $(this.options.right_id).blur();
    return false; 
  },
  
  wheeling : function(){
    if(!(this.wheeler.hasClass("stop_wheeling")) || this.clickWay == "left" || this.clickWay == "right"){
      if(!this.busy){      
        this.busy = true;
        
			  if(Browser.opera){
					this.wheeler_items.setStyle("width", this.wheeler.getWidth() / this.wheeler_count);
				}

        if(this.clickWay == "left"){
          if(this.now == 0){
            //alert("move to end");
            this.now = this.items_copy_count - 7;
            this.showcase.setStyle("left", -this.now*this.item_width);
            this.now--;
          } else {
            this.now--;
          }
        } else {
          if(this.now == this.items_copy_count - 7){
            //alert("move to start");
            this.now = 0;
            this.showcase.setStyle("left", -this.now*this.item_width);
            this.now++;
          } else {
            this.now++;
          }
        }
        
        // move showcase
        if(this.options.full_showcase){
          this.showcase_tween.start(this.showcase.getStyle("left"), -this.now*this.item_width);
        } else {
          this.showcase_tween.start(this.showcase.getStyle("left"), -this.now*this.item_width).chain(function(){ // busy wait 
            this.busy = false;
          }.bind(this));;
        }
        this.items_copy.removeClass("act");
        this.items_copy[this.now+3].addClass("act");
        
        if(this.options.full_showcase){
          // change heading
          var old_real_index = this.heading.getElement("h2.act").id.replace("heading_item_", "");
          this.heading_tween[old_real_index].start(-20, -320).chain(function(){ // busy wait 
            this.heading_items[old_real_index].removeClass("act");
            this.busy = false;
          }.bind(this));
          var new_real_index = this.items_copy[this.now+3].getElement("h2").className.replace("item_", "");
          this.heading_items[new_real_index].addClass("act");
          this.heading_tween[new_real_index].start(-320, -20);
    
          // move wheeler      
          this.wheeler.setStyle("left", -new_real_index * 100 + "%" );
        }
      }
    }
  },

  stop_wheeling : function(){
    if(!this.wheeler.hasClass("stop_wheeling")){
      this.wheeler.addClass("stop_wheeling");
    }
  },

  start_wheeling : function(){
      this.wheeler.removeClass("stop_wheeling");
  },
  
  showTitle : function(showcase){
  	this.stop_wheeling();
    $("showcase_title").setStyle("left", showcase.item.getCoordinates().left + this.options.title_left_offset);
    $("showcase_title_heading").set("html", showcase.item.getElement("h2 a").get("html"));
    $("showcase_title_content").set("html", showcase.item.getElement(".title").get("html"));
    $("showcase_title").addClass("act");
  },
  
  hideTitle : function(showcase){
    $("showcase_title_heading").set("html", "");
    $("showcase_title_content").set("html", "");
    $("showcase_title").removeClass("act");
  },
  
  presetProductIndex : function(){
  	var index_value = this.showcase.className.substr(8);
		if(index_value != ""){
			var index = parseInt(index_value);
		  if(this.items_count > 7){
		
		  	if((this.items_count-index) > 2){
					this.now = index+3;
				} else {
					this.now = 3-(this.items_count-index);
				}
		
		    this.items_copy.removeClass("act");
		    this.items_copy[this.now+3].addClass("act");
		    this.showcase.setStyle("left", -this.now*this.item_width);
		  } else {
		    this.items.removeClass("act");
		    this.items[index-1].addClass("act");
			}
		} else {
			if(this.items_count > 7){
		    this.items_copy.removeClass("act");
			} else {
		    this.items.removeClass("act");
			}
		}
	}
   
});
