﻿var hc = {};
hc.widget = {};
hc.util = {};
hc.util.getElement = function(el) {
	if (el && typeof el == "string")
		return document.getElementById(el);
	return el;
};
hc.util.getElementChildren = function(el) {
	var children = [];
	var child = el.firstChild;
	while (child)
	{
		if (child.nodeType == 1)
			children.push(child);
		child = child.nextSibling;
	}
	return children;
};
/*= nav 
--------------------------------------------------*/
hc.widget.nav = function(el,opt) {
    this.el = hc.util.getElement(el);
}
hc.widget.nav.prototype.getGroup = function() {
	return hc.util.getElementChildren(this.el)[0];
};
hc.widget.nav.prototype.getItems = function() {
	return hc.util.getElementChildren(this.getGroup());
};
/*= slider
--------------------------------------------------*/
hc.widget.slider = function(el,opt) {
    this.el = hc.util.getElement(el);
    this.curPanel = null;
    if(typeof opt != 'undefined' && typeof opt.def == 'number') { 
        //set default index
		this.curPanel = this.getPanels()[opt.def];
    }
    else
	    this.curPanel = this.getPanels()[0];
    this.curPanel.style.display = 'block';
};
hc.widget.slider.prototype.getGroup = function() {
	return hc.util.getElementChildren(this.el)[0];
};
hc.widget.slider.prototype.getPanels = function() {
	return hc.util.getElementChildren(this.getGroup());
};
hc.widget.slider.prototype.showPanel = function(tabIndex) {
    try {
        this.curPanel.style.display = 'none';
        this.curPanel = this.getPanels()[tabIndex];
        this.curPanel.style.display = 'block';
    }
    catch (ex) {}
};
/*= tabbed panels
--------------------------------------------------*/
hc.widget.tabbed = function (el,opt) {
    this.el = hc.util.getElement(el);
    this.mySlider = new hc.widget.slider(this.getContentGroup(),{def:opt.def});
}; 
hc.widget.tabbed.prototype.getTabGroup = function() {
	if (this.el) {
		var children = hc.util.getElementChildren(this.el);
		if (children.length)
			return children[0];
	}
	return null;
};
hc.widget.tabbed.prototype.getTabs = function() {
	var tabs = [];
	var tg = this.getTabGroup();
	if (tg) tabs = hc.util.getElementChildren(tg);
	return tabs;
};
hc.widget.tabbed.prototype.getContentGroup = function() {
	if(this.el) {
	    var children = hc.util.getElementChildren(this.el);
	    if (children.length) {
	        return children[1];
	    }
	}
	return null;
};
hc.widget.tabbed.prototype.showPanel = function(tabIndex){
    this.mySlider.showPanel(tabIndex);
};
