/**
 * On page ready
 */
Ext.onReady(function(){
	if(navigator.appName != "Microsoft Internet Explorer"){
		initAllInnerLinks();
		/**
		 * Check page hash 4 time per second.
		 * Why? To use browser's back-forward functionality
		 */
		setInterval(function(){
			if (document.location.hash !== currentHash) {
				var menuElement = Ext.query("*[href="+encodeURI(document.location.hash.replace(/^#/, ''))+"]");
				if(menuElement.length)
					menuItemClick(false, menuElement[0], false);
					return true;
				}
				return false;
			}, 250);
	}
});

/**
 * Current page hash
 * @type String
 */
var currentHash = '';

/**
 * Listener handler for menu element click (standard ExtJs params)
 * @param {} evt - event
 * @param {} el - element
 */
var menuItemClick = function(evt, el, o){
	if(evt){
		evt.preventDefault();
	}
	el = Ext.get(el);
	if(!el.dom.href)
		el = el.parent('a');
	if(el.dom.href.indexOf('javascript:') >= 0)
		return;
	//forse loading indicator to pop out
	var loadingBox = Ext.get('loadingBox');
	loadingBox.show();
	loadingBox.center();
	
	//change current hash
	currentHash = '#'+el.dom.href;
	document.location.hash = currentHash;
	
	//load JSon data
	pageStore = new Ext.data.Store({
		autoLoad: true,
		proxy: new Ext.data.HttpProxy({
			url: el.dom.href,
			method: 'POST'
		}),
		reader: new Ext.data.JsonReader({
			root: 'page',
			fields: [
				{name: 'title', type: 'string'},
				{name: 'path', type: 'string'},
				{name: 'content', type: 'string'}
			]
		}),
		baseParams: {'ASMANA_ASYNC': 'YES'},
		listeners: {
			'load': {fn:contentLoadAsync, scope: this}
		}
	});
}
/**
 * Load main div contents asynchroniously.
 * @param {} store it has respond information
 */
var contentLoadAsync = function(store, records, options){
	if(store.getCount()){
		page = store.getAt(0);
		//update title
		document.title = page.get('title') + ' - Veterinarijos klinika Kaune';
		//update path
		websitePath = Ext.get('websitePath');
		websitePath.update(page.get('path'));
		//update contents
		el = new Ext.Element(document.createElement('div'));
		el.addClass('cont');
		el.update(store.getAt(0).get('content'), true);
		elParent = Ext.get('content');
		elParent.update('');
		el.appendTo(elParent);
		el.show({duration: .6});
		elParent.setHeight(el.dom.offsetHeight, {duration: .3});
		Ext.get('loadingBox').hide({duration: .1});
		initAllInnerLinks();
	}
}

/**
 * Parses all inner links (links, what points to website itself) and rewrite them, so that they act as asynchronious links.
 */
var initAllInnerLinks = function(){
	//get all inner links
	innerLinks = Ext.query("[href^="+Ext.PAGE_ROOT+"]");
	var selectedURL = document.location.hash.replace(/^#/, '');
	for(var i = 0; i < innerLinks.length; i++){
		//add onclick event
		var oneLink = Ext.get(innerLinks[i]);
		//check on or off selection of link
		if(selectedURL.indexOf(innerLinks[i].href) == 0)
			oneLink.addClass('selected');
		else
			oneLink.removeClass('selected');
		//add listener to link
		oneLink.removeListener('click', menuItemClick);
		oneLink.addListener('click', menuItemClick);
	}	
}
