$(function() {

	$('.integrated_system_tabs').tabs();
	$('.integrated_system_tabs').bind('tabsselect', function(event, ui) {  
		ans = xtractFile(location.pathname)
		urchinTracker(ans.path + '/integrated/' + ui.panel.id);
	});
	
	function xtractFile(data){
		var m = data.match(/(.*)[\/\\]([^\/\\]+\.\w+)?$/);
		if (m == null) {
			alert("The data string does not fit the pattern of location.pathname.\nPlease click Pathname or type a new string.");
			return {path: "", file: ""};
		} else {
			return {path: m[1], file: m[2]}
		}
	}

	$('#systemMap area').click(function() {
		$('.integrated_system_tabs').tabs('select',$(this).attr('href'));
		return false;
	});
	
	//Circle Hover functionality
	$('#systemMap area').hover(function() {
		var hoverID = $(this).attr('href');
		if(hoverID=='#insulin') {
			$('#system').attr('src', '/images/integrated/active_insulin.jpg');
		} else if(hoverID=='#glucose') {                         
			$('#system').attr('src', '/images/integrated/active_glucose.jpg');
		} else if(hoverID=='#therapy') {                        
			$('#system').attr('src', '/images/integrated/active_therapy.jpg');
		}
	}, function() {
		circleswitch($(".tabs li.ui-state-active a").attr('href'));
	});
	
	//Circle default on hash functionality
	if (location.hash) {
		circleswitch(location.hash);
	}

	$(".tabs li a").click(function(){
		circleswitch($(this).attr('href'));
	});

	function circleswitch(tabID) {
		if(tabID=='#insulin') {
			$('#system').attr('src', '/images/integrated/active_insulin.jpg');
		} else if(tabID=='#glucose') {                           
			$('#system').attr('src', '/images/integrated/active_glucose.jpg');
		} else if(tabID=='#therapy') {                           
			$('#system').attr('src', '/images/integrated/active_therapy.jpg');
		} else if(tabID=='#overview') {                          
			$('#system').attr('src', '/images/integrated/system_00.jpg');
		}
	}

})