
// This code is in the public domain. Feel free to link back to http://jan.moesen.nu/
function sprintf()
{
	if (!arguments || arguments.length < 1 || !RegExp)
	{
		return;
	}
	var str = arguments[0];
	var re = /([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X)(.*)/;
	var a = b = [], numSubstitutions = 0, numMatches = 0;
	while (a = re.exec(str))
	{
		var leftpart = a[1], pPad = a[2], pJustify = a[3], pMinLength = a[4];
		var pPrecision = a[5], pType = a[6], rightPart = a[7];
		
		//alert(a + '\n' + [a[0], leftpart, pPad, pJustify, pMinLength, pPrecision);

		numMatches++;
		if (pType == '%')
		{
			subst = '%';
		}
		else
		{
			numSubstitutions++;
			if (numSubstitutions >= arguments.length)
			{
				alert('Error! Not enough function arguments (' + (arguments.length - 1) + ', excluding the string)\nfor the number of substitution parameters in string (' + numSubstitutions + ' so far).');
			}
			var param = arguments[numSubstitutions];
			var pad = '';
			       if (pPad && pPad.substr(0,1) == "'") pad = leftpart.substr(1,1);
			  else if (pPad) pad = pPad;
			var justifyRight = true;
			       if (pJustify && pJustify === "-") justifyRight = false;
			var minLength = -1;
			       if (pMinLength) minLength = parseInt(pMinLength);
			var precision = -1;
			       if (pPrecision && pType == 'f') precision = parseInt(pPrecision.substring(1));
			var subst = param;
			       if (pType == 'b') subst = parseInt(param).toString(2);
			  else if (pType == 'c') subst = String.fromCharCode(parseInt(param));
			  else if (pType == 'd') subst = parseInt(param) ? parseInt(param) : 0;
			  else if (pType == 'u') subst = Math.abs(param);
			  else if (pType == 'f') subst = (precision > -1) ? Math.round(parseFloat(param) * Math.pow(10, precision)) / Math.pow(10, precision): parseFloat(param);
			  else if (pType == 'o') subst = parseInt(param).toString(8);
			  else if (pType == 's') subst = param;
			  else if (pType == 'x') subst = ('' + parseInt(param).toString(16)).toLowerCase();
			  else if (pType == 'X') subst = ('' + parseInt(param).toString(16)).toUpperCase();
		}
		str = leftpart + subst + rightPart;
	}
	return str;
}

function number_format (number, decimals, dec_point) {
	var factor = Math.pow(10, decimals);
	number = Math.round(number * factor) / factor;
	number = number+''; 
	number = number.replace(/\./, dec_point);
	return number;
}

function setVotingInfo(objResponse) {

	// prepare response
	var strInput = objResponse.responseText;
	var arrInput = strInput.split('||');
	for(i in arrInput) {
		if(arrInput[i] == '') {
			arrInput.splice(i,1);
		}
	}

	// change info text
	$('divDCVotingInfoVotes_'+arrInput[0]).innerHTML = number_format(arrInput[2],1,',');
	$('divDCVotingInfoPoints_'+arrInput[0]).innerHTML = number_format(arrInput[1],1,',');
	$('divDCVotingInfoVisits_'+arrInput[0]).innerHTML = number_format(arrInput[3],1,',');
	if(arrInput[2] == 1) {
		$('divDCVotingTextVotes_'+arrInput[0]).innerHTML = ''+arrVotingTextVotes[0];
	} else {
		$('divDCVotingTextVotes_'+arrInput[0]).innerHTML = ''+arrVotingTextVotes[1];
	}
	if(arrInput[1] == 1) {
		$('divDCVotingTextPoints_'+arrInput[0]).innerHTML = ''+arrVotingTextPoints[0];
	} else {
		$('divDCVotingTextPoints_'+arrInput[0]).innerHTML = ''+arrVotingTextPoints[1];
	}

	var floPoints = parseFloat(arrInput[1]);

	// change stars
	var intWidthPoint = 19;
	var intWidthTotal = 124;
	var intWidthLeft = Math.round(floPoints * intWidthPoint + Math.floor(floPoints - 0.1) * 7);

	if(intWidthLeft < 0) {
		intWidthLeft = 0;
	}
	if(intWidthLeft > intWidthTotal) {
		intWidthLeft = intWidthTotal;
	}
	intWidthRight = intWidthTotal - intWidthLeft;

	$('divDCVotingInactive_'+arrInput[0]).style.width = intWidthRight+'px';
	$('divDCVotingActive_'+arrInput[0]).style.width = intWidthLeft+'px';

	objItem = $('divDCVotingFeedback_'+arrInput[0]);
	objItem.innerHTML = arrInput[4]+' ';
	Effect.Appear(objItem, {duration:0.5, queue: {position: 'front', scope: 'voting'}});
	Effect.Fade(objItem, {duration:4, queue: {position: 'end', scope: 'voting'}});

}

function firstChildDiv(objInput) {

	var objChild = objInput.firstChild;
	while(objChild != null) {
		if(objChild.nodeName.toUpperCase() == 'DIV') {
			return objChild;
		}
		objChild = objChild.nextSibling;
	}

	return null;

}

function nextDiv(objInput) {

	var objChild = objInput.nextSibling;
	while(objChild != null) {
		if(objChild.nodeName.toUpperCase() == 'DIV') {
			return objChild;
		}
		objChild = objChild.nextSibling;
	}

	return null;

}

function switchTabItems(objElement) {

	// needed objects
	var objTabItem = objElement.parentNode.parentNode.parentNode;
	var objContainer = objTabItem.parentNode;
	var objContent = nextDiv(objContainer);

	// array with content divs
	var arrContent = new Array();
	var intContentCounter = 0;
	var objContentChild = firstChildDiv(firstChildDiv(objContent));

	while (objContentChild != null) {
		arrContent[intContentCounter] = objContentChild;
		objContentChild = nextDiv(objContentChild);
		intContentCounter++;
	}

	// Switch all active tabs to inactive
	var objChild = objContainer.firstChild;
	var intTabCounter = 0;
	while (objChild != null) {
		
		if(
			objChild.className == 'divHomepageTabItemActive' || 
			objChild.className == 'divHomepageTabItemInactive'
		) {
			
			if(objChild.className == 'divHomepageTabItemActive') {

				// hide active tab
				arrContent[intTabCounter].style.display = 'none';

				objChild.className = 'divHomepageTabItemInactive';
				var objInnerChild = objChild.firstChild;
				while (objInnerChild != null) {
					if(objInnerChild.className == 'divHomepageTabItemContent') {
						var objImg = objInnerChild.firstChild.firstChild;
						var strTemp = objImg.src;
						strTemp = strTemp.replace(/header_active/,'header_inactive');
						strTemp = strTemp.replace(/iconTableRedHeader/,'iconTableHeader');
						objImg.src = strTemp;
					}
					objInnerChild = nextDiv(objInnerChild);
				}
	
			}
			
			// activate current tab content
			if(objChild == objTabItem) {
				arrContent[intTabCounter].style.display = 'block';
			}

			intTabCounter++;

		}

		objChild = nextDiv(objChild);
	}
	
	// Check if it is the last tab
	var bolLastItem = 1;
	if(nextDiv(objTabItem)) {
		bolLastItem = 0;
	}
	
	// Switch background if last tab is changed
	if(!bolLastItem) {
		objContainer.style.background = 'url(/_img/_tabs/bg_tab_inactive.gif) no-repeat';
	} else {
		objContainer.style.background = 'url(/_img/_tabs/bg_tab_active.gif) no-repeat';
	}

	// Switch current tab to active
	objTabItem.className = 'divHomepageTabItemActive';
	var strTemp = objElement.src;
	strTemp = strTemp.replace(/header_hover/,'header_active');
	strTemp = strTemp.replace(/header_inactive/,'header_active');
	strTemp = strTemp.replace(/iconTableHeader/,'iconTableRedHeader');
	objElement.src = strTemp;

	// Apply all changes in the DOM structure
	Behaviour.apply();
	
	return true;
	
}

function spawn() {
    if (self.Spawnextended) {
        Spawnextended();
    }

    if (self.Spawnbody) {
        Spawnbody();
    }

    if (self.Spawnnugget) {
        Spawnnugget();
    }
}

var intEntryFormCategoryCounter = 0;

function addEntryFormCategorySelect() {
	intEntryFormCategoryCounter++;
	var strSelect = $('divDCEntryFormCategory').innerHTML;
	strSelect = strSelect.replace(/SPACER_ID/gi, intEntryFormCategoryCounter);
	strSelect = strSelect.replace(/SPACER_CAT/gi, 'categories');

	$('divDCEntryFormCategoryContainer').innerHTML += strSelect;
}

function removeEntryFormCategorySelect(intItem) {
	if(intEntryFormCategoryCounter > 0) {
		$('divDCEntryFormCategory_'+intItem).remove();
	}
}

function showEntryFormCategoryNew(objSelect,intItem) {
	if ( intItem == null ) {
		var objParent = objSelect.up();
		var strId = objParent.id;
		intItem = strId.replace(/divDCEntryFormCategory_/gi, '');
		intItem = parseInt(intItem);
	}
	if(objSelect && objSelect.value == 'NEW') {
		Element.show('divDCEntryFormNewCategory_'+intItem);
	} else {
		Element.hide('divDCEntryFormNewCategory_'+intItem);
	}
}

var browserName = navigator.appName;
var browserVer = parseInt(navigator.appVersion);
var version = "";
var msie4 = (browserName == "Microsoft Internet Explorer" && browserVer >= 4);
if ((browserName == "Netscape" && browserVer >= 3) || msie4 || browserName=="Konqueror" || browserName=="Opera") {version = "n3";} else {version = "n2";}
	// Blurring links:
function blurLink(theObject)	{	//
	if (msie4)	{theObject.blur();}
}




/**
 * 	other global variables
 */
var objInfoBoxTimer 	= '';
var objInfoBoxElement	= '';


/**
 * 	This object features the complete logic for showing the 
 * 	infoboxes of the service list including a timer
 * 
 * 	@author Mario Heiderich <mario.heiderich@doccheck.com>
 */
var objInfoBoxLogic = {
	
	initiateInfoBoxTimer : function(objElement){
		
		var objInfoTextContainer 	= $('divServiceInfoTextContainer');
		var objInfoText 			= $('divServiceInfoTextBox');

		var arrPos = Position.cumulativeOffset(objElement);
		var intWidth = objElement.width;
		var intHeight = objElement.height;

		var intTop = arrPos[1] + intHeight + 5;
		var intLeft = arrPos[0] + intWidth + 5;
			
		if(intLeft > (	document.getElementById("divDCContentLeftColumnContainer").clientWidth 		// left coloum
						+ 500 		// content coloum
						+ 30		// margin betweeen coloums
						- 240)		// width from fadeble box
						){
			intLeft = intLeft - 240; 
		}
				
		objInfoTextContainer.style.display 	= 'block';
		objInfoTextContainer.style.top 		= intTop + 'px';	
		objInfoTextContainer.style.left 	= intLeft + 'px';
		objInfoText.innerHTML 				= objElement.alt;
	}, 

	destroyInfoBoxTimer : function(){
		objInfoBoxLogic.hideServiceInfoText();			
	}, 

	hideServiceInfoText : function(){
		var objInfoTextContainer 			= $('divServiceInfoTextContainer');
		var objInfoText 					= $('divServiceInfoTextBox');
		
		objInfoTextContainer.style.display 	= 'none';
		objInfoText.innerHTML 				= '';
	}
}

function initiateInfoBoxTimer(objElement){

	var objInfoTextContainer 	= $('divServiceInfoTextContainer');
	var objInfoText 			= $('divServiceInfoTextBox');
	
	objInfoTextContainer.style.display 	= 'block';
	objInfoTextContainer.style.top 		= (y + 10) + 'px';	
	objInfoTextContainer.style.left 	= (x + 10) + 'px';
}

function hideServiceInfoText(){
	var objInfoTextContainer 	= $('divServiceInfoTextContainer');
	var objInfoText 			= $('divServiceInfoTextBox');
	
	objInfoTextContainer.style.display 	= 'none';
}

function updateInfoBox(e) {
	var objInfoTextContainer 	= $('divServiceInfoTextContainer');
	x = (document.all) ? window.event.x + document.body.scrollLeft : e.pageX;
	y = (document.all) ? window.event.y + document.body.scrollTop  : e.pageY;
	if (objInfoTextContainer != null) {
		objInfoTextContainer.style.left = (x + 20) + "px";
		objInfoTextContainer.style.top 	= (y + 20) + "px";
	}
}

