/*
Written by: Adam Crownoble (adam@bryan.edu)
Date: April 3 2006
License: LGPL (http://www.gnu.org/copyleft/lesser.html)
Dependencies: mootools (mootools.net)
*/

var DropDownMenu = new Class({

 initialize: function(menuElement) {

  this.menu = menuElement;
  this.id = menuElement.id;
  this.duration = 400;
  this.buttons = [];


  $A(this.menu.getElementsByTagName('li')).each(
   function(li) {
    if(li.parentNode == menuElement) {this.buttons.push($(li));}}.bind(this)
  );
  this.submenus = $A(this.menu.getElementsByTagName('ul'));

  this.submenus.each(
   function(submenu) {
    submenu = $(submenu);
    submenu.setStyle('display','');
    submenu.originalHeight = submenu.offsetHeight;
    submenu.effect = new Fx.Style(submenu, 'height', {
    	duration: this.duration,
    	transition:Fx.Transitions.quartOut
    });
    submenu.effect.set(0);
   }.bind(this)
  );

  this.buttons.each(
   function(button) {
    button.addEvent('mouseover',this.expand.bindAsEventListener(this));
    button.addEvent('mouseout', this.collapse.bindAsEventListener(this));
   }.bind(this)
  );

 },

 findButton: function(element) {
  var button = false;
  while(element.parentNode) {
   if(this.buttons.test(element)) {
   	button = element;
   }
   element = element.parentNode;
  }
  return button;
 },

 findSubmenu: function(element) {
  var button = this.findButton(element);
  var submenu = button.getElementsByTagName('ul')[0];
  return submenu;
 },

 expand: function(event) {
  var submenu = this.findSubmenu(event.target || event.srcElement);
  if(submenu) {
   submenu.effect.clearTimer();
   submenu.effect.start(submenu.originalHeight);
  }
 },

 collapse: function(event) {
  var submenu = this.findSubmenu(event.target || event.srcElement);
  if(submenu) {
   submenu.effect.clearTimer();
   submenu.effect.start(0);
  }
 }

});

/*
Written by: Adam Khan (adam@engaging.net)
Dependencies: mootools (mootools.net)
*/

function erase(box) 
{
   if (box.defaultValue == "Username" || box.defaultValue == "Password" )
   box.value = "";
}


var imageSuffix="png";
if (browser == "ie") imageSuffix="gif";

elementColorUse = "#" + elementColor;
waterColorUse = "#011c41";
earthColorUse = "#c7512d";
airColorUse = "#023E87";
thisElement = element;
helptabstatus = "closed";

holders = new Array();
mapclicks="0";

function supportMover()
{
	var firetabHeight = $('fire').offsetHeight;
	var bleederHeight = $('bleeder').offsetHeight;
	if ($('content-inner2'))
	{
		var contentInnerHeight = $('content-inner').offsetHeight;
	}
	diffHeight = firetabHeight - bleederHeight;
	useHeight = diffHeight;
	if (browser != "ie" && browser != "ie6" ) useHeight = useHeight + 55;
	if (browser == "ie" ) useHeight = useHeight + 55;
	if (browser == "ie6" ) useHeight = useHeight + 10;
	$('support').style.marginTop = useHeight + "px";
}

function tabHeighter(container)
{
	var tabMaxHeight = "0";
	var tabs = $(container).getElementsByTagName('div');
	for (var i=0; i<tabs.length; i++)
	{
		if ( (tabs[i].className.indexOf('bd-c') !=-1) )
		{
			if (tabs[i].offsetHeight > tabMaxHeight) tabMaxHeight = tabs[i].offsetHeight;
		}
	}
	tabMaxHeight = tabMaxHeight - 0;
	if (browser == "ie") tabMaxHeight = tabMaxHeight + 0;
	for (var i=0; i<tabs.length; i++)
	{
		if ( (tabs[i].className.indexOf('bd-c') !=-1) )
		{
			if (tabs[i].offsetHeight < tabMaxHeight) tabs[i].style.height = tabMaxHeight + "px";
		}
	}
}

function baattyHeighter()
{
	var tabMaxHeight = "0";
	baatts = new Array();
	if ($('Water-baatty')) baatts[0] = $('Water-baatty');
	if ($('Earth-baatty')) baatts[1] = $('Earth-baatty');
	if ($('Air-baatty')) baatts[2] = $('Air-baatty');
	for (var i=0; i<baatts.length; i++)
	{
		if ( (baatts[i].className.indexOf('bd-c') !=-1) )
		{
			if (baatts[i].offsetHeight > tabMaxHeight) tabMaxHeight = baatts[i].offsetHeight;
		}
	}
	tabMaxHeight = tabMaxHeight - 0;
	if (browser == "ie") tabMaxHeight = tabMaxHeight + 0;
	for (var i=0; i<baatts.length; i++)
	{
		if ( (baatts[i].className.indexOf('bd-c') !=-1) )
		{
			if (baatts[i].offsetHeight < tabMaxHeight) baatts[i].style.height = tabMaxHeight + "px";
		}
	}
}

function tabWidther(container)
{
	var thiscontainer = document.getElementById(container);
	var tabs = thiscontainer.getElementsByTagName('div');
	for (var i=0; i<tabs.length; i++)
	{
		if ( (tabs[i].className.indexOf('bd-s') !=-1) )
		{
			firstline = new Array();
			secondline = new Array();
			firstlineuse="";
			secondlineuse="";
			disparity = new Array();
			texttraversed="";
			text = tabs[i].innerHTML;
			words = text.split(' ');
			textremaining = text;
			usethislayout = 0;
			usethisdisparity = 999;
			firstlineuse = text;
			longerline = firstlineuse;
			for (var j=0; j<words.length; j++)
			{
				if ( (textremaining.length > texttraversed.length) && (j+1) < words.length)
				{
					wordlength = words[j].length;
					wordlength = wordlength + 1;
					texttraversed = texttraversed + words[j] + " ";
					textremaining = textremaining.substring(wordlength);
					textremaininglength = textremaining.length;
					firstline[j] = texttraversed;
					secondline[j] = textremaining;
					firstlinelength = firstline[j].length;
					secondlinelength = secondline[j].length;
					disparity[j] = firstlinelength - secondlinelength;
					if (disparity[j] < 0)
					{
						disparity[j] = disparity[j] * -1;
					}

					if (disparity[j] < usethisdisparity)
					{
						usethisdisparity = disparity[j];
						usethislayout = j;
					}
				}
			}
			if (words.length > 1)
			{
				firstlineuse = firstline[usethislayout];
				secondlineuse = secondline[usethislayout];
				if (secondlineuse != "" && firstlineuse != "" )
				{
					firstlineuse = firstlineuse.slice(0,-1); 
				}
				tabs[i].innerHTML = firstlineuse + "<br />" + secondlineuse;
				longerline = secondlineuse;
				if (firstlineuse.length > secondlineuse.length) 
				{
					longerline = firstlineuse;
				}
			}
			longerlinelength = (longerline.length)/1.5;
			tabs[i].style.width = longerlinelength + "em";
			divname = (tabs[i].id);
			divnamesliced = divname.slice(0,-5);
			tab = document.getElementById(divnamesliced + "-tab");
			tabwidth = longerlinelength + 2;
			tab.style.width = tabwidth + "em";
		}
	}
}

function initMenus()
{
	if(DropDownMenu && $('elements-menu')) 
	{
		 var elementsMenu = new DropDownMenu($('elements-menu'));
	}

	if(DropDownMenu && $('competitions-menu')) 
	{
		 var elementsMenu = new DropDownMenu($('competitions-menu'));
	}

	if(DropDownMenu && $('destinations-menu')) 
	{
		var destinationsMenu = new DropDownMenu($('destinations-menu'));
	}

	if(DropDownMenu && $('servicess-menu')) 
	{
		var servicesMenu = new DropDownMenu($('servicess-menu'));
	}

	if(DropDownMenu && $('tv-menu')) 
	{
		 var elementsMenu = new DropDownMenu($('tv-menu'));
	}
}
  			
function mapWorks()
{
	var spacemakerSlide = new Fx.Slide('spacemaker', {
		duration: 500,
		transition: Fx.Transitions.circInOut
	});
	var mapSlide = new Fx.Slide('map', {
		duration: 500,
		transition: Fx.Transitions.circInOut
	});
			
	$('map-tab').addEvent('click', function(e)
	{
		e = new Event(e);
		mapclicks++;
		if (mapclicks == 1)
		{startMap();}

		spacemakerSlide.toggle();
		mapSlide.toggle();
		e.stop();
	});

	mapSlide.hide();
	spacemakerSlide.hide();
	$('map').setStyle('height', '350px');
	$('spacemaker').setStyle('height', '225px');
}

function switchCurrentTabOff(area)
{
	$E('div.tab-on', area	).toggleClass('tab-on');
/*
	$E('div.hd-on', area	).toggleClass('hd-on');
	$E('div.hd-c-on', area	).toggleClass('hd-c-on');
	$E('div.bd-on', area	).toggleClass('bd-on');
	$E('div.bd-c-on', area	).toggleClass('bd-c-on');
	$E('div.bd-s-on', area	).toggleClass('bd-s-on');
*/
}

function switchThisTabOn(tabname)
{
	$(tabname + '-tab'	).toggleClass('tab-on');
/*
	$(tabname + '-hd'	).toggleClass('hd-on');
	$(tabname + '-hd-c'	).toggleClass('hd-c-on');
	$(tabname + '-bd'	).toggleClass('bd-on');
	$(tabname + '-bd-c'	).toggleClass('bd-c-on');
	$(tabname + '-bd-s'	).toggleClass('bd-s-on');
*/
}

function thumbSwap(label, sourceSection)
{
	if ($(label + '-pic')) 
	{
		thumb = $(label + '-pic').getProperty('src');
		thumbarray = thumb.split('-s.');
		if (thumbarray[0] != thumb)
		{
			picWithPath = thumbarray[0] + ".jpg";
			$(label + '-pic').setProperty('src', picWithPath);
		}
	}
}

function captionSet(label, sourceSection)
{
	picWithPath = $(label + '-pic').getProperty('src');
	picarray = picWithPath.split('http://www.extremeelements.tv/photos/');
	picWithDir = picarray[1];
	finalpicarray = picWithDir.split('/');
	picurltitle = finalpicarray[1];
	picurltitle = picurltitle.toLowerCase();
	picurltitlearray = picurltitle.split('.jpg');
	picurltitle = picurltitlearray[0] + "jpg";
	retrieveContent(picurltitle + "-caption", sourceSection, 'caption', picurltitle, '', '', '', 'caption-outer');
}
function captionSet2( label, sourceSection, galleryFirstImg )
{
	picWithPath = $(label + '-pic').getProperty('src');
	picarray = picWithPath.split('http://www.extremeelements.tv/photos/');
	picWithDir = picarray[1];
	finalpicarray = picWithDir.split('/');
	picurltitle = finalpicarray[1];
	picurltitle = picurltitle.toLowerCase();
	picurltitlearray = picurltitle.split('.jpg');
	picurltitle = picurltitlearray[0] + "jpg";
	retrieveContent( galleryFirstImg + "-caption", sourceSection, 'caption', galleryFirstImg, '', '', '', 'caption-outer');
}

function setHolder(label, destination, method)
{
	if (method == "initial")
	{thumbSwap(label);}
	holders[label] = $(destination).innerHTML;
}

function retrieveContent(label, sourceSection, sourceTemplate, sourceEntry, callerSection, callerTemplate, callerEntry, destination, date, place, galleryFirstImg)
{
	if (!callerSection)
	{
		callerSection = sourceSection;
	}
	if (!callerTemplate)
	{
		callerTemplate = "index";
	}
	if (!callerEntry)
	{
		callerEntry = sourceEntry;
	}
	type="";
	if (destination == "content-inner" || destination == "competition-content-inner" || destination == "baatty-holder")
	{
		type = "tab";
	}
	if (type == "tab")
	{
		if (destination == "baatty-holder")
		{switchCurrentTabOff('taabbyset');}
		else
		{switchCurrentTabOff('bleeder');}
		switchThisTabOn(label);
	}

	url = "http://www.extremeelements.tv/" + sourceSection + "/" + sourceTemplate + "/" + sourceEntry + "/" + callerSection + "/" + callerTemplate + "/" + callerEntry;
		// alert(url);
	
	if (!holders[label])
	{
		if ($('tych'))
		{$('tych').addClass('ajax-loading');}
		if ($('menu'))
		{$('menu').addClass('ajax-loading');}
		if ($('ajax-loading'))
		{$('ajax-loading').addClass('ajax-loading');}
		if ($('triptych-holder'))
		{$('triptych-holder').addClass('ajax-loading');}
		new Ajax(url, {
			method: 'get',
			update: $(destination),
			onComplete: function() 
			{
				postRetrieveContent(label, sourceSection, sourceTemplate, sourceEntry, callerSection, callerTemplate, callerEntry, destination, date, place);
				setHolder(label, destination);
				if ($('tych'))
				{$('tych').removeClass('ajax-loading');}
				if ($('menu'))
				{$('menu').removeClass('ajax-loading');}
				if ($('triptych-holder'))
				{$('triptych-holder').removeClass('ajax-loading');}}
		}).request();
	}
	else
	{
		$(destination).innerHTML = holders[label];
		if (sourceTemplate == "offer-bat")
		{
			thumbSwap(label, 'offers');
		}
		if (type == "tab")
		{
			if (callerSection == "competitions" || callerSection == "competition")
			{
				// captionSet(date, sourceSection);
				captionSet2( date, sourceSection, galleryFirstImg );
				if ($(label + '-destination-box') && place)
				{
					retrieveContent(label + '-destination-box', 'competitions', 'destination-box/locations', place, '', '', '', label + '-destination-box');
				}
			}
			if (callerSection == "hotels")
			{
				retrieveContent('hotel-offers', 'hotels', 'hotel-offers', '', '', '', '', 'hotel-offers');
			}
			if (callerSection == "centers")
			{
				retrieveContent('center-offers', 'centers', 'center-offers', '', '', '', '', 'center-offers');
			}
		}
	}
}

function postRetrieveContent(label, sourceSection, sourceTemplate, sourceEntry, callerSection, callerTemplate, callerEntry, destination, date, place)
{
	if (destination == "triptych-inner")
	{
		// baattyHeighter();
	}
	if (sourceTemplate == "offer-bat")
	{
		thumbSwap(label, 'offers');
	}
	if (sourceTemplate == "locatych" || sourceTemplate == "sportych")
	{
		tabWidther('taabbyset');
		tabHeighter('taabbyset');
	}
	if (type == "tab")
	{
		if (callerSection == "competitions" || callerSection == "competition")
		{
			thumbSwap(date, sourceSection);
			captionSet(date, sourceSection);
			if ($(label + '-destination-box') && place)
			{
				retrieveContent(label + '-destination-box', 'competitions', 'destination-box/locations', place, '', '', '', label + '-destination-box');
			}
		}
		if (callerSection == "destinations")
		{
			if (sourceTemplate == "location-baatty")
			{
				thumbSwap(label, sourceSection);
			}
		}
		if (callerSection == "hotels")
		{
			retrieveContent('hotel-offers', 'hotels', 'hotel-offers', '', '', '', '', 'hotel-offers');
		}
		if (callerSection == "centers")
		{
			retrieveContent('center-offers', 'centers', 'center-offers', '', '', '', '', 'center-offers');
		}
	}
}

function galleryClick(picTitle, entrydate, thiscompetition, picUrltitle)
{
	retrieveContent(picUrltitle + '-caption', 'competition-show', 'caption', picUrltitle, 'tv', 'commentary-gallery', picUrltitle, 'caption-outer');
	picDisplay = document.getElementById(entrydate + '-pic');
	picDisplay.src = "http://www.extremeelements.tv/photos/" + thiscompetition + "/" + picTitle;
}

function toggleVisible(id) 
{
	var item = document.getElementById(id);
	var value = item.style.display ? '' : 'none';
	item.style.display = value;
}

function init2()
{
	tabWidther('bleeder'); tabHeighter('bleeder'); supportMover(); initMenus(); mapWorks();
}

function initCompetitions()
{
	supportMover(); initMenus(); mapWorks();
}

function unload()
{
	holders = "";
}


function openWindow( url ) {
   window.open( url )
}