var lsFindMeClass = new Class({

	Implements: [Options],

	options: {
		url: {
			save_height: 'http://findme.livestreet.ru/ajaxSaveHeight.php',
			next_url: 'http://findme.livestreet.ru/ajaxNextUrl.php'
		},
		framePrefix: '/?fromframe=1'
	},

	initialize: function(options){
		this.setOptions(options);

		this.aUrl=new Hash();
		
		this.bSaveHeight=false;
		
		this.checkLoadToFrame.periodical(200,this);
	},
	
	loadToFrame: function($sUrl) {		
		$('findme-iframe-page').src=$sUrl+this.options.framePrefix;
		$('findme-iframe-page').addEvent('load',function(){
			//this.onLoadToFrame();
		}.bind(this));
	},
	
	onLoadToFrame: function() {
		// загружаем следующий URL
		if (this.bSaveHeight) {
			this.bSaveHeight=false;
			this.getNextUrl.delay(500,this);
		}
	},
	
	checkLoadToFrame: function() {
		if (this.bSaveHeight) {
			this.bSaveHeight=false;
			this.getNextUrl.delay(100,this);
		}
	},
	
	getNextUrl: function() {
		new Request.JSON({
			url: this.options.url.next_url,
			noCache: true,
			data: {},
			onSuccess: function(resp){				
				if (resp.bStateError) {
					//alert(resp.sMsg);
				} else {
					this.loadToFrame(resp.sUrl);
				}
			}.bind(this),
			onFailure: function(){
				alert('Please try again later');
			}.bind(this)
		}).send();
	},
	
	cmdFrame: function(cmd,value) {
		if ($('findme-iframe-cmd')) {
			$('findme-iframe-cmd').destroy();
		}
		var src='http://findme.livestreet.ru/frame.html#cmd='+cmd+'&value='+value+'&url='+location.toString();
		var iframe=new Element('iframe',{'style':'visibility: hidden; position: absolute; height: 0pt; width: 0pt; left: 0pt; top: 0pt;','src':src,'id':'findme-iframe-cmd'});
		iframe.inject(document.body);
	},
	
	cmdFromHash: function(hash) {
		var aCmd=hash.replace("#","").parseQueryString();
		var sUrl=aCmd.url.replace(this.options.framePrefix,"");
		if (this.cmd[aCmd.cmd]) {
			this.cmd[aCmd.cmd].call(this,aCmd.value,sUrl);
		}
	},
		
	cmd: {
		retriveHeight: function(value,url) {	
			//return;
					
			new Request.JSON({
				url: this.options.url.save_height,
				noCache: true,
				data: {url:url,value:value},
				onSuccess: function(resp){	
					
					this.bSaveHeight=true;
									
					if (resp.bStateError) {
						
					} else {
						
					}
				}.bind(this),
				onFailure: function(){
					
					this.bSaveHeight=true;
					
					alert('Please try again later');					
				}.bind(this)
			}).send();			
		},
		
		pageNotFound: function() {
			this.bSaveHeight=true;
		}
	}
});