/**
 * Requires
 * Prototype...... 1.5.1.1            http://prototypejs.org/
 * lowpro..........  0.4                 http://script.aculo.us/
 * scriptaculous..  1.7.1.beta3    http://www.danwebb.net/lowpro
 * */

Event.addBehavior({
   
   
  'body': function() {
    var busy = $div({id:'busy'}, "Working...");
    busy.hide();
    // NOTE You could also use 'this' here since it is the document.body.
    document.body.appendChild(busy);
  },

  'a.clip-pattern-to-ws-xx:click' : function(e) {
    alert('Will call with '+  this.readAttribute('href') ); 
    new Ajax.Updater(this.parentNode, this.readAttribute('href'));
   
    return false;
    
  },
  
  'a.clip-pattern-to-ws:click': function() {
    new Ajax.Request( this.readAttribute('href'), {
      onSuccess: function(transport   ) {
         
        var json = transport.responseText.evalJSON();
        var thisNode       = $(this);
        var parentNode = thisNode.parentNode;
        var pass          =  json['pass'] ; 
        var totalInWS =  json['total'] ;
        var flipImgUrl = thisNode.readAttribute('flipImg');
        
        //parentNode.update('<img  src=" '+flipImgUrl+'  "/>');
         parentNode.innerHTML = '<img  src=" '+flipImgUrl+'  "/>';
        
        parentNode.title="Clipped to your your Worksheet.";
        
        var spans =$$('#user-ws-content-count');
        spans.each(
                     function (e){
                        $(e).innerHTML = totalInWS;
                     }
        );
        
        
      }.bind(this)
    });
    
     return false;
    
  }, //'a.clip-pattern-to-ws:click': function() {

  'a.unclip-pattern-from-ws:click': function() {
    new Ajax.Request( this.readAttribute('href'), {
      onSuccess: function(transport   ) {
         
        var json = transport.responseText.evalJSON();
        var thisNode       = $(this);
        var puffNode       = $(thisNode.readAttribute('puffBlockId'));
        var pass           =  json['pass'] ; 
        var totalInWS      =  json['total'] ;
        
        new Effect.Squish(puffNode);
        
        var spans =$$('#user-ws-content-count');
        spans.each(
                     function (e){
                        $(e).innerHTML = totalInWS;
                     }
        );
        
        
      }.bind(this)
    });
    
     return false;
    
  } //'a.unclip-pattern-from-ws': function() {


});





/* From http://mir.aculo.us/2005/11/14/ajax-activity-indicators-with-rails-0-14-3*/ 
Ajax.Responders.register({
  
  onCreate: function() {
    if($('busy') && Ajax.activeRequestCount > 0)
      Effect.Appear('busy',{duration:0.5,queue:'end'});
  },
  
  onComplete: function() {
    if($('busy') && Ajax.activeRequestCount == 0)
      Effect.Fade('busy',{duration:1.0,queue:'end'});
  }
    
});




/**
 * Accordion Effect for Script.aculo.us
 * Created by Lucas van Dijk
 * http://www.lucasvd.nl
 *
 * Copyright 2007 by Lucas van Dijk
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 */

Effect.ScrollVertical = Class.create();

Object.extend(Object.extend(Effect.ScrollVertical.prototype, Effect.Base.prototype), 
{
    initialize: function(element) 
    {
        if(typeof element == "string")
        {        
            this.element = $(element);
            if(!this.element) 
            {
                throw(Effect._elementDoesNotExistError);
            }
        }

        var options = Object.extend({
            from: this.element.scrollTop || 0,
            to:   this.element.offsetHeight
        }, arguments[1] || {});

        this.start(options);
    },

    update: function(position) 
    {
        this.element.scrollTop = position;
    }
});

Effect.ScrollHorizontal = Class.create();

Object.extend(Object.extend(Effect.ScrollHorizontal.prototype, Effect.Base.prototype), 
{
    initialize: function(element) 
    {
        if(typeof element == "string")
        {        
            this.element = $(element);
            if(!this.element) 
            {
                throw(Effect._elementDoesNotExistError);
            }
        }

        var options = Object.extend({
            from: this.element.scrollLeft || 0,
            to:   this.element.offsetWidth
        }, arguments[1] || {});

        this.start(options);
    },

    update: function(position) 
    {
        this.element.scrollLeft = position;
    }
});

