
/* show/hide */

function showHide(){

	var target, container, nodeClass, pattern; 
	var items = new Array();
	this.current = new Object;
	
	this.initControl = function(){

		for(var i = 0; i < items.length; i++){
			a = items[i].getElementsByTagName('a')[0];
			a.onclick = function(){
			hider.show(hider.current+1);
			return false;
			}
		}
	};
	
	this.collectItems = function(value){
		
		var temp = new Array();

		temp = container.getElementsByTagName(value);	
		
		for(var i = 0; i < temp.length; i++){
			
			if(nodeClass){
				
				if(pattern.test(temp[i].className)){
					items.push(temp[i]);
				}
				 
			}else{
				items.push(temp[i]);
			}
		}
		return items;		
	}
	
	this.setContainer = function(value){
		container = document.getElementById(value);
	}
	
	this.setClass = function(value){
		nodeClass = value;
		pattern = new RegExp("(^| )" + nodeClass + "( |$)");
	}
	
	this.hide = function(){
	}
	
	this.show = function(value){
		
		if(typeof value == "undefined" || typeof this.current == "undefined"){
			this.current = 0;
		}
		else
		{
			this.current = parseInt(value);			
		}
		
		if(this.current < items.length && this.current >= 0){
			this.toggle(this.current);			
		}
		else
		{
			this.current = (this.current == items.length ? items.length - 1: 0);
			this.toggle(this.current);	
			
		}

	}
	
	this.toggle = function(id){
	
		for(var i = 0; i < items.length; i++){
				
					if(i == this.current){
						removeClass(items[i], 'hidden');
						addClass(items[i],'shown');						
					}
					else
					{
						removeClass(items[i], 'shown');
						addClass(items[i],'hidden');
					}				
			}
	}
	
	return true;	
}

function initHiders(){

	var hider = new showHide();
	hider.setContainer('header');
	hider.setClass('hidable');
	hider.collectItems('FIELDSET');
	//hider.initControl('A');
	hider.show(0);	
	//var prev = document.getElementById('prev');
	var next = getElementsByAttribute('class','nextBtn');
	for(var i = 0; i < next.length; i++){
		next[i].onclick = function(){
			hider.show(hider.current+1);
			return false;
		}
	};
	/*prev.onclick = function(){
		hider.show(hider.current-1);
		return false;
	}*/
}

addLoadListener(initHiders);
addLoadListener(initGlobalJSNotifier);

function addLoadListener(fn){
	if (typeof window.addEventListener != 'undefined'){
    window.addEventListener('load', fn, false);
  }else if (typeof document.addEventListener != 'undefined'){
	  document.addEventListener('load', fn, false);
	  }else if (typeof window.attachEvent != 'undefined'){
    window.attachEvent('onload', fn);
	}else{
    var oldfn = window.onload;
    if(typeof window.onload != 'function'){
      window.onload = fn;
    }else{
      window.onload = function(){
        oldfn();
        fn();
      };
    }
  }
}

function addClass(target, classValue){
	
  var pattern = new RegExp("(^| )" + classValue + "( |$)");

  if (!pattern.test(target.className)){
	  
    if (target.className == ""){
      target.className = classValue;
    
	}else{
      target.className += " " + classValue;
    }
  }

  return true;
}

function removeClass(target, classValue){
	
  var removedClass = target.className;
  var pattern = new RegExp("(^| )" + classValue + "( |$)");

  removedClass = removedClass.replace(pattern, "$1");
  removedClass = removedClass.replace(/ $/, "");

  target.className = removedClass;

  return true;
}

function initGlobalJSNotifier(){
	var h = document.getElementsByTagName('HTML')[0];
	addClass(h,'js');
}

function getElementsByAttribute(attribute, attributeValue, container){
	
  var elementArray = new Array();
  var matchedArray = new Array();

	if(typeof container == "undefined"){
	
	  if (document.all){
		elementArray = document.all;
	  
	  }else{
		elementArray = document.getElementsByTagName("*");
	  }
	
	}else if(typeof container == "string"){
		elementArray = document.getElementsByTagName(container);
		alert('Is string: ' + container.length);
	}else{
		elementArray.push(container);
		alert('Is ref: ' + container.length);
	}
	  
  for (var i = 0; i < elementArray.length; i++){
    if (attribute == "class")
	{
      var pattern = new RegExp("(^| )" + attributeValue + "( |$)");

      if (pattern.test(elementArray[i].className)){
        matchedArray[matchedArray.length] = elementArray[i];
      }
    
	}else if (attribute == "for"){
		
      if (elementArray[i].getAttribute("htmlFor") || elementArray[i].getAttribute("for")){
        if (elementArray[i].htmlFor == attributeValue){
          matchedArray[matchedArray.length] = elementArray[i];
        }
      }
    }
    else if (elementArray[i].getAttribute(attribute) == attributeValue){
      matchedArray[matchedArray.length] = elementArray[i];
    }
  }

  return matchedArray;
}