var flash;
var loc;
var count = 0;

// PageLoad function
	// This function is called when:
	// 1. after calling $.historyInit();
	// 2. after calling $.historyLoad();
	// 3. after pushing "Go Back" button of a browser
function pageload(hash) {
	
	count++;
	loc = hash;
	
	//alert(count);
	
	// hash doesn't contain the first # character.
	if(hash) {
		// restore ajax loaded state
		if($.browser.msie) {
			// jquery's $.load() function does't work when hash include special characters like едц.
			hash = encodeURIComponent(hash);
		}
		if (count > 1) {
			flash.asUpdateLocation(parseHash(hash));
		}
	} else {
		if (count > 1) {
			flash.asUpdateLocation({page:"home"});
		}
	}
}

function goBack() {
	history.back();	
}

function goForward() {
	history.forward();	
}

function updateHash(hash) {
	$.historyLoad(hash);
}

function getHash() {
	if (loc) {
		return(parseHash(loc));
	} else {
		return false ;	
	}
}

function getURL() {
	var url = location.href;
	url = url.replace(/&/g, "%26")

	return(url);
	
}

function upCount() {
	count++;
}

function parseHash(hash) {
	var o = new Object();
	var a = hash.split("&");
	for (var i=0; i<a.length; i++) {
		var pair = a[i].split("=");
		o[pair[0]] = (pair[0] == "page") ? pair[1] : parseInt(pair[1], 10);
	}
	return o;
}

window.onload = function() {
	flash = document.getElementById("flash");
	
	// Initialize history plugin.
	// The callback is called at once by present location.hash.
	$.historyInit(pageload);
	
};
