﻿// Extending MooTools Element
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);
	},
	pulsate: function (from, to, freq, limit) {
		var from = from || 0, to = to || 1, freq = freq || 500, el = this, counter = 0;
		var tween = new Fx.Tween(el, {
			duration: freq,
			onComplete: function () {
				if (limit && el.getStyle('opacity') == to) {
					counter += 1;
				}
			}
		});
		
		var puls = function () {
			if (!limit || limit > counter) {
				el.getStyle('opacity') == from ? 
					tween.start('opacity', from, to) : tween.start('opacity', to, from);
			}
		}
		
		return puls.periodical(freq);
	}
});

// 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) {
			element.hide();
			
			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);
		}
	}
});