﻿/*global _gat, Animate *//*jslint browser: true, nomen: false, plusplus: true, undef: true, white: true */

document.addEvent('ready', function () {
    if (document.get('admin') && document.getElementsByTagName('form').length > 0) {
        var i, input, inputs = document.get('input[value="Delete"]') || [], click = function (event) {
            if (!confirm('Are you sure you want to delete this item?\nOnce complete, this operation cannot be undone.')) {
                event.preventDefault();
            }
        };
        for (i = 0; (input = inputs[i]); i += 1) {
            input.addEvent('click', click);
        }
    }
});
document.addEvent('ready', function () {
    if (document.get('admin') && document.getElementsByClassName('manage').length > 0) {
        var i, input, inputs = document.get('input[type="checkbox"]') || [];
        for (i = 0; (input = inputs[i]); i += 1) {
			input.addEvent('click', function () {
				if (this.getProperty('checked')) {
			        document.get('manage' + this.getProperty('value')).removeClass('hide');
				} else {
			        document.get('manage' + this.getProperty('value')).addClass('hide');
				}
			});
        }
    }
});
document.addEvent('ready', function () {
    if (document.get('admin') && document.getElementsByTagName('form').length > 0) {
        var i, input, inputs = document.get('input[value="Upload"]') || [], click = function (event) {
            if (!confirm('Are you sure you want to replace this item?\nOnce complete, this operation cannot be undone.')) {
                event.preventDefault();
            }
        };
        for (i = 0; (input = inputs[i]); i += 1) {
            input.addEvent('click', click);
        }
    }
});
document.addEvent('ready', function () {
    var i, link, links = document.links;
    for (i = 0; (link = links[i]); i += 1) {
		if ((!(/depts\.washington\.edu\/psoweb/).test(link.href) && (/^http(s)?:/).test(link.href)) || (link.rel == 'external')) {
			link.setProperty('target', '_blank').setProperty('title', 'Link opens in new window');
        }
    }
});
var Slideshow = function (container, interval, pause) {
    this.initialize(container, interval, pause);
}.inherit({
    initialize: function (container, interval, pause) {
        this.container = document.getElementById(container);
        if (!this.container) {
            return;
        }
        this.interval = interval || 2000;
        if (pause) {
            this.container.addEvent('mouseover', function () {
                clearTimeout(this.slideshow);
            }.bind(this)).addEvent('mouseout', function () {
                this.slideshow = setTimeout(function () {
                    this.fade();
                }.bind(this), this.interval);
            }.bind(this));
        }
        this.current = 0;
        this.images = this.container.getElementsByTagName('img');
        this.images[0].style.display = 'block';
        this.slideshow = setTimeout(function () {
            this.fade();
        }.bind(this), this.interval);
    },
    fade: function () {
        clearTimeout(this.slideshow);
        var current = this.current, fadeIn, fadeOut, images = this.images, next = images[current + 1] ? current + 1 : 0;
        images[next].style.display = 'block';
        fadeIn = new Animate(images[next], 'opacity', Animate.easeIn, 0, 100, 1, '');
        fadeOut = new Animate(images[current], 'opacity', Animate.easeIn, 100, 0, 1, '');
        fadeOut.onfinish = function () {
            images[current].style.display = 'none';
            this.current = next;
            this.slideshow = setTimeout(function () {
                this.fade();
            }.bind(this), this.interval);                 
        }.bind(this);
        fadeIn.start();
        fadeOut.start();
    }
});
document.addEvent('ready', function () {
    if (document.getElementById('home')) {
		var i, slides = document.set('div', {'id': 'slides'});
		for (i = 1; i < 12; i += 1) {
			slides.appendChild(document.set('img', { 'alt': 'University of Washington Professional Staff Organization', 'src': ('/psoweb/assets/img/slides/' + ((i > 9) ? i : '0' + i) + '.jpg') }));
		}
		document.get('header').appendChild(slides);
		new Slideshow('slides', 3000, false);
	}			 
});
document.addEvent('ready', function () {
    var animate = function (event) {
		var close, height = heights[this.getProperty('rel')], open, target = this.parentNode.getElementsByTagName('ul')[0];
		if (target.offsetHeight === 0) {
			open = new Animate(target.style, 'height', Animate.easeOut, 0, height, 1.0, 'px');
			if (active) {
				close = new Animate(active.style, 'height', Animate.easeIn, heights[active.parentNode.getElementsByTagName('a')[0].getProperty('rel')], 0, 0.5, 'px');
				close.onfinish = function () {
			        open.start();
				}
				close.start();
			} else {
			    open.start();
			}
			active = target;
		} else {
			close = new Animate(target.style, 'height', Animate.easeIn, height, 0, 0.5, 'px');
			close.start();
			active = null;
		}
		event.preventDefault();
	}, active = null, heights = { 'about': 185, 'events': 43, 'publications': 107 }, i, link, links = document.get('#menu li a[rel]');
	for (i = 0; (link = links[i]); i += 1) {
		link.addEvent('click', animate);
		if (document.getElementById(link.getProperty('rel'))) {
			active = document.get('#menu li.' + link.getProperty('rel') + ' ul')[0];
		}
	}
});
document.addEvent('ready', function () {
	var expand = function (event) {
		var animate, target = document.get(this.getProperty('href').replace('#', '')), link = this;
		if (target.offsetHeight === 0) {
			animate = new Animate(target.style, 'height', Animate.easeOut, 0, target.defaultHeight, 1.0, 'px');
			animate.start();
			this.setProperty('text', 'Hide Bio');
		} else {
			animate = new Animate(target.style, 'height', Animate.easeIn, target.offsetHeight, 0, 0.5, 'px');
			animate.start();
			animate.onfinish = function () {
				link.setProperty('text', 'View Bio');
			};
		}
		event.preventDefault();
	}, i, link, links = document.get('a[rel="bio"]') || [], target;
	for (i = 0; (link = links[i]); i += 1) {
		target = document.get(link.getProperty('href').replace('#', ''));
		target.defaultHeight = target.offsetHeight + 12;
		target.setProperty('style', 'height:0px;overflow:hidden;');
		link.addEvent('click', expand);
	}
});
document.ready();
try {
    var pageTracker = _gat._getTracker('UA-7220825-1');
    pageTracker._trackPageview();
} catch (e) {}