
// Events

var Rules = {
	'#menu li:mouseover': function(element, e) {
		element.addClassName("hover");
	},

	'#menu li:mouseout': function(element, e) {
		element.removeClassName("hover");
    },

    '#leaguemenu li:mouseover': function(element, e) {
        element.addClassName("hover");
    },

    '#leaguemenu li:mouseout': function(element, e) {
        element.removeClassName("hover");
    },

	'div.subcategory a.expandable:click': function(element, e) {
		if (element.hasClassName('subcategoryOnLink')) {
			element.removeClassName('subcategoryOnLink');
			element.up().next().removeClassName('block');
			element.up().next().addClassName('hide');
		}
		else {
			element.addClassName('subcategoryOnLink');
			element.up().next().removeClassName('hide');
			element.up().next().addClassName('block');
		}
	},

	'a#forward_button:click': function(element, e) {
		PhotoViewer.StopSlideshow();
		PhotoViewer.Next();
		Event.stop(e);
	},

	'a#back_button:click': function(element, e) {
		PhotoViewer.StopSlideshow();
		PhotoViewer.Previous();
		Event.stop(e);
	},

	'a#play_button:click': function(element, e) {
		PhotoViewer.PlayPause();
		Event.stop(e);
	}
}

EventSelectors.register(Rules);

Event.observe(window, "load", function() {
    EventSelectors.start();
}, false);


// Client validation functions

var WpsValidator = {

	RequiredCheckbox: function(source, args) {
		args.IsValid = $(source.controltovalidate).checked;
		$(source).display = "None";
	},

	AgeCheck: function(source, args) {
		source = $(source);
		source.display = "None";

		var year = $F(source.controltovalidate + "_year");
		var month = $F(source.controltovalidate + "_month");
		var day = $F(source.controltovalidate + "_day");

		var today = new Date();

		args.IsValid = new Date(today.getFullYear(), today.getMonth(), today.getDate()) > new Date(parseInt(year) + 13, parseInt(month) - 1, parseInt(day) - 1);
	},

	ModuleAgeCheck: function(source, args) {
		args.IsValid = true;
		this.AgeCheck(source, args);

		if (!args.IsValid)
			alert("You must be at least 13 years old.");
	},

	ModuleRequiredCheckbox: function(source, args) {
		this.RequiredCheckbox(source, args);

		if (!args.IsValid)
			alert("You must check 'I certify that I am 13 years or older.'");
	},

	ModuleEmail: function(source, args) {
		source = $F(source.controltovalidate);
		args.IsValid = source.match(/^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$/);
		if (!args.IsValid)
			alert("Email address is not valid.");
	}

}

function HoverPlayer(imgElement, src, url, setElement) {
    
    imgElement.src = src;
    imgElement.parentNode.href = url;

    var topDiv = imgElement.parentNode.parentNode;

    if (topDiv.className == 'thumbWrapperInner') {
        topDiv = topDiv.parentNode;
    }

    var collectionDiv = topDiv.nextSibling;

    if (collectionDiv.tagName != 'DIV') {
        collectionDiv = topDiv.nextSibling.nextSibling;
    }
    
    var divCollection = collectionDiv.getElementsByTagName('DIV');

    for (var i = 0; i < divCollection.length; i++) {

        if (divCollection[i].className == "content topRow") {
            divCollection[i].className = "content";
        }
    }

    var setCollection = setElement.getElementsByTagName('DIV');

    for (var i = 0; i < setCollection.length; i++) {

        if (setCollection[i].className == "content") {
            setCollection[i].className = "content topRow";
        }
    }
}

//MultiTabControl Content Switch
function MultiTabControlToggle(rootItem, itemToShowIndex)
{
   var topDiv = rootItem.parentNode.parentNode.parentNode.parentNode.parentNode;
   
   while(topDiv.previousSibling != null){
        topDiv = topDiv.previousSibling;
   }

   var turnOff = topDiv;
   
   while(turnOff.nextSibling != null){
        if(typeof(turnOff.tagName) != "undefined"){
            turnOff.style.display = 'none';
        }
        turnOff = turnOff.nextSibling;
   }
   
    if(typeof(turnOff.tagName) != "undefined"){
        turnOff.style.display = 'none';
    }
    
   var turnOn = topDiv;
   
   var i = 1;
   var lastOn = false;
   
   while(turnOn.nextSibling != null){
        if(typeof(turnOn.tagName) != "undefined"){
            i++;
            if(Math.floor(( i - (i % 2)) / 2)  == itemToShowIndex + 1){
                turnOn.style.display = 'block';
                lastOn = true;
            }
            else{
                lastOn = false;
            }
        }
        
        turnOn = turnOn.nextSibling;
   }
   
   
    if(typeof(turnOn.tagName) != "undefined" && lastOn){
        turnOn.style.display = 'block';
    }
}

// Countdown

var Countdown = {

	Start: function(endTime) {
		// run it it's own pseudo-thread
		setTimeout(function() { this.DoCountdown(endTime); } .bind(this), 0);
	},

	DoCountdown: function(endTime) {
		if (endTime < new Date())
			return;

		setTimeout(function() { this.DoCountdown(endTime); } .bind(this), 1000);

		var timeLeft = this.CalculateTimeLeft(endTime);
		this.Draw(timeLeft);
	},

	CalculateTimeLeft: function(endTime) {
		var dd = endTime - new Date();
		var days = Math.floor(dd / (60 * 60 * 1000 * 24) * 1);
		var hours = Math.floor((dd % (60 * 60 * 1000 * 24)) / (60 * 60 * 1000) * 1);
		var minutes = Math.floor(((dd % (60 * 60 * 1000 * 24)) % (60 * 60 * 1000)) / (60 * 1000) * 1);
		var seconds = Math.floor((((dd % (60 * 60 * 1000 * 24)) % (60 * 60 * 1000)) % (60 * 1000)) / 1000 * 1);

		return { Days: days, Hours: hours, Minutes: minutes, Seconds: seconds };
	},

	Draw: function(timeLeft) {
		$('tzcd_days').innerHTML = timeLeft.Days;
		$('tzcd_hours').innerHTML = timeLeft.Hours;
		$('tzcd_mins').innerHTML = timeLeft.Minutes;
		$('tzcd_secs').innerHTML = timeLeft.Seconds;
	}

};

var PhotoViewer = {

	CurrentPhoto: null,
	Timer: null,
	SlideShowSpeed: 3,

	Next: function() {
		if (this.CurrentPhoto == null)
			return;

		var nextPhoto = this.ShowPhoto(this.CurrentPhoto, this._getNextPhoto(this.CurrentPhoto));

		if (nextPhoto == null)
			this.ShowPhoto(this.CurrentPhoto, $('image_container').down());
	},

	Previous: function() {
		if (this.CurrentPhoto == null)
			return;

		var previousPhoto = this._getPreviousPhoto(this.CurrentPhoto);

		if (previousPhoto == null) {
			var current = this.CurrentPhoto;
			while (true) {
				var next = this._getNextPhoto(current);
				if (next == null) {
					previousPhoto = current;
					break;
				}

				current = next;
				continue;
			}
		}

		this.ShowPhoto(this.CurrentPhoto, previousPhoto);
	},

	PlayPause: function() {
		if (this.Timer)
			this.StopSlideshow();
		else
			this.StartSlideshow();
	},

	ShowPhoto: function(currentPhoto, nextPhoto) {
		if (nextPhoto == null)
			return null;

		if (currentPhoto != null)
			currentPhoto.hide();

		nextPhoto.show();

		$('current_image_number').innerHTML = nextPhoto.Number;
		$('credits').innerHTML = nextPhoto.Credits;
		
		if ($('photo_date') != null)
			$('photo_date').innerHTML = nextPhoto.Date;
		
		if ($('photo_caption') != null)
			$('photo_caption').innerHTML = nextPhoto.Caption;
		
		this.CurrentPhoto = nextPhoto;

		return nextPhoto;
	},

	StartSlideshow: function() {
		if (this.Timer != null)
			return;

		$('play').addClassName('pause');

		this._continueSlideshow();
	},

	StopSlideshow: function() {
		if (this.Timer == null)
			return;
			
		clearTimeout(this.Timer);
		this.Timer = null;

		$('play').removeClassName('pause');
	},
	
	ShowAt: function(at) {
		if (this.CurrentPhoto == null)
			return;
			
		var firstNode = this.CurrentPhoto;
		
		while(firstNode.previousSibling != null)
		{
		    firstNode = firstNode.previousSibling;
		}
		
		var i = 1;
		
		var showNode = firstNode;
		
		if(showNode.tagName != "IMG")
		{
		     i--;
		}
		
		while(i < at)
		{
		    showNode = showNode.nextSibling;
		    
		    while(showNode.tagName != "IMG")
		    {
		        showNode = showNode.nextSibling;
		    }
		    
		    i++;
		}
		
		this.ShowPhoto(this.CurrentPhoto, showNode);
		
	},

	_getNextPhoto: function(currentPhoto) {
		while (currentPhoto.next() != null && typeof (currentPhoto.next().Number) == "undefined") { currentPhoto = currentPhoto.next(); }
		return currentPhoto.next();
	},

	_getPreviousPhoto: function(currentPhoto) {
		while (currentPhoto.previous() != null && typeof (currentPhoto.previous().Number) == "undefined") { currentPhoto = currentPhoto.previous(); }
		return currentPhoto.previous();
	},

	_continueSlideshow: function() {
		this.Timer = setTimeout(function() { this._runSlideshow() } .bind(this), this.SlideShowSpeed * 1000);
	},

	_runSlideshow: function() {
		this.Next();
		this._continueSlideshow();
	}
}
