var printRecipe = new Class({

  /**
   * Inicializace objektu
   *
   * @param array options  konfigurace
   */
  initialize: function(options) {
    if (!options || !options['cssHref'] || !options['windowMessage']) return;
    this.cssHref = options['cssHref'];
    this.windowMessage = options['windowMessage'];
    this.showWindow();
    this.printRecipe();
  },


  /**
   * Prepnuti tiskoveho stylu, tisk receptu
   */
  printRecipe: function() {
    this.disabledElements = Array();
    var head = $$('head')[0];
    //projde vsechny tagy v head
    $each(head.getChildren(), function(element){
      if (element.get('tag') == 'link' && element.type && element.type.toLowerCase().trim() == 'text/css' && element.media && element.media.split(',').contains('print')) {
        //vypnuti tiskoveho stylu
        element.disable = true;
        this.disabledElements.push(element);
      }
    },this);
    //vlozeni tiskoveho stylu pro tisk samotneho receptu
    this.printElement = new Element('link', {
      rel: 'stylesheet',
      type: 'text/CSS',
      media: 'print',
      href: this.cssHref
    });
    head.adopt(this.printElement);
    window.print(); //tisk
  },


  /**
   * Vytvoreni a zobrazeni okna pro potvrzeni tisku
   */
  showWindow: function() {
    this.frame = new Element('div', {
      id: 'tx_odtatraproducts_print_recipe_frame',
      styles: {
        opacity: 0.6,
        position: 'absolute',
        left: 0,
        top: 0,
        backgroundColor: '#000',
        width: window.getScrollWidth(),
        height: window.getScrollHeight()-1,
        zIndex: 5000
      }
    });
    $$('body').adopt(this.frame);
    this.win = new Element('div', {
      id: 'tx_odtatraproducts_print_recipe_window',
      styles: {
        position: 'absolute',
        padding: 10,
        textAlign: 'center',
        backgroundColor: '#fff',
        border: '2px solid black',
        width: 300,
        height: 100,
        zIndex: 5001
      }
    });
    this.windowRePosition();
    var p = new Element('p');
    p.innerHTML = this.windowMessage;
    this.win.adopt(p);
    var button = new Element('input', {
      type: 'button',
      value: 'OK'
    });
    button.addEvent('click',this.exitPrintRecipe.bindWithEvent(this));
    this.win.adopt(button);
    $$('body').adopt(this.win);
    window.addEvent('resize',this.windowRePosition.bindWithEvent(this));
    window.addEvent('scroll',this.windowRePosition.bindWithEvent(this));
  },


  /**
   * Prepocitani pozice okna po zmene okna prohlizece nebo scrolovani
   */
  windowRePosition: function() {
    this.win.setStyles({
      left: (window.getScrollLeft()+(window.getWidth()/2) - 150),
      top: (window.getScrollTop()+(window.getHeight()/2) - 50)
    });
  },


  /**
   * prepnuti na puvodni tiskovy styl
   */
  exitPrintRecipe: function() {
    //zruseni okna
    window.removeEvents('resize');
    window.removeEvents('scroll');
    var morpHideTooltipObject = new Fx.Morph(this.frame);
    morpHideTooltipObject.onComplete = function(){
      this.element.dispose();
    };
    morpHideTooltipObject.start({
      opacity: 0
    });
    this.win.dispose();
    //zruseni tiskoveho stylu pro recept
    this.printElement.dispose();
    //obnova puvodnich stylu
    $each(this.disabledElements,function(element){
      element.disabled = false;
    },this);
  }

})
