/*dbug = {
	firebug: false, debug: false, log: function(msg) {},
	enable: function() { if(this.firebug) this.debug = true; dbug.log = console.debug; dbug.log('enabling dbug');	},
	disable: function(){ if(this.firebug) this.debug = false; dbug.log = function(){}; }
}
if (typeof console != "undefined") { // safari, firebug
	if (typeof console.debug != "undefined") { // firebug
		dbug.firebug = true; if(window.location.href.indexOf("debug=true")>0) dbug.enable();
	}
}

//dbug.enable();
 */
var tabs = '';
var pages = '';
var pageHeights = '';
var scroll = '';
var currentTabId ='';
var currentPageId ='';
var currentPageHeight ='';
var previousPageId = '';
 
document.addEvent('domready', function() {
	scroll = new Fx.Scroll($('Main'), {
		wait: false,
		wheelStops: false,
		duration: 1500,
		offset: {'x': 0, 'y': 0},
		transition: Fx.Transitions.Quad.easeInOut
	});
	
	tabs = $('Navigation').getElementsByTagName('li');
	
	pages = $$('.page');
	if(window.ie){
		pageHeights = ["800px", "1400px", "1869px", "500px", "682px", "1426px"];
	}else{
		pageHeights = ["782px", "1250px", "1869px", "500px", "682px", "1426px"];
	}
	
	currentTabId = 'nav_Overview';
	previousPageId = 'nav_Overview';
	movePage(currentTabId);
	
 	$('nav_Overview').addEvent('click', function(event) {
		movePage('nav_Overview', event);
	});
	
	$('nav_Showcase').addEvent('click', function(event) {
		movePage('nav_Showcase', event);
	});	
	
	$('nav_FAQ').addEvent('click', function(event) {
		movePage('nav_FAQ', event);
	});
	
	$('nav_Demo').addEvent('click', function(event) {
		movePage('nav_Demo', event);
	});
	
	$('nav_Webinar').addEvent('click', function(event) {
		movePage('nav_Webinar', event);
	});
	
	$('nav_Features').addEvent('click', function(event) {
		movePage('nav_Features', event);
	});
	
	$('ArrowRight').addEvent('click', function(event) {
		event = new Event(event).stop();
		
		//the next tab's id
		nextTabId = "";
		
		//loop through all the tabs
		for(i=0; i< tabs.length; i++){ 
			//once we find the current tab within the array
			if(tabs[i].id == currentTabId){ 
				//get the next tag index.
				nextID = i + 1;
				
				//if that next tag index is outside the bound of the array, go to the beginning
				if(nextID >= tabs.length){
					nextTabId = tabs[0].id;
				}else{
					nextTabId = tabs[nextID].id;
				}
			}
		}
		//set the tab
		//dbug.log(nextTabId);
		movePage(nextTabId, event);
		 
	});
	
	$('ArrowLeft').addEvent('click', function(event) {
		event = new Event(event).stop();
		
		//the prev tab's id
		prevTabId = "";
		
		//loop through all the tabs
		for(i=0; i< tabs.length; i++){ 
			//once we find the current tab within the array
			if(tabs[i].id == currentTabId){ 
				//get the previous tag index.
				prevID = i - 1;
				
				//if that prev tag index is outside the bound of the array, go to the beginning
				if(prevID < 0){
					prevTabId = tabs[tabs.length-1].id;
				}else{
					prevTabId = tabs[prevID].id;
				}
			}
		}
		//set the tab
		//dbug.log(prevTabId);
		movePage(prevTabId, event);
		
	});
	//hideAnswers();
 
		 
});

function movePage(tabID, event){
	if($defined(event)){
		event = new Event(event).stop();
	}
	
	//clear the tab's classes
	for(i=0; i< tabs.length; i++){
		tabs[i].className = "";	
	}
	
	//set the passed tab to be current
	$(tabID).className="current";
	
	//set the tab as current tab
	currentTabId = tabID;
	
	//get the current page Id by splitting up the tabId
	pageId = "content_" + currentTabId.split("_")[1];
	
	//set the currentPageHeight from the pageHeights array using the current Page's index
	currentPageHeight = pageHeights[getPageIndex(pageId)];

	
	//before we change the currentPageId, save the priorPageId;
	previousPageId = currentPageId;
	currentPageId = pageId;
	
	//move the content
	scroll.toElement(currentPageId);
	
	//change the height of the window
	setWindowToPageHeight(currentPageId);
}
 
 

function hideAnswers(){
	var answers = $$('div.answer');
//	dbug.log(answers);
	for (i=0; i< answers.length; i++){
		answers[i].className="answer hide";
	}
}

function showAnswer(element){
	hideAnswers();
	
	FAQBlock = element.parentNode;
	answer = FAQBlock.getElementsByTagName('div')[0];
	//dbug.log(answer.className);
	answer.className = 'answer show'; 

}

function setWindowToPageHeight(pageId){
	pages.each(function(page){
		//dbug.log($(page).getStyle('height'));
		//dbug.log("currentPageHeight: " + currentPageHeight.toInt());
		//dbug.log("futurePageHeight: " + pageHeights[getPageIndex(pageId)]);
		//dbug.log("pageId: " + pageId);
		//dbug.log("page Index: " + getPageIndex(pageId)); 
		//$(page).setStyle('height',pageHeights[getPageIndex(pageId)]);
		previousHeight=pageHeights[getPageIndex(previousPageId)];
		currentHeight=pageHeights[getPageIndex(pageId)].toInt();
		
		if(previousHeight < 500){
			previousHeight = '500';
		}
		
		if(currentHeight.toInt() < 500){
			currentHeight = '500';
		}
		
		$(page).effect('height',{
		duration: 1000,
		transition: Fx.Transitions.Quad.easeInOut
		}).start(previousHeight,currentHeight);

	});
	
	
}

function getPageIndex(pageId){
	for(i=0; i< pages.length; i++){ 
		//once we find the current tab within the array
		if(pages[i].id == pageId){ 
			return i;
			break;
		}
	}
	return -1; 	
}


