// Globals
// ------------------------------

var show_trend_lines = 1;

var show_aa_index_0 = 1;
var show_custom_index_1 = 0;
var show_custom_index_2 = 0;

var lines = new Array();
	
	lines[0] = 0;
	lines[1] = 0;
	lines[2] = 0;

var data_point = 0;
var data_point_line = 0;
var data_point_line_down = 0;
var arrow_down = 0;
var data_point_text = '';

var tlines = new Array();

	tlines[0] = 0;
	tlines[1] = 0;
	tlines[2] = 0;


line_color_0 = '#E7342D';	
line_color_1 = '#006FC1';	
line_color_2 = '#3CCC3C';	

/*
line_color_0 = '#ff3340';	
line_color_1 = '#006FC1';	
line_color_2 = '#00eea2';
*/

var nullText;




// On load
// ------------------------------
$(document).ready(function(){
	
	// Prepare, print popup
	$('#print_graph_link').click(function(){

		show_custom_index_1 = $('#line_ci_1_toggle').is(':checked')? 1 : 0;
		show_custom_index_2 = $('#line_ci_2_toggle').is(':checked')? 1 : 0;
		
		dropsize = $('#dropsize').val();
				
			var edu_inc_gen_1 = $('#update_edu_inc_gen_list_target_1 dt a span').html();
			var age_1 = $('#update_age_list_target_1 dt a span').html();
			var race_1 = $('#update_race_list_target_1 dt a span').html();
			var hht_1 = $('#update_household_list_target_1 dt a span').html();				

			var edu_inc_gen_2 = $('#update_edu_inc_gen_list_target_2 dt a span').html();
			var age_2 = $('#update_age_list_target_2 dt a span').html();
			var race_2 = $('#update_race_list_target_2 dt a span').html();
			var hht_2 = $('#update_household_list_target_2 dt a span').html();				
		
		var index_type = getIndexType();
		
		popitup('http://economicsecurityindex.org/print_reports.php?dropsize='+dropsize+'&index_type='+index_type+'&show_index_1='+show_custom_index_1+'&eig_1='+edu_inc_gen_1+'&age_1='+age_1+'&race_1='+race_1+'&hht_1='+hht_1+'&show_index_2='+show_custom_index_2+'&eig_2='+edu_inc_gen_2+'&age_2='+age_2+'&race_2='+race_2+'&hht_2='+hht_2+'', 830, 800);

		return false;

	});
	
	$('#print_this_page_link').click(function(){
		window.print();
		return false;
	});

	$('#close_this_window_link').click(function(){
		window.close();
		return false;
	});


    
    // Toggles "index checkboxes"
	$('#checkbox_include_moop').click(function(){
    	if( $(this).is(':checked') ){
    		$('#checkbox_include_debt').parent().fadeIn();
    		$('#checkbox_include_buffer').parent().fadeIn();
    	}
    	else {
    		$('#checkbox_include_debt').parent().fadeOut();
    		$('#checkbox_include_buffer').parent().fadeOut();
    		$('#checkbox_include_debt').attr('checked', false);
    		$('#checkbox_include_buffer').attr('checked', false);
    	}
    });

	$('#checkbox_include_debt').click(function(){
    	( $(this).is(':checked') )? false : $('#checkbox_include_buffer').attr('checked', false);
    });

	$('#checkbox_include_buffer').click(function(){
    	( $(this).is(':checked') )? $('#checkbox_include_debt').attr('checked', true) : false;
    });    


	$('.checkbox').click(function(){		
				
	    // Toggles trend lines
	    if( $(this).attr('id') == 'checkbox_trend_toggle'){
	    	if( $(this).is(':checked') ){
	    		show_trend_lines = 1;
	    	}
	    	else {
	    		show_trend_lines = 0;
	    		tlines[0].attr({ stroke: 'transparent' });
	    		if(show_custom_index_1 == 1){ tlines[1].attr({ stroke: 'transparent' }); }
	    		if(show_custom_index_2 == 1){ tlines[2].attr({ stroke: 'transparent' }); }
	    	}
		}
		
		var dropsize = $('#dropsize').val();
		getAllAmericanData(paper, 'update', dropsize, line_color_0);
		if(show_custom_index_1 == 1 && $('#line_ci_1_toggle').is(':checked') ){ getDemographicData(paper, 'update', 1, dropsize, line_color_1); }
		if(show_custom_index_2 == 1 && $('#line_ci_2_toggle').is(':checked') ){ getDemographicData(paper, 'update', 2, dropsize, line_color_2); }
		
		// remove loading graphic if no demographic data (this usually happens on complete of demographic loading)
		$('#graph_loading').show('slow', function(){
			if(show_custom_index_1 == 0){
				$('#graph_loading').fadeOut();
			}
		});		
		
	});
   
    
	
	
	// TOGGLE line and tline
	$('#line_aa_toggle').click(function(){
		
		if( ! $(this).is(':checked') && show_aa_index_0 == 1){
			
			lines[0].hide();
			tlines[0].hide();
			//lines[0].attr({ stroke: 'transparent' });
			//tlines[0].attr({ stroke: 'transparent' });
		}
		else if( $(this).is(':checked') && show_aa_index_0 == 1 ){

			//lines[0].attr({ stroke: line_color_0 });
			lines[0].show();
			
			if(show_trend_lines){ 
				//tlines[0].attr({ stroke: line_color_0 }); 
				tlines[0].show();
				}
		}
	});

	$('#line_ci_1_toggle').click(function(){
		
		if( ! $(this).is(':checked') && show_custom_index_1 == 1){
			lines[1].hide();
			tlines[1].hide();
			//lines[1].attr({ stroke: 'transparent' });
			//tlines[1].attr({ stroke: 'transparent' });
			$('#index_1_trans').val('1');
		}
		else if( $(this).is(':checked') && show_custom_index_1 == 1 ){
			getDemographicData(paper, 'update', 1, dropsize, line_color_1);
			lines[1].show();
			tlines[1].show();
			$('#index_1_trans').val('0');
		}
	});

	$('#line_ci_2_toggle').click(function(){
		if( ! $(this).is(':checked') && show_custom_index_2 == 1){
			//lines[2].attr({ stroke: 'transparent' });
			//tlines[2].attr({ stroke: 'transparent' });
			lines[2].hide();
			tlines[2].hide();
			$('#index_2_trans').val('1');
		}
		else if( $(this).is(':checked') && show_custom_index_2 == 1 ){
			lines[2].show();
			tlines[2].show();
			getDemographicData(paper, 'update', 2, dropsize, line_color_2);
			$('#index_2_trans').val('0');
		}
	});

    
    
    // Toggles the drop down display
	$(".dropdown dt a").click(function() {
		
		// First, hide all the dropdowns. 
		// This prevents the user from 
		// selecting one dropdown, then another, leaving the first open.
		$("dd ul").hide();
		
		var great_parent_id = $(this).parent().parent().attr('id');
		
		selected_html = $(this).html();
		$("#"+great_parent_id+" dd ul li").each(function(){
			$(this).find('a').css('display', 'block');
			
			if(selected_html == $(this).find('a').html()){
				$(this).find('a').css('display', 'none');
			}
		});
		
		$("#"+great_parent_id+" dd ul").toggle();

		return false;
	});
	
	
	// Replaces the "selected" text with the "clicked" text
	$(".dropdown dd ul li a").click(function() {

		var great_parent_id = $(this).parent().parent().parent().parent().attr('id');		
		
		var text = $(this).html();
		$("#"+great_parent_id+" dt a").html(text);
		$("#"+great_parent_id+" dd ul").hide();
		
        var real_select_id = great_parent_id.replace('_target', '');
		var source = $("#"+real_select_id+"");
		source.val($(this).find("span.value").html());
		
		return false;
	});


	// Hides the drop down if the user clicks on anything else
	$(document).bind('click', function(e) {
		var $clicked = $(e.target);
		if (! $clicked.parents().hasClass("dropdown")){
			$(".dropdown dd ul").hide();
		}
	});

	
	// When Primary tabs are clicked, build a link for new display, redirect to it
	$('.dropsize_tab').click(function(){
						
		if(show_custom_index_1){
			var i1t = $('#index_1_trans').val();
			var edu_inc_gen_1 = $('#update_edu_inc_gen_list_target_1 dt a span').html();
			var age_1 = $('#update_age_list_target_1 dt a span').html();
			var race_1 = $('#update_race_list_target_1 dt a span').html();
			var hht_1 = $('#update_household_list_target_1 dt a span').html();				
		}

		if(show_custom_index_2){
			var i2t = $('#index_2_trans').val();
			var edu_inc_gen_2 = $('#update_edu_inc_gen_list_target_2 dt a span').html();
			var age_2 = $('#update_age_list_target_2 dt a span').html();
			var race_2 = $('#update_race_list_target_2 dt a span').html();
			var hht_2 = $('#update_household_list_target_2 dt a span').html();				
		}
		
		var index_type = getIndexType();
			
		new_view = $(this).attr('href');
		
		new_view = 'http://economicsecurityindex.org/'+new_view+'&i1t='+i1t+'&i2t='+i2t+'&index_type='+index_type+'&show_index_1='+show_custom_index_1+'&eig_1='+edu_inc_gen_1+'&age_1='+age_1+'&race_1='+race_1+'&hht_1='+hht_1+'&show_index_2='+show_custom_index_2+'&eig_2='+edu_inc_gen_2+'&age_2='+age_2+'&race_2='+race_2+'&hht_2='+hht_2;
		
		window.location = new_view;

		return false;
		
	});
	
	
	
	
	
	


	if($('#paper').length){
	
		// Setup Dims
		paperWidth = $('#paper').css('width').replace('px', '');
		paperHeight = $('#paper').css('height').replace('px', '');	
		
		xScale = 25;	// Number of cols
		yScale = 5;		// Number of rows
		
		var dropsize = $('#dropsize').val();
		switch(dropsize){
			
			case '1o4': yRange = 25; break;
			case '1o3': yRange = 20; break;
			case '1o2': yRange = 10; break;
			
			default: yRange = 25;
		}

		
		bottomLabelHeight = 40;
		
		graphWidth = paperWidth;
		graphHeight = paperHeight-bottomLabelHeight;
	
		// Create Canvas	
		var paper = Raphael('paper', paperWidth, paperHeight);
	
		// Draw Graph area
		drawVerticalBoxes(paper, graphWidth, graphHeight, xScale, yScale, bottomLabelHeight);
		drawHorizontalBoxes(paper, graphWidth, graphHeight, xScale, yScale, bottomLabelHeight);
		drawDecorativeLine(paper, graphHeight, graphWidth);	
		
		drawNullData(paper, graphWidth, graphHeight, xScale, yScale);
	
		// Get Initial Data
		var dropsize = $('#dropsize').val();
		getAllAmericanData(paper, 'init', dropsize, line_color_0);
		
		show_custom_index_1 = $('#show_index_1').val();
		show_custom_index_2 = $('#show_index_2').val();

		i1t = $('#index_1_trans').val();
		i2t = $('#index_2_trans').val();
				
		if(show_custom_index_1 == 1){
			type = 'init';
			if(i1t == '1'){
				getDemographicData(paper, type, 1, dropsize, 'transparent');
			}
			else {
				$('#line_ci_1_toggle').attr('checked', 'checked');
				getDemographicData(paper, type, 1, dropsize, line_color_1);
			}
		}	
		if(show_custom_index_2 == 1){
			type = 'init';
			if(i2t == '1'){
				getDemographicData(paper, type, 2, dropsize, 'transparent');
			}
			else {
				$('#line_ci_2_toggle').attr('checked', 'checked');
				getDemographicData(paper, type, 2, dropsize, line_color_2);
			}
		}	
	
	
		$('.dd_set_1 dd ul li a').click(function(){
						
			var dropsize = $('#dropsize').val();
					
			if(show_custom_index_1 == 0 &&  ! $('#line_ci_1_toggle').is(':checked')){
				type = 'init';
				$('#line_ci_1_toggle').attr('checked', true);
				show_custom_index_1 = 1;
				getDemographicData(paper, type, 1, dropsize, line_color_1);
			}
			else if (show_custom_index_1 == 0 &&  $('#line_ci_1_toggle').is(':checked')){
				type = 'init';
				show_custom_index_1 = 1;
				getDemographicData(paper, type, 1, dropsize, line_color_1);		
			}
			else {
				type = 'update';
				$('#line_ci_1_toggle').attr('checked', true);
				getDemographicData(paper, type, 1, dropsize, line_color_1);
			}
			
			$('#graph_loading').show();
			
		});
	
		$('.dd_set_2 dd ul li a').click(function(){
	
			var dropsize = $('#dropsize').val();
	
			if(show_custom_index_2 == 0 &&  ! $('#line_ci_2_toggle').is(':checked')){
				type = 'init';
				$('#line_ci_2_toggle').attr('checked', true);
				show_custom_index_2 = 1;
				getDemographicData(paper, type, 2, dropsize, line_color_2);
			}
			else if (show_custom_index_2 == 0 &&  $('#line_ci_2_toggle').is(':checked')){
				type = 'init';
				show_custom_index_2 = 1;
				getDemographicData(paper, type, 2, dropsize, line_color_2);		
			}
			else {
				type = 'update';
				$('#line_ci_2_toggle').attr('checked', true);
				getDemographicData(paper, type, 2, dropsize, line_color_2);
			}
			
			$('#graph_loading').show();
			
		});
	
	}
	
	
});







// Get All American Data
// ------------------------------
function getAllAmericanData(paper, type, dropsize, line_color_0){	

	var index_type = getIndexType();
	
	$.ajax ({	
		type: 'post',
		url: 'actions/get_data.php', 
		data: 'type=allamerican&dropsize='+dropsize+'&index_type='+index_type+'', 
		success: function(data) {
			
			var x = [], y = [];
			
			data = data.split('|||');
			
			years = data[0];
			fits = data[1];
			
			years = years.split(',');
			fits = fits.split(',');

			for(var i=0; i<years.length; i++){
				year = years[i].split(':');
				x[i] = i;				
				y[i] = year[1]; // year with no data will be 0			
			}
			
			// Create "All Americans" Data
			newPath_allAmericans = '';
			transX = graphWidth/xScale;
			
			newPath_allAmericans = findNewPath(x,y,newPath_allAmericans);
			
			for(var i=0; i < (xScale-1); i++){
				
				// Draw Explanatory Text and Arrow
				if(i == (xScale-2)){
					
					switch( dropsize ){
						case '1o4' : dropsize_text = 'one fourth'; break;
						case '1o3' : dropsize_text = 'one third'; break;
						case '1o2' : dropsize_text = 'half'; break;
					}

					// Draw Data Point/Arrow
					if(type=='init'){
						data_point = paper.circle(x1, y1, 5).attr({"fill": line_color_0, "stroke-width": 0});
						data_point_text = paper
						.text( graphWidth - 320, (20), "In 2009, more than "+Math.floor(y[last_known_x+1])+" of every 100 Americans\nlost "+dropsize_text+" of their income (projected)")
						.attr({ "font-family" : "Fontin Sans, Georgia", "font-size" : "12px", "font-weight" : "bold", fill: "#888", 'text-anchor': 'start' });
						data_point_line = paper.path("M"+(graphWidth/2)+",35L"+( graphWidth - (transX/2) )+",35").attr({ stroke: '#888', "stroke-width": 1, "stroke-dasharray": "- " });
						data_point_line_down = paper.path("M"+(graphWidth - 13)+",35L"+(graphWidth - 13)+","+(y1 - 15)+"").attr({ stroke: '#444', "stroke-width": 1, "stroke-dasharray": ". " });
						arrow_down = paper.path("M"+(graphWidth - 18)+","+(y1 - 15)+" L"+(graphWidth - 8)+","+(y1 - 15)+", M"+(graphWidth - 8)+","+(y1 - 15)+" L"+(graphWidth - 13)+","+(y1 - 10)+" M"+(graphWidth - 13)+","+(y1 - 10)+" L"+(graphWidth - 18)+","+(y1 - 15)+"").attr({ stroke: '#888', "stroke-width": 1 });
					}
					else {
						data_point.animate({cx: x1, cy: y1 }, 600, '<>', function(){ });
						data_point_text.attr({ text: "In 2009, more than "+Math.floor(y[last_known_x+1])+" of every 100 Americans\nlost "+dropsize_text+" of their income (projected)" });
						data_point_line_down.animate({path: "M"+(graphWidth - 13)+",35L"+(graphWidth - 13)+","+(y1 - 15)+"" }, 900, '<>', function(){ });
						arrow_down.animate({path: "M"+(graphWidth - 18)+","+(y1 - 15)+" L"+(graphWidth - 8)+","+(y1 - 15)+", M"+(graphWidth - 8)+","+(y1 - 15)+" L"+(graphWidth - 13)+","+(y1 - 10)+" M"+(graphWidth - 13)+","+(y1 - 10)+" L"+(graphWidth - 18)+","+(y1 - 15)+"" }, 900, '<>', function(){ });
					}
				}
			}
			
			
			lines[0] = drawDataLine(paper, dropsize, type, lines[0], 0, newPath_allAmericans, line_color_0);

			tlines[0] = showTrendLines(paper, type, tlines[0], 0, line_color_0, show_trend_lines);
			
			if( 
				( ! $('#line_ci_1_toggle').is(':checked') && ! $('#line_ci_2_toggle').is(':checked') ) 
					|| 
				(show_custom_index_1 == 0 && show_custom_index_2 == 0)
			  ){
				
				$('#graph_loading').fadeOut();
			}			
			
		}
		
	});

}


// Get demographic line
// ------------------------------
function getDemographicData(paper, type, num, dropsize, line_color){
	
	var age = $('#update_age_list_target_'+num+' dt a span').html();
	var race = $('#update_race_list_target_'+num+' dt a span').html();
	var hht = $('#update_household_list_target_'+num+' dt a span').html();
		
	var edu_inc_gen = $('#update_edu_inc_gen_list_target_'+num+' dt a span').html();

	var index_type = getIndexType();
	
	
	
	
	$.ajax ({	
		type: 'post',
		url: 'actions/get_data.php', 
		data: 'type=demographic&dropsize='+dropsize+'&age='+age+'&race='+race+'&hht='+hht+'&edu_inc_gen='+edu_inc_gen+'&index_type='+index_type+'', 
		success: function(data) {
			
			$('#graph_loading').fadeOut();
			
			// If data returned nothing, meaning this combination has no data
			// display nullText and hide the data line
			if(data == 'NULL'){
				nullText.show();
				if(lines[num]){
					//lines[num].attr({ stroke: 'transparent' });
					//tlines[num].attr({ stroke: 'transparent' });
					lines[num].hide();
					tlines[num].hide();
				}
			}
			// Otherwise, draw the line
			else {
				
				// Hide nullText message
				nullText.hide();
				
				var x = [], y = [];
				
				data = data.split('|||');
				
				years = data[0];
				fits = data[1];
				
				years = years.split(',');
				fits = fits.split(',');
	
				for(var i=0; i<years.length; i++){
					year = years[i].split(':');
					x[i] = i;
					y[i] = year[1]; // year with no data will be 0
				}
				
				newPath = '';
				transX = graphWidth/xScale;
				
				newPath = findNewPath(x,y,newPath);

				lines[num] = drawDataLine(paper, dropsize, type, lines[num], num, newPath, line_color);
				
				tlines[num] = showTrendLines(paper, type, tlines[num], num, line_color, show_trend_lines);
								
			}
					
		}
		
	});	
		
}



// Find New Path
function findNewPath(x,y,newPath){
	
	// yRange
	// graphHeight
		
	last_known_x = 0;
	
	for(var i=0; i < (xScale-1); i++){
		if(y[i-1] == 0 && y[i] != 0 && y[last_known_x] != 0 ){
						
			if(y[last_known_x+1] != 0 ){
				last_known_x = last_known_x+1;
			}
			
			// Col width = graphWidth / xScale
			x0 = x[last_known_x] + ((transX)/2) + ( (transX - 1) * last_known_x);
			y0 = graphHeight - ( (graphHeight/yRange) * y[last_known_x] );
		
			x1 = x[i-1] + ((transX)/2) + ( (transX - 1) * (i-1)) + (transX);
			y1 = graphHeight - ( (graphHeight/yRange) * y[i] );
										
			newPath += "M"+(x0)+","+(y0)+" L"+(x1)+","+(y1)+" ";
			
			// add a "point"
			newPath += "M"+(x0)+","+(y0 - 2)+" L"+(x0)+","+(y0 + 2)+" ";
		}
							
		if(y[i+1] != 0 && y[i] != 0){
					
			// Col width = graphWidth / xScale
			x0 = x[i] + ((transX)/2) + ( (transX - 1) * i);
			y0 = graphHeight - ( (graphHeight/yRange) * y[i] );
		
			x1 = x[i+1] + ((transX)/2) + ( (transX - 1) * i) + (transX - 1);
			y1 = graphHeight - ( (graphHeight/yRange) * y[i+1] );
									
			newPath += "M"+(x0)+","+(y0)+" L"+(x1)+","+(y1)+" ";

			// add a "point"
			newPath += "M"+(x0)+","+(y0 - 2)+" L"+(x0)+","+(y0 + 2)+" ";
						
			last_known_x = i;
		}
		
		if(y[i-1] == 0 && y[i+1] == 0 && y[i] != 0){
			last_known_x = i;
		}
	
		if(y[i-1] != 0 && y[i+1] != 0 && y[i] == 0 && i != 0){
			last_known_x = i-1;
		}
	}
	
	return newPath;
}

// Draw Data Line
// ------------------------------
function drawDataLine(paper, dropsize, type, line, num, path, line_color){
	if(type=='init'){	
		line = paper.path(path).attr({ stroke: line_color, "stroke-width": 2, opacity: '0.9' }).animate({}, 800, '<>').toFront();
	}
	else {
		line.show().attr({ stroke: line_color }).animate({path: ""+path+"" }, 800, '<>').toFront(); 
	}	
	return line;
}

// Show Trend Lines
// ------------------------------
function showTrendLines(paper, type, line, num, line_color, show_trend_lines){
	fits_x0 = ((transX)/2);
	fits_y0 = graphHeight - ( (graphHeight/yRange) * fits[0] );
	fits_x1 = 22 + ((transX)/2) + ( (transX - 1) * 22);
	fits_y1 = graphHeight - ( (graphHeight/yRange) * fits[1] );
	
	if(type=='init'){
		line = paper.path("M"+fits_x0+","+fits_y0+"L"+fits_x1+","+fits_y1+"").attr({ stroke: line_color, "stroke-width": 1 });
	}
	else {
		line.show().attr({ stroke: line_color }).animate({path: "M"+fits_x0+","+fits_y0+"L"+fits_x1+","+fits_y1+"" }, 800, '<>', function(){ });
	}
	
	if(!show_trend_lines){ line.attr({ stroke: 'transparent' }); }
	
	return line;
}

// Get Index Type
// ------------------------------
function getIndexType(){
	var index_type = "income%2Bannuity";
	if($('#checkbox_include_moop').is(':checked')){		index_type = "income%2Bannuity%2Bmoop"; }
	if($('#checkbox_include_debt').is(':checked')){		index_type = "income%2Bannuity%2Bmoop%2Bdebt"; }	
	if($('#checkbox_include_buffer').is(':checked')){	index_type = "income%2Bannuity%2Bmoop%2Bdebt,buffer"; }
	return index_type;	
}


// Draw Null Data
// ------------------------------
function drawNullData(paper, graphWidth, graphHeight, xScale, yScale){
	
	nullText = paper
	.text( ( (graphWidth/2) ), (graphHeight/4), "There is not sufficient data for this combination.")
	.attr({ "font-family" : "Fontin Sans, Georgia", "font-size" : "14px", fill: "#444" });
	
	// Set nullText to hidden initially
	nullText.hide();
}

// Draw Vertical Boxes
// ------------------------------
function drawVerticalBoxes(paper, graphWidth, graphHeight, xScale, yScale, bottomLabelHeight){
	for(var i=0; i<xScale; i++){	
		(i%2)? color = "#ddd" : color = "#eee";
		(i%2)? color2 = "#e5e5e5" : color2 = "#f4f4f4";
		(i%2)? color4 = "#777" : color4 = "#777";
		
		if(i < xScale -2){
			color_bottom = color2;
			font_color = "#444";
		}
		else {
			color_bottom = color4;
			font_color = "#fff";
		}
		
		paper
		.rect(( ((graphWidth/xScale) * i)), 0, (graphWidth/xScale), graphHeight)
		.attr({ stroke: color, fill: color });

		paper
		.rect(( ((graphWidth/xScale) * i)), graphHeight, (graphWidth/xScale), bottomLabelHeight)
		.attr({ stroke: color2, fill: color_bottom });
		
		// Add bottom Labels
		year = ""+(1985+i)+"";
		year = year.split('');
		year = "'"+year[2]+""+year[3];
		
		paper
		.text( (((graphWidth/xScale) * i) + (graphWidth/xScale/2) ), (graphHeight + (bottomLabelHeight/2)), ""+year+"")
		.attr({ "font-family" : "Fontin Sans, Georgia", "font-size" : "11px", fill: font_color });		
	}
}

// Draw Horizontal boxes (or lines)
// ------------------------------
function drawHorizontalBoxes(paper, graphWidth, graphHeight, xScale, yScale, bottomLabelHeight){
	addHorizontalPaths = '';
	
	for(var i=0; i<yScale+1; i++){
		addLine = "M0,"+(i*(graphHeight / yScale))+" L"+(graphWidth)+","+(i*(graphHeight / yScale))+"";
		addHorizontalPaths += addLine;
	}
	
	var gridy = paper.path(addHorizontalPaths).attr({ stroke: "#ccc" });
}

// Draw Decorative line
// ------------------------------
function drawDecorativeLine(paper, graphHeight, graphWidth){
	paper.rect(0, (graphHeight), graphWidth, 2).attr({ stroke: "#ccc", fill: "#ccc" });	
}

// Popup
// ------------------------------
function popitup(url, height, width){
	newwindow=window.open(url,'name','height='+height+', width='+width+'');
	if(window.focus){newwindow.focus()}
	return false;
}

