// Opening and closing of the drop down information
morphs = new Array();
function createMorph(elm,speed) {
	if(!speed) speed = 500;
	morphs[elm] = new Fx.Morph(elm, {wait: false, duration: speed, transition: Fx.Transitions.Sine.linear});
}
function closeElement(elm) {
	if(elm.id) elm = elm.id;
	if(!morphs[elm]) createMorph(elm);
	morphs[elm].start({ 'height': 0 });
	$(elm).fade(0);
	(function() { $('overlay').setStyle('display', 'none'); }).delay(500);
}
function openElement(elm) {
	if(elm.id) elm = elm.id;
	if ($('overlay').getStyle('display') == "block")
		(function() { $('overlay').setStyle('display', 'block'); }).delay(500);
	else $('overlay').setStyle('display', 'block');
	if(!morphs[elm]) createMorph(elm);
	var oldHeight = $(elm).getStyle('height');
	$(elm).style.height = '600px';
	var newHeight = $(elm).offsetHeight - $(elm).getStyle('padding-top').toInt() - $(elm).getStyle('padding-bottom').toInt();
	$(elm).style.height = oldHeight;
	morphs[elm].start({ 'height': newHeight });
	$(elm).fade(1);
}
function dropDowns(sect) {
	checkLastAction();
	if(hist.getCurrentHash() == sect)
	{
		hist.addState('carousel'+mf.index);;
		lastAction = false;
	}
	else 
	{
		openElement(sect + 'Text');
		hist.addState(sect);
		lastAction = sect;
	}
}

// Some page initializations and what-not
   var coverText = new Array();
   coverText['about-us'] = $('node1Text').innerHTML;
   coverText['what-we-do'] = $('node2Text').innerHTML;
   coverText['contact'] = $('node3Text').innerHTML;
   coverText['news'] = $('node4Text').innerHTML;

   var learnMoreGraphics = new Array();
   learnMoreGraphics['about-us'] = $('learnMoreGraphic1');
   learnMoreGraphics['what-we-do'] = $('learnMoreGraphic2');
   learnMoreGraphics['contact'] = $('learnMoreGraphic3');
   learnMoreGraphics['news'] = $('learnMoreGraphic4');

   var text = $('textChange');
//   var learnMore = $('learnMore');
   var goBack = $('goBack');
   var faqScroll, prev;
   var scrollBar = false;

   var cMorph = new Fx.Morph($('carouselHome'));
   var textMorph = new Fx.Morph(text);
   var learnMoreGraphicMorph = new Fx.Morph($('learnMoreGraphic'));
   var supportMorph = new Fx.Morph($('overlay'), {duration: 'long', transition: Fx.Transitions.Quad.easeInOut});
   var faqMorph = new Fx.Morph($('faqText'), {duration: 'long', transition: Fx.Transitions.Quad.easeInOut});

   var initMultiBox;
   var mf;

   var currTryItState = 0;

   var hist = new HistoryManager();



// Undoes the previous action
var lastAction = false;
function checkLastAction() {
	if (lastAction == "buyNow")
		closeElement('buyNowText');
	if (lastAction == "support")
		closeElement('supportText');
	if (lastAction == "faq")
		closeElement('faqText');
	if (lastAction == "tryIt")
		initMultiBox.close();
	if (lastAction && lastAction.indexOf("learnMore") > -1)
		learnMoreToggle("close");
	lastAction = false;
}
// Creates the events for back/forward browser buttons
hist.addEvent('onHistoryChange', function()
{
	checkLastAction();
	if (hist.getCurrentHash() == "buyNow")
	{
		openElement('buyNowText');
		lastAction = 'buyNow';
	}
	else if (hist.getCurrentHash() == "support")
	{
		openElement('supportText');
		lastAction = 'support';
	}
	else if (hist.getCurrentHash() == "faq")
	{
		openElement('faqText');
		lastAction = 'faq';
	}
	else if (hist.getCurrentHash() == "tryIt")
	{
		initMultiBox.open(currTryItState, $$('.mb'));
		lastAction = 'tryIt';
	}
	else if (hist.getCurrentHash().indexOf("learnMore") > -1)
	{
		learnMoreToggle("open");
		lastAction = hist.getCurrentHash();
	}
	else if (hist.getCurrentHash().substr("carousel"))
	{
		mf.clickTo(hist.getCurrentHash().replace('carousel',''));
		lastAction = hist.getCurrentHash();
	}
});

window.addEvent('load', function() {
	if(document.body.offsetWidth < 1100 && document.body.offsetHeight < 750)
		$('mb2').setAttribute('rel','[loginGroup],width:'+(document.body.offsetWidth-50)+',height:'+(document.body.offsetHeight-80)+',ajax:true');
	else if(document.body.offsetWidth < 1100)
		$('mb2').setAttribute('rel','[loginGroup],width:'+(document.body.offsetWidth-50)+',height:670,ajax:true');
	else if(document.body.offsetWidth < 1100 && document.body.offsetHeight < 750)
		$('mb2').setAttribute('rel','[loginGroup],width:1100,height:'+(document.body.offsetHeight-80)+',ajax:true');

	// Initial fade to make sure fading works properly
	$('buyNowText').fade('hide');
	$('supportText').fade('hide');
	$('faqText').fade('hide');
    $('goBack').fade('hide');
    $$('.learnMore').each(function(learnMores){
		learnMores.fade('show');
	});

	
	// Click Events
	$('buyNow').addEvent('click',function() {
		dropDowns('buyNow');
	});  
	$('goBackBuyNow').addEvent('click', function() {
		dropDowns('buyNow');
   });

	$('footerIconSupport').addEvent('click', function()	{
		dropDowns('support');
   });
   $('goBackOverlay').addEvent('click', function() {
		dropDowns('support');
	});
	
	$('faqOverlay').addEvent('click', function() {
		dropDowns('faq');
	});
	$('goBackFaq').addEvent('click', function() {
		dropDowns('faq');
   });
   $('logo').addEvent('click', function() {
		checkLastAction();
		hist.addState('carousel'+mf.index);;
		lastAction = false;
   });
	
	// Handles all the multibox events
	initMultiBox = new multiBox({
		mbClass: '.mb',//class you need to add links that you want to trigger multiBox with (remember and update CSS files)
		container: $(document.body),//where to inject multiBox
		descClassName: 'multiBoxDesc', //the class name of the description divs
		useKeyboard: 'false'
	});
	$('mb1').addEvent('click', function() {
		checkLastAction();
		if(hist.getCurrentHash() == "tryIt")
		{
			hist.addState('carousel'+mf.index);
			lastAction = false;
		}
		else 
		{
			hist.addState('tryIt');
			lastAction = 'tryIt';
		}
   });
   $$('.MultiBoxClose')[0].addEvent('click', function() {
		checkLastAction();
        hist.addState('carousel'+mf.index);
		lastAction = false;
   });
   
   // Accordion for the FAQs
	var myAccordion = new Accordion($('accordion'), 'h3.toggler', 'div.element', {
	onComplete: function(){
		var currentScroll = $('accordion').getElement('.contentEl').scrollTop;
        faqScroll.refresh();
		faqScroll.setSlider(currentScroll);
    }
	});
	faqScroll = new MooScroll(
	{
		selector: '#accordion',
		scrollControlsYClass: 'accScrollControlsY'
	});
	(function() { $('overlay').style.display = 'none'; }).delay(1000);
	
	mf = new MooFlow($('MooFlow'), {
		startIndex: 2,
		useMouseWheel: true,
		useKeyInput: true,
		bgColor: '#FFF',
		factor: '145',
		offsetY: '-0.1',
		reflection: '.3'
	});
	$('MooFlow').getElements('img').each(function(carIt) {
		carIt.id = 'carousel' + carouselItems;
		carouselItems++;
	});
	hist.addState('carousel2');
	(function() { populateText(2); }).delay(1000);

      $('goBack').addEvent('click', function()
      {
         learnMoreToggle("close");
         hist.addState('carousel' + mf.index);
         lastAction = hist.getCurrentHash();
      });
	  
	  $$('.learnMore').each(function(learnMores){
	    learnMores.addEvent('click', function(){
           checkLastAction();
           learnMoreToggle("open");
           hist.addState('learnMore' + mf.index);
           lastAction = hist.getCurrentHash();
	    });
      });
	  
	  if(document.body.scrollHeight <= 600)
	  {
			document.body.style.overflow = "auto";
			$('content').style.marginTop = 0;
      }
});

function learnMoreToggle(option)
{
   var learnMoreGraphic = $('learnMoreGraphic' + mf.index);
   var learnMoreGraphicMorph = new Fx.Morph(learnMoreGraphic);
   if (parseInt($('textChange').getStyle('height')) == 119 && option && option != "close" || (parseInt($('textChange').getStyle('height')) == 119 && !option))
   {
      populateTextInstant(mf.index);

      (function ()
      {
         if ($$('.scrollControlsY')[0]) $$('.scrollControlsY')[0].dispose();
         if (!scrollBar) scrollBar = new MooScroll({ selector: '#textChange' });
         $$('.scrollControlsY')[0].setStyle('display', 'block');
         scrollBar.refresh();
      }).delay(600);

      // Animate the header graphic
      if (learnMoreGraphic.getStyle('display') != "block") learnMoreGraphic.setStyle('height', '1px');
      learnMoreGraphic.setStyle('opacity', '1');
      learnMoreGraphic.setStyle('display', 'block');
      learnMoreGraphicMorph.start({ height: '181px', marginBottom: '30px' });

      // Move the Carousel up
      cMorph.start({ marginTop: '-400px', opacity: '0' });
      (function() { $('carouselHome').style.display = "none"; }).delay(500);

      // Open up the text box
      textMorph.start({ height: '300px' });

      // Fade the learn more and fade in go back
      (function()
      {
         $$('.learnMore').each(function(learnMores){
			learnMores.fade(0);
         });
         $('goBack').fade(1);
      }).delay(500);
   }
   else if (parseInt($('textChange').getStyle('height')) != 119 && option && option != "open" || (parseInt($('textChange').getStyle('height')) != 119 && !option))
   {
      $('carouselHome').style.display = "block";

      $$('.scrollControlsY').each(function(scroller)
      {
         scroller.setStyle('display', 'none');
      });


      cMorph.start({ marginTop: '0px', opacity: '1' });
      textMorph.start({ height: '119px' });
	  learnMoreGraphic.style.height = learnMoreGraphic.offsetHeight + 'px';
      learnMoreGraphicMorph.start({ 
         height: '1px',
         marginBottom: '5px'
      }).chain(function(){
         learnMoreGraphic.setStyle('display', 'none');
         populateTextInstant(mf.index);
      });

      // Fade the goBack and fade in learnMore
      (function()
      {
         $('goBack').fade(0);
         $$('.learnMore').each(function(learnMores){
			learnMores.fade(1);
         });
         scrollBar = false;
      }).delay(500);
      if(scrollBar) scrollBar.setSlider('top');
   }
}

function alignMultiBox()
{
   if ($$('.MultiBoxContainer')[0].innerHTML.length > 1)
      initMultiBox.alignBox();
}

function populateText(index)
{
   var n = index;
   $('textChange').fade(0);
   (function()
   {
      if (parseInt($('textChange').getStyle('height')) > 119) learnMoreToggle();
	  if($('textChange').getElement('.nodeText')) $('textDock').appendChild($('textChange').getElement('.nodeText'));
	  $('textChange').innerHTML = "";
      $('textChange').appendChild($('node' + index + 'Text'));

   }).delay(500);

   (function()
   {
      $('textChange').fade(1);
   }).delay(500);
}

function populateTextInstant(index)
{
	  if($('textChange').getElement('.nodeText')) $('textDock').appendChild($('textChange').getElement('.nodeText'));
	  $('textChange').innerHTML = "";
      $('textChange').appendChild($('node' + index + 'Text'));
}

// Temporarily stops an even from firing for a specified time
function pauseEvent(ele, event, time)
{
   if (ele.id) ele = ele.id;
   var tmpEle = new Element('div');
   tmpEle.cloneEvents($(ele), event);
   $(ele).removeEvent(event);

   (function()
   {
      $(ele).cloneEvents(tmpEle, event);
   }).delay(time);
}