﻿/**
 * Prefixes an arrow the given links in a given container
 * @return void
 */
function addArrow2Link( linkClass , containerId , containerElType )
{
	var arrow = '› ';
	var containerId = containerId || 'maincell';
	var containerElType = containerElType || 'div';
	var links , myLink;
	var linkContainers = Tangora.DOM.GetCollectionByClassName(containerId,containerElType,linkClass);
	for( var i=0; i<linkContainers.length; i++ )
	{
		links = linkContainers[i].getElementsByTagName('a');
		for( var j=0; j<links.length; j++ )
		{
			myLink = links[j];
			myLink.innerHTML = arrow + myLink.innerHTML;
		}
	}
}



/**
 * Moves text from formelements labels into the formelements
 * @return void
 */
function implantLabelTxt( container )
{
	var container = Tangora.DOM.EnsureElement(container);
	var labels = container.getElementsByTagName('label');
	var forVal, related,txt, tag,type;
	for(var i=0; i<labels.length; i++)
	{
		forVal = labels[i].getAttribute('for') || labels[i].getAttribute('htmlFor');//htmlFor in case of IE
		related = document.getElementById( forVal );
		if( !related ) continue;
		txt = labels[i].firstChild.nodeValue;
		tag = related.tagName.toLowerCase();
		switch( tag )
		{
			case 'select':
				related.firstChild.firstChild.nodeValue = txt;
				labels[i].style.display = 'none';
				break;
			case 'input':
				type = related.getAttribute('type');
				if( !type || type.toLowerCase() == 'text' )
				{
					if(related.value == '') related.value = txt;
					related.onfocus = function(){ this.value = ''; };
					related.onblur = mkFunc_setValueOnEvent( txt );
					labels[i].style.display = 'none';
				}
				break;
		}
	}
}

/**
 * Can pass a variable from the caller functions scope, to the in that function attached eventHandler function
 * which otherwise would be out of the eventHandler functions scope
 * @return void
 */
function mkFunc_setValueOnEvent( val )
{
	var call = function()
	{
		if(this.value == '')
			this.value = val;
	}
	return call;
}




/**
 * Sets up that when the form is submitted it empties the searchbox when it has got its default value ('Keywords here..' eg)
 * to stop the deafult text from being sendt with the query
 * Checks if there are any buttons that want to submit the form with a JS (form.onsubmit will not work in that case)
 * these actions will be replaced by calling submitExceptDefKeyword()
 * When not found the submitExceptDefKeyword() will be called on the forms submit event
 * @return void
 */
function allowMyDefKeyword( formName , input , keyword )
{
	var form = document.forms[formName];
	var input = Tangora.DOM.EnsureElement(input);
	var defKeyword = keyword || input.value;
	var els = form.getElementsByTagName('input');
	var currOnClickAttr;
	for( var i=0; i<els.length; i++ )
	{
		currOnClickAttr = els[i].getAttribute('onclick');
		if( currOnClickAttr && (currOnClickAttr.toString().toLowerCase().indexOf('.submit()') != -1) )
		{
			// An element wants to submit the form by a javascript, we want to intercept that
			els[i].onclick = mkFunc_submitExceptOnEvent(formName,input.id,defKeyword);
			return;
		}
	}
	// No buttons with onclick events that trigger a submision, so we can set a onsubmit on the form its self
	form.onsubmit = mkFunc_submitExceptOnEvent(formName,input.id,defKeyword);
}

/**
 * Can pass a variable from the caller functions scope, to the in that function attached eventHandler function
 * which otherwise would be out of the eventHandler functions scope
 * @return void
 */
function mkFunc_submitExceptOnEvent( formName , input , keyword )
{
	var call = function()
	{
		submitExceptDefKeyword( formName , input , keyword )
	}
	return call;
}


/**
 * Submits a given form, but before that it empties the searchboxes defaulvalue if it's set
 * @ return boolean
 */
function submitExceptDefKeyword( formName , input , keyword ){
	var form = document.forms[formName];
	var input = Tangora.DOM.EnsureElement(input);
	if( input.value == keyword )
		input.value = '';
	form.submit();
	return false;// Stop any deafult action
}





function findPos(el) {
	var obj = Tangora.DOM.EnsureElement(el);
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}


function getStyle(el,styleProp)
{
	var obj = Tangora.DOM.EnsureElement(el);
	if (obj.currentStyle)
		var y = obj.currentStyle[styleProp];
	else if (window.getComputedStyle)
		var y = document.defaultView.getComputedStyle(obj,null).getPropertyValue(styleProp);
	return y;
}


// Get the actual height (using the computed CSS) of an element
function getHeight( elem ) {
    // Gets the computed CSS value and parses out a usable number
    return parseInt( getStyle( elem, 'height' ) );
}

// Get the actual width (using the computed CSS) of an element
function getWidth( elem ) {
    // Gets the computed CSS value and parses out a usable number
    return parseInt( getStyle( elem, 'width' ) );
}


/*IE min-height hack*/
function setMinHeight( elem , val )
{	
	var obj = Tangora.DOM.EnsureElement(elem);
	obj.style.height = 'expression( this.scrollHeight < ' + (parseInt(val)+1) + ' ? "' + val + 'px" : "auto" )';
	obj.style.minHeight = val + 'px';
}




function getQueryVariable(variable) {
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) {
			return pair[1];
		}
	}
	return false;
}



/**
 * Activates menu-item in navigation
 * Finds pageId from query variable 'origPageId'
 * Usefull when menu-items are link-pages
 * @return void
 */
function activateNav()
{
	var callerPageId = getQueryVariable('origPageId');
	if( !callerPageId ) return;
	var navItems = Tangora.DOM.GetCollection('nav2list','a');
	var myUri;
	for( var i=0; i<navItems.length; i++ )
	{
		myUri = navItems[i].getAttribute('href');
		if( myUri.indexOf(callerPageId + '.aspx') != -1 )
			navItems[i].className += 'active'; 
	}
}





/* ############ PRODUCT PAGE ############ */


/**
 * Checks selectElement el and sets page-title-prefix to el's value
 * unless it isn't set, then it hides the entire page-title
 * @return void
 */
function displayPageTitleSuffix()
{
	if( !industryEl || !pageTitle || !pageTitle_suffix ) return;
	
	var selIndex = industryEl.selectedIndex;
	var selValue = industryEl.options[selIndex].value;
	var selText = industryEl.options[selIndex].text;
	if( parseInt(selIndex) > 0 )
		pageTitle_suffix.innerHTML = selText;
	else
		pageTitle.style.display = 'none';
}



function defineProdRightCols()
{
	var rightCols = Tangora.DOM.GetCollectionByClassName('maincell','div','rightColPlaceholder');
	var rightCol,myEl,marginLeft,marginTop,left,top;
	for( var i=0; i<rightCols.length; i++  )
	{
		
		rightCol = rightCols[i];
		myEl = (rightCol.nextSibling.nodeType == 3) || (rightCol.nextSibling.nodeType == 8) ? rightCol.nextSibling.nextSibling : rightCol.nextSibling;//DOM-whitespace-bug : if first NodeType is text (3) or comment (8), take the next one
		//myEl = (rightCol.childNodes[0].nodeType == 3) || (rightCol.childNodes[0].nodeType == 8) ? rightCol.childNodes[1] : rightCol.childNodes[0];//DOM-whitespace-bug : if first NodeType is text (3) or comment (8), take the next one
		marginLeft = getStyle(rightCol,'marginLeft') || getStyle(rightCol,'margin-left');
		marginTop = getStyle(rightCol,'marginTop') || getStyle(rightCol,'margin-top');
		marginLeft = marginLeft.indexOf('px') != -1 ? parseInt(marginLeft) : 0;
		marginTop = marginTop.indexOf('px') != -1 ? parseInt(marginTop) : 0;
		left = findPos(rightCol)[0] - marginLeft;
		top = findPos(rightCol)[1] - marginTop;
		if( left > 0 && top > 0 )
		{
			myEl.style.position = 'absolute';
			myEl.style.left = left + 'px';
			myEl.style.top = top + 'px';
		}
	}
//	window.onresize = placeProdRightCols;
}

function placeProdRightCols()
{
	var rightCols = Tangora.DOM.GetCollectionByClassName('maincell','div','rightcol');
	var rightCol;
	for( var i=0; i<rightCols.length; i++  )
	{
		rightCol = rightCols[i];
		if( rightCol.posTop != null && rightCol.posTop !=null )
		{
			rightCol.style.position = 'absolute';
			rightCol.style.left = rightCol.posLeft + 'px';
			rightCol.style.top = rightCol.posTop + 'px';
		}
	}
}

/*
function initPresentation()
{
	var groupAvail = Tangora.DOM.GetCollectionByClassName('maincell','div','list-group-detail');
	if( groupAvail.length < 1 )
	{
		// No productgroup presentations available ; remove this option
		document.getElementById('presentationContainer').style.display = 'none';
	}else{
		// Do we need to show the presentations?
		if( document.getElementById('presentation').checked == true )
			showPresentation( 'show' );
		else
			showPresentation( 'hide' );
	}
}



function presentationHandler(e)
{
	var me	= (e.target) ? e.target : e.srcElement;
	
	if( me.checked == true )
		showPresentation( 'show' );
	else
		showPresentation( 'hide' );
}


function showPresentation( mode )
{
	var mode			 = mode || 'show';
	var groups			 = Tangora.DOM.GetCollectionByClassName('maincell','div','group');
	var listings		 = Tangora.DOM.GetCollectionByClassName('maincell','div','listing');
	var tableHeaders	 = Tangora.DOM.GetCollectionByClassName('maincell','tr','tableheader');
	
	if( mode == 'show' )
	{
		// Show presentation
		for( var i=0; i<groups.length; i++ ) groups[i].style.display = 'block';
		for( var i=0; i<listings.length; i++ ) listings[i].className = 'listing_presentation';
		for( var i=1; i<tableHeaders.length; i++ ) tableHeaders[i].style.display = 'table-row';
		defineProdRightCols();
	}else{
		// Hide presentation
		for( var i=0; i<groups.length; i++ ) groups[i].style.display = 'none';
		for( var i=0; i<listings.length; i++ ) listings[i].className = 'listing';
		for( var i=1; i<tableHeaders.length; i++ ) tableHeaders[i].style.display = 'none';
	}
}
*/
