// *+++  Pane with tabs  +++*
// **************************

// tabsPane  MT / July 2007 / Virtuona d.o.o.
//
// called after loading the page
// Value "mytabs" is defined in the "tabsClass" variable

// Syntax as appears on a Wiki page is:
// %%mytabs
// * first_empty_tab_Title
// * second_tab_Title
// ** second_tab_content
// * third_tab_Title
// ** third_tab_content
// %%

// After loading the page but before execution of the script
 
// <div class="mytabs" >
//   <ul>
//     <li>first_empty_tab_Title
//     </li>
//     <li>second_tab_Title
//       <ul>
//     		<li>second_tab_content
//     		</li>
//       </ul>
//     </li>
//     <li>third_tab_Title
//       <ul>
//     		<li>third_tab_content
//     		</li>
//       </ul>
//     </li>
//   </ul>
// </div>
//

// After execution of the script: what is shown in the user's browser

// <div id="tabs_frame" class="mytabs" >
// 	<div id="tabs_header" >
//  	<ul id="tabs_primary">
//			<li>
//				<span id="first_empty_tab_Title" class="tabs_primary current" 
//					onclick="selectTab('first_empty_tab_Title', 'tabs_primary');">first_empty_tab_Title</span>
//			</li>
//			<li>
//				<span id="second_tab_Title" class="tabs_primary" 
//					onclick="selectTab('second_tab_Title', 'tabs_primary');">second_tab_Title</span>
//			</li>
//			<li>
//				<span id="third_tab_Title" class="tabs_primary" 
//					onclick="selectTab('third_tab_Title', 'tabs_primary');">third_tab_Title</span>
//			</li>
//		</ul>
//	</div>
// 	<div id="tabs_main" >
//		<fieldset id="first_empty_tab_Title_tabs_fields" class="tabs_tab">
//		</fieldset>
//		<fieldset id="second_tab_Title_tabs_fields" style="display: none;" class="tabs_tab">
//			second_tab_content
//		</fieldset>
//		<fieldset id="third_tab_Title_tabs_fields" style="display: none;" class="tabs_tab">
//			third_tab_content
//		</fieldset>
//	</div>
// </div>
//

function setDefault(name,val) {
	if (typeof(window[name])=="undefined" || window[name]==null) {
		window[name]=val;
	}
}
// +++++ Defaults and Constants +++++
	setDefault("tabsClass","mytabs");
	setDefault("tabs_primaryClass","tabs_primary");
	setDefault("tabs_tabClass","tabs_tab");
	setDefault("tabs_frameID","tabs_frame");
	setDefault("tabs_headerID","tabs_header");
	setDefault("tabs_primaryID","tabs_primary");
	setDefault("tabs_mainID","tabs_main");
	
	setDefault("tabs_currentSuffix"," current");
	setDefault("tabs_fieldsSuffix","_tabs_fields");
// +++++ Defaults and Constants +++++ end

// +++++ Events +++++
function swapTabs(newId, oldId) {
	Element.hide(oldId);
	Element.show(newId);
	$(oldId + '_tab').className = '';
	$(newId + '_tab').className = 'current';
}

function selectTab(tab, group) {
	var elements = document.getElementsByClassName(group);
	for (var i = 0; i < elements.length; i++) {
		elements[i].className = group;
		Element.hide(elements[i].id + tabs_fieldsSuffix);
	}
	$(tab).className += tabs_currentSuffix;
	Element.show(tab + tabs_fieldsSuffix);
}
// +++++ Events +++++ end


document.getElementsByTagAndClassName = function(tagName, className) {
  // Based on code from http://prototype.conio.net/
  var children = document.getElementsByTagName(tagName);
  var elements = new Array();
  
  for (var i = 0; i < children.length; i++) {
    var child = children[i];
    var classNames = child.className.split(' ');
    for (var j = 0; j < classNames.length; j++) {
      if (classNames[j] == className) {
        elements.push(child);
        break;
      }
    }
  }
  
  return elements;
}

// Search the document for DIV elements with the correct CLASS name, then process them
var vrednost;
var brliIE=0;
var brliNV=0;

function tabsPane() {
	if (!document.createElement) { return; } // Without createElement, we can't do anything
	
	// Processing all panes with tabs on the page: Process all divs with tabsClass CSS classes
	tabsPanes = document.getElementsByTagAndClassName("div",tabsClass);
	for (var tpi=0;tpi<tabsPanes.length;tpi++) {
		var tabsPane=tabsPanes[tpi];
		if (!tabsPane.childNodes || tabsPane.childNodes.length==0) {
			return;
		} else {
			tabsPane.id = tabs_frameID;
			tabsPane.className = tabsClass;
			// Procesing first (only) UL child element of the DIV
			if (!tabsPane.getElementsByTagName("ul") || tabsPane.getElementsByTagName("ul").length<1) {
				return;
			} else {
				var inlist = tabsPane.getElementsByTagName("ul")[0];
				var theader = document.createElement("div");
				theader.id = tabs_headerID;
				var theaderlist = document.createElement("ul");
				theaderlist.id = tabs_primaryID;
				theader.appendChild(theaderlist);
				var tmain = document.createElement("div");
				tmain.id = tabs_mainID;
				processTabsList(inlist, theaderlist, tmain);
				tabsPane.removeChild(inlist);
			}
		}
	}
}

// Process a UL tag and all its children, to convert to a pane with tabs
var expand;
function processTabsList(ul, headerlist, mainlist) {
	if (!ul.childNodes || ul.childNodes.length==0) { return; }
	// Iterate LIs
	var lis = ul.getElementsByTagName("li");
	var item;
	for (var itemi=0;itemi<lis.length;itemi++) {
		item = lis[itemi];
		
			// Iterate things in this LI
			var subLists = false;
			var sitem;
			for (var sitemi=0;sitemi<item.childNodes.length;sitemi++) {
				sitem = item.childNodes[sitemi];			
				if (sitem.nodeName=="UL") {
					subLists = true;
					processTabsList(sitem);
				}
			}
			var s= document.createElement("span");
			var t= '\u00A0'; // &nbsp;
			s.className = nodeLinkClass;
			if (subLists) {
				brliIE = brliIE + 1;
				// This LI has UL's in it, so it's a +/- node
				if (item.className==null || item.className=="") {
					item.className = nodeClosedClass;
				}
				// If it's just text, make the text work as the link also
				if (item.firstChild.nodeName=="#text") {
					// Napraviti procesiranje za tekst tako da novi red ne ide odmah levo
				
					t = t+item.firstChild.nodeValue;
					item.removeChild(item.firstChild);
				}
				item.id = "id" + brliIE;

				s.onclick = function () {
					var sul=sitem.childNodes;
					if (this.parentNode.className==nodeOpenClass) {
						//Izbacivanje iz cookie
						var pomid=this.parentNode.id;
						var start2 = document.cookie.indexOf(pomid);
						var start3 = document.cookie.indexOf("tree=");
						var len3 = start3 + 6;
						var endofcookie = document.cookie.indexOf(";",len3);
	
	
						if (start2!=-1){
							//alert(document.cookie.substring(start3,start2));
							//alert(document.cookie.substring(start2+pomid.length,endofcookie));
	
							var vrednost3 = document.cookie.substring(start3,start2) + document.cookie.substring(start2+pomid.length,endofcookie)+";";
							//alert(vrednost3);
							Set_Cookie(vrednost3);
						}
						this.parentNode.className = nodeClosedClass;
					} else {
						//Inserting into cookie
						var start1 = document.cookie.indexOf("tree=");
						var brliIE1 = this.parentNode.id;
						if (start1 != -1) {
							var len1 = start1 + 6;
							var end1 = document.cookie.indexOf(";",len1);
							var vrednost1=document.cookie.substring(start1,end1);
							if (vrednost1.indexOf(brliIE1)==-1) {
								vrednost1 = vrednost1 + brliIE1 +";";
								Set_Cookie(vrednost1);
							}
						} else{
							var vrednost2= "tree=" + brliIE1 + ";";
							Set_Cookie(vrednost2);
						}
						this.parentNode.className = nodeOpenClass;
					}

					return false;
				}


			} else {
				// No sublists, so it's just a bullet node
				item.className = nodeBulletClass;
				s.onclick = function () { return false; }
			}
			//t = t + "BR";
			
			s.appendChild(document.createTextNode(t));
		
			item.insertBefore(s,item.firstChild);
			//t.style.text-indent=10+"px";
		
	}
}

