/*
Class: RakoPopUp [Built on MooTools framework: www.mootools.net]
Author: Martin Jezek 2011
Version: 1.0
*/  
var RakoPopUp = new Class ({
  // Options
  Implements : Options,
  options : { 
    product_class : "",
    product_info_class : "",
    popup_id : "",
    top_offset : 0,
    left_offset : 0
  },
  
  // Initialize
  initialize : function(options){
    this.setOptions(options);
    if($$("." + this.options.product_class).length > 0){
      this.initPopUp();
      this.initEvents();
    }
  },
    
  // Methods
  initPopUp : function(){
    this.products = $$("." + this.options.product_class);
    this.products_info = $$("." + this.options.product_info_class);
    this.popup = new Element("div", {
      "id" : this.options.popup_id
    });
    this.popup.inject(document.body);
    this.popup.set("html", '<div id="popup_image"></div><div id="popup_info"></div><span class="pul"></span><span class="pur"></span><span class="put"></span><span class="pub"></span><span class="pult"></span><span class="purt"></span><span class="pulb"></span><span class="purb"></span><!--a id="popup_add" href="">+<span class="ir"></span></a-->');
  },
  
  initEvents : function(){
    //show popup
    this.products.each(function(product, index){
      product.getElement("img").addEvent("mouseenter", this.showPopUp.bind(this, {"product" : product, "index": index}));
    }, this);
    //hide popup
    this.popup.addEvent("mouseleave", this.hidePopUp.bind(this));
  },
  
  showPopUp : function(show){
    if(this.products[show.index].getElement("img")){
      this.popup.setStyle("left", show.product.getCoordinates().left + this.options.left_offset);
      this.popup.setStyle("top", show.product.getCoordinates().top + this.options.top_offset);
      this.popup.addClass("act");
      var popup_href = this.products_info[show.index].getElement("a").href;
      var popup_image = '<a href="' + popup_href + '">' + this.products[show.index].get("html").replace("_thumb", "") + '</a>';
      $("popup_image").set("html", popup_image);
      $("popup_info").set("html", this.products_info[show.index].get("html"));
      /*$("popup_add").href = popup_href;*/
    }
  },
  
  hidePopUp : function(){
    this.popup.removeClass("act");
  }

});
