/**
 * @author 
 */
// Phoenix Contact JavaScript Files

/**
 *  themescript.js
 *  Functions available for all portlets
 *	@copyright IBM Center for Solution Innovation Hamburg, 2007	
 *	@version $Id: themescript.js,v 1.1 2010/01/14 07:25:00 iofh01 Exp $   
 * 	@classDescription Browser detection for relevant browsers
 * 
 */
var phx_browser = {
	ns4: (document.layers) ? 1 : 0, // Netscape 4
	op: (window.opera) ? 1 : 0, // Opera 
	op7: (navigator.userAgent.match(new RegExp("[\w\s ]*Opera.7[\w\s ]*"))) ? 1 : 0, // Opera 7
	op8: (navigator.userAgent.match(new RegExp("[\w\s ]*Opera.8[\w\s ]*"))) ? 1 : 0, // Opera 8
	ie: (document.all && !window.opera) ? 1 : 0, // MS Internet Explorer
	ie50: (document.all && !window.opera && !document.createEventObject) ? 1 : 0, // MS Internet Explorer 5.0
	ie7: (window.XMLHttpRequest && document.all &&!window.opera) ? 1 : 0, // MS Internet Explorer 7.0
	moz: (document.getElementById && !document.all && !window.opera) ? 1 : 0, // Mozilla/Gecko Browser
	saf: (navigator.userAgent.lastIndexOf("Safari") > 0),
	osx: (navigator.userAgent.lastIndexOf("OS X") > 0),
	
	/**
	 * control output of all browsers
	 */
	showBrowsers: function(){
		var allBrowsers = "";
		for (var i in this) {
			if(i!="showBrowsers")
				allBrowsers+=i +" : " +phx_browser[i] +"\n";
		}
		alert(allBrowsers);
	}
}

var phx_helper = {
	
	/**
	 * gets all elements of a given css class
	 * @param {String} searchClass   - class name to look for 
	 * @param {Node} node - optional: Dom node to start search
	 * @param {String} tag   - optional: tag name, only include elements of this tag
	 * @returns {Array} - all elements of given tag and classname
	 */
	getElementsByClass: function(searchClass,node,tag) { // node+tag are optional
		var classElements = new Array();
		if(node==null) node = document;
		if(tag==null) tag = '*';
		var els = node.getElementsByTagName(tag);
		var elsLen = els.length;
		var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
		var i;
		var j;
		for(i=0, j=0; i<elsLen; i++) {
			if(pattern.test(els[i].className)) {
				classElements[j] = els[i];
				j++;
			}
		}
		return classElements;
	},
	
	/**
	 * provides operations on css classes of a specified element
	 * @param {String} strAct   - activity to perform [flip|add|remove|verify]
	 * @param {Node} obj - Dom element to perform action
	 * @param {String} strClass1   - class name to use with performed action
	 * @param {String} cstrClass2   - second class name to use with flip action
	 * @returns {Boolean} - when using verify returns true if obj has strClass1, false if not
	 */
	// cssClassChanger(flip|add|remove|verify,object,classname,second classname (needed with 'flip')
	cssClassChanger: function (strAct,obj,strClass1,cstrClass2){
		if(strAct=="flip"){
			obj.className = (!this.cssClassChanger('verify',obj,strClass1)) ? obj.className.replace(cstrClass2,strClass1) : obj.className.replace(strClass1,cstrClass2);
		}
		else if(strAct=="add"){
			if(!this.cssClassChanger("verify",obj,strClass1)){obj.className+=obj.className?' '+strClass1:strClass1;}
		}
		else if(strAct=="remove"){
			var rep=obj.className.match(' '+strClass1)?' '+strClass1:strClass1;
			obj.className=obj.className.replace(rep,'');
		}
		else if(strAct=="verify"){
			return new RegExp('\\b'+strClass1+'\\b').test(obj.className);
		}
	}
}

/*
	Based on the accessible menu by Matthew Carrol (http://carroll.org.uk/)
	Original by Patrick Griffiths and Dan Webb (http://www.htmldog.com/articles/suckerfish/dropdowns/)
	Modified by IBM Center for Solution Innovation, Hamburg Germany, 2007
	$Revision: 1.1 $
*/
var phx_topnav = {
	navhor: "phx_mainnav_list", // id of main navigation top ul element
	active: "phx_act", // class for active tab
	leftClass: "phx_iehover", // class for internet explorer, li:hover substitution class for left aligned flyout
	rightClass: "phx_lefthover", // class for right aligned flyout menu for internet explorer 
	navheight: "2.4em", // top position for iframe to be placed
	
	/**
	 * adds an invisible iframe under the flyout ul of the main navigation to cover windowed elements in IE 
	 * @param {Node} navelement   - container element of main navigation
	 * @param {String} posleft - left position to place iframe
	 * @param {String} posright   - right position to place iframe
	 */
	addIframe: function(navelement,posleft,posright,shift) {
		if (navelement.getElementsByTagName("ul").length) {
			var icover = navelement.lastChild;
	
			if (!(icover) || (icover.tagName != "IFRAME")) {
				navelement.lastChild.insertAdjacentHTML("afterEnd","<iframe src='javascript:false' longdesc='no content' scrolling='no' frameborder='0' style='display:none;'></iframe>");
				icover = navelement.lastChild;
			}
			var icstyle = icover.style;
			icstyle.filter = "progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)";
			icstyle.position = "absolute";
			if (shift == "true") {
				icstyle.top = this.navheight; //on 2nd level only
			} else {
				icstyle.top = "0";
			}
			icstyle.right = posright;
			icstyle.left = posleft;
			var ul = navelement.getElementsByTagName("UL");
			icstyle.width = ul[0].clientWidth + 2;
			icstyle.height = ul[0].clientHeight;
			icstyle.zIndex = "499";
			icstyle.display = "block";
		}
	},
	
	/**
	 * hides invisible iframe under the flyout ul of the main navigation
	 * @param {Node} obj   - element containing the iframe, usually the LI main element containing the protected UL 
	 */
	killIframe: function(obj) {
		var icover = obj.lastChild;
		if (icover && icover.tagName == "IFRAME") {
			icover.style.display = "none";
		}
	},
	
	/**
	 * appends menu functionality on all menu items, substitutes the li:hover effect for IE
	 */
	sfHover: function() {
		var navHorElem = document.getElementById(phx_topnav.navhor);
		if (!navHorElem) return;
		var sfEls = navHorElem.childNodes;			
		for (var i in sfEls) {		
			if (phx_browser.ie && !phx_browser.ie50 && !phx_browser.ie7) {
				sfEls[i].onmouseenter=function() {
					phx_helper.cssClassChanger("add",this, phx_topnav.leftClass);
					phx_helper.cssClassChanger("verify",this,phx_topnav.rightClass) ? phx_topnav.addIframe(this, "auto", "-1px", "true") : phx_topnav.addIframe(this, "0", "auto", "true");
				}
				sfEls[i].onmouseleave=function() {
					phx_helper.cssClassChanger("remove", this, phx_topnav.leftClass);
					phx_topnav.killIframe(this);
				}
			}
		}
		
		/* 3rd level flyout */
		var subEls;	
		for (var j in sfEls) {	
			subEls = sfEls[j].childNodes;
			var liEls; //
			//for all 2nd level elements 
			for (var k in subEls) {	
					if (subEls[k].tagName == "UL") {
					 	//get li elements
					 	liEls = subEls[k].childNodes;
					 	for (l in liEls) {
					 		liEls[l].onmouseenter=function() {
								phx_helper.cssClassChanger("add",this, phx_topnav.leftClass);
								phx_helper.cssClassChanger("verify",this,phx_topnav.rightClass) ? phx_topnav.addIframe(this, "auto", this.clientWidth, "true") : phx_topnav.addIframe(this, "0", "auto", "true"); //NEU
							}
							liEls[l].onmouseleave=function() {
								phx_helper.cssClassChanger("remove",this, phx_topnav.leftClass);
								phx_topnav.killIframe(this);
							}
					 	}					 					 	
					}
			}
		}	
		
	},
	
	mcAccessible: function() {
		var navHorElem = document.getElementById(phx_topnav.navhor);
		if (!navHorElem) return;
		var mcEls = navHorElem.getElementsByTagName("A");
		for (var i in mcEls) {
			mcEls[i].onfocus=function() {
				phx_helper.cssClassChanger("add", this, "iefocus"); //a:focus
				if(this.parentNode.parentNode.parentNode.nodeName == "LI") {
					phx_helper.cssClassChanger("add", this.parentNode.parentNode.parentNode, phx_topnav.leftClass);
					if (phx_browser.ie && ! phx_browser.ie50) phx_topnav.addIframe(this.parentNode.parentNode.parentNode, phx_helper.cssClassChanger("verify",this,phx_topnav.rightClass) ? "auto" : "-1px", phx_helper.cssClassChanger("verify",this,phx_topnav.rightClass) ? "0" : "auto");
				} else {
					phx_helper.cssClassChanger("add", this.parentNode, phx_topnav.leftClass);
					if (phx_browser.ie && ! phx_browser.ie50) phx_topnav.addIframe(this.parentNode,phx_helper.cssClassChanger("verify",this,phx_topnav.rightClass) ? "auto" : "-1px", phx_helper.cssClassChanger("verify",this,phx_topnav.rightClass) ? "0" : "auto");
				}
			}
			mcEls[i].onblur=function() {
				this.className=this.className.replace(new RegExp("( ?|^)iefocus\\b"), "");
				if(this.parentNode.parentNode.parentNode.nodeName == "LI") {
					phx_helper.cssClassChanger("remove", this.parentNode.parentNode.parentNode, phx_topnav.leftClass);
					if (phx_browser.ie && ! phx_browser.ie50) phx_topnav.killIframe(this.parentNode.parentNode.parentNode);
				} else {
					phx_helper.cssClassChanger("remove", this.parentNode, phx_topnav.leftClass);
					if (phx_browser.ie && ! phx_browser.ie50) phx_topnav.killIframe(this.parentNode);
				}
			}
		}
	},
	
	/**
	 * menu initialization
	 */
	initMenu: function () {
		if (phx_browser.ie && !phx_browser.ie7) {
			this.sfHover();
		}
		// this.mcAccessible();
	}
}

var phx_leftnav = {
	leftnav: "phx_leftnav_list",
	leftnavHoverClass: "phx_leftnv_hvr",
	
	/**
	 * appends menu functionality on all left navigation items, substitutes the li:hover effect for IE
	 */
	initHover: function() {
		if (phx_browser.ie && !phx_browser.ie7) {
			var leftElem = document.getElementById(this.leftnav);
			if (!leftElem) return;
			var sfEls = leftElem.childNodes;
			for (var i in sfEls) {
				if (phx_browser.ie && !phx_browser.ie7) {
					sfEls[i].onmouseenter=function() {
						phx_helper.cssClassChanger("add",this, phx_leftnav.leftnavHoverClass);
					}
					sfEls[i].onmouseleave=function() {
						phx_helper.cssClassChanger("remove", this, phx_leftnav.leftnavHoverClass);
					}
				} 
			}
		}
	}
}

/* Script for Buttons on Phoenix Portal
*/
// Form handler generic (static)
function phx_gf() { this.frm = ""; } //phx_phx_fs is generic form, f is the form
phx_gf.prototype = new Object();
phx_gf.prototype.rf = function(o) {
	var so = o.parentNode; // search object
	if(so.tagName == "FORM") this.frm = so; // if form is found, this is it 
	else this.rf(so); // else, keep on searching
}
// submitForm generic (static)
function phx_fs(o) { this.rf(o); } //phx_fs:form submit, rf:recurse form
phx_fs.prototype = new phx_gf();
phx_fs.prototype.submitMe = function() { this.frm.submit();this.frm.onsubmit(); }
function submitForm(obj) { // this function will be called from the buttons
	form = new phx_fs(obj);
	form.submitMe();
}

/* START: Button Object -----------------------------------------------------------------------
Example:
	var b1 = new Button({ 
		action:'http://link',	 					// submit|back|link|close|popup|print|func
		func:'alert(\"param1, param2\");',			// function to be called (after submit)
		valFunc:'formValidationMethod',				// function that returns true|false before onsubmit
		url:'http://de.wikipedia.org/wiki/Button',  // url
		label:'Zur?ck',								// any character but " ' and \
		setHidden:'id',
		setHiddenVal:'123',
		setFormId:'id',
		setFormAction:'action',
		cssclass:'CSSclassname'						// multiple class names space separated
		disabled:'true'								// button will be disabled
		nocookieurl:'http://de.wikipedia.org/wiki/Cookie' // url for a browser without cookie support (but with JS support :-) )
		onclick:'event'								// OnClick-Event for links
	});
// all attributes are optional. the default-button is a back-button
*/
function phx_button(p) {
	this.a = "back"; // default-button is always a back-button
	this.t = "Back (Default)"; // TODO: should this var rather be empty?
	this.z = "<div>[No button defined!]</div>"; // just a backup, this should never be seen...
	this.ds = this.c = this.u = this.f = this.shid = this.shv = this.vf = ""; // if a function/link is not specified btn is still being painted, but nothing happens
	this.dis=false;		// button ist enabled as default. use disabled to make button inactive (grey & not clickable)
	if(typeof p == "object") {
		
		this.a = p.action?p.action:"back";
		this.c = p.cssclass?p.cssclass:"lnk-btn"; // css class name
		this.f = p.func?p.func:"";
		this.vf = p.valFunc?p.valFunc:"";
		this.u = p.url?p.url:"";
		this.t = p.label?p.label:"";
		this.shid = p.setHidden?p.setHidden:"";
		this.shv = p.setHiddenVal?p.setHiddenVal:"";
		this.fid = p.setFormId?p.setFormId:"";
		this.fa = p.setFormAction?p.setFormAction:"";
		this.dis = p.disabled?p.disabled:false;
		this.ds = p.submitId?p.submitId:""; // work around the generic form handler
		this.ncurl = p.nocookieurl?p.nocookieurl:"";
		if (this.ncurl.indexOf("http") == -1) {
			this.ncurl = "";
		}
		this.onc = p.onclick?p.onclick:"";
	}
	this.init();
	this.paint();
}
phx_button.prototype.init = function() {
	// prod version
	if(this.shid!="") this.f += " document.getElementById(\""+this.shid+"\").value=\""+this.shv+"\";"; // sets the hidden field + value
	if(this.fid!="" && this.fa!="") this.f += "document.getElementById(\""+this.fid+"\").action=\""+this.fa+"\";"; // sets the form id + form action 
	//debug version:
	// if(this.shid!="") this.f += " document.getElementById(\""+this.shid+"\").value=\""+this.shv+"\";alert(\"Hidden Field: "+this.shid+"=\"+document.getElementById(\""+this.shid+"\").value);"; // sets the hidden field
	// if(this.fid!="" && this.fa!="") this.f += "document.getElementById(\""+this.fid+"\").action=\""+this.fa+"\";alert(\"Action: \" + document.getElementById(\""+this.fid+"\").action);";
	if(this.dis=="true") //if button is disabled just show a styled div
		this.z = "<div class=\"lnk-btn-disabled\">"+this.t+"</div>"
	else{
		// only when a nocookieurl is given me must test if the cookies are disable
		if(this.ncurl!="")
		{
			if(this.ncurl != "" && !cookieEnable() && this.a == "submit")
			{
				// cookies are disable and a form must be submitted, therefor the formaction is changed.
				if(this.ds == "") this.f += "if(!cookieEnable()){fr = new phx_fs(this);fr.frm.action=\""+this.ncurl+"\"};";
				if(this.ds != "") this.f += "if(!cookieEnable()){document.getElementById(\""+this.ds+"\").action=\""+this.ncurl+"\"};";
			}
		}
		// Validation Handler
		var v1=v2=""; if(this.vf!=""){v1="if("+this.vf+"()){";v2 = "}";} 
		if (this.a == "back") { // default
			this.z = "<a class='"+this.c+"' href='#' onclick='history.back(); return false;'><span>"+this.t+"<\/span><\/a>"
		} else if (this.a == "close") {
		  	this.z = "<a class='"+this.c+"' href='#' onclick='window.close(); return false;'><span>"+this.t+"<\/span><\/a>";
		} else if (this.a == "func") { 
		  	this.z = "<a class='"+this.c+"' href='#' onclick='"+v1+this.f+v2+"; return false;'><span>"+this.t+"<\/span><\/a>";
		} else if (this.a == "submit" && this.ds == "") {
			this.z = "<a class='"+this.c+"' href='#' onclick='"+v1+this.f+"submitForm(this)"+v2+"; return false;'><span>"+this.t+"<\/span><\/a>";
		} else if (this.a == "submit" && this.ds != "") {
			this.z = "<a class='"+this.c+"' href='#' onclick='"+v1+this.f+"document.getElementById(\""+this.ds+"\").submit()"+v2+"; return false;'><span>"+this.t+"<\/span><\/a>";
		} else if (this.a == "link" && this.onc == "") {
			this.z = "<a class='"+this.c+"' href='"+this.u+"'><span>"+this.t+"<\/span><\/a>";
		} else if (this.a == "link" && this.onc != "") {
			this.z = "<a class='"+this.c+"' href='"+this.u+"' onclick='"+this.onc+"'><span>"+this.t+"<\/span><\/a>";
		}
	}
}
phx_button.prototype.paint = function() { document.write(this.z); } // phx_button.paint();
phx_button.prototype.setLink = function(v){ // not in use; might be handy later on; sets the link
	function gT() { window.location.href = v; return false; }
	if (this.id != "") {
		var a = document.getElementById(this.id);
		a.onclick = gT;
	}
}
/* END: phx_button Object ----------------------------------------------------------------------- */
function imagePopup(anchor) {
	var imgWin = window.open(anchor.href, "Bildfenster", "dependent=yes,scrollbars=yes,menubar=yes,toolbar=no,status=no,location=no,locationbar=no,resizable=yes,width=600,height=450,top=20,left=20");
	imgWin.focus();
}

/* Expand/collapse an area */
function expandCollapse(id, hide, show) {
  if (document.getElementById(id).style.display == 'none') {
    document.getElementById(id).style.display = 'block';
    document.getElementById(id + "_1").className = 'phx_expanded';
    document.getElementById(id + "_2").innerHTML = hide;
  } else {
    document.getElementById(id).style.display = 'none';
    document.getElementById(id + "_1").className = 'phx_closed';
    document.getElementById(id + "_2").innerHTML = show;
  }
}

/* Expand/collapse a stand-alone area */
function expandCollapseStandalone(id) {
  if (document.getElementById(id).style.display == 'none') {
    document.getElementById(id).style.display = '';
    document.getElementById(id + "_1").src = '/layout/img/btn_minus_19_19.png';
  } else {
    document.getElementById(id).style.display = 'none';
    document.getElementById(id + "_1").src = '/layout/img/btn_plus_19_19.png';
  }
}

/* Show loader layer */
function showLoader(){
	document.getElementById('loader').style.display = 'block';
	document.getElementById('statusWindow').style.display = 'block';
};

/* Hide loader layer */
function hideLoader(){
	document.getElementById('loader').style.display = 'none';
	document.getElementById('statusWindow').style.display = 'none';
};