﻿pandora.util.history.Base.prototype = {
	locator : null,
	flag : false,
	currentKey : "",
	options : null,
	callback : null,
	timer : null,
	interval: null,
	
	initialize : function(options){
		this.interval = 200;
		this.options = Object.extend(options||{});         
		this.callback = this.options.callback || Prototype.emtpyfunction; 
		
		if(Prototype.Browser.IE) this.locator = new pandora.util.history.IE('HistoryIFrame', 'blank.html');         
		else this.locator = new pandora.util.history.ETC();
		this.currentKey = "";
	},
	
	add : function(key){
		this.flag = true;
		clearTimeout(this.timer);  
		this.locator.save(key);
		this.currentKey = this.locator.load();
		this.timer = setTimeout(this.check.bind(this), this.interval);   
		this.flag = false;
	},
	
	get : function(){
		return this.locator.load();
	},
	
	check: function(){  
		if(!this.flag){
			var load = this.locator.load();			
			if(load!=this.currentKey){
				if(this.currentKey!="") {
					if(this.currentKey!=undefined) {
//						this.callback(load);
					}
				}
				this.currentKey = load;
			}
		}         
		this.timer = setTimeout(this.check.bind(this), this.interval);     
	}
};

pandora.util.history.IE.prototype = {
	id : null,
	src : null,
	url : null,
	
	initialize : function(id, src){
		this.id = id || 'HHdle';         
		this.src = src || '';  		
		this.url = '';
		//document.write('<iframe src="'+this.src+'" id="'+this.id+'" name="'+this.id+'" style="display: none;"></iframe>');
		document.write('<iframe id="'+this.id+'" name="'+this.id+'" style="display: none;"></iframe>');
	},
	
	save: function(key){
		var target = $(this.id);
		if (target) {
			target.setAttribute('src', this.src + '?' + key);
			window.location.href = this.url + '#' + key;
		}
	},	
	
	load: function(){         
		try {             
			return (document.frames[this.id].location.href || '?').split('?')[1];         
		}catch(e){ 
			return ''; 
		}     
	},

	load2 : function(){
		try {             
			return window.location.hash.substring(1) || '';
		}catch(e){ 
			return ''; 
		}     		
	}	
};

pandora.util.history.ETC.prototype = {
	initialize : function(){
	},
	
	save : function(key){
		window.location.hash = key;
	},
	
	load : function(){
		return window.location.hash.substring(1) || '';
	}
};

var cHistoryMgr = Class.create();
	cHistoryMgr._instance_ = null;
	cHistoryMgr.getInstance = function(options){
			if(this._instance_==null) this._instance_ = new pandora.util.history.Base(options);
			return this._instance_;
	};
