/**
 * tools.tabs 1.0.1E - Tabs done rigth.
 * 
 * Copyright (c) 2009 Tero Piirainen
 * http://flowplayer.org/tools/tabs.html
 * 
 * Edited:
 * Copyright (c) 2009-2011 Ivan Vovnenko, Corporate Voice
 * http://www.corporatevoice.com
 *
 * Dual licensed under MIT and GPL 2+ licenses
 * http://www.opensource.org/licenses
 *
 * Launch  : November 2008
 * Date: 2009-06-12 11:02:45 +0000 (Fri, 12 Jun 2009)
 * Revision: 1911 
 */ 
(function($) {
		
	// static constructs
	$.tools = $.tools || {version: {}};
	
	$.tools.version.tabs = '1.0.1E';
	
	$.tools.addTabEffect = function(name, fn) {
		effects[name] = fn;
	};		
	
	
	var effects = { 
		'default': function(i) { 
			this.getPanes().hide().eq(i).show(); 
		}, 
		
		// custom configuration variable: fadeInSpeed
		fade: function(i) {
			this.getPanes().hide().eq(i).fadeIn(this.getConf().fadeInSpeed);	
		},
		
		slide: function(i) {
			this.getCurrentPane().slideUp("fast");
			this.getPanes().eq(i).slideDown();			 
		},

		horizontal: function(i) {
			
			// store original width of a pane into memory
			if (!$._hW) { $._hW = this.getPanes().eq(0).width(); }
			
			// set current pane's width to zero
			this.getCurrentPane().animate({width: 0}, function() { $(this).hide(); });
			
			// grow opened pane to it's original width
			this.getPanes().eq(i).animate({width: $._hW}, function() { $(this).show(); });			 
		}		
	};   	
	 

	function Tabs(tabs, panes, opts) { 
		
		var self = this;
		var current;

		// generic binding function
		function bind(name, fn) {
			$(self).bind(name, function(e, args)  {
				if (fn && fn.call(this, args.index) === false && args) {
					args.proceed = false;	
				}	
			});
			return self;
		}
		
		// bind all callbacks from configuration
		$.each(opts, function(name, fn) {
			if ($.isFunction(fn)) { bind(name, fn); }
		});
		
		
		// public methods
		$.extend(this, {				
			click: function(i) {
				
				if (i === current) { return self; }
				
				var pane = self.getCurrentPane();				
				var tab = tabs.eq(i);												 
				
				if (typeof i == 'string') {
					tab = tabs.filter("[href=" +i+ "]");
					i = tabs.index(tab);
				}
				
				if (!tab.length) { 
					if (current >= 0) { return self; }
					i = opts.initialIndex;
					tab = tabs.eq(i);
				}				
				
				// possibility to cancel click action
				var args = {index: i, proceed: true};
				$(self).triggerHandler("onBeforeClick", args);				
				if (!args.proceed) { return self; }				
				
				tab.addClass(opts.current);
				
				// call the effect
				effects[opts.effect].call(self, i);
				
				// onClick callback
				$(self).triggerHandler("onClick", args);	
				
				tabs.removeClass(opts.current);	
				tab.addClass(opts.current);											
				current = i;
				return self;
			},
			
			getConf: function() {
				return opts;	
			},

			getTabs: function() {
				return tabs;	
			},
			
			getPanes: function() {
				return panes;	
			},
			
			getCurrentPane: function() {
				return panes.eq(current);	
			},
			
			getCurrentTab: function() {
				return tabs.eq(current);	
			},
			
			getIndex: function() {
				return current;	
			},
			
			next: function() {
				return self.click(current + 1);
			},
			
			prev: function() {
				return self.click(current - 1);	
			}, 
			
			onBeforeClick: function(fn) {
				return bind("onBeforeClick", fn);	
			},
			
			onClick: function(fn) {
				return bind("onClick", fn);	
			}			
		
		});
		
		
		// setup click actions for each tab
		tabs.each(function(i) { 
			$(this).bind(opts.event, function(e) {
				self.click(i);
				if (!opts.history) { 
					return e.preventDefault();
				}
			});			
		});
		
		// enable history plugin
		if (opts.history) {
			$(window).hashchange(function(e) {
				location.hash && self.click(location.hash);
			});
		}

		// if no pane is visible --> click on the first tab
		if (location.hash) {
			self.click(location.hash);	
		} else {
			self.click(opts.initialIndex);	
		}		
		
		// cross tab anchor link
		panes.find("a[href^=#]").click(function() {
			self.click($(this).attr("href"));		
		});
		
	}
	
	
	// jQuery plugin implementation
	$.fn.tabs = function(query, arg) {
		
		// return existing instance
		var el = this.eq(typeof conf == 'number' ? conf : 0).data("tabs");
		if (el) { return el; }

		
		// setup options
		var opts = {
			tabs: 'a',
			current: 'current',
			onBeforeClick: null,
			onClick: null, 
			effect: 'default',
			history: false,
			initialIndex: 0,			
			event: 'click',
			api:false
		};
		
		if ($.isFunction(arg)) {
			arg = {onBeforeClick: arg};
		}
		
		$.extend(opts, arg);
		
		// install tabs for each items in jQuery		
		this.each(function(i) {				
			var els = $(this).find(opts.tabs);
			
			if (!els.length) {
				els = $(this).children();	
			}

			var panes = null;
			if(query.jquery){
				panes=query;
			}else{
				var obj = $(query, this);
				if(obj.size()>0){
					panes = obj;
				}else{
					panes = $(this).siblings(query);
				}
			}

			el = new Tabs(els, panes, opts);
			$(this).data("tabs", el);	 
		});		
		
		return opts.api ? el: this;		
	};		
		
}) (jQuery); 


/*
 * jQuery hashchange event - v1.3 - 7/21/2010
 * http://benalman.com/projects/jquery-hashchange-plugin/
 * 
 * Copyright (c) 2010 "Cowboy" Ben Alman
 * Dual licensed under the MIT and GPL licenses.
 * http://benalman.com/about/license/
 */
(function($,e,b){var c="hashchange",h=document,f,g=$.event.special,i=h.documentMode,d="on"+c in e&&(i===b||i>7);function a(j){j=j||location.href;return"#"+j.replace(/^[^#]*#?(.*)$/,"$1")}$.fn[c]=function(j){return j?this.bind(c,j):this.trigger(c)};$.fn[c].delay=50;g[c]=$.extend(g[c],{setup:function(){if(d){return false}$(f.start)},teardown:function(){if(d){return false}$(f.stop)}});f=(function(){var j={},p,m=a(),k=function(q){return q},l=k,o=k;j.start=function(){p||n()};j.stop=function(){p&&clearTimeout(p);p=b};function n(){var r=a(),q=o(m);if(r!==m){l(m=r,q);$(e).trigger(c)}else{if(q!==m){location.href=location.href.replace(/#.*/,"")+q}}p=setTimeout(n,$.fn[c].delay)}$.browser.msie&&!d&&(function(){var q,r;j.start=function(){if(!q){r=$.fn[c].src;r=r&&r+a();q=$('<iframe tabindex="-1" title="empty"/>').hide().one("load",function(){r||l(a());n()}).attr("src",r||"javascript:0").insertAfter("body")[0].contentWindow;h.onpropertychange=function(){try{if(event.propertyName==="title"){q.document.title=h.title}}catch(s){}}}};j.stop=k;o=function(){return a(q.location.href)};l=function(v,s){var u=q.document,t=$.fn[c].domain;if(v!==s){u.title=h.title;u.open();t&&u.write('<script>document.domain="'+t+'"<\/script>');u.close();q.location.hash=v}}})();return j})()})(jQuery,this);
