//include.js ( 'library/flash.js' );

/**
 Object: historyPage
 Version: 1.0
 Autor: Александр Хрищанович (dirmax@cosmostv.by)
 Description: Объект - для реализации кнопок back, forward в броузерах (для ajax, flash, ...)
 Require: 
 	- functions.js v.1.0
 	- flash.js v.1.0
*/
var historyPage = {

	/* Пнтервал проверки смены адреса */
	timeOutInterval : 0.5,

	/* Функция обратного вызова при смене адреса */
	callBackFunction : null,

	/* Путь к флэшке-пустышке */
	flashSrc : 'js/flash.swf',

	/* Предыдущее состояние якоря */
	oldLocation : '',

	/* Объект div для якорей (IE) */
	objectToAnchors : null,	

	/* Объект для флэшки-зуглушки (IE & Opera) */
	objectToFlash : null,


	StartDispatch : function ( ) {
		
		Timer.Start( this.TestChangeLocation.bind( this ), this.timeOutInterval, 'history'  );
		
	},
	
	StopDisplatch : function ( ) {
		
		Timer.Stop( 'history'  );
		
	},

	/* Инициализация объекта */
	Init : function ( ) {

		this.StartDispatch();

		if ( !browser.isGecko && !browser.isWebKit ) {
			this.GenerateSWFCode( );
			this.RefreshSWF( );
		}
		
		var hash_temp = location.toString().split('#');

		if ( hash_temp[1] ) {
			this.GoTo( hash_temp[1] , true );	
		}

	},

	/* Переход по url */
	GoTo : function ( url, isInit ) {

		this.CreateAnchor( url );

		if ( !isInit ) {
			location.hash = url;
		}

	},
	
	/* Перезагрузка флэшки (IE & Opera) */
	RefreshSWF : function ( ) {

		if ( !this.objectToFlash ) {

			this.objectToFlash = document.createElement( 'div' );
			document.body.appendChild(this.objectToFlash);
			this.objectToFlash.innerHTML = this.flashCode;
			this.objectToFlash.className = 'test_hidden';
			this.objectToFlash.style.height = '0px';
			this.objectToFlash.style.width = '0px';

		}

		if ( browser.isIE ) {

			this.objectToFlash.innerHTML = this.flashCode;

		}

	},

	/* Генерирование кода для флэшки-пустышки */
	GenerateSWFCode : function ( ) {

		var flashObj = new Flash();
		flashObj.srcFlash = this.flashSrc;
		flashObj.width = '0px';
		flashObj.height = '0px';
		flashObj.background = '#fff';
		flashObj.transparent = true;						
		this.flashCode = flashObj.GenerateHtmlFlash( );	

	},

	/* Проверка смены якоря */
	TestChangeLocation : function ( ) {

		if ( browser.isIE ) {

			var oldTitle = document.title;

			this.RefreshSWF( );

			var nowTitle = document.title;

			var index = nowTitle.lastIndexOf( '#' );
         	
			var hash = (index == -1) ? '' : nowTitle.substr( index );
	
			var normalTitle = oldTitle.split( '#' );
	
			document.title = normalTitle[0];

		}
		else {

			var temp = location.toString().split('#');
			if ( !temp[1] ) {
				hash = '';
			}
			else {
				var hash = '#' + temp[1];
			}

		}


		if ( this.oldLocation != hash  ) {

			this.oldLocation = hash;
			this.CallFunction( hash );

		}
				
	},

	/* Создание ссылок-якорей для IE */
	CreateAnchor : function ( name ) {

		if ( browser.isIE ) {

			if ( !this.objectToAnchors ) {
				this.objectToAnchors = document.createElement('div');
				this.objectToAnchors.style.display = 'none';
				document.body.insertBefore(this.objectToAnchors, document.body.firstChild);
				this.anchorNames = new Array( );
			}

			if ( !this.anchorNames[name] ) {

				this.anchorNames[name] = true;
				this.objectToAnchors.innerHTML += '<a name="' + name + '">' + name + '</a>';

			}	

		}

	},

	/* Вызов callBack функции */
	CallFunction : function ( name ) {

		if ( this.callBackFunction ) {

			var urlQuery = name.split('#');
			urlQuery = urlQuery[1] ? urlQuery[1] : '';
			this.callBackFunction( urlQuery );

		}

	}

};

/* Вызов historyPage.Init при загрузке страницы */
Events.Attach( window, 'load', function ( ) { historyPage.Init( ); } );