/*
Script: tabs.class.js
	Contains <Tabs>

Author:
	Olivier Gasc, <jahjah@mibhouse.org>

License:
	MIT-style license.

*/

/*
Class: tab system class

Arguments:
	sInitUrl		: url to load first (or when all tabs are closed)
	sPagesDir		: the pages' directory
	oTransition		: the transition to play with the effect
*/

var Tabs = new Class({
	options:
	{
		sInitUrl		: 'hockey.php',
		sPagesDir		: '',
		oTransition 	: Fx.Transitions.linear
	},

    /**
	 * Initialization class
	 */
	initialize: function(options)
    {
		this.setOptions(options);

		// Init vars for id names
		this.sUpdateContent		= 'tabsContent'; 	// Id of the content div
		this.sUpdateIndex		= 'tabsIndex'; 		// Id of the index
		this.sUpdateInIndex		= 'inTabsIndex';	// Id of each index button
		this.sLoading			= 'tabsLoading';	// Id of the loading div

		// Init vars for class names
		this.sClassContent		= 'TabsContent';	// ClassName of the content div
		this.sClassInContent	= 'TabsInContent';	// ClassName of each page in content
		this.sClassIndex		= 'TabsIndex';		// ClassName of the index
		this.sClassInIndex		= 'InTabsIndex';	// ClassName of each index button
		this.sClassTitle		= 'TabsTitle';		// ClassName of each page's title (used for index)
		this.sClassLoading		= 'TabsLoading';	// ClassName of the loading div

		window.addEvent('domready',function(){
			// Divs containers
			new Element('div', {
				'id'	: this.sUpdateContent,
				'class'	: this.sClassContent
			}).inject(document.body);

			new Element('div', {
				'id'	: this.sUpdateIndex,
				'class'	: this.sClassIndex
			}).inject(document.body);

			// The effect mustbe played ?
			this.bDisplayEffect = true;

			// the effect
			this.oFxSlide = new Fx.Slide(this.sUpdateContent,
			{
				transition: this.options.oTransition,
				duration: 500,
				wait: false
			});

			// If a front page is defined, load it
			if (this.options.sInitUrl)
				this.load(this.options.sInitUrl);
		}.bind(this));
    },

    /**
	 * Load a page
	 */
	load: function(sIdcurrentPage, mParams, sMethod, bReload)
	{
		this.showLoading();

		// Fixes the method of query
		var sTestMethod = 'get';

        if ($defined(sMethod) && sMethod == 'post')
			sTestMethod = 'post';

		// Check the params
		var sTypeParam = $type(mParams);

		if (sTypeParam != 'element' && sTypeParam != 'object' && sTypeParam != 'string')
			mParams = '';

		this.sIdcurrentPage = sIdcurrentPage;

		// Hide all the other pages
		this.hide();
		this.unFocusIndex();

		// Load/Show the page
		if (!$(this.sIdcurrentPage)) {
			new Ajax(this.options.sPagesDir + this.sIdcurrentPage, {
				method		: sTestMethod,
				data        : mParams,
				evalScripts	: true,
				onclick		: this.clear_running(this.sIdcurrentPage),
				onComplete	: this.loadComplete.bind(this)
			}).request();
		} else {
            if ($defined(bReload)) {
                this.reload(sIdcurrentPage, mParams, sMethod, true);
                
            } else {
                $(this.sIdcurrentPage).style.display = 'block';
				/*
                if (this.bDisplayEffect) {
                //    this.oFxSlide.hide();
                //    this.oFxSlide.slideIn();
                }
				*/
                this.hideLoading();

                this.focusIndex(this.sIdcurrentPage);
            }

		}
	},
	clear_running:function(nopage)
	{
		//alert(nopage);
		
		if(nopage=='hockey_feed.php?status=all')
		{
			if(typeof(show_live)=="number")
			{
				clearInterval(show_live);
				CTabs.remove2('hockey_feed.php?status=live')								
			} 
			if(typeof(show_fin)=="number")
			{
				clearInterval(show_fin);
				CTabs.remove2('hockey_feed.php?status=fin')								
			}
			if(typeof(show_sched)=="number")
			{
				clearInterval(show_sched);
				CTabs.remove2('hockey_feed.php?status=sched')								
			}			
		}
		if(nopage=='hockey_feed.php?status=live')
		{
			if(typeof(show_all)=="number")
			{
				clearInterval(show_all);
				CTabs.remove2('hockey_feed.php?status=all')								
			} 
			if(typeof(show_fin)=="number")
			{
				clearInterval(show_fin);
				CTabs.remove2('hockey_feed.php?status=fin')								
			}
			if(typeof(show_sched)=="number")
			{
				clearInterval(show_sched);
				CTabs.remove2('hockey_feed.php?status=sched')								
			}						
		}
		if(nopage=='hockey_feed.php?status=fin')
		{
			if(typeof(show_all)=="number")
			{
				clearInterval(show_all);
				CTabs.remove2('hockey_feed.php?status=all')								
			} 
			if(typeof(show_live)=="number")
			{
				clearInterval(show_live);
				CTabs.remove2('hockey_feed.php?status=live')								
			}
			if(typeof(show_sched)=="number")
			{
				clearInterval(show_sched);
				CTabs.remove2('hockey_feed.php?status=sched')								
			}						
		}
		if(nopage=='hockey_feed.php?status=sched')
		{
			if(typeof(show_all)=="number")
			{
				clearInterval(show_all);
				CTabs.remove2('hockey_feed.php?status=all')								
			} 
			if(typeof(show_fin)=="number")
			{
				clearInterval(show_fin);
				CTabs.remove2('hockey_feed.php?status=fin')								
			}
			if(typeof(show_live)=="number")
			{
				clearInterval(show_live);
				CTabs.remove2('hockey_feed.php?status=live')								
			}						
		}
		
	},	
    /**
	 * Fired when page's loaded, adds the page and builds the index
	 */
	loadComplete: function(sRequest)
	{
		// Container
		var oDiv = new Element('DIV', {
			'id'	: this.sIdcurrentPage,
			'class'	: this.sClassInContent
		}).inject($(this.sUpdateContent));

		oDiv.innerHTML = sRequest;

        // Effect
	//	this.oFxSlide.hide();
    //  this.oFxSlide.slideIn();

		// Built the tab index
		this.addIndex(this.sIdcurrentPage);
		this.focusIndex(this.sIdcurrentPage);
		this.hideLoading();
	},

    /**
	 * Reload the page
	 */
	reload: function(sIdPage, mParams, sMethod, bShow)
	{
		this.showLoading();

		// Fixes the method of query
		var sTestMethod = 'get';

        if ($defined(sMethod) && sMethod == 'post')
			sTestMethod = 'post';

		// Check the params
		var sTypeParam = $type(mParams);
		if (sTypeParam != 'element' && sTypeParam != 'object' && sTypeParam != 'string')
			mParams = '';
		this.sIdPageReloaded    = sIdPage;
		this.bShow              = bShow;

		if ($(sIdPage)) {
			new Ajax(this.options.sPagesDir + this.sIdPageReloaded, {
				method		: sTestMethod,
				data        : mParams,
				evalScripts	: true,				
				onComplete	: this.reloadComplete.bind(this)
			}).request();
		} else {
			this.load(sIdPage);
		}
	},

    /**
	 * Fired when page's reloaded, replaces the page
	 */
	reloadComplete: function(sRequest)
	{
		$(this.sIdPageReloaded).innerHTML = sRequest;

		// If the page is the currently displayed one, play load effect
		if (this.sIdPageReloaded == this.sIdcurrentPage) {
		//	this.oFxSlide.hide();
		//	this.oFxSlide.slideIn();
		}

        if ($defined(this.bShow)) {
            this.focusIndex(this.sIdcurrentPage);
            this.hide();
            this.show(this.sIdPageReloaded);
		//	this.oFxSlide.hide();
		//	this.oFxSlide.slideIn();
        }

		this.hideLoading();
	},

    /**
	 * Hide all the pages
	 */
	hide: function() {
		var aContent = $ES('div.' + this.sClassInContent, this.sUpdateContent);

		aContent.each(function(oDiv) {
			oDiv.style.display = 'none';
		});
	},

    /**
	 * Show a page
	 */
	show: function(sIdPage) {
		$(sIdPage).style.display = 'block';
	},

    /**
	 * Removes a page, if no more pages exist, load the init page
	 */
	remove: function(sIdPage)
	{
		// Removes the page
		if($(sIdPage)) {
			$(sIdPage).remove();
			this.removeIndex(sIdPage);
		}

		// If other pages are open, opens the last one
		if ($(this.sUpdateIndex).getChildren() != '') {
			var sIdPrevious = $(this.sUpdateIndex).getLast().getProperty('id');

			// If the page doesn't change, don't play load effect
			if (sIdPrevious == this.sUpdateIndex + '_' + this.sIdcurrentPage)
				this.bDisplayEffect = false;

			this.load(sIdPrevious.substring(this.sUpdateIndex.length + 1, sIdPrevious.length));
		// Else opens the front page
		} else if (this.options.sInitUrl) {
			this.load(this.options.sInitUrl);
		}
	},
	remove2 : function(sIdPage)
	{
		if($(sIdPage)) {
			$(sIdPage).remove();
			this.removeIndex(sIdPage);
		}	
	},
    /**
	 * Close all pages, loads the front page
	 */
	reset: function()
	{
		$(this.sUpdateContent).empty();
		this.resetIndex();

		if (this.options.sInitUrl)
			this.load(this.options.sInitUrl);
	},

    /**
	 * Add the page's index
	 */
	addIndex: function(sIdPage)
	{
		var oDiv = new Element('DIV', {
			'id'	: this.sUpdateIndex + '_' + sIdPage,
			'class'	: this.sClassInIndex
		}).inject($(this.sUpdateIndex));

		new Element('IMG', {
			'src'	: 'images/close.png',
			'events': {
				'click'	: this.remove.bind(this, sIdPage, true)
			}
		}).inject(oDiv);

		new Element('IMG', {
			'src'	: 'images/reload.png',
			'events': {
				'click'	: this.reload.bind(this, sIdPage, true)
			}
		}).inject(oDiv);

        // Get the title (defined as a class)
		var sTitle = '';

		var sTmpTitle = $E('.' + this.sClassTitle, sIdPage);
		if (sTmpTitle)
            sTitle = sTmpTitle.getText();

        new Element('A', {
			'events': {
				'click'	: this.load.bind(this, sIdPage, true)
			}
		}).setText(sTitle).inject(oDiv);
	},

    /**
	 * Removes the page's index
	 */
	removeIndex: function(sIdPage)
	{
		$(this.sUpdateIndex + '_' + sIdPage).remove();
	},

    /**
	 * Removes all indexes
	 */
	resetIndex: function()
	{
		if ($(this.sUpdateIndex))
			$(this.sUpdateIndex).empty();
	},

    /**
	 * Focus on the page's index
	 */
	focusIndex: function(sIdPage)
	{
		$(this.sUpdateIndex + '_' + sIdPage).addClass('SelectedTab');
	},

	/**
	 * Unfocus all indexes
	 */
	unFocusIndex: function()
	{
		var aIndex = $ES('div', this.sUpdateIndex);

		aIndex.each(function(oDiv) {
			oDiv.removeClass('SelectedTab');
		});
	},

	/**
	 * Show a loading div
	 */
	showLoading: function()
	{
		if ($(this.sLoading)) {
			$(this.sLoading).setStyle('display', 'none');
		} else {
			/*
			new Element('DIV', {
				'id'	: this.sLoading,
				'class'	: this.sClassLoading
			}).setText('Loading...').inject(document.body);
			*/
		}
	},

	/**
	 * Hide the loading div
	 */
	hideLoading: function()
	{
		if ($(this.sLoading))
			$(this.sLoading).setStyle('display', 'none');
	}
});

Tabs.implement(new Options);

var CTabs = new Tabs({
	sInitUrl		: 'hockey_feed.php?status=all',
	sPagesDir		: '',
	oTransition 	: Fx.Transition(Fx.Transitions.Back, 3).easeOut
});
