////////////////////////////////////////////////////////////////
// Initialize
////////////////////////////////////////////////////////////////
function init() { 
	// Initiliaze Menu
	treeSetup();

	// Start Menu Collapsed
	treeCollapse();

	// Check if soBanner container exists
	if (document.getElementById('sobanner') != null) { 
		// Start Banners Collapsed
		initBanners();
		
		// Initialize Banner Timer
		if (itemTotalCount > 1) initTimer();
	}
}




////////////////////////////////////////////////////////////////
// SoBanner
////////////////////////////////////////////////////////////////
images = new Array;
var itemCount;
var itemTotalCount = images.length;
var itemID;
var timerID = null;
var timerRunning = false;
var delay = 10000;

function initTimer() {
	firstItemID = document.getElementById('sb1');
	lastItemID = document.getElementById('sb' + itemTotalCount);
		
	itemCount = 1;
	
	// document.getElementById('loading').className = 'hide';
	
	firstItemID.className = 'show';
	
	stopTimer();
	startTimer();
}

function stopTimer() {
	if (timerRunning) clearTimeout(timerID);
	timerRunning = false;
}

function startTimer() {
	for (i = 1; i <= itemTotalCount; i++) {
		itemID = document.getElementById('sb' + i);
		itemID.className = (i != itemCount) ? 'hide' : 'show';

		itemMenuID = document.getElementById('sbmenu' + i);
		itemMenuID.className = (i != itemCount) ? '' : 'current';
	}

	// document.getElementById('log').innerHTML += ', ' + itemCount + '|' + itemTotalCount;

	itemCount = (itemCount == itemTotalCount) ? 1 : itemCount + 1;
	
	timerRunning = true;
	timerID = self.setTimeout("startTimer()", delay);
}

function soNav(navID) { 	
	// Stop Timer
	stopTimer();

	// Get Current Banner/Menu IDs
	currentItemID 		= document.getElementById('sb' + (itemCount - 1));
	currentItemMenuID 	= document.getElementById('sbmenu' + (itemCount - 1));
	
	// Set Current Banner/Menu to inactive
	currentItemID.className = 'hide';
	currentItemMenuID.className = '';

	// Get New Banner/Menu IDs
	navNum = navID.replace('sbnav', '');
	newItemID = document.getElementById('sb' + navNum);
	newItemMenuID = document.getElementById('sbmenu' + navNum);

	// Set Banner/Menu to active
	newItemID.className = 'show';
	newItemMenuID.className = 'current';
	
	// Update current Item #
	itemCount = (navNum * 1) + 1;
}

// Collapse all
function initBanners() {
	if (document.getElementById('sobanner') != null) { 
		var list = document.getElementById('sobanner').getElementsByTagName('img');
		
		// Start 1, to skip 0 (first image to show)
		for (var i = 0; i < list.length; i++) { 
			if (i > 0) { list[i].className = 'hide'; }
			images[i] = list[i].src;
		}

		itemTotalCount = images.length;
	}
}



////////////////////////////////////////////////////////////////
// Menu
////////////////////////////////////////////////////////////////
// Toggle
function toggle(x){
	x.className = (x.className == 'show') ? 'hide' : 'show';
	
	// Color Sub Link Black when Expanded, or back to blue when Collapsed.
	x.childNodes[0].style.color = (x.className == 'show') ? '#000' : '#003399';

	// x.className = (x.className == 'show') ? 'show current' : x.className;
}
// Collapse all
function treeCollapse() {
	if (document.getElementById('panela') != null) { 
		var list = document.getElementById('panela').getElementsByTagName('li');
		for (var i=0; i < list.length; i++) { 
			if (list[i].className == 'show') list[i].className = 'hide';

			if (parentCategoryID != null) { 
				if (list[i].id == 'm' + parentCategoryID) toggle(list[i]);
			}
		}
	}
}
// Tree Setup
function treeSetup() { 
	if (document.getElementById('panela') != null) { 
		// Get <li>'s of Panel A
		var list = document.getElementById('panela').getElementsByTagName('li');
	
		// Loop through <li>'s
		for (var i = 0; i < list.length; i++) { 
			// Loop through any <li>'s with class='show'
			if (list[i].className == 'hide') { 
				// First child of <li show> will be the <a> link
				// Add onclick function(toggle()) to first child (<a>) of <li show>
				list[i].childNodes[0].onclick = function() { toggle(this.parentNode); return false; }
			}
		}
	}
}




////////////////////////////////////////////////////////////////
// Form Validate (Newsletter)
////////////////////////////////////////////////////////////////
function validateNLform() { 
	var regexPattern;
	var errors	= '';

	email = document.getElementById('mpemail');
	
	if (email.value == '') { errors += 'Please fill in your Email Address.\n'; }
	else {
		regexPattern = /\b[a-zA-Z0-9._%-]+@[a-zA-Z0-9._%-]+\.[a-zA-Z]{2,4}\b/;
		if (!regexPattern.test(email.value)) errors += 'Email is invalid format.\n'; 
	}			

	// Check Errors
	if (errors) { alert(errors); return false; } else { return true; }
}



function goback() {
    history.go(-1);
}



onload = function() {
	init();
}
