
// Replace the current page copy with the copy contained in the columnName column in the tableName table.
var columnNameGlobal = null;

function getCopyNew( columnName, tableName, title, id )
{
	if( id === undefined )
		id = -1;
		
	if( document.getElementById( 'scroll_btns' ) != null )
		document.getElementById( 'scroll_btns' ).style.display = "";
	
	// this is for use in the setSectionImagePos function.
	columnNameGlobal = columnName;
	
	parameters = 'tableName=' + tableName + '&' + 'columnName=' + columnName + '&' + 'title=' + title;
	
	if( id != -1 )
		parameters += '&' + 'primaryKeyValue=' + id;
		
	// Get page copy
	new Ajax.Request( '/includes/GetCopy.php', {
		method: 'get',
		parameters: parameters,
  		onSuccess: function( transport )
		{
			array = transport.responseText.split( '|' );
			
			if( document.getElementById( 'flashHolder' ) != null )
				document.getElementById( 'flashHolder' ).style.display = "none";
			
			if( document.getElementById( 'mobile_display' ) != null )
				document.getElementById( 'mobile_display' ).style.display = "none";
				
			idxPaginationStart = document.getElementById( 'ajax_content_wrapper' ).innerHTML.indexOf
				( '<div id="paginationStart"' );
			
			appendHTML = "";
			
			if( idxPaginationStart != -1 )
				appendHTML = document.getElementById( 'ajax_content_wrapper' ).innerHTML.substring
				( idxPaginationStart, document.getElementById( 'ajax_content_wrapper' ).innerHTML.length );
				
			document.getElementById( 'ajax_content_wrapper' ).innerHTML = array[0];
			
			if( appendHTML != "" )
				document.getElementById( 'ajax_content_wrapper' ).innerHTML += appendHTML;
				
			document.getElementById( 'section_image' ).innerHTML = array[1];
			document.getElementById( 'ajax_content_wrapper' ).style.display = "";
			document.getElementById( 'section_image' ).style.display = "";
			document.getElementById( 'promo_gallery' ).style.display = "";
			
			// Dynamically position the section image.
			
			sectionImage = "";
			
			if( document.getElementById( 'links' ) != null )
			{
				sectionImage = document.getElementById( 'ajax_content_wrapper' ).offsetHeight + 
					document.getElementById( 'promo_gallery' ).offsetHeight - 150 - 
					document.getElementById( 'links' ).offsetHeight + "px";
			}
			else
			{
				sectionImage = document.getElementById( 'ajax_content_wrapper' ).offsetHeight + 
					document.getElementById( 'promo_gallery' ).offsetHeight - 150 - 
					document.getElementById( 'vertical_nested_container' ).offsetHeight + "px";
			}

			$j("#section_image").css("margin",sectionImage + " 0 0 0");
		
			div = document.getElementById( 'vertical_nested_container' );
			
			if( div == null )
				div = document.getElementById( 'links' );
				
			columnName = array[2];
			
			listItems = div.getElementsByTagName( 'li' );			
			var length = listItems.length;

			for( i = 0; i < length; i++ )
			{
				if( listItems[i].firstChild.id != columnName )
				{
					className = listItems[i].firstChild.className.toString();
					className = className.replace( /selected/, '' );
					listItems[i].firstChild.className = className;
				}
			}
			
			if( document.getElementById( columnName ) != null )
			{
				if( document.getElementById( columnName ).className == "" )
					document.getElementById( columnName ).className = "selected";
			}
		}
	});
}

// Calculate position of side image.

function calculateSideImagesPos()
{	
	var intouchLogo = 0;
	var orangeBox = 0;
	var logoHeight = 48;
	var sectionImageHeight = 150;
	var wrapperHeight = 0;
	var campaignImageHeight = 300;

	orangeBox = document.getElementById( 'sub_content' ).offsetHeight;
	
	var textHeight = document.getElementById( 'text' ).offsetHeight;
	
	if( textHeight > orangeBox && orangeBox != 0)
	{	
		// Height of the text minus the logoheight and the orange box height.
		
		intouchLogo = textHeight - orangeBox - logoHeight;
		
		wrapperHeight = textHeight + campaignImageHeight - sectionImageHeight;
		
	}
	else
	{
		//Height of orange box plus the top image and the logoheight as this is now the page length.
		
		wrapperHeight = orangeBox + campaignImageHeight + logoHeight - sectionImageHeight;
		
		// Just goes directly beneath the orange box so just give a small margin above.
		
		intouchLogo = 10;		
	}

	$j("#intouch_logo").css("margin",intouchLogo + "px 0 0 0");
	$j("#vertical_nested_container").css("height",wrapperHeight + "px");
}

// Get the product copy for the specific product clicked on.

function getProduct( productID )
{
	new Ajax.Request( '/includes/GetProduct.php', {
		method: 'get',
		parameters: 'productID=' + productID,
  		onSuccess: function( transport )
		{
			var array = transport.responseText.split( '|' );

			document.getElementById( 'ajax_content_wrapper' ).innerHTML = array[0];
			document.getElementById( 'section_image' ).innerHTML = array[1];
			
			calculateSideImagesPos();		
			
			document.getElementById( productID ).className = "selected";
			
			listItems = document.getElementById( productID ).parentNode.parentNode.getElementsByTagName
				( 'li' );
			
			for( i = 0; i < listItems.length; i++ )
			{
				if( listItems[i].firstChild.id != productID )
					listItems[i].firstChild.className = "";
			}
		}
	});
}

// Get the next training downloads for the page using SmartyPaginate (via Ajax) to navigate through a 
// list.

function getTrainingDownloads( next )
{
	new Ajax.Request( '/includes/GetTrainingDownloads.php', {
		method: 'get',
		parameters: 'next=' + next,
  		onSuccess: function( transport )
		{
			var array = transport.responseText.split( '\|' );
			
			document.getElementById( 'ajax_content_wrapper' ).innerHTML = array[0];
			document.getElementById( 'section_image' ).style.display = "none";
		}
	});
}

// Get the next knowledge base articles for the page using SmartyPaginate (via Ajax) to navigate 
// through a list.

function getKnowledgeBaseArticles( absolutePath, webRoot, next )
{
	new Ajax.Request( webRoot + '/includes/GetKnowledgebaseArticles.php', {
		method: 'get',
		parameters: 'next=' + next + '&' + 'webRoot=' + webRoot + '&' + 'absolutePath=' + absolutePath,
  		onSuccess: function( transport )
		{
			var array = transport.responseText.split( '\|' );
			
			document.getElementById( 'ajax_content_wrapper' ).innerHTML = array[0];
			
			// Call function to position the section image. 
			setSectionImagePos();
		}
	});
}

// Get the next news articles for the page using SmartyPaginate (via Ajax) to navigate through a list.

function getNews( absolutePath, webRoot, next )
{
	new Ajax.Request( webRoot + '/includes/GetNewsArticles.php', {
		method: 'get',
		parameters: 'next=' + next + '&' + 'webRoot=' + webRoot + '&' + 'absolutePath=' + absolutePath,
  		onSuccess: function( transport )
		{
			var array = transport.responseText.split( '\|' );
			document.getElementById( 'list' ).innerHTML = array[0];
			document.getElementById( 'prev_and_next' ).innerHTML = array[1];
	
			idxGetVariable = window.location.href.toString().indexOf( '?' );
			
			if( idxGetVariable != -1 )
			{
				window.location.href = window.location.href.toString().substring( 0, idxGetVariable ) + 
					"?next=" + next;
			}
			else
				window.location.href += "?next=" + next;
		}
	});
}

// Get the next case studies for the page using SmartyPaginate (via Ajax) to navigate through a list.

function getCaseStudies( absolutePath, webRoot, next )
{
	new Ajax.Request( webRoot + '/includes/GetCaseStudies.php', {
		method: 'get',
		parameters: 'next=' + next + '&' + 'webRoot=' + webRoot + '&' + 'absolutePath=' + absolutePath,
  		onSuccess: function( transport )
		{
			var array = transport.responseText.split( '\|' );
			
			if( document.getElementById( 'text' ) != null )
				document.getElementById( 'text' ).style.display = "none";
			
			if( document.getElementById( 'list_content' ) != null )
				document.getElementById( 'list_content' ).style.display = "";
				
			document.getElementById( 'list' ).innerHTML = array[0];
			document.getElementById( 'prev_and_next' ).innerHTML = array[1];
		}
	});
}

// Set position of "in touch" image on left-hand side.

function setInTouchImagePos()
{
	if( document.getElementById( 'sub_content' ) == null )
	{
		document.getElementById( 'intouch_logo' ).style.marginTop = 
			document.getElementById( 'text' ).offsetHeight - 48 + "px";
	}
	else
	{
		if( document.getElementById( 'text' ).offsetHeight > 200 )
		{
			document.getElementById( 'intouch_logo' ).style.marginTop = 
				document.getElementById( 'text' ).offsetHeight - 48 - 
				document.getElementById( 'sub_content' ).offsetHeight + "px";
		}
		else
		{
			document.getElementById( 'sub_content' ).parentNode.removeChild
				( document.getElementById( 'sub_content' ) );
					
			document.getElementById( 'intouch_logo' ).style.marginTop = 
				document.getElementById( 'text' ).offsetHeight - 48 + "px";
		}				
	}
}

// Display the content specified of the type specified in a Shadowbox at the dimensions specified.

function openShadowbox( type, content, height, width )
{
	Shadowbox.init();
	
	Shadowbox.open({
		type:	type,
        content:	content,
		height:	height,
		width: width
    });
}

// Display video in a shadowbox.

function displayVideo( filename )
{
	Shadowbox.init();
	
	Shadowbox.open({
		type: 'swf',
        content: filename,
		height:	300,
		width: 680
    });
}

// For displaying connection process.

function showConnectionProcess()
{
	openShadowbox( 'iframe', 'ConnectionProcess.php', 322, 817 );
}

// For displaying installation process.

function showInstallationProcess()
{
	openShadowbox( 'iframe', 'InstallationProcess.php', 322, 817 );
}

// For fault categories process.

function showFaultCategories()
{
	openShadowbox( 'iframe', 'FaultCategories.php', 340, 497 );
}

// For mobile fault categories.

function showMobileFaultCategories()
{
	openShadowbox( 'iframe', 'FaultCategories.php', 372, 497 );
}

// Display image at shadowbox.

function displayImage( filename )
{
	Shadowbox.init();
	
	Shadowbox.open({
		type: 'img',
        content: filename
    });
}