function getID(id){
	if(document.getElementById(id)){
		return document.getElementById(id);
	}else{
		alert(id+" does not exist!");
		return false;
	}
}

// this beauty actually comes from http://www.quirksmode.org/js/detect.html
var jsBrowser = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "An unknown version";
		this.OS = this.searchString(this.dataOS) || "An unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]
};

jsBrowser.init();
// alert(jsBrowser.browser);
// alert(jsBrowser.version);
// alert(jsBrowser.OS);


var jsAnim = {
	getO : function(id){
		if(document.getElementById(id)){
			return document.getElementById(id);
		}else{
			alert(id+" does not exist!");
			return false;
		}
	},
	getW : function(id){
		return parseInt(this.getO(id).offsetWidth);
	},
	getH : function(id){
		return parseInt(this.getO(id).offsetHeight);
	},
	setWH : function(id,w,h){
		this.setW(id,w);
		this.setH(id,h);
	},
	setW : function(id,w){
		this.getO(id).style.width = parseInt(w) + "px";
	},
	setH : function(id,h){
		this.getO(id).style.height = parseInt(h) + "px";
	},
	getX : function(id){
		var obj = this.getO(id);
		if (window.self != window.top){
			var curleft = window.top.jsAnim.getX('xa');
		}else{
			var curleft = 0;
		}
		if(obj.offsetParent){
			while(1) {
				curleft += obj.offsetLeft;
				if(!obj.offsetParent){
					break;
				}
				obj = obj.offsetParent;
			}
		}else if(obj.x){
			curleft += obj.x;
		}
		return curleft;
	},
	getY : function(id){
		var obj = this.getO(id);
		if (window.self != window.top){
			var curtop = window.top.jsAnim.getY('xa');
		}else{
			var curtop = 0;
		}
		if(obj.offsetParent){
			while(1){
				curtop += obj.offsetTop;
				if(!obj.offsetParent){
					break;
				}
				obj = obj.offsetParent;
			}
		}else if(obj.y){
			curtop += obj.y;
		}
		return curtop;
	},
	setXY : function(id,x,y){
		this.setX(id,x);
		this.setY(id,y);
	},
	setX : function(id,x){
		this.getO(id).style.left = parseInt(x) + "px";
	},
	setY : function(id,y){
		this.getO(id).style.top = parseInt(y) + "px";
	},
	toggleO : function(){
		var args = arguments.length;
		for(i=0;i<args;i++){
			if(this.getO(arguments[i])){
				var f = this.getO(arguments[i]).style.display;
				if(f == "none"){
					this.showO(arguments[i]);
				}else{
					this.hideO(arguments[i]);
				}
			}
		}
	},
	hideO : function(id){
		if(this.getO(id).style.display != "none"){
			this.getO(id).style.display = "none";
		}
	},
	showO : function(id){
		if(this.getO(id).style.display == "none"){
			this.getO(id).style.display = "";
		}
	}
};

var jsMenu = {
	
	trycount : 0,
	menu_id : false,
	button_id : false,
	mask_id : false,
	close_id : false,
	visible : false,
	button : {},
	
	getO : function(id){
		if(document.getElementById(id)){
			return document.getElementById(id);
		}else{
			alert(id+" does not exist!");
			return false;
		}
	},
	init : function(menu_id,button_id,close_id){
		this.menu_id = menu_id;
		this.mask_id = "maskFrame";
		this.button_id = button_id;
		srcObj = this;
		if(close_id){
			this.close_id = close_id;
			jsEvent.addHandler(this.close_id,"onclick",function(){ srcObj.hide.apply(srcObj) });
		}else{
			this.close_id = false;
			jsEvent.addHandler(this.button_id,"onmouseout",function(){ srcObj.hide.apply(srcObj) });
		}
		jsEvent.addHandler(this.menu_id,"onmouseover",function(){ srcObj.show.apply(srcObj) });
		jsEvent.addHandler(this.menu_id,"onmouseout",function(){ srcObj.hide.apply(srcObj) });
		jsEvent.addHandler(this.menu_id,"onclick",function(){ srcObj.hide.apply(srcObj) });
	},
	display : function(menu_width,menu_height,menu_loc,menu_content){
	
		// hide menu if previously open
		this.hide();
		
		// get button position
		if(this.button_id){
			this.button.x = jsAnim.getX(this.button_id);
			this.button.y = jsAnim.getY(this.button_id);
			this.button.w = jsAnim.getW(this.button_id);
			this.button.h = jsAnim.getH(this.button_id);
		}else{
			alert("this.button_id is not defined!");
			return false;
		}
		
		// get relative target position
		var target_x = 0;
		var target_y = 0;
		
		switch(menu_loc){
			case "BR":
				target_x = this.button.x;
				target_y = this.button.y + this.button.h;
				break;
			case "BL":
				target_x = this.button.x + this.button.w - menu_width; 
				target_y = this.button.y + this.button.h;
				break;
			case "TR":
				target_x = this.button.x;
				target_y = this.button.y - menu_height;
				break;
			case "TL":
				target_x = this.button.x + this.button.w - menu_width; 
				target_y = this.button.y - menu_height;
				break;
		}
		
		winW = document.body.offsetWidth;
  		winH = document.body.offsetHeight;
		
		if(this.trycount <= 8){
			switch(menu_loc){
				case "BR":
					if(target_x + menu_width > winW){
						this.trycount++;
						this.display(menu_width,menu_height,'BL',menu_content);
						return false;
					}
					if(target_y + menu_height > winH){
						this.trycount++;
						this.display(menu_width,menu_height,'TR',menu_content);
						return false;
					}
					break;
				case "BL":
					if(target_x < 0){
						this.trycount++;
						this.display(menu_width,menu_height,'BR',menu_content);
						return false;
					}
					if(target_y + menu_height > winH){
						this.trycount++;
						this.display(menu_width,menu_height,'TL',menu_content);
						return false;
					}
					break;
				case "TR":
					if(target_x + menu_width > winW){
						this.trycount++;
						this.display(menu_width,menu_height,'TL',menu_content);
						return false;
					}
					if(target_y < 0){
						this.trycount++;
						this.display(menu_width,menu_height,'BR',menu_content);
						return false;
					}
					break;
				case "TL":
					if(target_x < 0){
						this.trycount++;
						this.display(menu_width,menu_height,'TR',menu_content);
						return false;
					}
					if(target_y < 0){
						this.trycount++;
						this.display(menu_width,menu_height,'BL',menu_content);
						return false;
					}
					break;
			}
		}else{
			this.trycount = 0;
			alert("Menu does not fit into screen");
			return false;
		}
		// resize iframe and menu
		if(jsBrowser.browser == "Explorer"){
			jsAnim.setWH(this.mask_id,menu_width,menu_height);
		}
		jsAnim.setWH(this.menu_id,menu_width,menu_height);
		// position iframe and menu
		if(this.close_id){
			jsAnim.setXY(this.close_id,this.button.x,this.button.y);
		}
		if(jsBrowser.browser == "Explorer"){
			jsAnim.setXY(this.mask_id,target_x,target_y);
		}
		jsAnim.setXY(this.menu_id,target_x,target_y);
		// fill menu with content
		this.getO(this.menu_id).innerHTML = menu_content;
		// display iframe and menu
		this.show();
		
	},
	show : function(){
		this.trycount = 0;
		if(this.close_id){
			jsAnim.showO(this.close_id);
		}
		if(jsBrowser.browser == "Explorer"){
			jsAnim.showO(this.mask_id);
		}
		jsAnim.showO(this.menu_id);
		this.visible = true;
	},
	hide : function(){
		if(this.close_id){
			jsAnim.hideO(this.close_id);
		}
		if(jsBrowser.browser == "Explorer"){
			jsAnim.hideO(this.mask_id);
		}
		jsAnim.hideO(this.menu_id);
		this.visible = false;
	}
};

var kiwiPanel = {
	open : function(label){
		jsAnim.setW('id_action_panel',270);
		getID('id_action_panel_label').innerHTML = label;
	},
	close : function(){
		if(jsMenu.menu_id){
			jsMenu.hide();
		}
		getID('id_action_panel_label').innerHTML = "";
		getID('xa').innerHTML = "";
		jsAnim.setW('id_action_panel',20);
	}
};

var jsEvent = {
	addHandler : function(id,event,func){
		if(window.addEventListener){
			eval("getID('"+id+"').addEventListener('"+this.stripOnPrefix(event)+"',"+func+",false);");
		}else{
			eval("getID('"+id+"').attachEvent('"+this.addOnPrefix(event)+"',"+func+",false);");
		}
	},
	removeHandler : function(id,event,func){
		if(window.addEventListener){
			eval("getID('"+id+"').removeEventListener('"+this.stripOnPrefix(event)+"',"+func+",false);");
		}else{
			eval("getID('"+id+"').detachEvent('"+this.addOnPrefix(event)+"',"+func+",false);");
		}
	},
	stripOnPrefix: function(e){
		e = e.toLowerCase();
		if (e.indexOf('on') == 0){
			e = e.replace(/on/,'');
		}
		return e;
	},
	addOnPrefix : function(e){
		e = e.toLowerCase();
		if (e.indexOf('on') != 0){
			e = 'on' + e;
		}
		return e;
	}
}


function toggleImage(){
	var args = toggleImage.arguments.length;
	var f_open = getID(toggleImage.arguments[0]+'_open').style.display;
	var f_closed = getID(toggleImage.arguments[0]+'_closed').style.display;
	for(i=0;i<args;i++){
		getID(toggleImage.arguments[i]+'_open').style.display = ( f_open == 'none') ? '' : 'none' ;
		getID(toggleImage.arguments[i]+'_closed').style.display = ( f_closed == 'none') ? '' : 'none' ;
	}
}

function toggleElement(){
	var args = toggleElement.arguments.length;
	var f = getID(toggleElement.arguments[0]).style.display;
	for(i=0;i<args;i++){
		if(getID(toggleElement.arguments[i])){
			getID(toggleElement.arguments[i]).style.display = ( f == 'none') ? '' : 'none' ;
		}
	}
}

function toggleClass(){
	var args = toggleClass.arguments.length;
	var f = getID(toggleClass.arguments[0]).className;
	for(i=0;i<args;i++){
		if(getID(toggleClass.arguments[i])){
			getID(toggleClass.arguments[i]).className = ( f == 'mockup_open') ? 'mockup_closed' : 'mockup_open' ;
		}
	}
}

function confirmForm(form,action,message){
	if(action){
		var choice = confirm(message);
		if (choice == true){
			submitForm(form,action);
		}else{
			return false;
		}
	}
}

function confirmAction(message){
	var choice = confirm(message);
	if (choice == true){
		return true;
	}else{
		return false;
	}
}

function submitForm(form,action){
	eval('document.'+form+'.action=\''+action+'\';');
	eval('document.'+form+'.submit();');
}

function checkAll(list_name){
	var el = document.getElementsByName('chk_'+list_name+'[]');
	for(i=0;i<el.length;i++){
		el[i].checked = true;
	}
}

function checkNone(list_name){
	var el = document.getElementsByName('chk_'+list_name+'[]');
	for(i=0;i<el.length;i++){
		el[i].checked = false;
	}
}

function checkInverse(list_name){
	var el = document.getElementsByName('chk_'+list_name+'[]');
	for(i=0;i<el.length;i++){
		el[i].checked = (el[i].checked == true) ? false : true;
	}
}

function getRadioValue(id,v){
	var cv = "";
	var r = eval('document.forms[0].'+id+';');
	for (counter=0;counter<r.length;counter++){
		if(r[counter].checked){
			cv = r[counter].value;
		}
	}
	if(v == cv){
		return true;
	}else{
		return false;
	}
}


