/**
 * $Id$
 *
 * Become a fan (location, artist, etc...)
 *
 * @created Wed Jun 10 13:12:00 CEST 2009
 * @author Ciprian Pop
 * @reviewer TODO
 */

var ConfirmAction = Class.create({
	options: {
		txt: {
			winTitle: 'Fan',
			question: 'Are you sure?',
			yes: 'Yes',
			no: 'No',
			error: 'error'
		}
	},

	initialize: function(actionLink, url, params, options) {
		this.actionLink = actionLink;
		this.params = params;
		this.url = url;
		Object.extend(this.options, options);

		this.contentPlaceholder = document.createElement('div');
		this.mWin = new ModalWindow();
		this.mWin.setTitle(this.options.txt.winTitle);
		this.mWin.setContent(this.contentPlaceholder)
		this.confirmAction();
	},

	confirmAction: function() {
		this.contentPlaceholder.innerHTML =
			'<div> \
				<div class="newbox">' + this.options.txt.question + '</div> \
				<div class="newbox noMargin" style="text-align:right;margin:0;padding:0 8px 8px 0;"> \
					<input type="button" id="btn_yes_fan" name="btn_yes_fan" class="btn_submit" value="' + this.options.txt.yes + '"/> \
					<input type="button" id="btn_no_fan" name="btn_no_fan" class="btn_submit" value="' + this.options.txt.no + '"/> \
				</div> \
			</div>';
		this.mWin.adjustHeight();
		var closeWindow = this.mWin.closeWindow.bind(this.mWin);
		Event.observe($('btn_no_fan'), 'click', closeWindow);
		var changeFanStatus = this.changeFanStatus.bind(this);
		Event.observe($('btn_yes_fan'), 'click', changeFanStatus);
	},

	changeFanStatus: function() {
		this.contentPlaceholder.innerHTML = '<img src="/img/icons/loader.gif">';
		var statusChanged = this.fanStatusChanged.bind(this);
		var params = this.params;
		var ajx = new Ajax.Request(this.url, {method: 'post', parameters: params, onComplete: statusChanged});
	},

	fanStatusChanged: function(response, json) {
		try {
			if (!json) {
				//if  not using json tips then evaluate the renderedText instead
				json = eval('(' + response.responseText + ')');
			}
		}
		catch (e) {
			this.contentPlaceholder.innerHTML = '<div class="newbox failure">' + this.options.txt.error + '</div>';
			return;
		}

		if (json.err && json.err.length > 0) {
			this.contentPlaceholder.innerHTML = '<div class="newbox failure">' + json.err + '</div>';
		}else if(json.statusMessage && json.statusMessage.length > 0) {
			this.contentPlaceholder.innerHTML = '<div class="newbox">' + json.statusMessage + '</div>';
			try {
				this.actionLink.remove();
			} catch(e){}
			if(this.options.elementsToRemove && this.options.elementsToRemove.length > 0) {
				this.options.elementsToRemove.each(function(element){
					try {
						if($(element)) {
							$(element).remove();
						}
					} catch(e){}
				});
			}

			try {
				if (json.facebook) {
					var facebookFeed = new Facebook_Feed();
					switch(json.facebook.storyType) {
						case "willAttendEvent":
							var feedBundle = facebookFeed.feedBundles.willAttendEvent;
							break;
						case "attendedEvent":
							var feedBundle = facebookFeed.feedBundles.attendedEvent;
							break;
						case "isLocationFan":
							var feedBundle = facebookFeed.feedBundles.isLocationFan;
							break;
						case "isPersonFan":
							var feedBundle = facebookFeed.feedBundles.isPersonFan;
							break;
					}

					facebookFeed.publishFeedEntry(feedBundle, json.facebook.params, {}, '',
						null,
						facebookFeed.requireConnect.require, null);
				}
			} catch(e) {
			}

		} else {
			this.contentPlaceholder.innerHTML = '<div class="newbox failure">' + this.options.txt.error + '</div>';
		}
		this.mWin.adjustHeight();
	}
});
