﻿// Extending MooTools 1.2. Basic show / hide / toggle and isVisible
Element.implement({
	show: function () {
		this.setStyle('display', 'block');
	},
	hide: function () {
		this.setStyle('display', 'none');
	},
	toggle: function () {
		this.isVisible() ? this.hide() : this.show();
	},
	isVisible: function () {
		return this.getStyle('display') !== 'none';
	},
	dissolve: function (speed) {
		var el = this, speed = speed || 500;
		el.setStyle('opacity', 1);
		el.set('tween', {
			duration: speed,
			onComplete: function () {
				el.hide();
			}
		});
		el.tween('opacity', 0);
	},
	reveal: function (speed) {
		var el = this, speed = speed || 500;
		el.setStyle('opacity', 0);
		el.set('tween', {
			duration: speed,
			onStart: function () {
				el.show();
			}
		});
		el.tween('opacity', 1);
	}
});

// Basic tab class
var MooTabs = new Class({

	Implements: [Options],

	options: {
		activeTabClass: 'active',
		tabSelector: 'ul li',
		panelSelector: 'div',
		selectedTab: 0,
		rememberTab: false,
		cookieID: ''
	},

	container: 'tabs', // the container id string/element
	tabs: [], // the tab elements
	panels: [], // the panel elements
	cookieName: 'tabFxRemember', // the cookie name (prefix)

	initialize: function (container, options) {

		this.setOptions(options);
		this.container = (container ? $(container) : $(this.container));

		this.cookieName = this.options.cookieID || this.cookieName;
		this.options.selectedTab = this.options.selectedTab || Cookie.read(this.cookieName) || 0;

		if (this.options.rememberTab && !Cookie.read(this.cookieName)) {
			Cookie.write(this.cookieName, this.options.selectedTab);
		}

		this.tabs = this.container.getElements(this.options.tabSelector);
		this.panels = this.container.getElements(this.options.panelSelector);

		if (this.tabs.length != this.panels.length) {
			return false;
		}

		this.panels.each(function (element, index) {
			if (index != this.options.selectedTab)
				element.hide();
		} .bind(this));

		this.tabs.each(function (element, index) {
			element.addEvent('click', function (e) {
				e.stop();
				this.activate(index);
			} .bind(this));

			if (index == this.options.selectedTab)
				element.addClass(this.options.activeTabClass);
		} .bind(this));
	},

	activate: function (tab) {
		this.panels.each(function (element, index) {
			if (element.isVisible() && tab != index) {
				element.hide();
			} else if (tab == index) {
				element.show();
			}
		});

		this.tabs.each(function (element, index) {
			(tab == index) ? element.addClass(this.options.activeTabClass) : element.removeClass(this.options.activeTabClass);
		} .bind(this));

		if (this.options.rememberTab) {
			Cookie.write(this.cookieName, tab);
		}
	}
});

window.addEvent('domready', function () {

	var slides = $('slides');
	if (slides) {
		// Scroll featured games
		var show = new SlideShow(slides, {
			delay: 10000,
			transition: 'fade',
			duration: 500,
			autoplay: false
		});

		show.play();

		$('slideNext').addEvent('click', function (e) {
			e.stop();
			show.pause();
			show.showNext({ transition: 'blindRight' });
		});

		$('slidePrev').addEvent('click', function (e) {
			e.stop();
			show.pause();
			show.showPrevious({ transition: 'blindLeft' });
		});
	}

	var tabOptions = { tabSelector: 'li', panelSelector: '.photo img', selectedTab: 0 };

	if ($('ogBox')) {
		var ogTabs = new MooTabs('ogBox', tabOptions);
		var hlTabs = new MooTabs('hlBox', tabOptions);
		var ffTabs = new MooTabs('ffBox', tabOptions);
		var pzTabs = new MooTabs('pzBox', tabOptions);
	}

});
