/************************************************************************************
*	Class IFrameRedirector
*	@ iIframeId - value of id attribute for element <iframe />
************************************************************************************/
function IFrameRedirector(iIFrameId)
{
	var oIFr = document.getElementById(iIFrameId);
	if (!oIFr || (oIFr.nodeName != 'IFRAME'))
	{
		var err = new Error("Can't create object IFrameRedirector.\n"+
							"The iframe element with id \""+ iIFrameId+ "\" not founded.");
		err.name = 'IFRAME_NOT_FOUND';
		throw err; 
	}
	
	this._oBaseWindow = window;
	this._arrNeededPages = new Array();
	
	// wait a 20 seconds until iframe is loading
	this._waitWhileIfrLoaded = parseInt(20*1000/100); 
	
	var z = this;
	
	if ($.browser.mozilla || $.browser.opera)
	{
		$(oIFr).load(function()
		{
			if (!z._ifrReady)
				z._ifrOnloadHandler(oIFr);
		});
	} else
	{
		if (!z._ifrReady)
			z._ifrOnloadHandler(oIFr);
	}
}

IFrameRedirector.prototype = 
{
// private declorations:
//////////////////////////////////////////////////////////////////////////////////////
	// flag what iframe is ready for DOM manipulation
	_ifrReady			: false,
	
	// array of pages who needs to reloading links from included iframe
	_arrNeededPages		: null,
	
	// reference to value returned from method setInterval
	_interval			: null,
	
	// reference to element <iframe />
	_oIFrame			: null,
	
	// reference to object window inside element <iframe />
	_oIFrameWindow		: null,
	
	// reference to object window in base window
	_oBaseWindow		: null,
	
	// counter for limit script execution if iframe content can't be loaded
	_ctr				: 0,
	
	// time interval in milliseconds in which current script wating for iframe loading
	_waitWhileIfrLoaded	: 0,
	
/************************************************************************************
*	this function performed after iframe is loaded
************************************************************************************/
	_ifrOnloadHandler	: function (oIFrame)
	{
		this._oIFrame = oIFrame;
		var oIFrameWindow = this._oIFrame.contentWindow || this._oIFrame.contentDocument;
		if (oIFrameWindow)
		{
			this._oIFrameWindow = oIFrameWindow;
			this._ifrReady = true;
		}
		
		//alert('oIfr DOM is loaded!'+oIFrame.nodeName)
		
		var ifrURL;
		ifrURL = getAttrValueFromLocation(window.location.href, 'ifrlnk');
		if (ifrURL.length>0)
		{
			this._ifrGoToURL(ifrURL);
		}
			//this._ifrOnloadHandler = this.initIfrLinks;
			//alert('not found');
		
	},
	
/************************************************************************************
*	reloaded iframe if her DOM is ready
*	 @ strURL - URL set's iframe location
************************************************************************************/
	_ifrGoToURL				: function(strURL)
	{
		var oBase64 = new Base64();
		strURL = oBase64.decode(strURL);
		if (!this._ifrReady)
			return;
		//alert('reloading iframe...\n current url:'+this._oIFrameWindow.location.href+'\ngo to url:'+strURL);
		this._oIFrameWindow.location = strURL;
	},
	
/************************************************************************************
*	combine a path for location in base window
*	@ strIfrURL - path from iframe location
************************************************************************************/
	_onClickRedirect		: function (strIfrURL)
	{
		var strRedirectURL = '';
		var strPage = '';
		var strLang = '';
		var arrMatches;
		var oLocation = this._oBaseWindow.location;
		var oBase64 = new Base64();
		strPage = getAttrValueFromLocation(oLocation.href, 'page');
		
		strRedirectURL = oLocation.href.replace(/\?.*$/, '');
		if(strPage.length>0)
		{
			arrMatches = strPage.match(/(?:\[)([a-z0-9]{2,3})(?:\])$/);
			if(arrMatches.length>=2)
				strLang = arrMatches[1];
			strPage = strPage.replace(/(?:\[)([a-z0-9]{2,3})(?:\])$/, '');
			strRedirectURL = strRedirectURL.concat('?page=');
			strRedirectURL = strRedirectURL.concat(strPage);
			strRedirectURL = strRedirectURL.concat('_details[');
			strRedirectURL = strRedirectURL.concat(strLang);
			strRedirectURL = strRedirectURL.concat(']&ifrlnk=');
			strRedirectURL = strRedirectURL.concat(oBase64.encode(strIfrURL));
		}
		//alert('!'+strRedirectURL);
		this._redirect(strRedirectURL);
		//alert(event.data.iframeRedirector);
		return false;
	},
	
/************************************************************************************
*	replace standart behavior for all links on a iframe document.
*	now all links call a _onClickRedirect method transform her  target
************************************************************************************/
	_setIfrLinksRedirection	: function()
	{
		if(!this._ifrReady)
			return;
		var oDoc = this._oIFrameWindow.document;
		if (!oDoc)
			return;
		
		var arrA = $('a',oDoc);
		//alert(arrA.length);
		var z = this;
		
		$('a',oDoc).bind(
			'click',
			{iframeRedirector : z},
			function(event)
			{
				//alert('path:'+$(this).attr('href')); return false;
				
				var oIfrLocation = event.data.iframeRedirector._oIFrameWindow.location;
				var strIfrURL = oIfrLocation.href;
				var strIfrHref = $(this).attr('href');
				var strIfrLang = '';
				
				strIfrLang = getAttrValueFromLocation(oIfrLocation.href, 'l');
				
				strIfrHref = strIfrHref.replace(/\s*/g, '');
				strIfrURL = strIfrURL.replace(/\?.*/g, '');
				
				strIfrURL = strIfrURL.substr(0, strIfrURL.lastIndexOf('/'));
				strIfrURL = strIfrURL.concat('/'+strIfrHref);
				if (strIfrLang.length>0)
					strIfrURL = strIfrURL.concat('&l='+strIfrLang);
				
				event.data.iframeRedirector._onClickRedirect(strIfrURL);
				return false; // disable <a /> element
			}
		);
	},
	
/************************************************************************************
*	start redirection process for base window
*	@ strURL - URL of new page
************************************************************************************/
	_redirect			: function(strURL)
	{
		//alert('redirect to '+strURL);
		this._oBaseWindow.location = strURL;
	},

	
// public declorations:
//////////////////////////////////////////////////////////////////////////////////////
/************************************************************************************
*	addNeededPage insert a page who needs for
*	redirection from links included in her <iframe /> element
************************************************************************************/
	addNeededPage			: function(strPageName)
	{
		if (!this._arrNeededPages.in_array(strPageName))
			this._arrNeededPages.push(strPageName);
		
		//for (var i=0; i<this._arrNeededPages.length; i++)
		//	alert('_arrNeededPages['+i+'] = '+this._arrNeededPages[i]+'\n');
	},
	
/************************************************************************************
*	start preparing DOM in iframe when it's ready.
*	process has been started only for pages who containts in _arrNeededPages[]
************************************************************************************/
	prepare					: function()
	{
		//alert('prepare...');
		var currentPage = getAttrValueFromLocation(this._oBaseWindow.location.href, 'page');
		if (!this._arrNeededPages.in_array(currentPage))
			return;
		
		if (!this._ifrReady)
		{
			var z = this;
			
			// clearing previouse interval
			clearInterval(this._interval);
			
			if (z._ctr++ < this._waitWhileIfrLoaded)
				// set new interval
				this._interval = setInterval(function(){z.prepare();}, 100);
		} else
		{
			clearInterval(this._interval);
			this._ctr++;
			//alert('method prepare was runed '+this._ctr+' times');	
				//alert('do prepare links');
			this._setIfrLinksRedirection();
			
		}
	}
	
}