/*
 * JavaScript Tetris - 2008/03/11
 *   
 * Copyright (c) 2008 Threeformed Media (http://www.threeformed.com)
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 */

function uurls(){
	/* Private Members */
	var curl = '';
	var inner = '';
	var timer = undefined;
	var callback = null;
	var that = this;	
	
	goToURLState = function(){
		if($.browser.msie){
			if(window.location.hash != curl){ 
				curl = window.location.hash; 
				clearInterval(timer);
				document.getElementById('locationFrame').src = '/state.php?curl='+curl.substring(1);
				$('#content').empty().html('<div class="loader">loading...</div>').load(((curl == '#')?window.location.pathname:curl.substring(1))+' #content > div', function(){
					callback();
				});
			}else if(inner != window['locationFrame'].document.body.innerHTML){
				window.location.hash = '#'+window['locationFrame'].document.body.innerHTML;
				inner = window['locationFrame'].document.body.innerHTML;
			}
		}else{
			if(window.location.hash != curl && window.location.hash != '#'){
				curl = window.location.hash; 
				$('#content').empty().html('<div class="loader">loading...</div>').load(((curl == '')?window.location.pathname:curl.substring(1))+' #content > div', function(){
					callback();
				});
			}	
		}
	}
	
	/* Privileged Members */
	this.setURLState = function(nurl){
		curl = '#'+nurl;
		window.location.hash = curl;
		if($.browser.msie){document.getElementById('locationFrame').src = '/state.php?curl='+nurl;}
	}	
	
	this.setCallback = function(cb){
		callback = cb;
	}
	
	this.update = function(){
		timer = window.setInterval(goToURLState, 100);
	}	
	
	this.init = function(){
		//opera 9.23 & 9.24 cancels all timers when the back button is clicked. this fixes it. all other opera 9 versions are fine.
		if($.browser.opera  && $.browser.version == (9.23 || 9.24)){
			$('body').append('<img src="javascript:location.href=\'javascript:uurls.opera9();\';" style="visibility:hidden" height="0" width="0" alt="History fix for Opera 9.23/9.24" />');
		}
		goToURLState();
		timer = window.setInterval(goToURLState, 100);
	}
	
	this.opera9 = function(){}	
}

var uurls = new uurls();
