function calcHeight(element) {
   return $(element).getSize().y
       + $(element).getStyle("margin-top").toInt()
       + $(element).getStyle("margin-bottom").toInt();
}


function toggleFlip(toggle, toggleControl) {
   toggle.toggle();
   toggleControl.getElement(".on").toggle();
   toggleControl.getElement(".off").toggle();
   positionFooter();
}

function bindToggles() {
   $$(".toggle").each(function(toggle) {
           var toggleControl = undefined;
           toggle.getAllPrevious()
               .extend(toggle.getAllNext())
               .each(function(sibling) {
                       if (sibling.hasClass("toggle_control"))
                           toggleControl = sibling;
                   });
           if (toggleControl) {
               toggleControl.addEvent("click", function(event) {
                       event.stop();
                       toggleFlip(toggle, toggleControl);
                   });
           }
       });
}

function bindTabs() {
   $$(".tab").each(function(tab, index) {
		tabs = $(tab.parentNode);
		tabbed = $(tabs.parentNode);
		if ($("selector")){
			$("selector").setStyle("width", tabbed.getElement(".tab.selected").getCoordinates()['width']);
			$("selector").setStyle("top", tabbed.getElement(".tab.selected").getPosition()['y']-17);
			$("selector").setStyle("left", tabbed.getElement(".tab.selected").getPosition()['x']);
		}
		tab.addEvent("click", function(event) {
			if (event) { event.stop(); }
			tabs.getElement(".tab.selected").removeClass("selected");
			tab.addClass("selected");
			tabbed.getElement(".tabc.selected").removeClass("selected");
			tabbed.getElements(".tabc")[index].addClass("selected");
			if ($("selector")){
				$("selector").setStyle("width", tabbed.getElement(".tab.selected").getCoordinates()['width']);
				$("selector").setStyle("left", tabbed.getElement(".tab.selected").getPosition()['x']);
			}


		});
		if (tabbed.hasClass("touch")){
			tab.addEvent("mouseenter", function(event) {
				tabs.getElement(".tab.selected").removeClass("selected");
				tab.addClass("selected");
				tabbed.getElement(".tabc.selected").removeClass("selected");
				tabbed.getElements(".tabc")[index].addClass("selected");
				if ($("selector")){
					$("selector").setStyle("width", tabbed.getElement(".tab.selected").getCoordinates()['width']);
					$("selector").setStyle("left", tabbed.getElement(".tab.selected").getPosition()['x']);
				}
			});
		}
	});
	var clickTab = window.location.hash;
	if ($$(clickTab)[0]){
		$$(clickTab)[0].fireEvent("click");
	}
}


function markselected(){
	var MarkedForSelection = MarkedSelected.split(/\s+/)
	for (var index = 0; index < MarkedForSelection.length; index++) {
		if (MarkedForSelection[index]) {
			$(MarkedForSelection[index]).addClass("selected");
		}
	}
}

function bindNavs() {
   $$("#nav_main li a").each(function(item, index) {
			if (item.hasClass("selected")) {
				$(item.id + "_sub").addClass("selected");
			}
           item.addEvent("mouseenter", function(event) {
           	$$("#nav_sub ul").setStyle("display", "none");
      			$(item.id + "_sub").setStyle("display", "block")
           });
           $("header").addEvent("mouseleave", function(event) {
           	$$("#nav_sub ul").setStyle("display", "none");
				$$("#nav_sub .selected").setStyle("display", "");
           });
   });
}

 
function bindTips(){
	//store titles and text
	$$('span.tooltip').each(function(element,index) {
		var content = element.get('title').split('::');
		element.store('tip:title', content[0]);
		element.store('tip:text', content[1]);
	});
	
	//create the tooltips
	var tipz = new Tips('.tooltip',{
		'className': 'tooltip',
		'fixed': true,
		'hideDelay': 50,
		'showDelay': 50
	});
}

/* function bindTips(){
	var myTips = new Tips($$('.tooltip'), {
		timeOut: 700,
		maxTitleChars: 50, // I like my captions a little long
		maxOpacity: .9 // let's leave a little transparancy in there
	});
} */

var AnimationChain = new Class({
        Implements: Chain,
        fxs: new Hash,
        running: false,
        run: function() {
            this.running = true;
            this.callChain();
        },
        stop: function() {
            this.fxs.each(function(fx) { fx.cancel(); });
            this.fxs.empty();
            this.clearChain();
			this.fxs.empty();
            this.running = false
        },
        done: function() {
//             this.running = false;
        }
    });

var animFadeStillTime = 2000;
var animFadeDuration = 500;
var animTimeBeforeBusinessSwitch = 10000;

var homeuserChain = new AnimationChain;
var businessChain = new AnimationChain;
var auChain = new AnimationChain;
var sandcChain = new AnimationChain;

function setupAnimations() {
	new Asset.images([
			"/images/homeanim/1.png", 
			"/images/homeanim/2.png", 
			"/images/homeanim/3.png", 
			"/images/homeanim/4.png", 
			"/images/homeanim/5.png", 
			"/images/homeanim/6.png", 
			"/images/homeanim/7.png", 
			"/images/homeanim/8.png",
			"/images/homeanim/9.png" 
		], {
		onComplete: function(){
			startChain(homeuserChain, 'homeuser');
		}
	});
}

function setupAUAnimations() {
	animFadeStillTime = 5000;
	new Asset.images([
			"/images/homeanim/au1.png", 
			"/images/homeanim/au2.png", 
			"/images/homeanim/au3.png", 
			"/images/homeanim/au4.png", 
			"/images/homeanim/au5.png"
		], {
		onComplete: function(){
   			startChain(auChain, 'ausplash');
		}
	});
}

function setupSCAnimations() {
	animTimeBeforeBusinessSwitch = 2000;
	startChain(sandcChain, 'scanim', setupSCAnimations, true);
}

function startChain(chain, prefix, done, loop) {
    var images = $("splash_" + prefix).getElements("div");
    var lastImage;
    images.each(function(image) {
            image.setStyle("opacity", 0);
            image.setStyle("display", "");
            chain.chain(function() {
                    if (lastImage) {
                        var fx = new Fx.Tween(lastImage, { duration: animFadeDuration,
                                                       property: "opacity",
                                                       onComplete: function() {
                                    chain.fxs.erase("-" + lastImage.id);} });
                        chain.fxs.set("-" + lastImage.id, fx);
                        fx.start("0.0");
                    }
                    var fx = new Fx.Tween(image, { duration: animFadeDuration,
                                                   property: "opacity",
                                                   onComplete: function() {
                                chain.fxs.erase(image.id);
                                chain.callChain(); } });
                    chain.fxs.set(image.id, fx);
                    fx.start("1.0");

                    lastImage = image;
                });
            chain.wait(animFadeStillTime);
        });
    if (done) {
        chain.wait(animTimeBeforeBusinessSwitch - animFadeStillTime);
        chain.chain(done);
    }
    if (! loop) {
        chain.chain(function() { chain.done(); });
    }
    chain.run();
}

function bindAUWatchAgain(){
	$("au_watchagain").addEvent("click", function(event) {
		homeuserChain.stop();
		startChain(homeuserChain, 'ausplash');
    });
}

function bindHomeTabs() {
	$("homeuser_tab").addEvent("click", function(event) {
        $("more_homeuser").setStyle("display", "");
        $("more_business").setStyle("display", "none");
        $("home_splash").setStyle("background-image", "url(images/backgrounds/consumer_bg.png)");
        businessChain.stop();
        homeuserChain.stop();
        startChain(homeuserChain, 'homeuser');

	});
	
	$("business_tab").addEvent("click", function(event) {
        $("more_homeuser").setStyle("display", "none");
        $("more_business").setStyle("display", "");
        $("home_splash").setStyle("background-image", "url(images/home_business_bg.png)");
        homeuserChain.stop();
        businessChain.stop();
        startChain(businessChain, 'business');

	});
	
	$$("#homeuser_skipintro, #business_skipintro").addEvent("click", function(event) {
		event.stop();
		skipIntro();
	});
}

function skipIntro() {
   $A(["homeuser", "business"]).each(function(prefix) {
       $$("#homeuser_skipintro, #business_skipintro").setStyle("display", "none");			
       var imgs = $("splash_" + prefix).getElements("div");
       imgs.setStyle("opacity", 0);
       imgs.setStyle("display", "none");

       img = $("splash_" + prefix + "_div" + imgs.length);
       img.setStyle("opacity", 1);
       img.setStyle("display", "");
       });
}

function setupFaq() {
   $$(".faq label").each(function(label) {
           label.next = label.getNext();
           label.addEvent("click", function(event) { faqClick(label, event); });
           new Fx.Slide(label.next, { hideOverflow: false }).hide();
       });
		var clickfaq = window.location.hash;
		if ($$(clickfaq)[0]){
			$$(clickfaq)[0].fireEvent("click");
		}
}

function faqClick(label, event) {
   if (label.next) {
       var fx = new Fx.Slide(label.next, { hideOverflow: false });
       fx.toggle();
   }

}

/* Functions from the old web site ********************/

function popUp_video(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=482,height=650');");
}

function NewsUp(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=1,scrollbars=1,location=1,statusbar=0,menubar=0,resizable=1,width=700,height=500');");
}

function toggle_visibility(id) {
  var e = document.getElementById(id);
  if(e.style.display == 'block')
     e.style.display = 'none';
  else
     e.style.display = 'block';
}


/****************************************************/


window.addEvent("domready", function() {

   bindToggles();
   bindTabs();
   markselected();
   bindNavs();
   bindTips();
   if ($("home_splash")) {
       setupAnimations();
       bindHomeTabs();
   }
   if ($("splash_ausplash")){
   		setupAUAnimations();
   		bindAUWatchAgain();
   } 
      if ($("splash_scanim")){
   		setupSCAnimations();
   } 
   setupFaq();
});

window.addEvent("resize", function() {

   bindTabs();
});

