/*
 * Function used to get elements which have a certain classname. The classname does not have to be
 * the only classname an object has.
 */
document.getElementsByClassName = function (needle) {
    var s = [document.documentElement || document.body], i = 0, r = [], l = 0, e;
    var re = new RegExp('(^|\\s)' + needle + '(\\s|$)');

    do {
        e = s[i];

        while (e) {
            if (e.nodeType == 1) {
                if (e.className && re.test(e.className)) r[l++] = e;

                s[i++] = e.firstChild;
            }

            e = e.nextSibling;
        }
    }
    while (i--);

    return r;
};

/*
 * Returns true if the passed value is found in the array.  Returns false if it is not.
 */
Array.prototype.inArray = function (value) {
	var i;
	
	for ( i = 0; i < this.length; i++ ) {
		/* Matches identical (===), not just similar (==). */
		if ( this[i] === value ) {
			return i;
		}
	}
	
	return false;
};

/*
 * Go to a new page to set the language. Get parameters from main window and URL are mixed. URL
 * parameters override main window parameters. When paramaters are found twice the later value is
 * used. When shift is pressed the function is disabled and default behaviour occurs.
 */
function fnSetLanguage(event) {
	if ( window.event ) {
		var shiftKey = window.event.shiftKey;
	} else {
		var shiftKey = event.shiftKey;
	}

	if ( !shiftKey ) {
		var imgs = this.getElementsByTagName('img');
		var strNewLang = imgs[0].getAttribute('alt');
		var strNewLocation = window.location.protocol + '//' + window.location.host
				     + window.location.pathname;
            	var separator = '?';
            	var paramListNames = new Array(); 
            	var paramListValues = new Array(); 
            	
            	/*
            	 * First add all GET variables from main window.
            	 */
            	var query = window.location.search.substring(1);

		if ( query.length > 0 ) {
			var params = query.split('&');

			for ( var i = 0 ; i < params.length ; i++ ) {
				var pos = params[i].indexOf('=');
				var name = params[i].substring(0, pos);
				var value = params[i].substring(pos + 1);

				var inArray = paramListNames.inArray(name);
				if ( inArray === false ) {
					paramListNames[paramListNames.length] = name;
					paramListValues[paramListValues.length] = value;
				} else {
					paramListValues[inArray] = value;
				}
			}
		}
		
		/*
		 * Secondly add GET parameters from this link
		 */
            	var query = this.search.substring(1);

		if ( query.length > 0 ) {
			var params = query.split('&');

			for ( i = 0 ; i < params.length ; i++ ) {
				var pos = params[i].indexOf('=');
				var name = params[i].substring(0, pos);
				var value = params[i].substring(pos + 1);

				var inArray = paramListNames.inArray(name);
				if ( inArray === false ) {
					paramListNames[paramListNames.length] = name;
					paramListValues[paramListValues.length] = value;
				} else {
					paramListValues[inArray] = value;
				}
			}
		}
		
		/*
		 * Now add the parameters to the URL
		 */
		for ( i = 0 ; i < paramListNames.length ; i++ ) { 
			strNewLocation += separator + paramListNames[i] + '=' + paramListValues[i];
			separator = '&';
		} 
		
		/*
		 * Going to new location.
		 * Using top because this function is potentially called from inside a frame.
		 * (Otherwise use window)
		 */
		top.location.href = strNewLocation;

	        return false;	 
	} else {
		return true;
	}
};

/*
 * Assign onclick to all language flag images having the classname "languageFlagLink" to replace
 * the standard html code.
 */
function fnAssignSetLanguageLinks() {
	var aAnchorList = document.getElementsByClassName( 'languageFlagLink' );
	var i = aAnchorList.length;

	while (i--) {
		oAnchor = aAnchorList[i];
		
		if ( oAnchor.href ) {
			oAnchor.onclick = fnSetLanguage;
		}
	}
};

/*
 * Basic popup function that will use a modal dialog when requested & available.
 */
function fnPopup(page, width, height, options, showModal) {
	var top = (screen.height-height)/2;
	var left = (screen.width-width)/2;
	
	/*
	if ( showModal && showModalDialog ) {
		showModalDialog( page
			       , ''
			       , 'top:' + top + 'px;'
				 + 'left:' + left + 'px;'
				 + 'dialogHeight:' + height + 'px;'
				 + 'dialogWidth:' + width + 'px;'
				 + 'resizable:yes;'
				 + 'status:no' );
	} else {*/
		popup = window.open( page
				   , ''
				   , 'top=' + top
				     + ', left=' + left
				     + ', width=' + width
				     + ', height=' + height
				     + ',' + options );
	/*}*/
};

/*
 * Show the picture in a popup window
 */
function fnShowPicture(link) {
	/* Open a new message in a popup window */
	fnPopup( link
	       , 635
	       , 625
	       , 'toolbar=no'
		 + ', directories=no'
		 + ', status=no'
		 + ', menubar=no'
		 + ', scrollbars=yes'
		 + ', copyhistory=no'
		 + ', resizable=yes'
	       , true );
};

/*
 * Set the height of the frame that this page loaded in.
 */
function fnResizeMainFrame(oIFrame, oMenu) {
	if ( window.parent ) {
		var minHeight = Math.max(400, oMenu.clientHeight);
		
		if ( oIFrame && ( oIFrame.clientHeight ) && ( oIFrame.clientHeight > minHeight ) ) {
			window.parent.fnSetPageHeight(oIFrame.clientHeight + 30);
		} else {
			window.parent.fnSetPageHeight(minHeight + 10);
		}
	}
};

function fnResizeMainFrame_fromFrame() {
	if ( window.parent ) {
		var oIFrame = document.getElementById('contents');
		var oMenu = window.parent.document.getElementById('mainMenu');
		fnResizeMainFrame(oIFrame, oMenu);
	}
};

function fnResizeMainFrame_fromMain() {
	if ( window.parent ) {
		var oTemp = document.getElementById('mainFrame');
		var oIFrame = (oTemp.contentWindow || oTemp.contentDocument);
		if (oIFrame.document) {
			oIFrame = oIFrame.document;
		}
		oIFrame = oIFrame.getElementById('contents');
		var oMenu = document.getElementById('mainMenu');
		if ( !oTemp.contentDocument ) {
			/*
			 * For some reason IE needs a height set before you can read the value.
			 * It makes no real sense, but this works...
			 */
			document.getElementById('mainMenu').style.height = '0px';
		}
		fnResizeMainFrame(oIFrame, oMenu);
	}
};

function fnShowPage(page_id, lang) {
	/* Open a new message in a popup window */
	fnPopup( 'page.php?page_id='+page_id+'&lang=' + lang
	       , 635
	       , 625
	       , 'toolbar=no'
		 + ', directories=no'
		 + ', status=no'
		 + ', menubar=no'
		 + ', scrollbars=yes'
		 + ', copyhistory=no'
		 + ', resizable=yes'
	       , true );
};