/*jslint browser: true, onevar: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: true, newcap: true, immed: true, strict: true */
"use strict";
(function ( ) {
	var tabs;
	
	tabs	=	function (container) {
		var $container, methods, selectors, maps, streetview;
		
		$container	=	$(container);
		
		methods		=	{
			"init":		function ( ) {
				methods.add(".tabBar a:not(.fake)", $container);
				methods.add(".verticalTab:not(.fake)");
				$container.find(".tabBar a").filter(":first").click();
				
				if (window.location.hash) {
					$container.find(".tabBar a").filter("[href=" + window.location.hash + "]").click();
				}
				
				return methods;
			},
			"activate":	function (e) {
				var $this, $target, verticalTab;
				
				e.preventDefault();
				$this	=	$(this);
				$target	=	$($this.attr("href"));
				
				if ($this.hasClass("verticalTab")) {
					verticalTab	=	true;
					$this		=	$(".tabBar a[href='" + $this.attr("href") + "']");
				}
				
				$container.find(".tabBar a").removeClass("active");
				$container.find(".tabContent li").removeClass("active");
				$(".verticalTab").removeClass("active");
				
				$this.addClass("active");
				$target.addClass("active");
				
				if ($target.find(".resize")[0]) {
					$target.find(".resize").each(initInput);
				}
				
				if ($target.find(".maps")[0]) {
					if (!maps) {
						maps		=	gMaps($target.find(".maps"), address);
					}
				}
				
				if ($target.find(".streetview")[0]) {
					if (!streetview) {
						streetview	=	gStreetview($target.find(".streetview"), address);
					}
				}
				
				if (verticalTab) {
					$(window).scrollTop($target.offset().top);
				}
				
				return methods;
			},
			"add":		function (selector, scope) {
				$(selector, scope).click(methods.activate);
				
				return methods;
			}
		};
		
		return methods.init();
	};
	
	$.fn.tabs	=	function ( ) {
		var instances;
		
		instances	=	[];
		
		this.each(function ( ) {
			instances.push($(this).data("tabs", tabs(this)));
		});
		
		if (instances.length == 1) {
			return instances[0].data("tabs");
		} else {
			return instances;
		}
	};
	
	$(".tabs").tabs();
	
}());

