/*!
 * jQuery.poetron - Plugin for www.poetron-zone.de
 * Copyright 2011, Dirk Koester 
 * Version 1.00
 * http://www.dirkoester.de
 */
(function( $ )
{
  var cache = [];
  var CLIENT_WIDTH = 1000;
  
  //Poetron-Funktionalität
  var methods = 
  {
    init : function( options ) { },
    resizeDep : function() 
    { 
      if(window.innerWidth > CLIENT_WIDTH)
      { 
        this.fadeIn("slow");
      }
      else
      {
        this.fadeOut("slow");
      }
      $(window).bind("resize.poetron", {caller: this}, methods.resizer); 
    },
    terminalText : function(input, callback) 
    {  
      var chars = input.split("");
      var iterator = 0;
      var txtarea = $(this);
      $(txtarea).val('');
      var settxt = function()
      {
        if(iterator < chars.length)
        {
          var x = $(txtarea).val();
          var y = x + chars[iterator]; 
          var delay = 30 + Math.floor(Math.random()*50);
          $(txtarea).val(y); 
          iterator++;
          setTimeout(function(){settxt()}, delay);
        }
        else
        {
          if(jQuery.isFunction(callback))
          {
            callback.call(this);
          }
          return true;
        }
      } 
      setTimeout(function(){settxt()}, 10);
    },
    resizer : function( evt ) 
    { 
      $this = evt.data.caller;
      if(window.innerWidth > CLIENT_WIDTH)
      { 
        $this.fadeIn("slow");
      }
      else
      {
        $this.fadeOut("slow");
      }
    },
    shake : function (intShakes, intDistance, intDuration) 
    {
      this.each(function() 
      {
        $(this).css("position","relative"); 
        for (var x=1; x <= intShakes; x++) 
        {
          $(this).animate({left:(intDistance*-1)}, (((intDuration/intShakes)/4)))
                 .animate({left:intDistance}, ((intDuration/intShakes)/2))
                 .animate({left:0}, (((intDuration/intShakes)/4)));
        }
      });
      return this;
    }
  };
  
  //Zeigt Spinner
  $.spinOn = function ()
  {
    $('#spinner').addClass("spin");
  }
  
  //Versteckt Spinner
  $.spinOff = function hideSpinner()
  {
    $('#spinner').removeClass("spin");
  }
  
  //Scrollt zum Seitenende
  $.toBottom = function ()
  {
    var y = $('html').height();
    $('html, body').animate({scrollTop: y}, 400);
  }
  
  //Inhalte drucken
  $.print = function (cnt, options) 
  {
    var c_date = new Date().getFullYear();
    var settings = {
      'title'       : 'Druckausgabe',
      'footer'      : 'Copyright (c) ' + c_date + ', www.poetron-zone.de',
      'destination' : '_blank',
      'type'        : 'text/html'
    };
    
    if (options) 
    { 
      $.extend(settings, options);
    }

    var a = window.open(settings.title, settings.destination);
      a.document.open(settings.type);
      a.document.write('');
      a.document.write('<h1><font face=\'sans-serif\'>' + settings.title + '</font></h1>');
      a.document.write('<pre>' + cnt + '</pre><br />');
      a.document.write('');
      a.document.write('<font face=\'sans-serif\' size=\'1\'>' + settings.footer + '</font>');
      a.document.close();
      a.print();
      a.close();
}

  //Vorladen von Bildern
  $.preload = function() 
  { 
    var args_len = arguments.length;
    for (var i = args_len; i--;) 
    {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
  
  //Druckfunktion für Gedichte-Navigation
  $.fn.printPoetry = function ()
  {
    this.each(function() 
    {
      var content = $(this).prev().html();
      var tit = $(this).prev().prev().html();
      var pb = $('<div>').addClass('printbutton');
      pb.attr('title','Drucken');
      pb.click(function(){$.print(content, {title: tit });});
      $(this).prepend(pb);      
    });
    return this;
  }

  //Poetron - Funktionen, mappt auf methods.Objekt
  $.fn.poetron = function( method ) 
  {
    if ( methods[method] ) 
    {
      return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
    } 
    else if ( typeof method === 'object' || ! method ) 
    {
      return methods.init.apply( this, arguments );
    } 
    else 
    {
      $.error( 'Methode ' +  method + ' gibts nicht bei jQuery.poetron' );
    }    
  };
})( jQuery );

