/**
 * Class for AJAX submitting of the quick rating buttons
 *
 * @author      Oliver Kliebisch
 * @copyright   2009-2010 Oliver Kliebisch
 * @license     Commercial
 */
var RenommeeQuickRating = Class.create({

	/**
	 * Initializes the quick rating
	 */
	initialize: function(objectType, activeUserModifier, objectData) {
		this.objectType = objectType;
		this.objectData = objectData;
		this.activeUserModifier = activeUserModifier;
		this.sending = false;
		this.options = Object.extend({
			icons: {
				help: RELATIVE_WCF_DIR + 'icon/helpS.png',
				close: RELATIVE_WCF_DIR + 'icon/closeS.png',
				positive: RELATIVE_WCF_DIR + 'icon/renommeePositiveS.png',
				neutral: RELATIVE_WCF_DIR + 'icon/renommeeNeutralS.png',
				negative: RELATIVE_WCF_DIR + 'icon/renommeeNegativeS.png'
			}
		}, arguments[3] || { });

		this.objectData.each(function(object) {
			if ($('renommeeButton' + object.key + 'Positive')) {
				$('renommeeButton' + object.key + 'Positive').onclick =  function() {
					return renommeePanel.submit('renommeeButton' + object.key + 'Positive', object.key, true);
				};				
			}
			if ($('renommeeButton' + object.key + 'Negative')) {
				$('renommeeButton' + object.key + 'Negative').onclick =  function() {
					return renommeePanel.submit('renommeeButton' + object.key + 'Negative', object.key, false);
				};
			}
		}.bind(this));

		this.initializeInlineObjectRenommee();
	},

	/**
	 * Submits the rating button
	 */
	submit: function(element, objectID, positive) {
		if (!this.sending) {
			this.sending = true;
			element = $(element);
			link = element.href;
			new Ajax.Request(element.href.gsub('&nbsp;', '&') + '&ajax=1', {
				method: 'get',
				onSuccess: function() {this.postProcess(objectID, positive ? 1 : -1)}.bind(this),
				onFailure: function() {document.location.href = fixURL(link)}.bind(this)
			});
		}
		return false;
	},

	/**
	 * Refreshes the button displays
	 */
	postProcess: function(objectID, state) {
		this.sending = false;
		var plusButton = $('renommeeButton' + objectID + 'Positive');
		var minusButton = $('renommeeButton' + objectID + 'Negative');

		if (showRenommee && !showRenommeeAlways) {
			var button = state == 1 ? plusButton : minusButton;
			button.href = fixURL('index.php?page=ObjectRenommee&objectType=' + this.objectType + '&objectID=' + objectID + SID_ARG_2ND);
			var renommee = this.objectData.get(objectID)['renommee'];			
			renommee += this.calculateRenommee(state, this.activeUserModifier);

			var children = button.childElements();
			if (renommee > 0) {
				children[0].src = this.options.icons.positive;
			}
			else if (renommee == 0) {
				children[0].src = this.options.icons.neutral;
			}
			else children[0].src = this.options.icons.negative;
			button.insert(renommee);

			button.onclick = null;
			var title = language['wcf.user.renommee.' + this.objectType + '.renommee'];
			title = title.replace(/\{\$renommee\}/, renommee);
			button.title = title;
			button.id = 'objectRenommeeButton' + objectID;
			button.addClassName('objectRenommeeButton');

			new Effect.Pulsate(button, {
				pulses: 2,
				duration: 1.5
			});
			var otherButton = state == 1 ? minusButton : plusButton;
			new Effect.Fade(otherButton.up(), {
				duration: 0.5
			});
		}
		else if (showRenommee && showRenommeeAlways) {
			new Effect.Fade(plusButton.up(), {
				duration: 0.5
			});
			new Effect.Fade(minusButton.up(), {
				duration: 0.5
			});

			var objectButton = $('objectRenommeeButton' + objectID);

			var renommee = this.objectData.get(objectID)['renommee'];			
			renommee += this.calculateRenommee(state, this.activeUserModifier);

			var children = objectButton.childElements();
			if (renommee > 0) {
				children[0].src = this.options.icons.positive;
			}
			else if (renommee == 0) {
				children[0].src = this.options.icons.neutral;
			}
			else children[0].src = this.options.icons.negative;
			children[1].update(renommee);
			var title = language['wcf.user.renommee.' + this.objectType + '.renommee'];
			title = title.replace(/\{\$renommee\}/, renommee);
			objectButton.title = title;

			new Effect.Pulsate(objectButton, {
				pulses: 2,
				duration: 1.5,
				delay: 0.5
			});
		}

		if (rerateThreshold) {
			this.hideRatingButtons(objectID, this.objectData.get(objectID)['userID']);
		}
	},

	/**
	 * Hides all obsolete rating buttons
	 */
	hideRatingButtons: function(activeObjectID, userID) {
		this.objectData.each(function(object) {
			var object = object.value;
			var objectID = object.objectID;
			if (objectID != activeObjectID && object['userID'] == userID) {
				var plusButton = $('renommeeButton' + objectID + 'Positive');
				var minusButton = $('renommeeButton' + objectID + 'Negative');
				if (plusButton) {
					new Effect.Fade(plusButton.up(), {
						duration: 0.5
					});
				}
				if (minusButton) {
					new Effect.Fade(minusButton.up(), {
						duration: 0.5
					});
				}
			}
		}.bind(this));

	},

	/**
	 * Initializes the inline object renommee buttons
	 */
	initializeInlineObjectRenommee: function(objectID) {
		if (objectID) {
			displayButton = $('objectRenommeeButton' + objectID);
			if (displayButton) {
				displayButton.onclick = function() {
					return renommeePanel.displayInlineObjectRenommee(this.objectData.get(objectID));
				}.bind(this);
				displayButton.ondblclick = function() {
					document.location.href = fixURL('index.php?page=ObjectRenommee&objectType=' + objectType + '&objectID=' + objectID + SID_ARG_2ND);
				};
			}
		}
		else {
			this.objectData.each(function(object) {
				var object = object.value;
				var objectID = object.objectID;
				var objectType = this.objectType;
				displayButton = $('objectRenommeeButton' + objectID);
				if (displayButton) {
					displayButton.onclick = function() {
						return renommeePanel.displayInlineObjectRenommee(this.objectData.get(objectID));
					}.bind(this);
					displayButton.ondblclick = function() {
						document.location.href = fixURL('index.php?page=ObjectRenommee&objectType=' + objectType + '&objectID=' + objectID + SID_ARG_2ND);
					};
				}
			}.bind(this));
		}
	},

	/**
	 * Displays the inline renommee of an object
	 */
	displayInlineObjectRenommee: function(object) {
		if ($('inlineObjectRenommee' + object.objectID)) {
			new Effect.Parallel([
				new Effect.BlindUp('inlineObjectRenommee' + object.objectID, {
					duration: 0.5
				}),
				new Effect.Fade('inlineObjectRenommee' + object.objectID, {
					duration: 0.5
				})],
				{
					duration: 0.5,
					afterFinish: function() {
						$('inlineObjectRenommee' + object.objectID).remove();
						loadingInlineRenommee = false;
					}
				});
		}
		else {
			if (loadingInlineRenommee) return false;
			// get button
			var displayButton = displayButton = $('objectRenommeeButton' + object.objectID);

			// show spinner
			var oldRenommeeImage = displayButton.down('img').src;
			displayButton.down('img').src = RELATIVE_WCF_DIR + 'images/renommeeSpinner.gif';

			// get smallButtons container
			smallButtons = displayButton.up('.smallButtons');

			var url = fixURL('index.php?page=ObjectRenommee&objectType=' + this.objectType + '&objectID=' + object.objectID + '&inline=1' + SID_ARG_2ND);
			loadingInlineRenommee = true;
			new Ajax.Request(url, {
				method: 'get',
				onSuccess: function(transport) {
					if (transport.responseText != '') {
						smallButtons.up().insert('<div class="inlineObjectRenommee" id="inlineObjectRenommee' + object.objectID + '" style="display: none;">' + transport.responseText + '</div>');
						new Effect.Parallel([
							new Effect.BlindDown('inlineObjectRenommee' + object.objectID, {
								duration: 0.5
							}),
							new Effect.Appear('inlineObjectRenommee' + object.objectID, {
								duration: 0.5
							})],
							{
								duration: 0.5,
								afterFinish: function() { displayButton.down('img').src = oldRenommeeImage; }.bind(this)
							});
						loadingInlineRenommee = false;
					}
				}.bind(this),
				onFailure: function () {
					document.location.href = fixURL('index.php?page=ObjectRenommee&objectType=' + this.objectType + '&objectID=' + object.objectID + SID_ARG_2ND)
				}.bind(this)
			});
		}

		return false;
	},

	/**
	 * Calculcates a new renommee value
	 */
	calculateRenommee: function(state, modifier) {
		value = 0;
		if (state == -1) {
			value = Math.round(state * modifier * (negativeRenommeeFactor / 100));
		}
		else value = state * modifier;

		return value;
	}
});

