window.onload = init;

function init(){
   var sTab = getTab();

   var t = $$('tabs');
   for (var n= 0;n<t.length;n++){
	   new Tabs(t[n], sTab);   
   }

   // offerform functionality
   if($('contact')){
	doOfferForm();
   }

}

function doOfferForm(){
	
	// get the datepickers
	if($('offerDate')){
		window.alert(datePickerController.createDatePicker);
		datePickerController.createDatePicker({
			id:"offerDate",
			format:"d-sl-m-sl-Y"
		});
	}
	
}

function Tabs(o, sTab){
	   this.dl = o;
	   if (this.dl){
	      this.dt = this.dl.getElementsByTagName('dt');
	      this.dd = this.dl.getElementsByTagName('dd');
	      this.tabs = [];
	      this.cTab = -1;
	      this.sTab = sTab;
	      this.createTabs();
	   }
	}

	Tabs.prototype = {

	   createTabs : function(){
	      var uTab = 0;
	      var cl = '';
	      for (var n = 0; n < this.dt.length; n++){
	    	  if (this.dt[n].parentNode == this.dl){
	    	      cl = this.dd[n].className;
	    	      if (cl == this.sTab) {
	    	          uTab = n;
	    	      }
	    		  this.tabs[n] = new Tab(this.dt[n], this.dd[n], n, this);
	    	  }
	      }
	      
	      if (this.tabs[uTab]){
	    	  this.tabs[uTab].doTab();  
	      }
	   }

	}

	function Tab(dt, dd, id, p){
	   this.dt = dt;
	   this.dd = dd;
	   this.id = id;
	   this.parent = p;
	   this.dt.onclick = bind(this, this.doTab);
	}

	Tab.prototype = {

	   doTab : function(){
	      if (this.id != this.parent.cTab){
	         this.dt.className += ' selected';
	         this.dd.className += ' selected';
	         this.dd.parentNode.style.height = this.dt.offsetHeight + (this.dd.offsetHeight - 5) + 'px';
	         if (this.parent.tabs[this.parent.cTab]){
	            this.parent.tabs[this.parent.cTab].unDoTab();
	         }
	         this.parent.cTab = this.id;
	      }
	   },
	   
	   unDoTab : function(){
	      this.dt.className = this.dt.className.replace(/selected/, '');
	      this.dd.className = this.dd.className.replace(/selected/, '');
	   }
	}

	 function bind(e, f){
	    return function() { f.call(e); }
	 }
	 
/*
 * returns the get variable 'tab'
 *
 */
function getTab() {
    var query = window.location.search.substring(1);
    var parms = query.split('&');
    for (var i=0; i<parms.length; i++) {
        var pos = parms[i].indexOf('=');
        if (pos > 0) {
            var key = parms[i].substring(0,pos);
            var val = parms[i].substring(pos+1);
            if (key == 'tab') {
                return val;
            }
        }
    }
    return false;
} 

function $(e){
	if (typeof(e) == 'string'){
		return document.getElementById(e);
	}else if(typeof(e) == 'object'){
		return e;
	}else{
		return false;
	}
}


/*
 * Function: $$(c, p)
 * getElementsByClassName, walks through the dom nodes and stores the found nodes in an array.
 * 
 * Parameters:
 * 
 * String c - The className that has to be found.
 * Object p - {hashtable} containing specific details (tagName & parent).
 * 
 * Returns:
 * 	Array containing the found nodes
 * 
 * Example:
 * 
 * > // get All the elements with className hoverLink
 * >    var hoverLinks = $$('hoverLink');
 * >
 * > // get All the links with className hoverLink
 * >    var hoverLinks = $$('hoverLink', {tagName:'a'});
 * >
 * > // get All the links tiwh className hoverLink in the content div
 * >    var hoverLinks = $$('hoverLink', {tagName:'a',parent:$('content')});
 * 
 */
function $$(c, p){
	var f = [];
	var e = [];
	var r = new RegExp(c);
	if (p){
	 	if (p.tagName){
	 		e = document.getElementsByTagName('a')
	 	}else if (p.parent){
	 		_$$($(p.parent), e);
	 	}else{
	 		_$$(document, e);
	 	}
	}else{
		_$$(document, e);
	}
	for (var n=0;n<e.length;n++){
		if (r.test(e[n].className)){
			f.push(e[n]);
		}
	}
	if (f.length > 0){
		return f;
	}else{
		return false;
	}
}

// getChildNodes from o and push in array a
function _$$(o, a){
	if (typeof o == 'object'){
	 	var x = o.childNodes;
		for (var n=0;n<x.length;n++){
			a.push(x[n]);
			_$$(x[n], a);
	 	}
	}
}
