$(document).ready(function(){

	closedPositions('day');
	
});

function closedPositions(type)
{
	
	if(type == 'day')
	{
		next = 'cmonth';
		head = 'Profit (pips) closed positions on Current Day';
	}
	/*else if(type == 'week')
	{
		next = 'month';
		head = 'Profit (pips) closed positions on Last Week';
	}
	else if(type == 'month')
	{
		next = 'year';
		
		var d = new Date();
		m = d.SubtractMonth(1);

		month = m.getMonth()+1;
		month = month<10?'0'+month:month;
		year = m.getFullYear();
    	prevMonth = month+'/'+year;
		
		head = 'Profit (pips) closed positions on Last Month - '+prevMonth;
	}*/
	else if(type == 'cmonth')
	{
		next = 'year';
		head = 'Profit (pips) closed positions on Current Month';
	}	
	/*else if(type == 'quarter')
	{
		next = 'year';
		
		var d_s = new Date();
		m_s = d_s.SubtractMonth(2);
		month_start = m_s.getMonth();
		month_start = month_start<10?'0'+month_start:month_start;
		year_start = m_s.getFullYear();
		
		var d_e = new Date();
		m_e = d_e.SubtractMonth(0);
		month_end = m_e.getMonth();
		month_end = month_end<10?'0'+month_end:month_end;
		year_end = m_e.getFullYear();

		quarter = month_start+'/'+year_start+' - '+month_end+'/'+year_end;
		
		head = 'Profit (pips) closed positions on Last 3 Month';
	}	*/
	else if(type == 'year')
	{
		next = 'day';
		head = 'Profit (pips) closed positions on Last 12 Month';
	}

	$.getJSON('/sites/all/sumator.php?type='+type+'&'+Math.random(),
		function(data) {
			$('#dynStatLabel').text(head);
			$.each(data, function(key, val) {
				var id = key.replace(/\//gi, "");
				$('#closedPositions #'+id).text(val === null?0:val);
		});
	});
	
	setTimeout("closedPositions('"+next+"')", 20000);
}

Date.prototype.SubtractMonth = function(numberOfMonths) {
	var d = this;
	d.setMonth(d.getMonth() - numberOfMonths);
	d.setDate(1);
	return d;
}

