/** Copyright (c) 2006 UPT Ltd. Версия $Id: xsight.cTabPane.js,v 1.3 2006/06/01 14:47:30 andy Exp $ */

/**
 * Класс-контрол, реализующий работу вкладок
 * @class
 * @extends cObject
 */
cTabPane = newClass(cObject, {

	element: null,

	pages: [],

	selectedIndex: null,

	useCookie: false,

	tabRow: null,

	isForm: false,

	classNameTag: "xsTabPaneControl",

	/**
	* Конструктор класса
	* @constructor
	* @param object pElement html-элемент контейнера вкладок
	* @param boolean pUseCookies использовать ли cookies. По-умолчанию true
	*/
	constructor:function(pElement, pUseCookies)
	{
		// вызов родительского конструктора
		this.constructor.prototype.constructor.call(this);

		if ( !cTabPane.fHasSupport() || pElement == null ) return;

		this.element = pElement;
		this.element.tabPane = this;
		this.useCookie = pUseCookies != null ? pUseCookies : true;


		// добавляет имя класса
		this.element.className = this.classNameTag + " " + this.element.className;

		// добавляем строку со вкладками
		this.tabRow = document.createElement("div");
		this.tabRow.className = "xsTabRow";
		pElement.insertBefore( this.tabRow, pElement.firstChild );

		var tabIndex = 0;

		this.isForm = cTabPane.fGetIsForm();

		if ( !this.isForm )
		{
			tabIndex = Number( this.fGetCookie( "xsTab_" + this.element.id ) );
			if ( isNaN( tabIndex ) )
				tabIndex = 0;
		}
		this.selectedIndex = tabIndex;

		// добавляем childNodes
		var cs = pElement.childNodes;
		var n;
		for (var i = 0; i < cs.length; i++)
		{
			if (cs[i].nodeType == 1 && cs[i].className == "xsTabPage")
			{
				this.fAddTabPage(cs[i]);
			}
		}
	},

	fSetSelectedIndex:function(pIndex)
	{
		if (this.selectedIndex != pIndex)
		{
			if (this.selectedIndex != null && this.pages[ this.selectedIndex ] != null )
				this.pages[ this.selectedIndex ].fHide();
			this.selectedIndex = pIndex;
			this.pages[ this.selectedIndex ].fShow();

			if ( !this.isForm )
				this.fSetCookie( "xsTab_" + this.element.id, pIndex );
		}
	},

	fGetSelectedIndex:function ()
	{
		return this.selectedIndex;
	},

	fAddTabPage:function (pElement)
	{
		if ( !cTabPane.fHasSupport() ) return;

		if ( pElement.tabPage == this )	// уже добавленная вкладка
			return pElement.tabPage;

		var n = this.pages.length;
		var tp = this.pages[n] = new cTabPage( pElement, this, n );
		tp.tabPane = this;

		// move the tab out of the box
		this.tabRow.appendChild( tp.tab );

		if ( n == this.selectedIndex )
			tp.fShow();
		else
			tp.fHide();

		return tp;
	},

	fDispose: function ()
	{
		this.element.tabPane = null;
		this.element = null;
		this.tabRow = null;

		for (var i = 0; i < this.pages.length; i++)
		{
			this.pages[i].fDispose();
			this.pages[i] = null;
		}
		this.pages = null;
	},

	fSetCookie:function ( pName, pValue, pDays )
	{
		var expires = "";
		if ( pDays )
		{
			var d = new Date();
			d.setTime( d.getTime() + pDays * 24 * 60 * 60 * 1000 );
			expires = "; expires=" + d.toGMTString();
		}

		document.cookie = pName + "=" + pValue + expires + "; path=/";
	},

	fGetCookie:function (pName)
	{
		var re = new RegExp( "(\;|^)[^;]*(" + pName + ")\=([^;]*)(;|$)" );
		var res = re.exec( document.cookie );
		return res != null ? res[3] : null;
	},

	fRemoveCookie: function ( pName )
	{
		this.fSetCookie( pName, "", -1 );
	}

});

cTabPane.fHasSupport = function()
{
	if (typeof cTabPane.fHasSupport.support != "undefined")
		return cTabPane.fHasSupport.support;

	var ie55 = /msie 5\.[56789]/i.test( navigator.userAgent );

	cTabPane.fHasSupport.support = ( typeof document.implementation != "undefined" &&
			document.implementation.hasFeature( "html", "1.0" ) || ie55 )

	// IE55 has a serious DOM1 bug... Patch it!
	if ( ie55 )
	{
		document._getElementsByTagName = document.getElementsByTagName;
		document.getElementsByTagName = function ( sTagName )
		{
			if ( sTagName == "*" )
				return document.all;
			else
				return document._getElementsByTagName( sTagName );
		};
	}

	return cTabPane.fHasSupport.support;
}

cTabPane.fGetIsForm = function()
{
	return ((document.location.href.indexOf("single=") != "-1") && typeof(window.dialogWidth) != "undefined" && typeof(window.dialogHeight) != "undefined")?true:false;
}

cTabPane.fSetTabs = function()
{
   if (cTabPane.fGetIsForm())
   {
   		if(typeof(parent.frames['iframe_add']) != 'undefined')
   		{
   			var iframe = parent.frames['iframe_add'];
			var new_rule;
			var width = iframe.document.body.clientWidth - 30;
			var height = iframe.document.body.clientHeight - 50;

			new_rule = document.styleSheets[0].addRule(".xsTabPaneControl .xsTabPage", "width:" + width + "px;height:" + height + "px;", 0);
   		}
   		else
   		{
	   		var new_rule;
			var width = window.dialogWidth;
			var height = window.dialogHeight;
			var obj = new cObject();
			width = obj.fStringReplace(width, "px", "") - 30;
			height = obj.fStringReplace(height, "px", "") - 110;
			new_rule = document.styleSheets[0].addRule(".xsTabPaneControl .xsTabPage", "width:" + width + "px;height:" + height + "px;", 0);
   		}
   }
}

cTabPane.tabpane = null;

cTabPane.aTab = null;

cTabPane.fInitAll = function()
{
	if ( !cTabPane.fHasSupport() ) return;

	var all = document.getElementsByTagName( "*" );
	var l = all.length;
	var tabPaneRe = /xsTabPane/;
	var tabPageRe = /xsTabPage/;
	var cn, el;
	var parentTabPane;

	for ( var i = 0; i < l; i++ )
	{
		el = all[i]
		cn = el.className;

		// no className
		if ( cn == "" ) continue;

		// uninitiated tab pane
		if ( tabPaneRe.test( cn ) && !el.tabPane )
		{
			cTabPane.tabpane = new cTabPane( el, !cTabPane.fGetIsForm() );
			el.style.visibility = "visible";
		}
		// unitiated tab page wit a valid tab pane parent
		else if ( tabPageRe.test( cn ) && !el.tabPage &&
					tabPaneRe.test( el.parentNode.className ) )
		{
			el.parentNode.tabPane.fAddTabPage( el );
		}
	}

	//alert(cTabPane.tabpane);

	cTabPane.fSetTabs();
	if (cTabPane.aTab != null)
	{
		cTabPane.tabpane.pages[cTabPane.aTab].fSelect();
	}
}

cTabPane.fDisposeAll = function()
{
	if ( !cTabPane.fHasSupport() ) return;

	var all = document.getElementsByTagName( "*" );
	var l = all.length;
	var tabPaneRe = /xsTabPane/;
	var cn, el;
	var tabPanes = [];

	for ( var i = 0; i < l; i++ )
	{
		el = all[i]
		cn = el.className;

		// no className
		if ( cn == "" ) continue;

		// tab pane
		if ( tabPaneRe.test( cn ) && el.tabPane )
			tabPanes[tabPanes.length] = el.tabPane;
	}

	for (var i = tabPanes.length - 1; i >= 0; i--)
	{
		tabPanes[i].fDispose();
		tabPanes[i] = null;
	}
}

cTabPane.fLoader = function()
{
	var obj = new cObject();
	obj.fAddHandler(window, "load", cTabPane.fInitAll);
	obj.fAddHandler(window, "unload", cTabPane.fDisposeAll);
}

cTabPane.fLoader();

/**
 * Класс-контрол, реализующий вкладку
 * @class
 * @extends cObject
 */
cTabPage = newClass(cObject, {

	element: null,

	index: null,

	aElement: null,

	/**
	* Конструктор класса
	* @constructor
	* @param object pElement html-элемент вкладки
	* @param object pTabPane элемент cTabPane
	* @param integer pIndex индекс
	*/
	constructor:function(pElement, pTabPane, pIndex)
	{
		// вызов родительского конструктора
		this.constructor.prototype.constructor.call(this);

		if ( !cTabPane.fHasSupport() || pElement == null ) return;

		this.element = pElement;
		this.element.tabPage = this;
		this.index = pIndex;

		var cs = pElement.childNodes;
		for (var i = 0; i < cs.length; i++)
		{
			if (cs[i].nodeType == 1 && cs[i].className == "xsTab")
			{
				this.tab = cs[i];
				break;
			}
		}

		var a = document.createElement( "A" );
		this.aElement = a;
		a.href = "#";
		this.fAddHandler(a, "click", function () { return false; });

		if (typeof (this.tab) != "undefined")
		{
			while ( this.tab.hasChildNodes() )
				a.appendChild( this.tab.firstChild );

			var lefter = document.createElement('span');
			lefter.setAttribute('class', 'xsTabLefter');
			lefter.className = 'xsTabLefter';
			this.tab.appendChild( lefter );

			this.tab.appendChild( a );

			var righter = document.createElement('span');
			righter.setAttribute('class', 'xsTabRighter');
			righter.className = 'xsTabRighter';
			this.tab.appendChild( righter );

			this.fAddHandler(this.tab, "click", function () { this.fSelect(); }.bind(this));
			this.fAddHandler(this.tab, "mouseover", function () { this.fTabOver(); }.bind(this));
			this.fAddHandler(this.tab, "mouseout", function () { this.fTabOut(); }.bind(this));
		}
	},

	fShow: function ()
	{
		var el = this.tab;
		var s = el.className + " selected";
		s = s.replace(/ +/g, " ");
		el.className = s;
		this.element.style.display = "block";
	},

	fHide: function ()
	{
		var el = this.tab;
		var s = el.className;
		s = s.replace(/ selected/g, "");
		el.className = s;
		this.element.style.display = "none";
	},

	fSelect: function ()
	{
		this.tabPane.fSetSelectedIndex( this.index );
	},

	fDispose: function ()
	{
		this.aElement.onclick = null;
		this.aElement = null;
		this.element.tabPage = null;
		this.fRemoveHandler(this.tab, "click", null);
		this.fRemoveHandler(this.tab, "mouseover", null);
		this.fRemoveHandler(this.tab, "mouseout", null);
		this.tab = null;
		this.tabPane = null;
		this.element = null;
	},

	fTabOver: function ()
	{
		/*var el = this.tab;
		var s = el.className + " hover";
		s = s.replace(/ +/g, " ");
		el.className = s;*/
	},

	fTabOut: function ()
	{
		/*var el = this.tab;
		var s = el.className;
		s = s.replace(/ hover/g, "");
		el.className = s;*/
	}
});
