(function ($) {

    var undefined; // speed up undefined compare


    $.fn.tabs = function (settings) {

        var config = { 
			listener : true
		};

        if (settings) $.extend(config, settings);

        // private functions
        var fn = {
            switchTo: function (context, config, index) {

                var current = undefined;
				
                $("li a.tab-link", context).each(function (indx) {
                    $(this).removeClass("active");
					$(this).parent().removeClass("active");
                    if (indx == index) {
                        current = $(this).addClass("active");
						$(this).parent().addClass("active");
                    }
                });

                $("div.tab-content", context.parent()).each(function (indx) {
                    $(this).addClass("tab-hide");
                    if (indx == index) {
                        $(this).removeClass("tab-hide");
                    }
                });

                if (current) {
                    try {
                        var path = location.href + "tab-" + $(current).attr('href').replace('#', '') + '/';
                        urchinTracker(path);
                    } catch (err) {
                        //if (console)
                            //console.log(err);
                    }
                }
            }
        };

        this.each(function () {

			var container = $("<div />").addClass("tabs-container");
		

			if(jQuery.browser.msie && ((document.compatMode || 'BackCompat') == 'BackCompat' || parseFloat(jQuery.browser.version) < 8.0)) { /* quirks */
				container.addClass("tabs-container-quirks");
			}
			
            var context = $(this)
                .removeClass("tabs-no-js")
				.addClass("tabs-has-js")
                .addClass("tabs")
                .wrap(container);

            $("li div.tab-content", this).detach().appendTo($(this).parent());

			var startIndx = -1;
			var cHash = undefined;
			
			if(location.hash) {
				cHash = location.hash.replace('#', '');
				
			}
			
			$("li a.tab-link", context).each(function(indx) {
				
				var $a = $(this);
				
				if(startIndx == -1 && $a.attr("href").replace('#', '') == cHash) {
					startIndx = indx;
				}
				
				$(window).bind('location.' + $(this).attr("href"), function() {
					if (!$a.hasClass("active")) {
						fn.switchTo(context, config, $a.parent().index());
					}
				});
				
				$a.attr("href", location.href.substring(0, location.href.indexOf('#') > -1 ? location.href.indexOf('#') : location.href.length) + $a.attr("href"));
			});
			
			if(startIndx == -1) startIndx = 0;
			
            fn.switchTo(context, config, startIndx);
        });

        return this;
    };
	
	$(document).ready(function () {
		
		setInterval(function () {
			var hash = $(window).data('hash');
			
			if (hash != location.hash) {
				hash = location.hash;
				$(window).data('hash', location.hash);
				
				$(window).trigger('location.' + hash, [ location.hash ]);
			}

		}, 200);
		
	});

})(jQuery);

