/*
   SiteComponents version:
   6.6.4.1, tag SC_6_6_4_1, created Tue Oct 27 21:28:23 +0100 2009
   $Name: HEAD $
   
   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 enhancement for the recommend rating
    
*/

var RecommendRating = 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.form.observe('submit', this.submitListener.bindAsEventListener(this));
    },
    
    initStyle: function() {
        this.img = new Image();
        
        Event.observe(this.img, 'load', this.imageLoadListener.bindAsEventListener(this));
        this.img.src = '/themes/' + getThemeName()  + '/images/rating/recommended.png';
        
        // No need to observe image load here, as this will only be used when
        // the user has clicked the recommend icon
        this.gray = new Image();
        this.gray.src = '/themes/' + getThemeName()  + '/images/rating/recommended_gray.png'; 
    },
    
    updateRecommendationCount: function(req) {
        var response = req.responseText.evalJSON();
        this.text.down().update(response['amount_with_rating']);
        
        this.submit.setStyle({
            background: 'url(' + this.gray.src + ') no-repeat'
        });
        
        this.removeListeners();
    },
    
    removeListeners: function() {
        this.form.stopObserving('submit');
        this.form.observe('submit', function(event) { event.stop(); });
    },
    
    // ========================================================================
    // Event listeners
    
    imageLoadListener: function(event) {
        this.submit = this.form.select('input[type=submit]')[0];
        this.submit.value = '';
        
        this.submit.setStyle({
            background: 'url(' + this.img.src + ') no-repeat',
            border: '0px',
            width: this.img.width + 'px',
            height: this.img.height + 'px'
        });
    },
    
    submitListener: function(event) {
	    event.stop();
	    
	    new Ajax.Request(this.form.getAttribute('action'), {
	        parameters: this.form.serialize(),
	        onSuccess: this.updateRecommendationCount.bindAsEventListener(this)
	    });
    }
    
});

