var CPosition=new Class({
	'max':			50,
	'interval':		5,
	
	'nextPositions': function()
	{
		var type;
		var keyword=this.keywords[this.keywordInd];
		var params='act=getPosition&host='+this.host;
		params+='&max='+this.max;
		for(var i=0; i<this.types.length; i++)
		{
			type=this.types[i];
			if(!this.typeParams[type]) this.typeParams[type]={'start': 0, 'search': true};
			if(this.typeParams[type].search)
			{
				params+='&types[]='+type;
				params+='&start['+type+']='+this.typeParams[type].start;
			}
		}
		params+='&page='+this.page;
		params+='&keyword='+this.keywords[this.keywordInd];
		this.page++;
		this.ajax.send('/public/php/positions.php', params);
	},
	
	'parseResponse': function(data)
	{
		var data=Ajax.JSON(data);
		if(data) this.iterations=data['iterations'];
		var type, tp, res, search=false;
		var keyword=this.keywords[this.keywordInd];
		for(var i=0; i<this.types.length; i++)
		{
			type=this.types[i];
			if(!this.typeParams[type]) this.typeParams[type]={'start': 0, 'search': true};
			tp=this.typeParams[type]; res=this.result[keyword];
			if(data && data[type])
			{
				if(data[type][0]) res[type]=data[type][0];
				if(!data[type][1] || data[type][0]) tp.search=false;
				tp.start+=data[type][1];
				if(tp.start >= this.max) tp.search=false;
			}
			if(tp.search) search=true;
		}
		return search;
	},
	
	'response': function()
	{
		var keyword=this.keywords[this.keywordInd];
		if(!this.result[keyword]) this.result[keyword]={};
		var search=this.parseResponse(this.ajax.response.text);
		this.fireEvent('response', [keyword, !search]);
		if(!search)
		{
			this.keywordInd++;
			if(!(this.keywordInd >= this.keywords.length))
			{
				search=true;
				this.typeParams={};
				this.page=0;
			}
			else this.fireEvent('complete');
		}
		if(search)
		{
			if(this.page||this.keywordInd) setTimeout(tool.bind(this.nextPositions, this), this.interval*1000);
			else this.nextPositions();
		}
	},
	
	'getPositions':	function(host, types, keywords)
	{
		this.result={};
		this.typeParams={};
		this.page=0;
		this.host=host; this.types=types; this.keywords=keywords;
		this.keywordInd=0;
		if(!this.ajax)
		{
			this.ajax=new Ajax();
			this.ajax.addEvent('success', tool.bind(this.response, this));
			this.response();
		}
	}
});
