/**
 * Prototype javascript for the CakePHP AJAX star rating plugin.
 * 
 * 
 * Copyright (c) 2009 Michael Schneidt
 *
 * 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.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 *
 * @author Michael Schneidt (michael.schneidt@arcor.de)
 * @link http://bakery.cakephp.org/articles/view/ajax-star-rating-plugin-1
 * @version 1.8
 */

/**
 * Holds the settings for all rating elements.
 */
var ratingSettings = new Array();

/**
 * Initializes the rating element.
 *
 * @param element Id of the rating lement
 * @param data JSON encoded rating data
 * @param options JSON encoded rating options
 * @param config JSON encoded plugin configurations
 * @param text The default text to display besides the rating stars
 * @param enabled Enable the rating for a user
 */
function ratingInit(element, data, options, config, enabled) {
  ratingSettings[element] = new Array();
  
  ratingSettings[element]['data'] = eval('(' + data + ')');
  ratingSettings[element]['options'] = eval('(' + options + ')');
  ratingSettings[element]['config'] = eval('(' + config + ')');
  ratingSettings[element]['enabled'] = enabled;
  
  for (var i = 1; i <= ratingSettings[element]['config']['maxRating']; i++) {
    $(element + '_' + i).observe('mouseover', function(e) {
      var value = this.id.match(/[0-9]*$/);
      
      // workaround against event after reload
      var target = e.relatedTarget || e.toElement;
      
      if (target && target.id != element + '_' + value) {
        if (ratingSettings[element]['enabled']) {
          ratingSet(element, value);
        }
        
        if (ratingSettings[element]['config']['showMouseOverMessages']) {
          ratingMessages(element, value);
        }
      }      
    });
    
    $(element + '_' + i).observe('click', function(e) {
      var value = this.id.match(/[0-9]*$/);
      
      ratingSave(element, value);
    });
  }
  
  $(element).observe('mouseout', function(e) {
    // workaround against mouseout event on child elements like links
    var target = e.relatedTarget || e.toElement;
    
    if (target && target.id != element + '_text' && target.id != '') {
      ratingReset(element);
    }
  });
  
  $(element + '_text').observe('mouseover', function(e) {
    // workaround against mouseover event on child elements like links
    var target = e.relatedTarget || e.toElement;
    
    if (target && target.id != element + '_text' && target.id != '') {
      ratingReset(element);
      
      if (ratingSettings[element]['config']['showMouseOverMessages']) {
        ratingMessages(element);
      }
    }
  });
  
  ratingReset(element);
}

/**
 * Sets the rating element to a rating value.
 *
 * @param element Name of element
 * @param value Rating value 
 */
function ratingSet(element, value) {
  var starImg = ratingSettings[element]['config']['starEmptyImageLocation'];
  
  for (i = 1; i <= ratingSettings[element]['config']['maxRating']; i++) {
    if (i <= Math.floor(value)) {
      starImg = ratingSettings[element]['config']['starFullImageLocation'];
    } else if (i == Math.floor(value) + 1 && value.toString().match(/[0-9]\.[5-9]/)) {
      starImg = ratingSettings[element]['config']['starHalfImageLocation'];
    } else {
      starImg = ratingSettings[element]['config']['starEmptyImageLocation'];
    }
    
    $(element + '_' + i).src = ratingSettings[element]['config']['appRoot'] + starImg;
    
    // set user mark
    if (ratingSettings[element]['config']['showUserRatingMark'] && i <= ratingSettings[element]['data']['userRating']) {
      $(element + '_' + i).className = 'rating-user';
    } else {
      $(element + '_' + i).className = 'rating';
    }
    
    // disable stars
    if (!ratingSettings[element]['enabled'] && !$(element + '_' + i).className.match(/-disabled/)) {
      $(element + '_' + i).className += '-disabled';
    }    
  }
}

/**
 * Resets the rating element.
 *
 * @param element Element id
 * @param e Event to check the derivation
 */
function ratingReset(element) {
  if (ratingSettings[element]['config']['showUserRatingStars']) {
    ratingSet(element, ratingSettings[element]['data']['userRating']);
  } else {
    ratingSet(element, ratingSettings[element]['data']['avgRating']);
  }
  
  $(element + '_text').update(ratingSettings[element]['config']['statusText']);
}

/**
 * Does the AJAX call to save the rating and 
 * updates the rating element.
 *
 * @param element Name of element
 * @param value Rating value
 */
function ratingSave(element, value) {
  data = element.split('_');
  
  if (ratingSettings[element]['enabled']) {
    new Ajax.Updater(element, ratingSettings[element]['config']['appRoot'] + 'rating/ratings/save/' + data[0] + '/' + data[3] + '/' + value + '?' + Math.floor(Math.random() * 999999), {               
      asynchronous: true,
      evalScripts: true,
      method: 'get',
      parameters: ratingSettings[element]['options'],
      onFailure: function error() {
        //alert('AJAX Error');
      },
      onLoading: function (request) {
        //Element.show('loader');
      },
      onComplete: function (request, json) {
        //Element.hide('loader');
      }
    });
  }
}

/**
 * Sets the mouseOverMessages.
 *
 * @param element Name of element
 * @param value Rating value
 */
function ratingMessages(element, value) {
  if (ratingSettings[element]['enabled'] && value > 0) {
    if (value == ratingSettings[element]['data']['userRating'] 
        && ratingSettings[element]['config']['allowDelete'] 
        && ratingSettings[element]['config']['mouseOverMessages']['delete']) {
      $(element + '_text').update(ratingSettings[element]['config']['mouseOverMessages']['delete']);
    } else if (ratingSettings[element]['config']['mouseOverMessages'][value]) {
      $(element + '_text').update(ratingSettings[element]['config']['mouseOverMessages'][value]);
    }
  } else if (!ratingSettings[element]['enabled'] 
      && ratingSettings[element]['data']['userRating'] == 0
      && ratingSettings[element]['config']['mouseOverMessages']['login']){
    $(element + '_text').update(ratingSettings[element]['config']['mouseOverMessages']['login']);
  } else if (!ratingSettings[element]['enabled'] 
      && ratingSettings[element]['data']['userRating'] > 0
      && ratingSettings[element]['config']['mouseOverMessages']['rated']){
    $(element + '_text').update(ratingSettings[element]['config']['mouseOverMessages']['rated']);
  } else {
    $(element + '_text').update(ratingSettings[element]['config']['statusText']);
  }
}
