/**
 Var: historyPage
 Version: 1.0f
 Autor: Александр Хрищанович (dirmax@cosmostv.by)
 Description: Объект для управление историей браузера и отслеживания нажатия кнопок back или forward
*/

var historyPage = {

	/* проверка на IE */
	isIE : (navigator.appName.indexOf("Microsoft") != -1) && !window["opera"],
	/* проверка на Opera */
	isOpera : window["opera"],
	/* объект iframe для хранения истории (IE) */
	iframeForIE : null,
	/* предыдущая ссылка */
	oldLocation : null,
	/* функция callBack вызова при смене страницы */
	callBackFunction : null,
	/* интервал проверки смены url страницы */
	timeOutInterval : 100,
	
	/* инициализация объекта */
	Init : function ( ) {

		if ( location.hash ) {
			this.GoTo( location.hash );
		}
		else {
			this.GoTo( '#main' );
		}

	},
	
	/* переход на следующую страницу */
	GoTo : function ( urlPath ) {

		var temp = urlPath.split('#');
		
		if ( this.isIE ) {
			if ( this.iframeForIE == null ) {
				this.iframeForIE = document.createElement('iframe');
				this.iframeForIE.style.display = 'none';
				document.body.appendChild( this.iframeForIE );
			}
			this.iframeForIE.src = './js/frame.html?' + temp[1];
		}
		if ( this.isOpera ) {
			/* Опера отрубает все setTimeout при нажатии на back. Поэтому их нужно запустить... */
			AttachEvent( window, 'mousemove', function ( ) { return false; } );
			AttachEvent( document, 'keyup', function ( ) { return false; } );
		}

		// переходим к указанному якорю		
		location.hash = temp[1];
		
		this.TestAnchorChange( );
		

	},

	/* проверка на нажатие кнопка back или forward */
	TestAnchorChange : function ( ) {

		if ( location.hash != this.oldLocation ) {
			this.oldLocation = location.hash;
			this.CallFunction( );
		}

		setTimeout( this.TestAnchorChange.bind1( this ), this.timeOutInterval );

	},
	
	/* вызов callBack функции */
	CallFunction : function ( ) {

		if ( this.callBackFunction ) {
			var urlQuery = location.href.split('#');
			this.callBackFunction( urlQuery[1] );
		}
		
	}

};

/* включение библиотеки */
//AttachEvent( window, 'load', historyPage.Init.bind( historyPage ) );