var PopularityHistory = {
	_container	: null,
	_content	: null,
	_pornstars	: [],
	_timeframe	: {unit:'day',value:7},
	_debug		: false,
	_timer		: null,
	_dragable	: 0,
	_dragTimer	: null,
	
	display : function(pornstarID)
	{
		if ( pornstarID )
			this._pornstars = [pornstarID];
		
		this._grabContainer();
		this._loadChart();
		
		if ( !this._timer )
			this._timer = window.setInterval("PopularityHistory._positionContainer()", 100);
	},
	
	compare : function(pornstarIDs)
	{
		if ( isNaN(pornstarIDs) )	
			for ( var i=0; i<pornstarIDs.length; i++ )
				this._pornstars.push(pornstarID);
		else
			this._pornstars.push(pornstarIDs);
			
		this.display();
	},
	
	setTimeFrame : function(timeframe)
	{
		timeframe				= timeframe.split(':');
		this._timeframe.unit	= timeframe[0];
		this._timeframe.value	= timeframe[1];
			
		this.display();
	},
	
	show : function()
	{
		if ( this._container == null )
			return false;
		
		this._container.style.display	= 'block';
		this._container.style.position	= 'absolute';
		this._container.style.zIndex	= getNextHighestZindex();
		this._dragable					= 0;
		this._positionContainer();
	},
	
	hide : function()
	{
		if ( this._container == null )
			return false;
		else
			return this._container.style.display = 'none';
	},
	
	initDrag : function(e)
	{
		try
		{
			if ( !e && document.all?true:false )
				e = window.event;
				
			var targ = e.target ? e.target : e.srcElement;
			if ( !targ.tagName || targ.tagName == 'A' || targ.tagName == 'SELECT' )
				return true;
		
			this._dragTimer = window.setTimeout(function()
			{
				PopularityHistory._dragable = 1;
				targ.onmouseup = function() { PopularityHistory._dragable = -1; };
			}, 1000);
			targ.onmouseup = function()
			{
				window.clearTimeout(PopularityHistory._dragTimer);
			};
				
			
			return true;
		}
		catch (err) { alert(err); }
	},
	
	_grabContainer : function()
	{
		if ( this._container && this._content )
			return true;
		
		this._container = document.getElementById('popularityHistory_ChartContainer');
		this._content = document.getElementById('popularityHistory_ChartContent');
		
		return ( this._container && this._content ); 
	},
	
	_positionContainer : function()
	{	
		var x = y = 0;
		
		if ( this._dragable == 0 )
		{
			var windowDims = fetchWindowDims();
			x = (( windowDims.width - this._container.offsetWidth ) / 2) + windowDims.scrollLeft;
			y = (( windowDims.height - this._container.offsetHeight ) / 2) + windowDims.scrollTop;
		}
		else if ( this._dragable == -1 )
			return;
		else
		{			
			var coords = getMouseCoords();
			x = coords.x - (this._container.offsetWidth/2);
			y = coords.y - 40;
		}
		
		this._container.style.left		= x + 'px';
		this._container.style.top		= y + 'px';
	},
	
	_loadChart : function()
	{
		var url = '/charts/graph/?timeframe[unit]=' + this._timeframe.unit + '&timeframe[value]=' + this._timeframe.value;
		for ( var i=0; i<this._pornstars.length; i++ )
			url += '&pornstarIDs[]=' + this._pornstars[i];
			
		url = url.replace(/&/g,'%26');
			
		if ( this._debug )
			url += '&debugMode=1';
			
		var chart = new FusionCharts("/scripts/pornstars/chart.swf", "PopularityHistoryChart", "100%", "480", "0", "0");
		chart.setDataURL(url);
		chart.render(this._content.id);
		
		this.show();
	}
}
