/*
# ------------------------------------------------------------------------
# JD tabs Module
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license - PHP files are GNU/GPL V2. CSS / JS are Copyrighted Commercial,
# bound by Proprietary License of JoomlArt. For details on licensing, 
# Please Read Terms of Use at http://www.joomlart.com/terms_of_use.html.
# Author: JoomlArt.com
# Websites:  http://www.joomlart.com -  http://www.joomlancers.com
# Redistribution, Modification or Re-licensing of this file in part of full, 
# is bound by the License applied. 
# ------------------------------------------------------------------------
*/
(function(jQuery) {
	jQuery.fn.JDTabs = function(vars) {
		vars = vars == undefined ? new Array() : vars;
		var animation = vars.animation == undefined ? 1 : vars.animation;
		var event = vars.event == undefined ? 1 : vars.event;
		var duration = vars.duration == undefined ? 1000 : vars.duration;
		var autoheight = vars.autoheight;
		var id = jQuery(this).attr('id');
		var t = 0;
		var width = jQuery("#" + id + " .ja_tab_contents").css('width');
		width = width.replace('px', '');
		var height = jQuery("#" + id + " .ja_tab_contents").css('height');
		// height = height.replace('px', '');
		height = height == 0 ? height.replace('px', '') : 200;

		var setHeight = function(key) {
			if (!autoheight)
				return;
			var c_height = jQuery("#" + id + " .ja-tab-content:eq(" + key + ")")
					.height();
			jQuery("#" + id + " .ja_tab_contents").animate( {
				'height' : c_height
			});
		}
		var showTab = function(key) {
			jQuery("#" + id + " .jd-tabs-title-item").removeClass('active');
			jQuery("#" + id + " .jd-tabs-title-item:eq(" + key + ")").addClass(
					'active');
			switch (animation) {
			case 0:
				jQuery("#" + id + " .ja-tab-content").hide();
				jQuery("#" + id + " .ja-tab-content:eq(" + key + ")").show();
				break;
			case 1:
				t = 1;
				jQuery("#" + id + " .ja-tab-content").fadeOut();
				jQuery("#" + id + " .ja-tab-content:eq(" + key + ")").fadeIn(
						duration, function() {
							// t = 0;
					});
				break;
			case 2:
				jQuery("#" + id + " .ja-tab-content").each(function(i) {
					oldLeft = jQuery(this).css('left').replace('px', '');
					newLeft = (i - key) * width;
					if (oldLeft != newLeft) {
						// t = 0;
						jQuery(this).stop(true, false).animate( {
							"left" : newLeft + 'px'
						}, duration, function() {
							// t = 0;
							});
					}
				});
				break;
			case 3:
				jQuery("#" + id + " .ja-tab-content").each(function(i) {
					oldTop = jQuery(this).css('top').replace('px', '');
					newTop = (i - key) * height;
					if (oldTop != newTop) {
						// t = 1;
						jQuery(this).stop(true, false).animate( {
							"top" : newTop + 'px'
						}, duration, function() {
							// t = 0;
							});
					}
				});
			}
		}
		var _init = function() {
			setHeight(0);
			jQuery("#" + id + " .first").addClass('active');
			switch (animation) {
			case 0:
			case 1: // fade
				jQuery("#" + id + " .ja-tab-content").each(function(i) {
					if (i)
						jQuery(this).hide();
				});
				break;
			case 2: // move left/right
				jQuery("#" + id + " .ja-tab-content").each(function(i) {
					jQuery(this).css('left', width * i + "px");
				});
				break;
			case 3: // move up/down

				jQuery("#" + id + " .ja-tab-content").each(function(i) {
					jQuery(this).css('top', height * i + "px");
				});
				break;
			default:
				break;
			}
			if (event == 1)
				jQuery("#" + id + " .jd-tabs-title-item").each(function(i) {
					jQuery(this).click(function() {

						showTab(i);
						setHeight(i);

					});
				});
			else {
				jQuery("#" + id + " .jd-tabs-title-item").each(function(i) {
					/*
					 * jQuery(this).mouseover(function() { if (t == 0)
					 * showTab(i); });
					 */
					jQuery(this).bind('mouseenter', function() {

						showTab(i);
					});
				});
			}
		}

		_init();
	}
})(jQuery);
