 $(document).ready(function () {
	
	var tabs,
		overview = 'tab0';
		linkSelector = '#productTabs ul:first a',
		contentSelector = '#productTabs div:first',
		currentSelector = function(value) {
			return 'a[href=#' + value + ']';
		};
            if ($.address.value() == '') {
                $.address.value(overview);
            }

		

            // Address handler
		$.address.init(function(event) {
                tabs = $("#productTabs").tabs({
                        // Content filter
                        load: function(event, ui) {
                            $(ui.panel).html($(contentSelector, ui.panel).html());
                        },
                        selected: $(linkSelector).index($(currentSelector(event.value))),
                        fx: { opacity: 'toggle' }
                    });
                // Enables the plugin for all the tab links
                $(linkSelector).address();  
            }).change(function(event) {
					if(event.pathNames.length == 0){ //default to the first tab.
						tabs.tabs('select' , 0);
					} else {
						//go to tab of first variable.
						tabs.tabs('select' , $(linkSelector).index($(currentSelector(event.pathNames[0]))));		
						//if second path exists, scroll to it.
						if(event.pathNames.length > 1)  {
							if($("#" + event.pathNames[1]))
								$.scrollTo($("#" + event.pathNames[1]),800, {offset:-125});	
						}
						 // assume we're going to a regular tab and remove the "/tab" from the event value and select the number;
					}
            }).externalChange(function(event) {
			//alert("ex");
                // Select the proper tab
               // tabs.tabs('select', $(currentSelector(event.value)).attr('href'));  
            }).history(true);
			
			
			/*
			This whole function has one simple purpose. 
			
			If you are on a particular tab, and click a link to scroll to a specific place. Then go back and click the same link,
			then this will be active and will re-scroll you to the same spot.
			thats it..
			*/
			$(".whiteBox").find("a").each(function() {
				var HREF = $(this).attr("href");
				if (HREF)
					if (HREF.length > 2)
						if (HREF.substring(0,2) == "#/") {
							$(this).click(function() {
								var arrayOfVars = HREF.split("/");
								var tab = $.address.value().split("/")[1];
								var anchor = $.address.value().split("/")[2];
								if (!tab)
									tab = arrayOfVars[1];							
								if(tab == arrayOfVars[1] && anchor==arrayOfVars[2]) {
									var GoHere = arrayOfVars[2];
									$.scrollTo($("a[id="+GoHere+"]"),800);
								}
							});
						}
							
			});

});
