/**
 * Create a new instance of VCMDetails
 * @classDescription	Class for details page
 * @param {VCMContent} content A reference to the VCMContent parent
 * @return {VCMDetails}	Returns a new VCMDetails object
 * @constructor
 */
function VCMDetails(content) {
	this._content = content;
}

VCMDetails.prototype = {
	/**
	 * Reference to the VCMContent parent
	 * @type {VCMContent}
	 */
	_content: null,
	
	/**
	 * Type of details
	 * @type {String}
	 */
	_type: null,
	
	/**
	 * Reference to the ImageViewer
	 * @type {MediaViewer}
	 */
	_imageViewer: null,
	
	/**
	 * jQuery reference to back button
	 * @type {jQuery}
	 */
	_jBackBtn: null,
	
	/**
	 * jQuery reference to SMS button
	 * @type {jQuery}
	 */
	_jSMSBtn: null,
	
	/**
	 * jQuery reference to videos
	 * @type {jQuery}
	 */
	_jVideos: null,
	
	/**
	 * Reference to a VCMActivityList
	 * @type {VCMActivityList}
	 */
	_activityPaginator: null,
	
	/**
	 * Reference to a VCMShowingList
	 * @type {VCMShowingList}
	 */
	_showingList: null,
	
	coeurList: null,	/* bouton pour le popup de la liste des coups de coeur */
	coeurJaime: null,	/* bouton pour ajouter un coup de coeur */
	
	_linkedEvent: null,

	/**
	 * Initialise the details page
	 */
	init: function() {
		console.log('VCMDetails.init()');
		
		if($('#detailContainer').hasClass("event")) {
			this._type = 'event';
		} else {
			this._type = $('#detailContainer').attr('class').replace(/[0-9_]+/gi, '');
		}

		// Page specific
		var line = 0;
		$('#divNext,#divPrevious').css({top:'auto',bottom:'100px'});
		
		this._activityPaginator = new VCMPaginator(this._content);
		this._activityPaginator.setOptions('items_per_page', this._type == 'event' ? 4 : 5);
		this._activityPaginator.init();
		if (this._type == 'activity') {
			line = 22;
			this._showingList = new VCMShowingList(this._content);
			this._showingList.init();
			this._linkedEvent = $('.detailEventLink');
		}
		else if (this._type == 'location' || this._type == 'event') {
			
			if (this._type == 'location') {
				line = 7;
				this._locationMap = new VCMLocationMap(this._content);
				this._locationMap.init();
			}
			else if (this._type == 'event') {
				line = 11;
			}
		}		
		// Image viewer
		this._imageViewer = new VCMImageViewer();
		this._imageViewer.init();
		// Buttons and events
		this._jBackBtn = $('.lnkReturnList');
		this._jSMSBtn = $('.envoieSMS');
		this._jVideos = $('.videoContainer a');
		/* pour les coups de coeur */
		this.coeurList = $('.coeurList');
		this.coeurJaime = $('.coeurJaime');
		
		this.addEvents();		
		// Addthis button
		addthis.button('.addthis_button', {}, {
			url: this.computeURL(location.href, $('.addthis_button').attr('href')),
			title: $('.detailTitle').attr('title')
		});
		// Read More/Less
		if(this._type != 'event') {
			if ($('#detailDescription~a.readMoreButton').length == 0){
				$('#detailDescription').readMoreLess({line:line,btnClass:'bouton right',readMore:readMore,readLess:readLess});
			}
		}
	},
	
	/**
	 * Comput URL
	 * @param {String} url1
	 * @param {String} url2
	 */
	computeURL: function(url1, url2) {
		if (url2.substr(0, 7) == 'http://') return url2;
		var url = '';
		var lastSlash = url1.lastIndexOf('/');
		url += url1.substr(0, lastSlash + 1);
		url += url2;
		var paths = url.split('/');
		url = paths.splice(0, 3).join('/') + '/';
		for(i = 0; i < paths.length; i++) {
			if (paths[i] == '..') {
				if (i == 0) {
					paths.splice(i, 1);
					i--;
				}
				else {
					paths.splice(i - 1, 2);
					i -= 2;
				}
			}
		}
		url += paths.join('/');
		return url;
	},
	
	/**
	 * Destroy the details
	 */
	destroy: function() {
		console.log('VCMDetails.destroy()');
		// Destroy the location map if present
		if (this._showingList) {
			this._showingList.destroy();
			this._showingList = null;
		}
		// Destroy the activity paginator if present
		if (this._activityPaginator) {
			this._activityPaginator.destroy();
			this._activityPaginator = null;
		}
		// Destroy the image viewer
		this._imageViewer.destroy();
		this._imageViewer = null;
		// Remove events
		this.removeEvents();
		// Destroy jQuery reference
		this._jBackBtn = null;
		this._jSMSBtn = null;
		this._jVideos = null;
		this._linkedEvent = null;
		// TODO Remove the addthis button
		// TODO Remove the readMoreLess
	},
	
	/**
	 * Add events for the details page
	 */
	addEvents: function() {
		console.log('VCMDetails.addEvents()');
		if (this._linkedEvent) {
			this._linkedEvent.click($.proxy(this, 'onLinkedEventClick'));
		}
		/**
		 * @type {VCMDetails}
		 */
		var me = this;
		this._jBackBtn.click($.proxy(this._content, 'goBack'));
		this._jSMSBtn.click(function() {
			me.showSMSPopup();
			return false;
		});
		this._jVideos.click(function() {
			me._content.getPage().getModal().show($(this).next().html());
			return false;
		});
		
		if(this.coeurJaime != null){
			this.coeurJaime.click(function(){
				var currentId = $(this).attr('id');
				var url = vcmBaseUrl + "doJSON?command=addLikeIt&activityId=" + VCMUtils.findNumber(currentId);
				$.getJSON(url,function(data){
					if(data.success){
						$("#activity"+VCMUtils.findNumber(currentId)+"coupDeCoeur"+">.nbCoupDeCoeur").html(data.msg);
						$("#"+currentId).removeClass("coeurJaime");
						$("#"+currentId).addClass("coeurJaimeDisable");
						//me.showThankYou();
					}
					else{
						me._content.getPage().getSideBar().getLogin().showLoginReminder();
					}
				});
			});
		}
		
		if(this.coeurList != null){
			this.coeurList.click(function(){
				var nbCC = VCMUtils.findNumber($(this).children("span.nbCoupDeCoeur").html());
				var activityId = VCMUtils.findNumber($(this).attr('id'));
				//alert("nbCC = " + nbCC);
				if(nbCC > 0){
					var command = VCMUtils.prepareCommand({activityId:activityId,command:'displayFans'});
					$("#popupContent").load(command,function(){
						me.show();
					});
				}
			});
		}
	},
	
	show: function() {		
		var me = this;

		console.log("Show Modal [Qui Aime]");

		$("#closePopupLnk,#popupCloseArea").click(function() {
			me._content.getPage().getModal().close();
			me.close();
		});

		$(".thumbnailImage").mouseover(function(){
			$("#clientImage").attr('src',$(this).attr('src'));
			$("#clientPseudo").html($(this).nextAll("span").html());
		});
		this._content.getPage().getModal().show();
	},

	showThankYou: function() {		
		var me = this;

		console.log("Show Modal [Qui Aime]");

		$("#closePopupLnk,#popupCloseArea").click(function() {
			me._content.getPage().getModal().close();
			me.close();
		});
		this._content.getPage().getModal().show("Merci!");
	},
	
	close: function() {
		$(".thumbnailImage").unbind('mouseover');
		$('#popupCloseArea').unbind('click');
		$("#closePopupLnk").unbind('click');
	},

	/**
	 * Remove events for the details page
	 */
	removeEvents: function() {
		console.log('VCMDetails.removeEvents()');
		this._jBackBtn.unbind('click');
		this._jSMSBtn.unbind('click');
		this._jVideos.unbind('click');
		if(this.coeurJaime != null){
			this.coeurJaime.unbind('click');
		}
		if(this.coeurList != null){
			this.coeurList.unbind('click');
		}
	},
	
	// TODO Remove SMS Popup things from here
	showSMSPopup: function () {
		var me = this;
		
		this._content.getPage().getModal().show($('#smsPopup').html());
		
		var cbRepresentation = '<select name="showingId" class="smsShowings">';
		$('.showingContainer').each(function() {			
			var text = $(this).find('.showingDate').text().replace(/\s+/gi, ' ');
			text += ' - ' + $(this).find('.showingLocation').text().replace(/\s+/gi, ' ');
			var value = VCMUtils.findNumber($(this).attr('id'));
			cbRepresentation += '<option value="' + value + '">' + text.toUpperCase() + '</option>';
		});
		cbRepresentation += '</select>';
		
		$('#popupContent .representation').append(cbRepresentation);

		$('#popupContent .phoneNumber').mask('999-999-9999');		
		
		$('#popupContent .envoyerTexto').click(function() {
			me.envoyerTexto();
			return false;
		});
		$('#popupContent .fermerTexto').click(function() {
			// TODO Remove SMS Popup listeners
			me._content.getPage().getModal().close();
			return false;
		});
	},
	
	envoyerTexto: function() {
		var url = rootPath + 'doJSON?command=SendSMS&phoneNumber=' + $('#popupContent .phoneNumber').val();
		url += '&provider=' + $('#popupContent .smsProviders').val();
		url += '&activityId=' + VCMUtils.findNumber($('#detailContainer').attr('class'));
		url += '&showingId=' + $('#popupContent .smsShowings').val();
		$.getJSON(url, function(data) {
			$('#popupContent ' + (data.success ? '.smsSucces' : '.smsErreur')).html(data.msg);
			if (data.success) $('#popupContent .envoyerTexto').hide();
		});
	},
	
	/**
	 * On linked event click
	 * @param {Event} evt The Event object 
	 */
	onLinkedEventClick: function(evt) {
		var jEl = $(evt.currentTarget);
		var id = VCMUtils.findNumber(jEl.attr('id'));
		this._content.showEvent(id);
		return false;
	}
};


