/*
   SiteComponents version:
   Id: agreedisagreerating.js,v 1.5 2009/03/27 07:54:02 kurt Exp 
   Name: SC_6_6_0 
   
   Disclaimer
   
   While we make every effort to ensure that this code is fit for its intended
   purpose, we make no guarantees as to its functionality. CoreTrek AS will
   accept no responsibility for the loss of data or any other damage or
   financial loss caused by use of this code.
   
   Copyright
   
   This programming code is copyright of CoreTrek AS. Permission to run this
   code is given to approved users of CoreTrek's publishing system CorePublish.
   
   This source code may not be copied, modified or otherwise repurposed for use
   by a third party without the written permission of CoreTrek AS.
   
   Contact webmaster@coretrek.com for information.
  
*/

/*

    ============================================================================
    IMPORTANT! This javascript is dependent on Prototype. If Scriptaculous is
               available the tooltip will use some nice effects.
    ============================================================================

    JavaScript enhancements for the agree disagree rating
    
*/

var AgreeDisagreeRating = Class.create({

    /**
     * Initialize the recommend rating from the plain HTML form
     */
    initialize: function(element) {
        this.form = element.down();
        this.text = this.form.next();
        
        this.initStyle();
        
        this.agree.observe('click', this.submitListener.bindAsEventListener(this));
        this.disagree.observe('click', this.submitListener.bindAsEventListener(this));
    },
    
    initStyle: function() {
        var buttons = this.form.select('button');
        
        // Icons from: http://www.famfamfam.com/lab/icons/silk/
        this.upIconPath = '/themes/' + getThemeName()  + '/images/rating/thumb_up.png';
        this.downIconPath = '/themes/' + getThemeName()  + '/images/rating/thumb_down.png';
        this.upIconGrayPath = '/themes/' + getThemeName()  + '/images/rating/thumb_up_gray.png';
        this.downIconGrayPath = '/themes/' + getThemeName()  + '/images/rating/thumb_down_gray.png';
        
        this.agree = this.transformButton(buttons[0], this.upIconPath);
        this.disagree = this.transformButton(buttons[1], this.downIconPath);
    },
    
    transformButton: function(button, imagePath) {
        var img = new Image();
        img.src = imagePath;
        
        Event.observe(img, 'load', function(event) {
	        button.update();
	        button.setStyle({
	            background: 'url(' + imagePath + ') no-repeat',
	            border: '0',
	            width: img.width + 'px',
	            height: img.height + 'px'
	        });
	    });
        
        return button;
    },
    
    updateRatingCount: function(req) {
        var response = req.responseText.evalJSON();
        
        this.eventTrigger.next().down().update(response['amount_with_rating']);
        
        // Lets disable the form
        this.agree.stopObserving('click');
        this.disagree.stopObserving('click');
        this.form.observe('submit', function(event) { event.stop(); });
        
        this.agree.setStyle({
            background: 'url(' + this.upIconGrayPath + ')'
        });
        
        this.disagree.setStyle({
            background: 'url(' + this.downIconGrayPath + ')'
        });
        
        new Effect.Shake(this.submit, {
            distance: 100
        });
    },
    
    // ========================================================================
    // Event listeners
    
    submitListener: function(event) {
	    event.stop();
	    params = this.form.serialize(true);
	    params['rating'] = event.element().value;
	    
	    this.eventTrigger = event.element();
	    
	    new Ajax.Request(this.form.getAttribute('action'), {
	        parameters: params,
	        onSuccess: this.updateRatingCount.bindAsEventListener(this),
	        onFailure: function(req) { }
	    });
    }
    
});
