//namespace definition
var Lsi = Lsi || {};
/**
 * LsiSelection manage selection on client side with ajax requests.
 */
Lsi.Selection = function(config) {
	//declaration and attributes
	var defaults = {
		
		selector          : "body",
		textDialogAdd    : "",
		textDialogRemove : "",
		textButtonAdd     : "",
		textButtonRemove  : ""
		
	};

	$.extend(this, defaults, config);

};


Lsi.Selection.prototype = {
	// methods
	
	/**
	 *Init the front end selection
	 *
	 *@return void
	 *
	 **/
	init : function() {
		
		var me = this;
		
		//bind event on result's page
		$(this.selector).click(function(e){
			me.onclickResult(e);
		});
		
		//bind drop down menu events
		$('.selection-summary').
		bind('mouseenter', function(e) {
			
			$('.selection-summary-ads').stop(true, true).fadeIn();
			
		}).
		bind('mouseleave', function(e) {
		
			$('.selection-summary-ads').stop(true, true).fadeOut();
			
		});
		
	},
	
	/**
	 * Method called as callback by onclick events
	 * 
	 * take the Event object in param
	 * 
	 * @param e Event
	 * @return void
	 */
	onclickResult : function(e) {
		
		
		//get the Dom element on which user click
		var target = $(e.target);
		
		var adWrapper;

		//get parent selector if we have children in .action-selection element
		var realTarget = target.closest(".action-selection");

		//if we click on an "add-selection button"
		if(realTarget.hasClass("add-selection")) {

			adWrapper = target.parents(".an-ad-wrapper");
			this.actionSelection(realTarget, adWrapper.attr("rel"), "add");

			if (this.animateOnAdd) {
				var s = $('.selection-summary').first();
				var p = $('.photo', adWrapper).first();
				var p2 = p.clone();
				$(document.body).append(p2);
				p2.addClass('selection-animation-photo');
				p2.css({
						position : "absolute",
						display  : "block",
						left     : p.offset().left,
						top      : p.offset().top,
						width    : p.width(),
						height   : p.height(),
						zIndex   : 20000
					});
				p2.animate({
						left   : s.offset().left,
						top    : s.offset().top,
						width  : s.width(),
						height : s.height()
					}, {
						duration     : 1000,
						easing       : 'easeOutBounce',
						complete     : function() {
							p2.remove();
						}
					});
			}

		}
		//if we click on a "remove-selection button"
		if(realTarget.hasClass("remove-selection")) {

			adWrapper = target.parents(".an-ad-wrapper");
			this.actionSelection(realTarget, adWrapper.attr("rel"), "remove");
		}
		
		
		//if we click on a "remove-selection button" in recap page
		if(realTarget.hasClass("remove-selection-all")) {

			adWrapper = target.parents(".line-selection");
			this.actionSelection(adWrapper, adWrapper.attr("rel"), "remove");
		}
		
		
	},
	
	/**
	 * make ajax request for adding/removing ad to selection
	 * 
	 * 
	 * @param adId 
	 * @param element jQuery element witch may be updated
	 * @param action "add|remove"
	 * @return void
	 */
	 actionSelection : function(element, adId, action) {
		 
		if (adId == "")
			return;
		
		$.ajax({
			url      : "/ajax/selection/"+action+"/"+adId,
			context  : this,
			dataType : "json",
			success  : function(data, textStatus, jqXHR){

				if (data.success)
					this.updateSelection(element);
			
			},
			error    : function(jqXHR, textStatus, errorThrown){
				
				
			}
		});
		
	},
	
	
	/**
	 * Actualize elements displaying infos about selection
	 * 
	 * 
	 * @param element jQuery object to update
	 * @return void 
	 */
	updateSelection : function(element) {
				
		$.ajax({
		  url      : "/ajax/selection/summary",
		  success  : function(data, textStatus, jqXHR){
			  
			  $(".selection-summary").html(data);
			  
		  },
		  error    : function(jqXHR, textStatus, errorThrown){
			  
		  }
		});
		
		if(element == null)
			//if no element were sent
			return;
		
		if (element.hasClass("add-selection")) {
			
			Lsi.Alert.show(this.textDialogAdd ,{
					wrapClass : 'lsi-selection-confirm',
					duration  : 1500
				});
			
			element.removeClass("add-selection");
			element.addClass("remove-selection");
			
			//permut text
			element.children("span").html(this.textButtonRemove);
			
			
				
			
			
		} else if(element.hasClass("remove-selection")) {

			Lsi.Alert.show(this.textDialogRemove ,{
					wrapClass : 'lsi-selection-confirm',
					duration  : 1500
				});
		  
			element.removeClass("remove-selection");
			element.addClass("add-selection");
			
			
			
			//permut text
			element.children("span").html(this.textButtonAdd);
		  
			

		} else if(element.hasClass("line-selection")) {

		  element.fadeOut();

		}

	}

	
};

