(function($) {
	//public functions
	var methods = {
		init : function(options) {
			var defaults = {
				graphData : []
			}, $t, $graph0Link, $graph1Link, i, $resultsGraphs;
			
			//initialize some vars 
			opts = $.extend(defaults, options);		
			currTermGraph="";
			currInfo="";
			$t = $(this);			
			
			$regional_info_switcher = $t.find("#regional_info_switcher");
			$region_info_list = $t.find("#region_info_list");			
			
			//init graphs	
			$resultsGraphs = $t.find("div.resultsGraph");		
			for(i=0;i<opts.graphData.length;i++){
				$graphs["graph"+i] = $resultsGraphs.filter(".graph"+i).barGraph({
										data		: opts.graphData[i].graphData,
										graphCeil	: opts.graphData[i].graphCeil,
										graphFloor	: opts.graphData[i].graphFloor,
										graphUnit	: opts.graphData[i].graphUnit,
										dollars		: opts.graphData[i].dollars
									}).hide();	
			}		

			//bind links			
			$results_graph_switcher	= $t.find("#results_graph_switcher");
			$graphLinks				= $results_graph_switcher.find("a"); 
			$graphLinks.click(graphLinkClick);

			//grab Blurbs
			$graphBlurbs = $t.find("p.graphBlurb");

			//show first graph after delay
			setTimeout(function(){
				showGraph("graph0");
			}, 1000);

			//fade in out region links
			$("#region_links_row a").not(".pdf").hover(region_img_over, region_img_out).mouseout();			
			//switch info
			$regional_info_switcher.delegate("a", "click", region_info_switcher_click);
		}
	};
	
	/*
		private vars
	*/
	var opts, $results_graph_switcher, $region_info_list, $regional_info_switcher,
	currTermGraph, currInfo, $graphLinks, $graphs=[], $graphBlurbs;
	
	/*
		private functions
	*/
	function graphLinkClick(){
		var $t=$(this), graphClass = $t.attr("rel");
		
		//links display correctly
		$graphLinks.removeClass("current");
		$graphLinks.filter("."+graphClass).addClass("current");
				
		//show graph data
		showGraph(graphClass);
		return false;
	}
	//loop through stored graph objects and show the right one,
	//hide the rest
	function showGraph(graphClass){		
		var $showGraph, gc;
		for(gc in $graphs){
			//find graph to show
			if(gc === graphClass){
				if($graphs[gc].is(":visible")){
					return false;
				}					
				$showGraph = $graphs[gc];			
			}else{
				//hide other graphs
				$graphs[gc].hide();
			}
		}		
		//wait till searchin is done and then show
		$showGraph.barGraph("show");
		
		//show right blurb
		$graphBlurbs.fadeOut(250);
		$graphBlurbs.filter("."+graphClass).delay(300).fadeIn(250);
		
	}
		
	function region_info_switcher_click(){
		var $link = $(this), href = $link.attr("href");
		if(href===currTermGraph){
			return false;
		}
		currTermGraph=href;
	
		$regional_info_switcher.find("a.current").removeClass("current");				
		$link.addClass("current");
		
		$region_info_list.children().hide();
		$(href).show();
		
		return false;
	}	
	
	function region_img_over(){
		$(this).children("img").clearQueue().animate({opacity:0.7}, 200);
	}
	
	function region_img_out(){
		$(this).children("img").animate({opacity:0.2}, 200);
	}
	
	$.fn.regionResultsInfo = function(method) {
		//Method Calling Login
		if (methods[method]) {
			return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
		} else if (typeof method === 'object' || !method) {
			return methods.init.apply(this, arguments);
		} else {
			$.error('Method ' + method + ' does not exist on jQuery.');
		}
	};
})(jQuery);
