/**
 * SimpleTabs - Unobtrusive Tabs with Ajax
 *
 * @version		1.0
 *
 * @license		MIT License
 * @author		Harald Kirschner <mail [at] digitarald.de>
 * @copyright	2007 Author, zmiany stryc
 */

var Cookies = {
    init: function () 
    {
	var allCookies = document.cookie.split('; ');
	for (var i=0;i<allCookies.length;i++) 
	{
	    var cookiePair = allCookies[i].split('=');
	    this[cookiePair[0]] = cookiePair[1];
	}
    },
    create: function (name,value,days) 
    {
        if (days) 
        {
    	var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();																}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
	this[name] = value;
	},
    erase: function (name) 
    {
        this.create(name,'',-1);
	this[name] = undefined;
    }
};

Cookies.init();
 
var SimpleTabs = new Class({

	Implements: [Events, Options],

	/**
	 * Options
	 */
	options: {
		efekt : 'none',
		show: 0,
		selector: '.tab-tab',
		classWrapper: 'tab-wrapper',
		classMenu: 'tab-menu',
		classContainer: 'tab-container',
		menuPlace : 0,	// 0- before content, 1 - after content
		animationSpeed : 0,	//0 no animation,
		onSelect: function(toggle, container, index, e) {
			toggle.addClass('tab-selected');
			container.setStyle('display', '');
			if ($(document.body.get('id')) && $(document.body.get('id')).getStyle('unicode-bidi') == 'embed') { // efekt tylko na froncie
				switch (this.options.efekt) {
					case 'fade':
						container.tween('opacity','1');
					break;
					case 'morph':
						if (container.getParent().getParent().hasClass("rotatorGlownyContener")) {
							container.morph({height: 170});
						} else {
							container.morph({height: 140});
						}
						
						break;
					case 'none':
						break;
					default:
						//container.tween('opacity','1');
						container.fade('in');
				}
			}
			
			if(this.options.selector == 'h2')
			{
				Cookies.create("zakladka", index, 3);
			}	
		},
		onDeselect: function(toggle, container, index) {
			toggle.removeClass('tab-selected');
			if ($(document.body.get('id')) && $(document.body.get('id')).getStyle('unicode-bidi') == 'embed') { // efekt tylko na froncie
				switch (this.options.efekt) {
					case 'fade':
						container.fade('out');
					break;
					case 'morph':
						container.morph({height: 0});
						break;	
					case 'none':
						break;
					default:
						container.tween('opacity','0');
				}
			}
			container.setStyle('display', 'none');
		},
		onRequest: function(toggle, container, index) {
			container.addClass('tab-ajax-loading');
		},
		onComplete: function(toggle, container, index) {
			container.removeClass('tab-ajax-loading');
		},
		onFailure: function(toggle, container, index) {
			container.removeClass('tab-ajax-loading');
		},
		onAdded: Class.empty,
		getContent: null,
		ajaxOptions: {},
		cache: true
		
	},

	/**
	 * Constructor
	 *
	 * @param {Element} The parent Element that holds the tab elements
	 * @param {Object} Options
	 */
	initialize: function(element, options) 
	{
		this.element = $(element);
		this.setOptions(options);
		this.selected = null;
		this.build();
	},

	build: function() {
		this.tabs = [];
		this.menu = new Element('ul', {'class': this.options.classMenu});
		this.wrapper = new Element('div', {'class': this.options.classWrapper});

		this.element.getElements(this.options.selector).each(function(el) {
			var content = el.get('href') || (this.options.getContent ? this.options.getContent.call(this, el) : el.getNext());
			this.addTab(el.innerHTML, el.title || el.innerHTML, content);
		}, this);
		
		if (this.options.menuPlace==0)	{
			this.element.empty().adopt(this.menu,this.wrapper);
			} else {
			this.element.empty().adopt(this.wrapper, this.menu);
			}

		if (this.tabs.length) this.select(this.options.show);
	},

	/**
	 * Add a new tab at the end of the tab menu
	 *
	 * @param {String} inner Text
	 * @param {String} Title
	 * @param {Element|String} Content Element or URL for Ajax
	 */
	addTab: function(text, title, content) {
		var grab = $(content);
		var container = (grab || new Element('div'))
			.setStyle('display', 'none')
			.addClass(this.options.classContainer)
			.inject(this.wrapper);
		var pos = this.tabs.length;
		var evt = (this.options.hover) ? 'mouseenter' : 'click';
		var tab = {
			container: container,
			toggle: new Element('li').grab(new Element('a', {
				href: '#',
				title: title
			}).grab(
				new Element('span', {html: text})
			)).addEvent(evt, this.onClick.bindWithEvent(this, [pos])).inject(this.menu)
		};
		if (!grab && $type(content) == 'string') tab.url = content;
		this.tabs.push(tab);
		return this.fireEvent('onAdded', [tab.toggle, tab.container, pos]);
	},

	onClick: function(evt, index) {
		this.select(index);
		return false;
	},

	/**
	 * Select the tab via tab-index
	 *
	 * @param {Number} Tab-index
	 */
	select: function(index) {
		if (this.selected === index || !this.tabs[index]) return this;
		if (this.ajax) this.ajax.cancel().removeEvents();
		var tab = this.tabs[index];
		var params = [tab.toggle, tab.container, index];
		if (this.selected !== null) {
			var current = this.tabs[this.selected];
			if (this.ajax && this.ajax.running) this.ajax.cancel();
			params.extend([current.toggle, current.container, this.selected]);
			this.fireEvent('onDeselect', [current.toggle, current.container, this.selected]);
		}

		this.fireEvent('onSelect', params);
		if (tab.url && (!tab.loaded || !this.options.cache)) {
			this.ajax = this.ajax || new Request.HTML();
			this.ajax.setOptions({
				url: tab.url,
				method: 'get',
				update: tab.container,
				onFailure: this.fireEvent.pass(['onFailure', params], this),
				onComplete: function(resp) {
					tab.loaded = true;
					this.fireEvent('onComplete', params);
				}.bind(this)
			}).setOptions(this.options.ajaxOptions);
			this.ajax.send();
			this.fireEvent('onRequest', params);
		}
		this.selected = index;
		return this;
	}

});
