/* -------------------------------------------------------------------------- */
/** 
 *    @fileoverview
 *       Tab View Control
 *
 *    @version rev003.2007-07-06
 *    @requires common.js
 *    @requires rollover.js (when using tab images)
 *    @requires tabView.css
 */
/* -------------------------------------------------------------------------- */


var ITV_TABVIEW_AUTOSETUP_INSTANCES = [];


/* -------------------- Settings for ITVTabViewAutoSetup -------------------- */

var ITV_TABVIEW_AUTOSETUP_ENABLED            = true;
var ITV_TABVIEW_AUTOSETUP_ADJUST_HEIGHT      = true;
var ITV_TABVIEW_AUTOSETUP_HIDE_SINGLED_TAB   = true;
var ITV_TABVIEW_AUTOSETUP_BASENODE_NODENAME  = 'div';
var ITV_TABVIEW_AUTOSETUP_BASENODE_CLASSNAME = 'ITVTabView';



/* -------------------- Settings for ITVTabView -------------------- */

var ITV_TABVIEW_TABIMAGE_STATUSSET = {
	'stay'   : '_o' ,
	'hover'  : '_o' ,
	'normal' : ''
};
var ITV_TABVIEW_TABNODES_CLASSNAME         = 'ITVTabView-Tab';
var ITV_TABVIEW_BASENODE_ENABLED_CLASSNAME = 'pseudo-enabled';
var ITV_TABVIEW_CURRENTPANE_CLASSNAME      = 'pseudo-current';
var ITV_TABVIEW_IS_SINGLE_PANE_CLASSNAME   = 'has-single-pane';



/* -------------------- Constructor : ITVTabView -------------------- */

function ITVTabView(node, adjustHeight) {
	this.node         = node;
	this.panes        = [];
	this.paneHeight   = 0;
	this.adjustHeight = adjustHeight;

	if (arguments.length) this.init();
}

ITVTabView.prototype = {
	init : function() {
		if (!this.node) {
			throw 'ITVTabView.init: base element node is not specified.';
		} else {
			var nodes = this.node.getElementsByClassNameITV(ITV_TABVIEW_TABNODES_CLASSNAME);
			nodes.forEach(function(node) {
				new ITVTabViewPane(node, this);
			}, this);
			if (this.panes.every(function(pane) { return !pane.isCurrent })) {
				this.switchTo(this.panes[0]);

			}
			if (this.panes.length == 1) {
				this.node.appendClassNameITV(ITV_TABVIEW_IS_SINGLE_PANE_CLASSNAME);
			}
//			tabBlock.normalizeTextNodeITV(true); // measuring to MacIE, but this crashes MacIE...
		}
	},

	addPane : function(pane) {
		if (this.panes.indexOf(pane) < 0) {
			this.panes.push(pane);
			this.adjustPaneHeight();
			this.node.appendClassNameITV(ITV_TABVIEW_BASENODE_ENABLED_CLASSNAME);
		}
	},

	getPaneHeight : function() {
		this.paneHeight = 0;
		this.panes.forEach(function(pane) { this.paneHeight = Math.max(this.paneHeight, pane.getHeight()) }, this);
		return this.paneHeight;
	},
	
	setPaneHeight : function(height) {
		if (typeof height != 'number' || height < 0) {
			throw 'ITVTabView.setPaneHeight: first argument must be a positive integer or 0.';
		} else {
			this.panes.forEach(function(pane) { pane.setHeight(height) });
			this.paneHeight = height;
		}
	},

	adjustPaneHeight : function() {
		if (this.adjustHeight) {
			this.setPaneHeight(this.getPaneHeight());
		}
	},

	switchTo : function(pane) {
		var idx = this.panes.indexOf(pane);
		if (idx != -1) {
			this.panes.forEach(function(_pane) { _pane.hide() });
			this.panes[idx].visible();
		}
	}
}



/* -------------------- Constructor : ITVTabViewPane -------------------- */

function ITVTabViewPane(tabNode, parentView) {
	this.node       = {
		tab  : tabNode,
		pane : null
	};
	this.parentView = parentView;
	this.tabImage   = null;
	this.isCurrent  = false;
	this.paneHeight = 0;
	if (arguments.length) this.init();
}

ITVTabViewPane.prototype = {
	init : function() {
		if (this.node.tab) {
			var anchor = (this.node.tab.nodeName.toLowerCase() == 'a') ?
			             	this.node.tab :
			             	this.node.tab.getElementsByTagNameITV('a')[0];
			if (anchor) {
				this.node.pane = document.getElementByIdITV(anchor.getAttributeITV('href').split('#')[1]);
				if (this.node.pane) {
					this.isCurrent = (this.node.tab .hasClassNameITV(ITV_TABVIEW_CURRENTPANE_CLASSNAME) &&
					                  this.node.pane.hasClassNameITV(ITV_TABVIEW_CURRENTPANE_CLASSNAME));
					this.initTabImage(anchor.getElementsByTagNameITV('img')[0]);
					this.parentView.addPane(this);
					anchor.addEventListenerITV('click', this.tabClicked, this);
					// aware of a behavior of ITVSmoothScroll.
					if (typeof ITVSmoothScroll == 'function' && typeof ITV_SMOOTHSCROLL_IGNORE_NODE_CNAME == 'string') {
						anchor.appendClassNameITV(ITV_SMOOTHSCROLL_IGNORE_NODE_CNAME);
					}
				}
			}
		}
	},

	initTabImage : function(imgNode) {
		if (imgNode) {
			if (typeof ITVRollover == 'function') {
				if (typeof ITV_TABVIEW_TABIMAGE_STATUSSET.hover != 'string') {
					ITV_TABVIEW_TABIMAGE_STATUSSET.hover = ITV_TABVIEW_TABIMAGE_STATUSSET.normal;
				}
				if (typeof ITV_TABVIEW_TABIMAGE_STATUSSET.stay_hover != 'string') {
					ITV_TABVIEW_TABIMAGE_STATUSSET.stay_hover = ITV_TABVIEW_TABIMAGE_STATUSSET.stay;
				}
				this.tabImage = new ITVRollover(imgNode, ITV_TABVIEW_TABIMAGE_STATUSSET);
				imgNode.addEventListenerITV('mouseover', this.setTabImageToHover  , this);
				imgNode.addEventListenerITV('mouseout' , this.setTabImageToDefault, this);
				if (this.isCurrent) {
					this.setTabImageToStay();
				}
			}
		}
	},

	tabClicked : function(e) {
		this.parentView.switchTo(this);
//		e.currentTarget.blur();
		e.preventDefault();
	},

	setTabImageToDefault : function() {
		if (this.tabImage) {
			this.tabImage.setStatus(this.isCurrent ? 'stay' : 'normal');
		}
	},

	setTabImageToHover : function() {
		this.parentView.switchTo(this);
		if (this.tabImage) {
			this.tabImage.setStatus(this.isCurrent ? 'stay_hover' : 'hover');
		}
	},

	setTabImageToStay : function() {
		if (this.tabImage && this.isCurrent) {
			this.tabImage.setStatus('stay');
		}
	},

	getHeight : function() {
		this.node.pane.appendClassNameITV(ITV_TABVIEW_CURRENTPANE_CLASSNAME);
		this.node.pane.style.height = 'auto';
		var height = this.node.pane.offsetHeight / ITVGetZoomRatio();
		this.setHeight(this.paneHeight);
		if (!this.isCurrent) {
			this.node.pane.removeClassNameITV(ITV_TABVIEW_CURRENTPANE_CLASSNAME);
		}
		return height;
	},
	
	setHeight : function(height) {
		if (typeof height != 'number' || height < 0) {
			throw 'ITVTabViewPane.setHeight: first argument must be a positive integer or 0.';
		} else {
			var ptn   = /px$/;
			var props = ['borderTopWidth', 'borderBottomWidth', 'paddingTop', 'paddingBottom'];
			var node  = this.node.pane;
			if (props.every(function(prop) { return ptn.test(node.getCurrentStyleITV(prop)) })) {
				props.forEach(function(prop) { height -= parseInt(node.getCurrentStyleITV(prop)) });
			}
			if (height > 0) {
				this.node.pane.style.height = height + 'px';
				this.paneHeight = height;
			}
		}
	},
	
	visible : function() {
		if (this.node.tab && this.node.pane) {
			this.isCurrent = true;
			this.node.tab .appendClassNameITV(ITV_TABVIEW_CURRENTPANE_CLASSNAME);
			this.node.pane.appendClassNameITV(ITV_TABVIEW_CURRENTPANE_CLASSNAME);
			this.setTabImageToStay();
			var itv_current_div = this.node.pane;
			itv_current_div.addEventListenerITV('click', this.hide, this);
		}
	},

	hide : function() {
		if (this.node.tab && this.node.pane) {
			this.isCurrent = false;
			this.node.tab .removeClassNameITV(ITV_TABVIEW_CURRENTPANE_CLASSNAME);
			this.node.pane.removeClassNameITV(ITV_TABVIEW_CURRENTPANE_CLASSNAME);
			this.setTabImageToDefault();
		}
	}
}






/* -------------------- Function : ITVTabViewAutoSetupPrepare -------------------- */

function ITVTabViewAutoSetupPrepare() {
	if (ITV.ua.isOpera) return;

	var sheet  = document.styleSheets[0];
	var seltxt = [ITV_TABVIEW_AUTOSETUP_BASENODE_NODENAME, ITV_TABVIEW_AUTOSETUP_BASENODE_CLASSNAME].join('.');
	var rule   = '{ display: none }';

	if (ITV.ua.isSafari) {
		// Safari
		var style  = new ITVTag('style', { type : 'text/css' });
		style.appendChildITV(seltxt + rule);
		document.write(style.toString());
	} else if (sheet.insertRule) {
		// Std DOM. (opera causes crash)
		sheet.insertRule(seltxt + rule, sheet.cssRules.length);
	} else if (sheet.addRule) {
		// IE
		sheet.addRule(seltxt, rule);
	}
}



/* -------------------- Function : ITVTabViewAutoSetup -------------------- */

function ITVTabViewAutoSetup() {
	if (ITV_TABVIEW_AUTOSETUP_ENABLED && !ITVAlreadyApplied(arguments.callee)) {
		var nodes = document.getElementsByClassNameITV(ITV_TABVIEW_AUTOSETUP_BASENODE_CLASSNAME, ITV_TABVIEW_AUTOSETUP_BASENODE_NODENAME);
		nodes.forEach(function(node) {
			ITV_TABVIEW_AUTOSETUP_INSTANCES.push(new ITVTabView(node, ITV_TABVIEW_AUTOSETUP_ADJUST_HEIGHT));
		});
		if (ITV.ua.isGecko) {
			window.addEventListenerITV('load', function() {
				ITV_TABVIEW_AUTOSETUP_INSTANCES.forEach(function(tabView) { tabView.adjustPaneHeight() });
			});
		}
	}
}






/* -------------------- Main : register start-up -------------------- */

if (typeof ITV == 'object' && ITV.ua.DOMok) {
	ITVTabViewAutoSetupPrepare();
	ITVAddOnload(ITVTabViewAutoSetup);
}
