// current page index
var _current = 0;

// whether we are in presentation mode or not
var presentationMode = false;	

// 
var companiesOpen = false;

var companiesIndex = 0;
var companyCancel = false;

var ngenCancel = false;

function thisMovie(movieName) {

	return document.getElementById(movieName);

}


ddd = function() {

	companiesIndex++;
	if (companiesIndex < 10 && !companyCancel) {
	
		var temp = thisMovie('CompaniesObject');
		temp.select(companiesIndex);
		setTimeout(ddd, 3000);	
	}
	
	companyCancel = false;
}

pauseCompany = function () {
	companyCancel = true;
}

playCompany = function () {
	setTimeout(ddd, 3000);
}

pauseNgen = function () {
	ngenCancel = true;
}

playNgen = function () {
	ngenNextWindow();
	
}

hideWindows = function () {
	
	$('ngen-message-ngen').hide();

	
	$('ngen-message-1812').hide();
	$('ngen-message-nin').hide();
	$('ngen-message-iat').hide();

	$('ngen-message-dc').hide();
	$('ngen-message-fg').hide();
	$('ngen-message-fe').hide();
	$('ngen-message-mi').hide();
	
	$('ngen-message').hide();
}


var nextWindowIndex = null;
ngenNextWindow = function () {
	var list = $$('.window-info');
	
	if (ngenCancel) {
		ngenCancel = false;
		return;
	}
	
	if (nextWindowIndex == null) {
		setTimeout(ngenNextWindow, 5000);
		
		nextWindowIndex = 0;
	} else {

		hideWindows();
	
		$('ngen-message').show();		
		list[nextWindowIndex].show();
		
		nextWindowIndex++;
		
		if (nextWindowIndex < list.length) {
			setTimeout(ngenNextWindow, 5000);	
		} else {
			nextWindowIndex = null;
		}
	}
	
}

showAdvantage = function (index) {
	$$('.advantages').each(function (e) { e.hide(); });
	
	if (!presentationMode) $(index.toLowerCase()).show();

}
 
preOpenResource = function(index) {
		hideWindows();
		if (index == 2 && !companiesOpen) {

			$$(".company").each(function(e) {e.hide();} );

			var firstIndex = Math.floor(Math.random()*10);
			
			switchToIndex(firstIndex);
			companiesIndex = firstIndex;
			companiesOpen = true;
		}


		if (index == 1 && !presentationMode) {
			$('container').setStyle({"height": "600px"});
			
		
		}

		if (index == 4 && !presentationMode) {
		
			var adv = thisMovie('Advantages');
			if (adv.load) adv.load();
			if (adv.start) adv.start();

		
			$('container').setStyle({"height": "640px"});
			
			$$('.advantages').each(function(d) {d.hide();});

		}


		if (index == 0 && !presentationMode) {
					$('container').setStyle({"height": "500px"});

		}
		
		if (index == 6 && !presentationMode) {
			$('container').setStyle({"height": "550px"});
			
		
		}
		
		if (index == 5 && !presentationMode) {
			$('container').setStyle({"height": "auto"});
			
		
		}

		if (index == 7 && !presentationMode) {
			$('container').setStyle({"height": "560px"});
			
		
		}


		if (index == 3 && !presentationMode) {
			$('container').setStyle({"height": "500px"});

		}

		if (index == 2 && !presentationMode) {
			
			$('container').setStyle({"height": "670px"});

		}
}

	openResources = function (index) {
		
		if (index == 0) {
		
			var mov = thisMovie('Home');
			mov.Play();
		}
		

		if (index == 1)	{

		
		}	
		
		if (index == 2) {
			$$(".companies").each(function(e) {e.show();} );
			d = function () { 
			
			var theMov = thisMovie('CompaniesObject');
			theMov.select(companiesIndex); }
			setTimeout(d, 1000);
			//var clist = $$(".companies").each(function(e) {e.show();} );
			if (presentationMode) playCompany();
		}
		
		if (index == 3) {
			if (presentationMode) ngenNextWindow();
		
		}
		
		if (index == 4) {
			var theAdv = thisMovie('Advantages');
			theAdv.load();
			theAdv.start();
		}

		
		if (index == 6) {
			var theIns = thisMovie('TheInspiration');
			theIns.load();
			if (!presentationMode) theIns.start();
		}
	
	
		if (index == 5) {
			$$(".videos").each(function(e) { e.show();});
		
			var theTest = thisMovie('Testimonial');
			if (theTest.load) theTest.load();
			if (!presentationMode) theTest.start();
		
		}
		
		if(index == 7) {
			$('takepart-start').hide();
			$('takepart-grow').hide();
			$('takepart-learn').show();
		}
		
		
	};
	
	closeResources = function (index) {
	
		if (index != 0) {
			var theHome = thisMovie('Home');
			
			theHome.StopPlay();

		}
		
		if (index != 3 && index != 4 && index != 0 && index != 2 && index != 1 && index != 5 && index != 6 && index != 7 && !presentationMode) {
			$('container').setStyle({"height": "auto"});
			
		}
		
		
		
		if (index != 4) {
		
			var theAdv = thisMovie('Advantages');
			if (theAdv.pause) theAdv.pause();
			
			$$('.advantages').each(function(d) {d.hide();});
		}
		
		
		if (index != 2) {
			$$(".companies").each(function(e) {e.hide();} );
			pauseCompany();
		}
	
		//close the rest
		if (index != 6)	{
			var theInsp = thisMovie('TheInspiration');
			if (theInsp.pause) theInsp.pause();
		}
		
		if (index != 5) {
			var theTest = thisMovie('Testimonial');
			if (theTest.pause) theTest.pause();
			$$(".videos").each(function(e) { e.hide();});

		}
	
	};
	

	switchToIndex = function(index) {
		$$(".companies").each(function(e) {e.show();} );

		
		list = $$('.companies .company');
		list.each(function(e) {
			e.hide();
		});
		list[index].show();
		companiesIndex = index;
	}


testimonialMouseClick = function(e) {
	e.stop();

	var w = $(Event.element(e)).up('.testimonials') ;

	var list = $$(".testimonials");

	for (i=0;i<list.length; i++) {
		if (list[i] == w) {
			var temp = thisMovie('Testimonial');
			temp.playByIndex(i);
			$($('Testimonial').parentNode).scrollTo();
			break;
		}
	
	}

	return false;

}


togglePresentationMode = function () {

	if (!presentationMode) {
		if (Prototype.Browser.Gecko) {
			location.href="presentation.html";
		} else {
			$$('body').each(function(e) { e.setStyle({"overflow": "hidden"}); });
			$('container').setStyle({"height": "480px"});
			$('page-box').scrollTo();
			$('company-control').show();
			$('ngen-control').show();

			$$(".hideForPresentation").each(function(e) { e.hide(); });
			$$('h1').each(function(e) { e.hide();});
			presentationMode = true;
		
		
			preOpenResource(_current);
			closeResources(_current);
			openResources(_current);
		}
	} else {
	
		$$('body').each(function(e) { e.setStyle({"overflow": "auto"}); });
		$('container').setStyle({"height": "auto"});
		$('company-control').hide();
		$$('h1').each(function(e) { e.show();});
		$$(".hideForPresentation").each(function(e) { e.show(); });
			$('ngen-control').hide();

		presentationMode = false;

		preOpenResource(_current);
		closeResources(_current);
		openResources(_current);

	}

}


showTakePart = function (theID) {

	$$('.learn').each(function(e) { e.hide(); });

	$(theID).show();
	

}

hideAndShowMessages = function(theID) {
	
	hideWindows();
	
	$('ngen-message').show();

	$(theID).show();


}



Event.observe(window, 'load', function() {

	$('container').setStyle({"height": "500px"});


	var _holder = $('tabslider');
	var _links = $$('#nav a');
	var _prev = $('prevSlide');
	var _next = $('nextSlide');
	var _step = 851;
	var _animated = false;
	var _duration = 0.5;
	var _length = _links.length;
	



	if(typeof sIFR == "function"){
	
			sIFR.replaceElement(named({sSelector:"#container h1", sFlashSrc:"swf/bell-gothic.swf", sColor:"#000000", sLinkColor:"#000000", nPaddingTop:"0", nPaddingBottom:"0", nPaddingLeft:"0", sWmode:"transparent", sCase:"upper"}));
			sIFR.replaceElement(named({sSelector:"#container h2", sFlashSrc:"swf/bell-gothic.swf", sColor:"#000000", sLinkColor:"#000000", nPaddingTop:"0", nPaddingBottom:"0", nPaddingLeft:"0", sWmode:"transparent", sCase:"upper"}));
			sIFR.replaceElement(named({sSelector:"#container h3", sFlashSrc:"swf/bell-gothic.swf", sColor:"#000000", sLinkColor:"#000000", nPaddingTop:"0", nPaddingBottom:"0", nPaddingLeft:"0", sWmode:"transparent"}));
		
	
	};


	
	_links.each(function(link, i){
		link.onclick = function(){
			if (!_animated) {
				preOpenResource(i);
				_links[_current].parentNode.className = '';
				this.parentNode.className = 'active';
				closeResources(i);
				new Effect.Morph(_holder, {
					style: {'marginLeft': -(_step*i)+'px'},
					duration: _duration,
					afterFinish: function () {
						openResources(i);
					}
					
				});
				_current = i;
			}		
			return false;
		};
	});
	
	_next.onclick = function(){
		preOpenResource(_current);
	
		_links[_current].parentNode.className = '';
		_current += 1;
		if (_current > _length-1) _current = 0;
		new Effect.Morph(_holder, {
			style: {'marginLeft': -(_step*_current)+'px'},
			duration: _duration,
			afterFinish: function () {
				closeResources(_current);
				openResources(_current);
				
			}
		});
		_links[_current].parentNode.className = 'active';
		return false;
	}
	_prev.onclick = function(){
	
		preOpenResource(_current);

		_links[_current].parentNode.className = '';
		_current -= 1;
		if (_current < 0) _current = _length-1;
		new Effect.Morph(_holder, {
			style: {'marginLeft': -(_step*_current)+'px'},
			duration: _duration,
			afterFinish: function () {
				closeResources(_current);
				openResources(_current);
				
			}
		});
		_links[_current].parentNode.className = 'active';
		return false;
	}
	
	inspirationComplete = function() {
		$('nextSlide').onclick();
	}
	
	$$('.testimonials a').each(function (d) { d.observe('click', testimonialMouseClick); });
	

  
});
