/**
 * Create a new instance of VCMShowingList
 * @classDescription VCM Showing List
 * @param {VCMContent} content The VCMContent reference
 * @return Returns a new instance of VCMShowingList
 * @constructor
 */
function VCMShowingList(content) {
	this._content = content;
}

VCMShowingList.prototype = {
	/**
	 * Reference to the VCMContent
	 * @type {VCMContent}
	 */
	_content: null,
	/**
	 * Reference to the VCMLocationMap
	 * @type {VCMLocationMap}
	 */
	_locationMap: null,
	/**
	 * jQuery reference to my activities button
	 * @type {jQuery}
	 */
	_jMyActivitiesBtn: null,
	
	/**
	 * Initialize
	 */
	init: function() {
		// Location map
		this._locationMap = new VCMLocationMap(this._content);
		this._locationMap.init();
		// Buttons
		this._jMyActivitiesBtn = $('.showingBtnTop');
		// Add events
		this._jMyActivitiesBtn.click($.proxy(this, 'onMyActivityBtnClick'));
	},
	
	/**
	 * Destroy
	 */
	destroy: function() {
		// Remove events
		this._jMyActivitiesBtn.unbind('click');
		// Location map
		this._locationMap.destroy();
		this._locationMap = null;
		// Buttons
		this._jMyActivitiesBtn = null;
	},
	
	/**
	 * On my activity button click
	 * @param {Event} evt The Event object
	 * @return Returns false
	 */
	onMyActivityBtnClick: function(evt) {
		var jBtn = $(evt.currentTarget),
			showingId = VCMUtils.findNumber(jBtn.parents('.showingContainer').attr('id')),
			action = jBtn.hasClass('addMyActivity') ? 'addToMyActivity' : 'removeFromMyActivity',
			command = VCMUtils.prepareCommand({url:'doJSON',command:action,showingId:showingId}),
			me = this;
		
		$.getJSON(command, function(json) { me.onMyActivityJSONResponse(json, jBtn); });
		return false;
	},
	
	/**
	 * On my activity json response
	 * @param {Object} json The JSON response
	 * @param {jQuery} jBtn The clicked button
	 */
	onMyActivityJSONResponse: function(json, jBtn) {		
		if (json && json.success) {
			if (jBtn.hasClass('addMyActivity')) {
				jBtn.removeClass('addMyActivity');
				jBtn.addClass('removeMyActivity');
				// Show fb popup
				//this.showFacebookShareModal(jBtn.attr('href'));
			} else {
				jBtn.removeClass('removeMyActivity');
				jBtn.addClass('addMyActivity');
				// If we are on the profile homepage
				if ($('#homePage').length == 1) {
					// remove the showings and the activity if there is no more showings for it
					var activity = jBtn.parents('.ligneContainer');
					jBtn.parents('.showingContainer').remove();
					if (activity.find('.showingContainer').length == 0) {
						activity.remove();
					}
				}
			}
		} else {
			// TODO Check for type of error?
			this._content.getPage().getSideBar().getLogin().showLoginReminder();
		}		
	},
	
	showFacebookShareModal: function(link) {
		// Show the modal
		this._content.getPage().getModal().show($('#fbShareModal').html(), $.proxy(this, 'hideFacebookShareModal'));
		// Add event
		$('#popupContent .fbShareYesBtn').attr('href', link).click($.proxy(this, 'onFBShareYesClick'));
		$('#popupContent .fbShareNoBtn').click($.proxy(this, 'onFBShareNoClick'));
	},
	
	hideFacebookShareModal: function() {
		// Remove event
		$('#popupContent .fbShareYesBtn').unbind('click');
		$('#popupContent .fbShareNoBtn').unbind('click');
	},
	
	onFBShareYesClick: function(evt) {
		window.open('http://www.facebook.com/sharer.php?u=' + encodeURIComponent(evt.currentTarget.href))
		this._content.getPage().getModal().close();
		return false;
	},
	
	onFBShareNoClick: function(evt) {
		this._content.getPage().getModal().close();
		return false;
	}
};

