if(!window.Financier)
	Financier=new Object();
Financier.Debug = false;

// Gestion des binds
$(function(){
	$('div.graph-year-legend').bind('click', {display:"month"}, Financier.displayGraph);
	$('div.graph-month-legend').bind('click', {display:"year"}, Financier.displayGraph);
	
	$('div.graph-month-legend').trigger('click');
});

Financier.displayGraph = function(event)
{
	Financier.debug("--- Financier.displayGraph : Start ------------------------------------------");

	var display = event.data.display;
	
	var graphYear = $('div#graph-year')[0];
	var graphMonth = $('div#graph-month')[0];
	
	if(display == 'month')
	{
		$(graphMonth).show();
		$(graphYear).hide();
	}
	else
	{
		$(graphMonth).hide();
		$(graphYear).show();
	}

	Financier.debug("--- Financier.displayGraph : End --------------------------------------------");
}

Financier.debug = function(message)
{
	if(Financier.Debug == true)
	{
		console.log(message);
	}
}

/* Pluggin jQuery pour afficher le tableau de valeur */
$.fn.extend({
	tablevl: function(ajaxUrl, vl, dateStart, dateEnd) {
		return this.each(function() {
			new $.TableVlMacsf(this, ajaxUrl, vl, dateStart, dateEnd);
		});
	}
});

$.TableVlMacsf = function(link, paramAjaxUrl, paramVl, paramDateStart, paramDateEnd) {

	// Create $ object for link element
	var $link = $(link);

	var months = new Array("Janvier", "Fevrier", "Mars", "Avril", "Mai", "Juin", "Juillet", "Aout", "Septembre", "Octobre", "Novembre", "Decembre");

	var dateStart = getDateC(paramDateStart);
	var dateEnd = getDateC(paramDateEnd);
	var dateCur = dateEnd;
	var ajaxUrl = paramAjaxUrl;
	var vl = paramVl;
	var memoryTab = Array();
	var divActionTable = null;
	
	$link.click(function() {show(); return false;});

	function init() {
		$link.parent().append('<div class="action_table" style="display: none;"></div>');
		divActionTable = $(".action_table", $link.parent());
		
		
		divActionTable.append(	'<div class="action_table_line1">'+
					'	<div class="action_table_fermer"><a href="#">x</a></div>'+
					'	<div class="action_table_current"></div>'+
					'</div>');
		divActionTable.append(	'<div class="action_table_line2">'+
					'	<div class="action_table_prev_year"><a href="#">&lt;&lt;</a></div>'+
					'	<div class="action_table_prev_month"><a href="#">&lt;</a></div>'+
					'	<div class="action_table_today"><a href="#">Aujourd\'hui</a></div>'+
					'	<div class="action_table_next_month"><a href="#">&gt;</a></div>'+
					'	<div class="action_table_next_year"><a href="#">&gt;&gt;</a></div>'+
					'</div>');
		divActionTable.append(	'<div class="action_table_line3"></div>');
		$(".action_table_fermer a", divActionTable).click(function() { close(); return false; });
		$(".action_table_prev_year a", divActionTable).click(function() { goTo = new Date(dateCur); goTo.setYear(goTo.getFullYear()-1); go(goTo); return false; });
		$(".action_table_prev_month a", divActionTable).click(function() { goTo = new Date(dateCur); goTo.setMonth(goTo.getMonth()-1); go(goTo); return false; });
		$(".action_table_today a", divActionTable).click(function() { go(dateEnd); return false; });
		$(".action_table_next_month a", divActionTable).click(function() { goTo = new Date(dateCur); goTo.setMonth(goTo.getMonth()+1); go(goTo); return false; });
		$(".action_table_next_year a", divActionTable).click(function() { goTo = new Date(dateCur); goTo.setYear(goTo.getFullYear()+1); go(goTo); return false; });
		$link.attr("tablevl","ok");
		return true;
	};
	
	function go(where) {
		var myWhere = where;
		if(dateCompare(myWhere, dateStart)<0) myWhere = dateStart;
		else if(dateCompare(myWhere, dateEnd)>0) myWhere = dateEnd;
		
		var myMonth = myWhere.getMonth();
		var myYear = myWhere.getFullYear();
		
		if(!memoryTab['s'+myYear+myMonth]) {
			$(".action_table_line3", divActionTable).html('<img src="/file/resources/macsf/site/images/loadinfo.gif" alt="chargement" />');
			$.get(ajaxUrl, {'isin':vl, 'month':myMonth+1, 'year':myYear },
				function(data){
					$('.load_info', divActionTable).hide();
					memoryTab['s'+myYear+myMonth] = data;
					$(".action_table_line3", divActionTable).html(data);
			});
		}
		else {
			$(".action_table_line3", divActionTable).html(memoryTab['s'+myYear+myMonth]);
		}

		if(dateEquals(dateStart, myWhere)) {
			//on masque les fleches de gauche
			$(".action_table_prev_year a", divActionTable).hide();
			$(".action_table_prev_month a", divActionTable).hide();

		} else {
			if(dateEquals(dateStart, dateCur)) {
				//on affiche les fleches de gauche
				$(".action_table_prev_year a", divActionTable).show();
				$(".action_table_prev_month a", divActionTable).show();
			}
		
			if(dateEquals(dateEnd, myWhere)) {
				//on masque les fleches de droite
				$(".action_table_next_month a", divActionTable).hide();
				$(".action_table_next_year a", divActionTable).hide();
			}
			else if(dateEquals(dateEnd, dateCur)) {
				//on affiche les fleche de droite
				$(".action_table_next_month a", divActionTable).show();
				$(".action_table_next_year a", divActionTable).show();
			}
		}
		dateCur = myWhere;
		$(".action_table_current", divActionTable).html(months[myMonth]+" "+myYear);
	};
	
	function show() {
		if(!$link.attr("tablevl")) init();
		go(dateEnd);
		divActionTable.show();
	};
	
	function close() {
		divActionTable.hide();
	};
	
	function getDate(month, year) {
		var date = new Date(-3600000);
		date.setFullYear(year,month,1);
		return date;
	}
	function getDateC(completeDate) {
		nums = (''+completeDate).split("/")
		return getDate(nums[1]-1, nums[2]);
	}

	function dateEquals(date1, date2) {
		if(date1.getMonth() == date2.getMonth() && date1.getFullYear() == date2.getFullYear()) return true;
		else return false;
	}

	function dateCompare(date1, date2) {
		if(dateEquals(date1, date2)) return 0;
		if(date1.getFullYear() == date2.getFullYear()) {
			if(date1.getMonth() > date2.getMonth()) return 1;
			else return -1;
		} else if(date1.getFullYear() > date2.getFullYear()) return 1;
		else  return -1;
	}

};

