var RightTooltip = Class.create();
/**
* Responsible for right tooltip behaviour
*/
RightTooltip.prototype = {

  initialize: function(candidates, reports) {
		this.candidates = candidates ? candidates : [];
		this.reports = reports ? reports : [];
  },

	/**
	*	Adds candidates HTML block
	* Increases total items count
	*/
	addCandidateBlock: function(id, name) {
		var insert_string = "<tr id='rt_c"+id+"'><td>"+name+"</td><td style='width:30px;'><img src='/images/right_tooltip/right_tooltip_remove.png' alt='tooltip:Remove from order' onclick='removeCandidateFromOrder("+id+");'></td></tr>"
		new Insertion.Bottom('right_tooltip_candidates', insert_string);
		this.increaseHeadTotalCount();
		this.refreshTotalsFromOrderData();
	},

	/**
	*	Adds test HTML block
	* Increases total items count
	*/
	addTestBlock: function(id, name) {
		var insert_string = "<tr id='rt_c"+id+"'><td>"+name+"</td><td style='width:30px;'><img src='/images/right_tooltip/right_tooltip_remove.png' alt='tooltip:Remove from order' onclick='removeCandidateFromOrder("+id+");'></td></tr>"
		new Insertion.Bottom('right_tooltip_tests', insert_string);
		//this.increaseHeadTotalCount();
		this.refreshTotalsFromOrderData();
	},

	/**
	* Removes candidates HTML block
	* Decreases total items count
	*/
	removeCandidateBlock: function(element) {
		if (element) {
			element.remove();
		}
		this.decreaseHeadTotalCount();
		this.refreshTotalsFromOrderData();
	},

	/**
	* Increases total items count
	*/
	increaseHeadTotalCount: function() {
		var value = parseInt($("rt_total_items").innerHTML);
		value++;
		$("rt_head_info").show();
		this.setTotalItems(value);
		this.makeScrollable();
	},

	/**
	* Decreases total items count
	*/
	decreaseHeadTotalCount: function() {
		var value = parseInt($("rt_total_items").innerHTML);
		value = value > 0 ? value-1 : 0;
		this.setTotalItems(value);
		if (value==0) {
			$("rt_head_info").hide();
		}
		this.makeScrollable();
	},

	/**
	* Helper function used in increaseHeadTotalCount, decreaseHeadTotalCount
	*/
	setTotalItems: function(value) {
		$("rt_total_items").innerHTML = ""+value;
		$("rt_total_items_postfix").innerHTML = value == 1 ? "" : "s";
	},

	/**
	* Refreshes "Reports * Candidates = Totals" data from Order object
	*/
	refreshTotalsFromOrderData: function() {
		Order._recalculate();
		if ($("rt_candidates_number")) {
			$("rt_candidates_number").innerHTML = ""+Order.candidates_count;
		}
		$("rt_total_price").innerHTML = ""+Order.cost;
		$("rt_reports_price").innerHTML = ""+Order.reports_count;
	},

	/**
	* Adds scroll
	* Used when too many items in order.
	*/
	makeScrollable: function() {
		max_height = 300;
		var table_container = $("right_tooltip_container");
    table_container.style.height="auto";
		var table = $("right_tooltip_candidates");
		var container_dimensions =  table_container.getDimensions();

		if (container_dimensions.height >= max_height) {
			var table_dimensions = table_container.getDimensions();
	    table_container.style.height=max_height+"px";
	    table_container.style.overflow="auto";
			table.style.width = (table_dimensions.width-17)+"px";
		} else {
	    table_container.style.height=container_dimensions.height+"px";
	    table_container.style.overflow="visible";
			table.style.width = "100%";
		}
	scrollHandler();
	}


}

/* right tooltip instance creating */
var rt = new RightTooltip();

