/**
 * IPB3 Javascript				
 * --------------------------------------------
 * ips.statisticstabjs - Statistics Tab JS
 * (c) IPS, Inc 2010				
 * --------------------------------------------- 
 * Author: Invision Modding (Martin Aronsen)		
 **/

var _statisticsTab = window.IPBoard;

_statisticsTab.prototype.statisticsTab = 
{
	init: function()
	{
		Debug.write("Initializing ips.statisticsTab.js");
	},
	
	initCharts: function()
	{
		if ( $( 'chart_poststats' ) )
		{
			ipb.statisticsTab.initPostByHourChart();
		}
		
		if ( $( 'chart_popularBoards' ) )
		{
			ipb.statisticsTab.initPopularBoardsChart();
		}
	},
	
	initPostByHourChart: function()
	{
		json = $( 'chart_poststats_data' ).innerHTML.evalJSON(true);
		ipb.statisticsTab.drawPostsByHourChart( json );
	},
	
	initPopularBoardsChart: function()
	{
		json = $( 'chart_popularBoards_data' ).innerHTML.evalJSON(true);
		ipb.statisticsTab.drawPopularBoardsChart( json );
	},
	
	drawPostsByHourChart: function( json )
	{	
		options = {
			chart: {
				renderTo: 'chart_poststats',
				backgroundColor: $$( '#statstab .general_box div.post_body' )[0].getStyle( 'background-color' ),
				width: $$( '.recent_activity' )[0].getWidth()-30,
				margin: [50, 50, 90, 30]
			},
			title: {
				text: ''
			},
			xAxis: {
				categories: [],
				title: {
					text: ''
				},
				labels:{
					rotation:300,
					y:30
				}
			},
			yAxis: {
				title: {
					text: ''
				},
				labels: {
					formatter: function() {
						return Highcharts.numberFormat(this.value, 0);
					}
				},
         		showFirstLabel: false,
				min:0
			},/*
			legend: {
				left: 'auto',
				bottom: 'auto',
				right: '70px',
				top: '10px'
			},*/
			
            legend: {
                style: {
					left: 'auto',
					bottom: 'auto',
					right: '70px',
					top: '10px'
                },
                backgroundColor: '#FFFFFF',
                borderColor: '#CCC',
                borderWidth: 2,
                shadow: true,
                floating: true
            },
			tooltip: {
				enabled: true,
				formatter: function() {
					return '<strong>' + this.series.name + '</strong><br/>' + this.x +' : '+ this.y;
				}
			},
			labels: {
				style: {
					color: '#CCC'
				}
			},
			plotOptions: {
				line: {
					dataLabels: {
						color: '#CCC'
					},
					marker: {
						lineColor: '#333'
					}
				},
				spline: {
					marker: {
						lineColor: '#333',
						enabled: true
					}
				},
				scatter: {
					marker: {
						lineColor: '#333'
					}
				}
			},
			series: [{
				name: ipb.lang['postByTime-topics'],
				type: 'spline',
				data: []
			},{
				name: ipb.lang['postByTime-posts'],
				type: 'spline',
				data: []
			},{
				name: ipb.lang['postByTime-total'],
				type: 'spline',
				data: []
			}]
		};		

		json['hours'].each( function( data )
		{
			options.xAxis.categories.push( data['hour'] );
			options.series[0].data.push( parseInt( data['topicCount'] ) );
			options.series[1].data.push( parseInt( data['postCount'] ) );
			options.series[2].data.push( parseInt( data['totalCount'] ) );
		});
		
		chart = new Highcharts.Chart(options);


			
	},
	
	drawPopularBoardsChart: function( json )
	{
		options = {
			chart: {
				renderTo: 'chart_popularBoards',
				backgroundColor: $$( '#statstab .general_box div.post_body' )[0].getStyle( 'background-color' ),
                defaultSeriesType: 'bar',
                margin:[10,50,50,150],
				width: $$( '.recent_activity' )[0].getWidth()-30
			},
			title: {
				text: ''
			},
			credits: {
				enabled: true
			},
			xAxis: {
				categories: [],
				labels:{
					rotation:340,
					x:-5
				}
			},
			yAxis: {
				min: 0,
				tickInterval: 50,
                title: {
                    text: '',
           			align: 'high'
                }
            },
            legend: {
                style: {
					left: 'auto',
					bottom: 'auto',
					right: '70px',
					top: '10px'
                },
                backgroundColor: '#FFFFFF',
                borderColor: '#CCC',
                borderWidth: 2,
                shadow: true,
                floating: true
            },
			tooltip: {
				formatter: function() {
					return '<strong>' + this.series.name + '</strong><br/>' + this.x +' : '+ this.y;
				}
			},
			plotOptions: {
				bar: {
                    dataLabels: {
                        enabled: false,
                        color: 'auto'
                    },
                }
			},
			series: [{
				name: ipb.lang['popularBoards-topics'],
				data: []
			},{
				name: ipb.lang['popularBoards-posts'],
				data: []
			},{
				name: ipb.lang['popularBoards-total'],
				data: []
			}]
		};		

		json['boards'].each( function( data )
		{
			data['name'] = data['name'].replace( '&amp;', '&' );
			options.xAxis.categories.push( '<a style="font-size: 12px" href="' + data['url'] + '">' + data['name'] + '</a>' );
			options.series[1].data.push( parseInt( data['postCount'] ) );
			options.series[0].data.push( parseInt( data['topicCount'] ) );
			options.series[2].data.push( parseInt( data['totalCount'] ) );
		});
		
		chart2 = new Highcharts.Chart(options);
	}
}

ipb.statisticsTab.init();
