/* main.js
 *
 * Javascript code used by the main page
 */

$(document).ready(function(){
	
	//this file is included by both the front and main page, so we need to check first if we are on the main page
	//before trying to plot the chart, otherwise it will break the JS
	if($("#fms-main-chart").length > 0){
			
		//plot the chart
		var options = {
			grid  : { hoverable: true, clickable: false },
			bars  : { show: true },
			xaxis : { tickSize: 3, tickFormatter: function(val, axis){ return parseInt(val); } },
			yaxis : { tickFormatter: function (val, axis) { return val.toFixed(axis.tickDecimals) + " \u20AC"; } } 
	    };
			
		$.plot($("#fms-main-chart"), [ d ], options );
		
		//bind the Hover behaviour to the chart
		var previousPoint = null;
		$("#fms-main-chart").bind("plothover", function (event, pos, item) {
		
			if (item) {
				if (previousPoint != item.datapoint) {
					previousPoint = item.datapoint;
					
					$("#tooltip").remove();
					var x = item.datapoint[0].toFixed(2),
						y = item.datapoint[1].toFixed(2);
					
					showTooltip(item.pageX, item.pageY, "Day "+parseInt(x)+":<br/>"+y+" euros");
				}
			}
			else {
				$("#tooltip").remove();
				previousPoint = null;            
			}
	    });
	}
	
	//attach the click behavior for the code inputs
	$("input.copy_me").click(function(){ this.focus(); this.select(); });
});

//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// Function used to show the full details of a news entry.
function showNewsDetails(id){
	
	$("#news-resumes-wrapper").slideUp('normal', function(){
		$("#news-details-"+id).slideDown('normal', function(){
			$("#news-details-"+id).jScrollPane({showArrows:true, scrollbarWidth:12, wheelSpeed:40, reinitialiseOnImageLoad:true});
		});
	})
}

//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// Function used to hide the full details of a news entry showing back the entries listing.
function hideNewsDetails(id, height){

	$("#news-details-"+id).parent().slideUp('normal', function(){
		$("#news-details-"+id).hide();
		$("#news-details-"+id).jScrollPaneRemove();
		$("#news-details-"+id).css('overflow', 'auto');
		$("#news-details-"+id).css('height', height+'px');
		$("#news-resumes-wrapper").slideDown();
	});
}

//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// Function used to show a tooltip on the x - 15 and y - 45 coordinates.
 function showTooltip(x, y, contents) {

	$('<div id="tooltip">' + contents + '</div>').css( {
		position: 'absolute',
		display: 'none',
		top: y - 45,
		left: x - 15,
		border: '1px solid #fdd',
		padding: '2px',
		'background-color': '#fee',
		opacity: 0.80
	}).appendTo("body").fadeIn(200);
}