var plusSign = 'images/plus.gif';
var minusSign = 'images/minus.gif';
var startToggler = null;
//ListToggler Class
ListToggler = Class.create();
ListToggler.prototype = {
	initialize: function(listID, myName){
		this.myName = myName;
		this.previousToggled = null;
		this.list = $(listID);
		Element.cleanWhitespace(this.list);
		this.listItems = this.list.childNodes;
		for(i=0;i<this.listItems.length;i=i+2){
			this.listItems[i+1].style.display = "none";
			var toggleLink = document.createElement('a');
			toggleLink.href = "#";
			toggleLink.onclick = new Function(this.myName+".toggle("+i+");");
			var toggleImage = document.createElement('img');
			toggleImage.border = "0";
			toggleLink.appendChild(toggleImage);
			toggleImage.src = plusSign;
			this.listItems[i].insertBefore(toggleLink, this.listItems[i].firstChild);
		}
	},
	toggle: function(i){
		if(this.listItems[i+1].style.display == "block"){
			this.listItems[i+1].style.display = 'none';
			this.listItems[i].getElementsByTagName('img')[0].src = plusSign;
			this.listItems[i].getElementsByTagName('img')[0].blur();
			this.previousToggled = null;
		}
		else{
			this.listItems[i+1].style.display = 'block';
			this.listItems[i].getElementsByTagName('img')[0].src = minusSign;
			this.listItems[i].getElementsByTagName('img')[0].blur();
			if(this.previousToggled!==null){
				this.toggle(this.previousToggled);
			}
			this.previousToggled = i;
		}
	}
};

TableToggler = Class.create();
TableToggler.prototype = {
	initialize: function(myName, boxes, names){
		this.names = [];
		this.elements = [];
		var j = 0;
		for(i=0;i<boxes.length;i++){
			if($(boxes[i])){
				this.elements[j] = $(boxes[i]);
				this.names[j] = names[i];
				j++;
			}
		}
		var sectionToggler = document.createElement('ul');
		sectionToggler.id = 'sectionToggler';
		sectionToggler.className = 'tabbernav';
		this.tabs = [];
		for(i=0;i<this.elements.length;i++){
			this.elements[i].style.display = "none";
			this.tabs[i] = document.createElement('li');
			var togglerLink = document.createElement('a');
			this.tabs[i].appendChild(togglerLink);
			togglerLink.onclick = new Function(myName+'.toggle('+i+');');
			togglerLink.href ="javascript:void(null);";
			togglerLink.innerHTML = this.names[i];
			sectionToggler.appendChild(this.tabs[i]);
		}
		this.elements[0].parentNode.parentNode.insertBefore(sectionToggler, this.elements[0].parentNode);
		this.toggle(0);
	},
	toggle: function(id){
		for(i=0;i<this.elements.length;i++){
			if(i == id){
				this.elements[i].style.display = '';
				this.tabs[i].className = 'tabberactive';
				this.tabs[i].blur();
			}
			else{
				this.elements[i].style.display = 'none';
				this.tabs[i].className = '';
			}
		}
	}
};

//Searchbox focus/blur Class
Searchbox = Class.create();
Searchbox.prototype = {
	initialize: function(frmField, myName){
		this.value = frmField.value;
		this.frmField = frmField;
		this.myName = myName;
		this.frmField.onfocus = new Function(this.myName+".focus();");
		this.frmField.onblur = new Function(this.myName+".blur();");
	},
	blur: function(){
		if(this.frmField.value === '') {
			this.frmField.value = this.value;
		}
	},
	focus: function(){
		if(this.frmField.value == this.value) {
			this.frmField.value = '';
		}
	}
};

var SmartTable;
SmartTable = Class.create();
SmartTable.prototype = {
	initialize: function(table,name, reverse, noNavigation,numberOfRows){
		navigation = $('smartTableNavigation');
		if(navigation){
			navigation.parentNode.removeChild(navigation);
		}
		if(noNavigation){
			this.showNavigation = false;
		}
		else{
			this.showNavigation = true;
		}
		this.name = name;
		this.earlier = 'Tidigare Matcher';
		this.later = 'Senare matcher';
		this.reverse = reverse;
		this.lastFilterString = "";
		this.current = 0;
		if(numberOfRows){
			this.numberOfRows = numberOfRows;
		}
		else{
			this.numberOfRows = 20;
		}
		this.displayTable = $(table).tBodies[0];
		Element.cleanWhitespace(this.displayTable);
		this.storeTable = this.displayTable.cloneNode(true);
		this.backupTable = this.displayTable.cloneNode(true);
		this.rows = this.storeTable.getElementsByTagName('tr');
		this.allRows = this.backupTable.getElementsByTagName('tr');
		this.allRowsText = [];
		for(i=0;i<this.allRows.length;i++){
			try{
				this.allRowsText[i] = this.allRows[i].getElementsByTagName('td')[1].textContent;
			}
			catch(e){
				this.allRowsText[i] = ' ';
			}
		}
		this.numberOfPages = Math.ceil(this.rows.length/this.numberOfRows);
		if(this.rows.length > 0){
			this.show(0);
		}
	},
	show: function(start){
		this.current = start;
		try{
		while(this.displayTable.rows.length){ // length=0 -> stop 
        	this.displayTable.deleteRow(this.displayTable.rows.length-1);
		}
		}
		catch(e){
			alert(e);
		}
		for(i=0;i<this.numberOfRows && i<this.rows.length-start;i++){
			this.displayTable.appendChild(this.rows[i+start].cloneNode(true));
		}
		
		for(i=0;i<this.displayTable.rows.length;i++){
			try{
				loop = true;
				for(j=0;loop && j<this.displayTable.rows[i].getElementsByTagName('td').length;j++){
					if(this.displayTable.rows[i].getElementsByTagName('td')[j].firstChild.href){
						this.displayTable.rows[i].onclick = new Function('document.location.href = "'+this.displayTable.rows[i].getElementsByTagName('td')[j].firstChild.href+'"');
						loop = false;
					}
				}
			}
			catch(e){
				//alert(e);
			}
		}
		
		if(this.numberOfPages>1 && this.showNavigation){
			this.buildNavigation();
		}
		else{
			navigation = $('smartTableNavigation');
			if(navigation){
				navigation.parentNode.removeChild(navigation);
			}
		}
	},
	toggle: function(amount){
		if(this.numberOfRows < this.allRows.length){
			this.showAllRows();
			this.displayTable.parentNode.tBodies[1].style.display = 'none';
			this.displayTable.parentNode.tBodies[2].style.display = '';	
		}
		else{
			this.setNumberOfRows(amount);
			this.displayTable.parentNode.tBodies[1].style.display = '';
			this.displayTable.parentNode.tBodies[2].style.display = 'none';		
		}
	},
	filter: function(text){
		var myRE = new RegExp(text, "i");
		while(this.storeTable.rows.length){ // length=0 -> stop 
        	this.storeTable.deleteRow(this.storeTable.rows.length-1);
		}
		this.storeTable.tBodies[0].appendChild(this.headerRow);
		var k = 0;
		for(i=1;i<this.allRows.length||i<this.numberOfRows;i++){
			if(this.allRowsText[i].match(myRE)){
				var node = this.allRows[i].cloneNode(true);
				if(k%2){
					node.className = 'odd';
					node.onmouseout = setOdd;
				}
				else{
					node.className = 'even';
					node.onmouseout = setEven;
				}
				this.storeTable.tBodies[0].appendChild(node);
				k++;
			}
		}
		this.rows = this.storeTable.rows;
		this.numberOfPages = Math.ceil(this.rows.length/this.numberOfRows);
		if(this.rows.length > 0){
			this.show(0);
		}
		this.lastFilterString = text;
	},
	setNumberOfRows: function(value){
		this.numberOfRows = parseInt(value);
		this.numberOfPages = Math.ceil(this.rows.length/this.numberOfRows);
		this.current = 0;
		this.show(this.current);
	},	
	showAllRows: function(){
		this.setNumberOfRows(this.allRows.length);
	},
	buildNavigation: function(){
		var navigation = $(this.name+'-smartTableNavigation');
		if(!navigation){
			var footer = document.createElement('tbody');
			var row = document.createElement('tr');
			var navigation = document.createElement('td');
			navigation.colSpan = 3;
			navigation.id = this.name+"-smartTableNavigation";
			row.appendChild(navigation);
			footer.appendChild(row);
			this.displayTable.parentNode.appendChild(footer);
		}
		var next = this.current+this.numberOfRows;
		var previous = this.current - this.numberOfRows;
		var page = Math.ceil(this.current/this.numberOfRows) + 1;
		navigation.innerHTML = "";
		var links = "";
		if(page>1){
			 links += '<a href="#" onclick="javascript:'+this.name+'.show('+previous+')">';
			 if(this.reverse){
				links += this.later;
			 }
			 else{
				links += this.earlier;
			 }
			 links += '</a> ';
		}
		links += 'Visar sida '+page+' av ' + this.numberOfPages;
		if(page<this.numberOfPages){
			links +='<a href="#" onclick="javascript:'+this.name+'.show('+next+')"> ';
			if(this.reverse){
				links += this.earlier;
			}
			else{
				links += this.later;
			}
			
			links += '</a>';
		}
		navigation.innerHTML = links;
	}
};