//  simple collapsible panel -------------------------------------------
function CPanelSimple(id_panel, id_arr_d, id_arr_r, is_opened) {
	var is_opened = is_opened || 0;
	var elem_panel = document.getElementById(id_panel);
	var elem_arr_d = document.getElementById(id_arr_d);
	var elem_arr_r = document.getElementById(id_arr_r);
	if (elem_panel && elem_arr_d && elem_arr_r) {
		this.id_panel = id_panel;
		this.id_arr_r = id_arr_r;
		this.id_arr_d = id_arr_d;
		elem_arr_d.style.cursor = "pointer";
		elem_arr_r.style.cursor = "pointer";
		if (is_opened) {
			this.isOpen = true;
			elem_panel.style.display = "block";
			elem_arr_d.style.display = "";
			elem_arr_r.style.display = "none";
		} else {
			this.isOpen = false;
			elem_panel.style.display = "none";
			elem_arr_d.style.display = "none";
			elem_arr_r.style.display = "";
		}
		this.attachListeners();
	}
}
CPanelSimple.addEventListener = function(obj, eventType, handler, capture) {
	try {
		if (obj.addEventListener)
			obj.addEventListener(eventType, handler, capture);
		else if (obj.attachEvent)
			obj.attachEvent("on" + eventType, handler);
	}
	catch (e) {}
}
CPanelSimple.prototype.onArrowClick = function(e) {
	var obj = document.getElementById(this.id_panel);
	if (obj.style.display == "block") {
		this.isOpen = false;
		obj.style.display = "none";
		document.getElementById(this.id_arr_d).style.display = "none";
		document.getElementById(this.id_arr_r).style.display = "";
	} else {
		this.isOpen = true;
		obj.style.display = "block";
		document.getElementById(this.id_arr_d).style.display = "";
		document.getElementById(this.id_arr_r).style.display = "none";
	}
}

CPanelSimple.prototype.attachListeners = function() {
	var self = this;
	CPanelSimple.addEventListener(
		document.getElementById(this.id_arr_d),
		"click",
		function(e) { return self.onArrowClick(e); },
		false);

	CPanelSimple.addEventListener(
		document.getElementById(this.id_arr_r),
		"click",
		function(e) { return self.onArrowClick(e); },
		false);
}

//  collapsible panel -------------------------------------------
function CPanelManager(id, moreLink) {
	this.id = id;
	this.moreLink = '';
	if (moreLink) {
		this.moreLink = moreLink;
		this.getHeadLink().style.cursor = "pointer";
	}
	this.isFirstOpen = true;
	this.init();
}

CPanelManager.prototype.getBlock = function() {
	return document.getElementById("cpanel_block_" + this.id);
}

CPanelManager.prototype.getContent = function() {
	return document.getElementById("cpanel_content_" + this.id);
}

CPanelManager.prototype.getHeadLink = function() {
	return document.getElementById("cpanel_head_link_" + this.id);
}

CPanelManager.prototype.getHeadInfo = function() {
	return document.getElementById("cpanel_head_info_" + this.id);
}

CPanelManager.prototype.getPanel = function() {
	return document.getElementById("cpanel_panel_" + this.id);
}

CPanelManager.prototype.setOnChangeListener = function(func) {
	this.onChangeHandler = func;
}

CPanelManager.prototype.onHeadClick = function(e) {
	var obj = this.getPanel();
	var firstOpen = false;
	if (obj.style.display) {
		obj.style.display = "";
		this.isOpen = true;
		if (this.isFirstOpen) {
			this.isFirstOpen = false;
			firstOpen = true;
		}
		document.getElementById("cpanel_head_arrow_down_" + this.id).style.display = "";
		document.getElementById("cpanel_head_arrow_right_" + this.id).style.display = "none";
		var btm = document.getElementById("cpanel_panel_bottom_" + this.id);
		if (btm) btm.style.display = "";
	} else {
		obj.style.display = "none";
		this.isOpen = false;
		document.getElementById("cpanel_head_arrow_down_" + this.id).style.display = "none";
		document.getElementById("cpanel_head_arrow_right_" + this.id).style.display = "";
		var btm = document.getElementById("cpanel_panel_bottom_" + this.id);
		if (btm) btm.style.display = "none";
	}
	if (typeof(this.onChangeHandler) == "function") {
		this.onChangeHandler(this.isOpen, firstOpen);
	}
}

CPanelManager.prototype.onHeadLinkClick = function(e) {
	if (this.moreLink) {
		document.location = this.moreLink;
	}
}

CPanelManager.addEventListener = function(obj, eventType, handler, capture) {
	try {
		if (obj.addEventListener)
			obj.addEventListener(eventType, handler, capture);
		else if (obj.attachEvent)
			obj.attachEvent("on" + eventType, handler);
	}
	catch (e) {}
}

CPanelManager.prototype.attachListeners = function() {
	var self = this;
	CPanelManager.addEventListener(
		document.getElementById("cpanel_head_arrow_down_" + this.id),
		"click",
		function(e) { return self.onHeadClick(e); },
		false);

	CPanelManager.addEventListener(
		document.getElementById("cpanel_head_arrow_right_" + this.id),
		"click",
		function(e) { return self.onHeadClick(e); },
		false);

	CPanelManager.addEventListener(
		document.getElementById("cpanel_head_link_" + this.id),
		"click",
		function(e) { return self.onHeadLinkClick(e); },
		false);
}

CPanelManager.prototype.init = function() {
	this.getBlock().style.display = "";
	this.attachListeners();
	this.setVisible(true);
}

CPanelManager.prototype.setVisible = function(value) {
	if (value) {
		this.getPanel().style.display = "";
		document.getElementById("cpanel_head_arrow_down_" + this.id).style.display = "";
		document.getElementById("cpanel_head_arrow_right_" + this.id).style.display = "none";
	} else {
		this.getPanel().style.display = "none";
		document.getElementById("cpanel_head_arrow_down_" + this.id).style.display = "none";
		document.getElementById("cpanel_head_arrow_right_" + this.id).style.display = "";
	}
}

// tabbed panel -------------------------------------------------------------------------------------
function TabbedPanelHeadLink(name, href) {
	this.name = name;
	this.href = href;
}
 
function TabbedPanelManager(id, tabs) {
	this.id = id;
	this.tabs = tabs;
	this.onTabListeners = new Array();
	this.headLinks = new Array();
	this.selectedIndex;
	// classes
	this.class_simpleTab = "TabbedPanel_simpleTab"
	this.class_selectedTab = "TabbedPanel_selectedTab";
	this.class_hoverTab = "TabbedPanel_hoverTab";
	this.class_visiblePanel = "TabbedPanel_visibleContent";
	this.class_simplePanel = "TabbedPanel_content";
}

TabbedPanelManager.addEventListener = function(obj, eventType, handler, capture) {
	try {
 		if (obj.addEventListener)
 			obj.addEventListener(eventType, handler, capture);
 		else if (obj.attachEvent)
 			obj.attachEvent("on" + eventType, handler);
 	} catch (e) {};
}

TabbedPanelManager.prototype.setHeadLink = function(index, name, href) {
	this.headLinks[index] = new TabbedPanelHeadLink(name, href);	
}

TabbedPanelManager.prototype.getTab = function(index) {
	return document.getElementById("tab_" + this.id + "_" + index);
}

TabbedPanelManager.prototype.getTabInfo = function(index) {
	return document.getElementById("tab_info_" + this.id + "_" + index);
}

TabbedPanelManager.prototype.getPanel = function(index) {
	return document.getElementById("TabbedPanel_" + this.id + "_" + index);
}

TabbedPanelManager.prototype.getContent = function(index) {
	return document.getElementById('TabbedPanel_content_' + this.id + '_' + index);
}

TabbedPanelManager.prototype.onclick = function(e, index) {
	if (typeof(this.onTabListeners[index]) == "function") {
		var r = this.onTabListeners[index]();
		if (r === false) return;
	}
	this.showPanel(index);
}

TabbedPanelManager.prototype.onTabMouseOver = function(e, index)
{
	var t = this.getTab(index);
	t.className = this.class_hoverTab;
}

TabbedPanelManager.prototype.onTabMouseOut = function(e, index)
{
	var t = this.getTab(index);
	if (this.selectedIndex == index) {
		t.className = this.class_selectedTab;
	} else {
		t.className = this.class_simpleTab;
	}
}

TabbedPanelManager.prototype.attachListeners = function(index) {
	var self = this;
	TabbedPanelManager.addEventListener(
			this.getTab(index),
			"click",
			function(e) { return self.onclick(e, index) },
			false);
	TabbedPanelManager.addEventListener(
			this.getTab(index),
			"mouseover",
			function(e) { return self.onTabMouseOver(e, index) },
			false);
	TabbedPanelManager.addEventListener(
			this.getTab(index),
			"mouseout",
			function(e) { return self.onTabMouseOut(e, index) },
			false);
}

TabbedPanelManager.prototype.setOnTabListener = function(index, func) {
	this.onTabListeners[index] = func;	
}

TabbedPanelManager.prototype.init = function(index) {
	for (var i = 1; i <= this.tabs; i++) {
		this.attachListeners(i);
		if (i <= this.tabs) {
			this.getTab(i).style.display = "";
		}
	}
	if (index > 0 && index <= this.tabs) {
		this.showPanel(index);
	} else {
		this.showPanel(1);
	}
}

TabbedPanelManager.prototype.showPanel = function(index) {
	this.selectedIndex = index;

	var t, p;	
	for (var i = 1; i <= this.tabs; i++)	{
		if (i != index)	{
			t = this.getTab(i);
			if (t)	t.className = this.class_simpleTab;
			p = this.getPanel(i);
			if (p)	{
				p.className = this.class_simplePanel;
				p.style.display = "none";
			}
		}
	}
	// if headLink enabled?
	var head = document.getElementById("TabbedPanel_head_" + this.id);		
	var a = this.headLinks[index];
	if (a) {
		var link = document.getElementById("TabbedPanel_link_" + this.id);		
		link.href = a.href;
		link.innerHTML = a.name;
		head.style.display = "";		
	} else {
		head.style.display = "none";		
	}

	t = this.getTab(index);
	t.className = this.class_selectedTab;
	p = this.getPanel(index);	
	p.className = this.class_visiblePanel;
	p.style.display = "block";
}

// -------------------------------------------------------------------------------------------