// Copyright (c) 2008 Solutio, Inc. - http://solutiosoftware.com
//
// Based on the original tab_slides.js:
// Copyright (c) 2006-2007 JoomlaWorks.gr - http://www.joomlaworks.gr
// Copyright (c) 2006 Alf Magne Kalleland
// Copyright (c) 2006 Patrick Fitzgerald

// create the Outlook class with MooTools
var Outlook = new Class({
	initialize: function(div) {
		// cookie name constant
		this.cookie = "saved-outlook-tab";
		
		// CSS class constants
		this.classMain = "outlook";
		this.classMainLive = "outlook-live";
		this.classTab = "outlook-tab";
		this.classNav = "outlook-nav";
		this.classTabHide = "outlook-tab-hide";
		this.classNavActive = "outlook-nav-active";
		
		// regular expressions from the above classes
		this.REclassMain=new RegExp('\\b' + this.classMain + '\\b','gi');
		this.REclassMainLive=new RegExp('\\b' + this.classMainLive + '\\b','gi');
		this.REclassTab=new RegExp('\\b' + this.classTab + '\\b','gi');
		this.REclassTabHide=new RegExp('\\b' + this.classTabHide + '\\b','gi');
		
		// create the tabs
		this.createTabs(div);
	},
	
	// method: createTabs() - extracts the divs for each tab and creates a tab object for each
	createTabs: function(mainDiv) {
		this.tabs = new Array();
		
		// create a tab for each child div with the outlook-tab class
		var nodes = mainDiv.childNodes;
		for (var i = 0; i < nodes.length; i++) {
			var node = nodes[i];
			if (node.className && node.className.match(this.REclassTab)) {
				// div tab found, add new tab object to the list
				var t = new Object();
				t.div = node;
				this.tabs[this.tabs.length] = t;
			}
		}
		
		// create an unordered list element that will be added to the DOM later
		// this list is styled with CSS to create the inline tabs across the top of the outlook
		var DOM_ul = document.createElement("ul");
		DOM_ul.className = this.classNav;
		
		// create a list item and a element for each tab that will be included in the navigation
		for (var i = 0; i < this.tabs.length; i++) {
			var t = this.tabs[i];
			
			// the title from the tab comes from the title attribute on the div
			t.headingText = t.div.title;
			
			// the list item
			var DOM_li = document.createElement("li");
			t.li = DOM_li;
			
			// the a tag
			DOM_a = document.createElement("a");
			DOM_a.appendChild(document.createTextNode(t.headingText));
			DOM_a.href = "javascript:void(null);";
			DOM_a.title = t.headingText;
			DOM_a.onclick = this.navClick;
			DOM_a.tabber = this;
			DOM_a.tabberIndex = i;
			
			// add the a tag to the list item and list item to the list
			DOM_li.appendChild(DOM_a);
			DOM_ul.appendChild(DOM_li);
		}
		
		// wrap the list in a div for extra styling possibilities
		var DOM_div = document.createElement("div");
		DOM_div.className = 'outlook-nav-wrapper';
		DOM_div.appendChild(DOM_ul);		
		
		// add the unordered list (navigator) to the main outlook div as the first item (before the tab content divs)
		mainDiv.insertBefore(DOM_div, mainDiv.firstChild);
		
		// switch the class on the outlook to the live class now that the navigation is setup
		mainDiv.className = mainDiv.className.replace(this.REclassMain, this.classMainLive);
		
		// show the first tab as a default
		this.tabShow(0);
		
		// check for a cookie to indicate which tab should be active at first
	    var val = parseInt(this.getCookie(this.cookie));
	    if (!isNaN(val)) { 
	    	this.tabShow(val); 
	    }
	},
	
	// method: setCookie() - set a value for a cookie
	setCookie: function(name, value) {
	    document.cookie = name + "=" + escape(value);
	},
	
	// method: getCookie() - get a cookie value
	getCookie: function(name) {
	    var dc = document.cookie;
	    var prefix = name + "=";
	    
	    // find the beginning of the cookie key/value pair
	    var begin = dc.indexOf("; " + prefix);
	    if (begin == -1) {
	        begin = dc.indexOf(prefix);
	        if (begin != 0) return null;
	    } 
	    else {
	        begin += 2;
	    }
	    
	    // find the end of the pair
	    var end = document.cookie.indexOf(";", begin);
	    if (end == -1) {
	        end = dc.length;
	    }
	    
	    // return the string
	    return unescape(dc.substring(begin + prefix.length, end));
	},
	
	// method: navClick() - this is called when the navigation bar is clicked
	navClick: function() {
		// the "this" object is the element that caused the event (a tag), not the object tied to this method
		var a = this;
		a.blur(); // remove focus
		
		// self will refer to "this" object (self is used as "this" in other languages)
		var self = a.tabber;
		
		// set a cookie to save the current tab
		self.setCookie(self.cookie, a.tabberIndex);
		
		// show the tab
		self.tabShow(a.tabberIndex);
		
		return false;
	},
	
	// method: tabShow() - display a tab and update the navigation bar
	tabShow: function(index) {
		// must have a valid index
		if(!this.tabs[index]) {
			return;
		}
	
		// hide all tabs first
		this.tabHideAll();
		
		// remove the hidden tab CSS class from this tab
		var div = this.tabs[index].div;
		div.className = div.className.replace(this.REclassTabHide,'');
		
		// update the navigation
		this.tabs[index].li.className = this.classNavActive;
	},
	
	// method: tabHide() - hide the tab contents and update the navigation
	tabHide: function(index) {
		// must have a valid index
		if (!this.tabs[index]) {
			return;
		}
	
		// if the hidden tab CSS class is not already in place, add it
		var div = this.tabs[index].div;
		if (!div.className.match(this.REclassTabHide)) {
			div.className += ' ' + this.classTabHide;
		}
	
		// update the navigation
		this.tabs[index].li.className = '';
	},
	
	// method: tabHideAll() - hide all the tabs
	tabHideAll: function() {
		// simply use the tabHide() method on all tabs
		for (var i = 0; i < this.tabs.length; i++) {
			this.tabHide(i);
		}
	}
});

// create the outlook object on DOM ready
window.addEvent('domready', function() {
	var div = document.getElementById('outlook-main');
	var outlook = new Outlook(div);
	// store it away on the div, not needed later
	div.outlook = outlook;
});