// JavaScript Document

var arExpandStatus = new Array();
var intOffset = 0;

$(document).ready(function () {
	//hide all content with javascript
	$("div.expand_body").slideUp(1, AdjustScrollRatio);
	
	$('h3.expand_head').click(function () {
		var intClicked = GetExpandID($(this));
		AlterStatus(intClicked);
		$("#expand_body"+intClicked).slideToggle('medium', AdjustScrollRatio);
    });
	
	$('img.collapse_button').click(function () {
		var intClicked = GetCollapseID($(this));
		
		ScrollToPoint(intClicked, "expand_head");
		/*
		//get height of text body, so we can scroll back up to the title
		var intBodyHeight = document.getElementById("expand_body"+intClicked).offsetHeight;
		//scroll back up to the title
		scrollbar.scrollBy(0, (intBodyHeight-20));
		*/
		
		//change the expand/collapse array status
		AlterStatus(intClicked);
		//expand/collapse text body
		$("#expand_body"+intClicked).slideToggle('medium', AdjustScrollRatio);
    });
	
	$('li.scroll_to').click(function () {
		var intClicked = GetTypeID($(this));
		ScrollToPoint(intClicked, "scroll_point");
    });
});

function InitIdIndexes() {
	//auto assign element id numbers - used for each product page
	$('h3.expand_head').each(
		function(intIndex){
			$(this)[0].id = "expand_head"+intIndex;
		}
	);
	$('div.expand_body').each(
		function(intIndex){
			$(this)[0].id = "expand_body"+intIndex;
		}
	);
	$('img.collapse_button').each(
		function(intIndex){
			$(this)[0].id = "collapse_button"+intIndex;
		}
	);
}

function ScrollOffset(_intOffset) {
	intOffset = _intOffset;
}

function ScrollToPoint(intPoint, strTitle) {
	var intBodyHeight = document.getElementById(strTitle+intPoint).offsetTop;
	intBodyHeight = intBodyHeight - intOffset;//100; //correction for products, hidden top corner
	scrollbar.scrollTo(0,intBodyHeight);
}

function GetExpandID(objExpandItem) {
	var strCurHover = objExpandItem[0].id;
	return strCurHover.substring(11);
}

function GetCollapseID(objCollapseImg) {
	var strClicked = objCollapseImg[0].id;
	return strClicked.substring(15);
}

function AlterStatus(intClicked) {
	//change the true/false status for the item id.
	//So we know which way to point the arrow and when to add/remove the dotted separation line
	if(arExpandStatus[intClicked] == true) {
		//expanded. Collapse div
		arExpandStatus[intClicked] = false;
		//remove dotted line
		document.getElementById("expand_body"+intClicked).style.borderBottom="none";
		//change expand/collapse arrow image
		document.getElementById("expand_head"+intClicked).style.backgroundImage="url(/img/layout/collapsed.png)";
	} else {
		//collapsed. Expand div
		arExpandStatus[intClicked] = true;
		//add dotted line to bottom
		document.getElementById("expand_body"+intClicked).style.borderBottom="1px dotted #FFF";
		//change expand/collapse arrow image
		document.getElementById("expand_head"+intClicked).style.backgroundImage="url(/img/layout/expanded.png)";
	}
}

function AutoExpand(intExpand) {
	//automatically expand the first item
	AlterStatus(intExpand);
	$("#expand_body"+intExpand).slideToggle(1700, AdjustScrollRatio);
}



function AdjustScrollRatio() {
	//update the total height variable so the scroll bar position works correctly
	scroller.totalHeight = scroller.content.offsetHeight;
	scrollbar._updateRatio();
	//alert("viewable: "+scroller.viewableHeight+"\ntotal: "+scroller.totalHeight);
}

function GetTypeID(objType) {
	//return number from list id="scroll_toX"
	var strClicked = objType[0].id;
	return strClicked.substring(9);
}
