/*
	name			: ClassBehaviours, the javascript framework based on class-name parsing
	update			: 9.11.9
	author			: Maurice van Creij
	dependencies	: jquery.classbehaviours.js
	info			: http://www.classbehaviours.com/

    This file is part of jQuery.classBehaviours.

    ClassBehaviours is a javascript framework based on class-name parsing.
    Copyright (C) 2008  Maurice van Creij

    ClassBehaviours is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    ClassBehaviours is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with ClassBehaviours. If not, see http://www.gnu.org/licenses/gpl.html.
*/

	// create the jQuery object if it doesn't already exist
	if(typeof(jQuery)=='undefined') jQuery = function(){};

	// create the root classbehaviours object if it doesn't already exist
	if(typeof(jQuery.classBehaviours)=='undefined') jQuery.classBehaviours = function(){};

	// create the handlers child object if it doesn't already exist
	if(typeof(jQuery.classBehaviours.handlers)=='undefined') jQuery.classBehaviours.handlers = function(){}

	// tabbed content
	jQuery.classBehaviours.handlers.tabbedContent = {
	    // properties
	    name: 'tabbedContent',
	    interval: null,
	    index: 0,
	    // methods
	    start: function(node) {
	        // give this pager an id if it doesn't have one yet
	        node.id = (node.id) ? node.id : this.name + this.index++;
	        // HEADER TABS
	        // get all tabs from the header
	        allTabs = node.getElementsByTagName('HEADER')[0].getElementsByTagName('A');
	        // store the most likely opened tab
	        openedTab = null;
	        //				openedTab = allTabs[0];
	        // for all tabs
	        for (var a = 0; a < allTabs.length; a++) {
	            // get the id this tab refers to
	            tabId = allTabs[a].href.split('#')[1];
	            //alert(tabId);
	            // apply onclick events to the referred tab
	            allTabs[a].onclick = this.open;
	            // apply the starting state of the tab if needed
	            if (allTabs[a].className.indexOf('closedTab') < 0 && allTabs[a].className.indexOf('openedTab') < 0) allTabs[a].className += ' closedTab';
	            // apply the starting state of the referred content if needed
	            document.getElementById(tabId).style.display = 'none';
	            // if this tab is referred to in the page url, remember it as active
	            if (document.location.href.indexOf(allTabs[a].href) > -1) openedTab = allTabs[a];
	            // if this tab was manualy set
	            if (allTabs[a].className.indexOf('openedTab') > -1) openedTab = allTabs[a];
	        }
	        // open the most likely first tab
	        if (openedTab != null) this.open(openedTab, true);
	        // FOOTER CONTROLS
	        // get all the control elements from the footer
	        allControls = node.getElementsByTagName('FOOTER')[0].getElementsByTagName('BUTTON');
	        for (var a = 0; a < allControls.length; a++) {
	            // assign the events for the buttons
	            if (allControls[a].className.indexOf('previousTab') > -1) allControls[a].onclick = this.previous;
	            if (allControls[a].className.indexOf('nextTab') > -1) allControls[a].onclick = this.next;
	            if (allControls[a].className.indexOf('pauseTabs') > -1) allControls[a].onclick = this.pause;
	            if (allControls[a].className.indexOf('playTabs') > -1) allControls[a].onclick = this.play;
	        }
	        // if there's an automatic timeout given
	        autoDelay = jQuery.classBehaviours.utilities.getClassParameter(node, 'auto', null);
	        if (autoDelay != null) {
	            // pause the show during mouse interaction
	            node.onmouseover = this.pause;
	            node.onmouseout = this.play;
	            // set an automatic click on the next button
	            this.play(node);
	        }
	    },
	    // events
	    play: function(that) {
	        var objNode = (typeof (this.nodeName) == 'undefined') ? that : this;
	        var tbd = jQuery.classBehaviours.handlers.tabbedContent;
	        // find the root node
	        rootNode = jQuery.classBehaviours.utilities.rootNode(objNode, null, null, tbd.name);
	        // cancel the previous timeout
	        clearInterval(tbd.interval);
	        // set the interval
	        tbd.interval = setInterval('jQuery.classBehaviours.handlers.tabbedContent.next(document.getElementById("' + rootNode.id + '"))', 4000);
	        // cancel the click
	        return false;
	    },
	    pause: function(that) {
	        var objNode = (typeof (this.nodeName) == 'undefined') ? that : this;
	        var tbd = jQuery.classBehaviours.handlers.tabbedContent;
	        // cancel the interval
	        clearInterval(tbd.interval);
	        // cancel the click
	        return false;
	    },
	    next: function(that) {
	        var objNode = (typeof (this.nodeName) == 'undefined') ? that : this;
	        var tbd = jQuery.classBehaviours.handlers.tabbedContent;
	        // find the root node
	        rootNode = jQuery.classBehaviours.utilities.rootNode(objNode, null, null, tbd.name);
	        // get the pager information
	        pagerInfo = rootNode.getElementsByTagName('FOOTER')[0].getElementsByTagName('METER')[0].firstChild.nodeValue;
	        // what is the current pagenumber
	        currentPage = parseInt(pagerInfo.split('/')[0]);
	        // how many pages are there
	        totalPages = parseInt(pagerInfo.split('/')[1]);
	        // what is the next page
	        nextPage = (currentPage < totalPages) ? currentPage + 1 : 1;
	        // what is the tabs strip
	        tabStrip = rootNode.getElementsByTagName('HEADER')[0].getElementsByTagName('MENU')[0];
	        // get the relevant page from the tab strip
	        targetTab = tabStrip.getElementsByTagName('A')[nextPage - 1];
	        // activate it's click
	        tbd.open(targetTab);
	        // cancel the click
	        return false;
	    },
	    previous: function(that) {
	        var objNode = (typeof (this.nodeName) == 'undefined') ? that : this;
	        var tbd = jQuery.classBehaviours.handlers.tabbedContent;
	        // find the root node
	        rootNode = jQuery.classBehaviours.utilities.rootNode(objNode, null, null, tbd.name);
	        // get the pager information
	        pagerInfo = rootNode.getElementsByTagName('FOOTER')[0].getElementsByTagName('METER')[0].firstChild.nodeValue;
	        // what is the current pagenumber
	        currentPage = parseInt(pagerInfo.split('/')[0]);
	        // how many pages are there
	        totalPages = parseInt(pagerInfo.split('/')[1]);
	        // what is the next page
	        previousPage = (currentPage > 1) ? currentPage - 1 : totalPages;
	        // what is the tabs strip
	        tabStrip = rootNode.getElementsByTagName('HEADER')[0].getElementsByTagName('MENU')[0];
	        // get the relevant page from the tab strip
	        targetTab = tabStrip.getElementsByTagName('A')[previousPage - 1];
	        // activate it's click
	        tbd.open(targetTab);
	        // cancel the click
	        return false;
	    },
	    open: function(that, noAnimation) {
	        var objNode = (typeof (this.nodeName) == 'undefined') ? that : this;
	        var tbd = jQuery.classBehaviours.handlers.tabbedContent;
	        // find the root node
	        rootNode = jQuery.classBehaviours.utilities.rootNode(objNode, null, null, tbd.name);
	        // INDEX THE TAB STATES
	        // get all tabs
	        var allTabs = rootNode.getElementsByTagName('HEADER')[0].getElementsByTagName('A');
	        var prevTab = null;
	        var pageNumber = 0;
	        // find the current tab
	        for (var a = 0; a < allTabs.length; a++) {
	            // rememeber the previous tab
	            if (allTabs[a].className.indexOf('openedTab') > -1) prevTab = allTabs[a];
	            // count the new pagenumber
	            if (allTabs[a] == objNode) pageNumber = a;
	        }
	        // if this is the current tab again
	        if (prevTab != objNode || noAnimation) {
	            // PREVIOUS TAB
	            if (prevTab) {
	                // mark the previous tab as passive
	                prevTab.className = prevTab.className.replace('openedTab', 'closedTab');
	                // if the tab has an image
	                tabImages = prevTab.getElementsByTagName('IMG');
	                if (tabImages.length > 0) tabImages[0].src = tabImages[0].src.replace('_active', '_link');
	                // id the previous tabbed content
	                prevContentId = prevTab.href.split('#')[1];
	            } else {
	                prevContentId = null;
	            }
	            // NEXT TAB
	            // mark the next tab as active
	            objNode.className = objNode.className.replace('closedTab', 'openedTab');
	            // if the tab has an image
	            tabImages = objNode.getElementsByTagName('IMG');
	            if (tabImages.length > 0) tabImages[0].src = tabImages[0].src.replace('_link', '_active').replace('_hover', '_active');
	            // id the next tabbed content
	            nextContentId = objNode.href.split('#')[1];
	            // FADE ANIMATION
	            tabHeight = jQuery.classBehaviours.utilities.getClassParameter(rootNode, 'off', '0');
	            isAnimated = (noAnimation) ? 'no' : jQuery.classBehaviours.utilities.getClassParameter(objNode, 'animated', 'yes');
	            if (isAnimated == 'yes') {
	                // make the previous tab float
	                if (prevTab) document.getElementById(prevContentId).style.position = 'absolute';
	                if (prevTab) document.getElementById(prevContentId).style.display = 'block';
	                if (prevTab) document.getElementById(prevContentId).style.top = tabHeight + 'px';
	                // make the next tab not float
	                document.getElementById(nextContentId).style.position = 'relative';
	                document.getElementById(nextContentId).style.display = 'block';
	                if (prevTab) document.getElementById(nextContentId).style.top = '0px';
	                // order the animation
	                if (prevTab) jQuery.classBehaviours.fader.fade(prevContentId, 100, 0, 1, 10, 1, 'document.getElementById("' + prevContentId + '").style.display = "none";');
	                jQuery.classBehaviours.fader.fade(nextContentId, 0, 100, 1, 10, 1, 'document.getElementById("' + nextContentId + '").style.display = "block";');
	            } else {
	                if (prevTab) document.getElementById(prevContentId).style.display = 'none';
	                document.getElementById(nextContentId).style.display = 'block';
	            }
	            // PAGE NUMBER
	            // update page-numbering
	            rootNode.getElementsByTagName('FOOTER')[0].getElementsByTagName('METER')[0].firstChild.nodeValue = (pageNumber + 1) + '/' + allTabs.length;
	        }
	        // cancel the jump to the anchor
	        objNode.blur();
	        return false;
	    }
	}

	// add this addon to the jQuery object
	if(typeof(jQuery.fn)!='undefined'){
		// extend jQuery with this method
		jQuery.fn.tabbedContent = function(){
			return this.each(
				function(){
					jQuery.classBehaviours.handlers.tabbedContent.start(this);
				}
			);
		};
		// set the event handler for this jQuery method
		$(document).ready(
			function(){
				$(".tabbedContent").tabbedContent();
			}
		);
	}

