/*	name			: ClassBehaviours, the javascript framework based on class-name parsing	update			: 20081124	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',
		// methods
		start: function(node){
			// get all lists within this node
			allLists = node.getElementsByTagName('ul');
			// get all tabs from the first list
			allTabs = allLists[0].getElementsByTagName('a');
			// store the most likely opened tab
		//		openedTab = allTabs[0];
			openedTab = null;
			// for all tabs
			for(var a=0; a<allTabs.length; a++){
				// get the id this tab refers to
				tabId = allTabs[a].href.split('#')[1];
				// 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];
			}
			// if there is a pager list, it'd better be the last one
			pager = (allLists[allLists.length-1].className.indexOf('contentPager')>-1) ? allLists[allLists.length-1] : null;
			if(pager!=null){
				// assign the events for the buttons
				pager.getElementsByTagName('button')[0].onclick = this.previous;
				pager.getElementsByTagName('button')[1].onclick = this.next;
			}
			// open the most likely first tab
			if(openedTab!=null) this.open(openedTab, true);
		},
		// events
		next: function(that){
			var objNode = (typeof(this.nodeName)=='undefined') ? that : this ;
			var tbd = jQuery.classBehaviours.handlers.tabbedContent;
			// get the pager information
			pagerInfo = objNode.parentNode.parentNode.getElementsByTagName('span')[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 = objNode.parentNode.parentNode.parentNode.getElementsByTagName('ul')[0];
			// get the relevant page from the tab strip
			targetTab = tabStrip.getElementsByTagName('a')[nextPage-1];
			// activate it's click
			tbd.open(targetTab);
		},
		previous: function(that){
			var objNode = (typeof(this.nodeName)=='undefined') ? that : this ;
			var tbd = jQuery.classBehaviours.handlers.tabbedContent;
			// get the pager information
			pagerInfo = objNode.parentNode.parentNode.getElementsByTagName('span')[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 = objNode.parentNode.parentNode.parentNode.getElementsByTagName('ul')[0];
			// get the relevant page from the tab strip
			targetTab = tabStrip.getElementsByTagName('a')[previousPage-1];
			// activate it's click
			tbd.open(targetTab);
		},
		open: function(that, noAnimation){
			var objNode = (typeof(this.nodeName)=='undefined') ? that : this ;
			var tbd = jQuery.classBehaviours.handlers.tabbedContent;								
	
			// INDEX THE TAB STATES
			// get all tabs
			var allTabs = objNode.parentNode.parentNode.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(objNode.parentNode.parentNode.parentNode, '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
				pager = objNode.parentNode.parentNode.parentNode.getElementsByTagName('ul')[objNode.parentNode.parentNode.parentNode.getElementsByTagName('ul').length-1];
				pager.getElementsByTagName('span')[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);
				}
			);
		};
		// if the main parser isn't present
		if(typeof(jQuery.classBehaviours.parser)=='undefined'){
			// set the event handler for this jQuery method
			$(document).ready(
				function(){
					$(".tabbedContent").tabbedContent();
				}
			);
		}
	}


