var showAll = false;
var fixIE6 = false;

function toggleToolBox(passedTargetId,calledFrom){
	var $tool_box = $('#'+passedTargetId).first();
	$tool_box.animate({height:'toggle'},250);
	$(calledFrom).toggleClass('upArrow');
}



// function to clear default search text on focus
function clearText(calledFrom)
{
	// test current content of text box
	if (calledFrom.value == "Search")
	{
		// clear the text box
		calledFrom.value = "";
	}
}

// function to restore default search text on blur
function restoreText(calledFrom)
{
	// test current content of text box
	if (calledFrom.value == "")
	{
		// clear the text box
		calledFrom.value = "Search";
	}
}

// function to show/hide additional info
function ShowHideAdditional()
{
	var additionalDiv = document.getElementById('additional');
	additionalDiv.style.display = additionalDiv.style.display == 'none' ? '' : 'none';
}

// function to create array of news item divs
function createNewsItemArray()
{
	// get all divs within news scroller
	var allDivsInScroller = document.getElementById('newsBlockScrollContainer').getElementsByTagName('div');
	// create and zero counter
	var j = 0;
	// create new global array for storing news item divs extracted from all scroller divs
	newsItemDivs = new Array();
	// loop through all divs in news scroller
	for (var i in allDivsInScroller)
	{
		// select only news item divs
		if (allDivsInScroller[i].className == "newsBlockItem")
		{
			// add element to new array
			newsItemDivs[j] = allDivsInScroller[i];
			// add increment to counter
			j++;
		}
	}
}

// function to count news items and prepare for display
function prepareNewsScroller()
{
	// run function to create array of news item divs
	createNewsItemArray();
	// get news next and prev buttons
	var newsNextButton = document.getElementById('newsNextButton');
	var newsPrevButton = document.getElementById('newsPrevButton');
	// get news next and prev button images
	var newsNextImage = newsNextButton.getElementsByTagName('img')[0];
	var newsPrevImage = newsPrevButton.getElementsByTagName('img')[0];
	// remove onclick and href from prev button
	newsPrevButton.removeAttribute('onclick');
	newsPrevButton.removeAttribute('href');
	// if number of news items is greater than 1
	if (newsItemDivs.length > 1)
	{
		// set next button arrow image to on-state
		newsNextImage.src = "/ltr/images/bullet_arrow_red_next.gif";
	}
	// if number of news items is not greater than 1
    else
	{
		// remove onclick and href from next button
		newsNextButton.removeAttribute('onclick');
		newsNextButton.removeAttribute('href');
	}
}

// function to scroll to next news item
function nextNewsItem(clickedLink)
{
	// run function to create array of news item divs
	createNewsItemArray();
	// loop through news item div array
	for (var i = 0; i < newsItemDivs.length ; i++ )
	{
		// if current div display is visible
		if (newsItemDivs[i].style.display != 'none')
		{
			// set display to none
			newsItemDivs[i].style.display = 'none';
			// get news next and prev buttons
			var newsNextButton = document.getElementById('newsNextButton');
			var newsPrevButton = document.getElementById('newsPrevButton');
			// get news next and prev button images
			var newsNextImage = newsNextButton.getElementsByTagName('img')[0];
			var newsPrevImage = newsPrevButton.getElementsByTagName('img')[0];
			// if current news item is last item
			if (i + 2 == newsItemDivs.length)
			{
				// set next button arrow image to off-state
				newsNextImage.src = "/ltr/images/bullet_arrow_grey_next.gif";
				// remove onclick and href from next button
				newsNextButton.removeAttribute("onclick");
				newsNextButton.removeAttribute("href");
			}
			// if more than one news item
			if (newsItemDivs.length > 1)
			{
				// set prev button arrow image to on-state
				newsPrevImage.src = "/ltr/images/bullet_arrow_red_prev.gif";
				// set onclick and href attributes of prev button
				newsPrevButton.setAttribute("onclick","prevNewsItem(this)");
				newsPrevButton.setAttribute("href","javascript:void(0)");
			}
			// end the loop
			break;
		}
	}
}

// function to scroll to previous news item
function prevNewsItem()
{
	// run function to create array of news item divs
	createNewsItemArray();
	// loop through news item div array backwards
	for (var i = newsItemDivs.length-1; i >= 0; i--)
	{
		// if current div display
		if (newsItemDivs[i].style.display == 'none')
		{
			// set display
			newsItemDivs[i].style.display = '';
			// get news next and prev buttons
			var newsNextButton = document.getElementById('newsNextButton');
			var newsPrevButton = document.getElementById('newsPrevButton');
			// get news next and prev button images
			var newsNextImage = newsNextButton.getElementsByTagName('img')[0];
			var newsPrevImage = newsPrevButton.getElementsByTagName('img')[0];
			// if current news item is first item
			if (i == 0)
			{
				// set prev button arrow image to off-state
				newsPrevImage.src = "/ltr/images/bullet_arrow_grey_prev.gif";
				// remove onclick and href from prev button
				newsPrevButton.removeAttribute('onclick');
				newsPrevButton.removeAttribute('href');
			}
			// if more than one news item
			if (newsItemDivs.length > 1)
			{
				// set next button arrow image to on-state
				newsNextImage.src = "/ltr/images/bullet_arrow_red_next.gif";
				// set onclick and href attributes of next button
				newsNextButton.setAttribute('onclick','nextNewsItem(this)');
				newsNextButton.setAttribute('href','javascript:void(0)');
			}
			// end the loop
			break;
		}
	}
}

// show tab content on the product pages
function openTab(tabId, link) {
	var tabName = tabId;
	if (link != "")
	{
		window.location = link;
		return;
	}
	if (tabName == "" || tabName == null)
	{
		$(".tab").each(function() {
			if (tabName == "" || tabName == null)
			{
				tabName = $(this).attr("id");
			}
		});
	}
	$(".productTabContent").each(function() {
		// general tab content and tab link control
		if ($(this).attr("tabName") == tabName)
		{
			$("#" + $(this).attr("tabName")).addClass("on");
			$(this).show();
			// remove focus from clicked button
			document.getElementById(tabName).blur();
		}
		else
		{
			$("#" + $(this).attr("tabName")).removeClass("on");
			$(this).hide();
		}
	});

	// custom tab content control
	if (tabName == "video")
		showVideo();
	if (tabName == "videoTutorial")
	{
		showTutorialVideo();
	}
	if (tabName == "pictures")
	{
		showPicture("", "");
		$("#pictureBox").load(function()
		{
			centerObjectVertically("pictureBox");
		});
	}
	if (tabName == "animation")
	{
		showAnimation();
	}
	if (tabName == "accessories")
	{
		showAccessories();
	}
	else
	{
		$("#productDescriptionArea").show();
		$("#accessoryDescriptionArea").hide();
	}
}

// show picture box and hide flash video
function showPicture(fileName, linkButton)
{
	// set the file name of the picture
	if (fileName != "")
	{
		document.getElementById("mainImage").src = fileName;
	}
	// remove focus from clicked button
	linkButton.blur();
	// highlight the selected picture
	if (linkButton != "")
	{
		// deselect every image, select clicked image
		$(".galleryThumbs a").each(function () {
			$(this).removeClass();
		});
		$(linkButton).addClass("on");
	}
	return false;
}

// narrow selection
function narrowSelection(calledFrom)
{
	// check that at least one checkbox has been checked
	atLeastOneChecked = false;
	// loop through all elements in narrow selection form
	for (x=0; x<document.prodTable.elements.length; x++)
	{
		// if element is a checkbox
		if (document.prodTable.elements[x].type == 'checkbox')
		{
			// if checkbox is selected
			if (document.prodTable.elements[x].checked == true &&
				document.prodTable.elements[x].id.indexOf("compare") < 0)
			{
				// check that at least one checkbox has been checked
				atLeastOneChecked = true;
				break;
			}
		}
	}
	// if at least one checkbox has been checked
	if (atLeastOneChecked)
	{
		// loop through all elements in narrow selection form
		for (x=0; x<document.prodTable.elements.length; x++)
		{
			// if element is a checkbox
			if (document.prodTable.elements[x].type == 'checkbox')
			{
				// if checkbox is not a compare checkbox
				if (document.prodTable.elements[x].id.indexOf("compare") < 0)
				{
					// if checkbox is not selected
					if (document.prodTable.elements[x].checked == false)
					{
						// set start point as current checkbox
						parentElement = document.prodTable.elements[x];
						// loop up through parent elements
						for (y=0; y<3; y++)
						{
							// step up to the next parent element
							parentElement = parentElement.parentNode;
							// if element is a table row
							if (parentElement.tagName == 'TR')
							{
								// if element is a table row
								if (showAll)
								{
									// set display to default
									parentElement.style.display = '';
									// end the loop
									break;
								}
								else
								{
									// set display to none
									parentElement.style.display = 'none';
									// end the loop
									break;
								}
							}
						}
					}
				}
			}
		}
		// toggle button visibility
		$("div.narrowSelection div.greyButton").toggle();
		showAll = showAll == false ? true : false;
		// get array of table rows
		arrayTableRows = document.prodTable.getElementsByTagName('tr');
		// set row colour switch counter
		switchCount = 1;
		// loop through all table rows and recolour in alternate shades
		for (x=1; x<arrayTableRows.length; x++)
		{
			// if table row is not hidden
			if (arrayTableRows[x].style.display != 'none')
			{
				// set colour of table rows in alternate shades
				switch(switchCount)
				{
					case 1:
						arrayTableRows[x].className = "light";
						break;
					case -1:
						arrayTableRows[x].className = "shade";
						break;
				}
				// set row colour switch counter
				switchCount = -switchCount;
			}
		}
	}
	// blur the focus from the selected link
	calledFrom.blur();
}

/* function to generate the add to comparison links dynamically to solve IE6 form POST problem */
function submitAddCompareURL() {
	var ReferURL = window.location.href;
	var strUrl = "";
	var lIndex = ReferURL.lastIndexOf("/");
	if (lIndex != (ReferURL.length-1)) {
		strUrl += "/";
	} 
	for (x=0; x<document.prodTable.elements.length; x++)
	{
		if(document.prodTable.elements[x].type == 'checkbox') {
			if(document.prodTable.elements[x].checked == true &&
				document.prodTable.elements[x].id.indexOf("compare") < 0) {
					strUrl += document.prodTable.elements[x].value;
					document.prodTable.elements[x].checked = false;
			}
		}
	}
	var ReferURL = window.location.href;
	var newUrl = ReferURL + strUrl;
	if (strUrl.length>1) {
		//window.localtion.href=newUrl;
		document.prodTable.action=newUrl;
		document.prodTable.submit();
	}
}


