//You need an anonymous function to wrap around your function to avoid conflict

(function($){

	//Attach this new method to jQuery  
	$.fn.extend({   
		//This is where you write your plugin's name  
		ubmRegionSelector: function(options) {
			//Set the default values, use comma to separate the settings, example:
			var defaults = {  
				fadespeed: 125,
				debug: true
			}
			var options 	= $.extend(defaults, options);
			
			if($("img#map-image", $(this)).attr('id') == undefined) {
				$.log('Cannot find node');
				return false;
			}
			
			//Iterate over the current set of matched elements  
			return this.each(function() {
				var obj = $(this);
				
				// Set basic accessors
				var mapImage = $("#map-image", obj);
				var mapDefault = mapImage.attr('src');

				var mapRegions = $(".map-regions", obj);
				var areas = $('area', obj);
				mapImage.attr('alt','').attr('title','');
				areas.each(function() { 
					$(this).attr('alt','');
					$(this).attr('title','');
				});			
				// Close all functionality
				var open = false;
				var closeall = function(){
									
					if(areas.length == 1){
						return;
					}
					
					//Hide all objects that should be hidden
					$('.hide',mapRegions).fadeOut(100);
					
					// Force the regions off (a bug in jQuery it seems)
					$('li .audiences',mapRegions).hide();
					$('li .prompt',mapRegions).hide();
					//$('li .description',mapRegions).hide();
					
					// Reset the map to its default image
					mapImage.attr('src',mapDefault);
					open = false;
				}
				
				// Catch all for closing all if currently open
				mapImage.click( function(e) {
					e.preventDefault();
					//alert(open);
					if(open){
						$.log('close all... ::');
						closeall();
					//}else{
					//	window.location = '/global';
					}
				});
				
				// Listen for the imagemap areas
				$("area", obj).click( function(e) {
					e.preventDefault();
					e.stopImmediatePropagation();
					$(this).blur();
					
					if(open){
						closeall();
						return;
					}
					
					$(this).trigger('mouseenter');
					
					var area = $(this);
					var areaid = area.attr('id');
					var region = $('#region-'+areaid, obj);
					
					var prompt = $('div.prompt', region);
					var description = $('div.description', region);
					var audiences = $('div.audiences', region);
					var audienceIntro = $('div.audience-intro', region);
					
					var audiencesLength = $('li', audiences);
					
					// If there is a description... then we know it has audinces
					if(audiencesLength.length > 0){
						open = true;
						// Fade in main objects
						//description.stop(true,true).fadeIn();
						prompt.stop(true,true).fadeIn();
						audienceIntro.stop(true,true).fadeIn();
						audiences.stop(true,true).fadeIn();
					}else{
						// Navigate to origonal link
						window.location = this.href;
					}
					
					$.log('areaClick... ::'+$(this).attr('id'));
				});
				
				// On rollover
				$("area", obj).mouseenter( function(e) {
						if(open) return;
						
						var area = $(this);
						var areaid = area.attr('id');
						var region = $('#region-'+areaid, obj);
						var image = $('img.map-image', region);
						var src = image.attr('src');
						
						// Set the map image source
						if(areas.length != 1){
							mapImage.attr('src',src);
						}
						
						//Fade in the main region object
						region.stop(true,true).animate({opacity: 1.0},200).fadeIn(defaults.fadespeed);
						
						$.log('areaOver... ::'+area.attr('id'));
					} );
					
				// Close all the regions
				$("area", obj).mouseleave( function(e) {
						if(open) return;
						closeall();
						$.log('areaOut...'+$(this).attr('id'));
					} );
				
				//If IE attach & hack
				if($.browser.msie) {
					//*				
					$(".audiences li", obj).each(function(e) { 
						var style = $(".audience-intro", $(this)).attr('style');
						var cssResetPos = {
							'position': 'absolute',
							'top': 0,
							'left': 0
						};
						$(".audience-intro", $(this)).css(cssResetPos);
						$(this).attr('style',style);
						$(this).css('background','none');
						var lft = $(this).css('left');
						lft.replace('px','');
						lft = 40+parseInt(lft);
						$(".audience-links", $(this)).css('position', 'absolute');
						$(".audience-links", $(this)).css('left', 115+lft);//$(this).css('left')
						$(".audience-links", $(this)).css('top', '9px');
						$(this).css('left', lft+'px');
					});
					//*/
					// Show the audience links
					$(".audiences li", obj).mouseenter( function(e) {
							e.stopImmediatePropagation();
							if(!open) return;
							$(".audience-links", $(this)).stop(true,true).animate({opacity: 1.0},200).fadeIn();
							$.log('over audience...');
						} );
						
					// Hide the audience links
					$(".audiences li", obj).mouseleave(function(e) {
							e.stopImmediatePropagation();
							if(!open) return;
							$(".audience-links", $(this)).stop(true,true).fadeOut();
							$.log('out audience...');
						} );
				//Not IE.
				} else {				
					// Show the audience links
					$(".audiences li", obj).mouseenter( function(e) {
						e.stopImmediatePropagation();
						if(!open) return;
						$(".audience-links", $(this)).stop(true,true).animate({opacity: 1.0},200).fadeIn();
						$.log('over audience...');
					});
					
					// Hide the audience links
					$(".audiences li", obj).mouseleave( function(e) {
						e.stopImmediatePropagation();
						if(!open) return;
						$(".audience-links", $(this)).stop(true,true).fadeOut();
						$.log('out audience...');
					});
				}
								
				//rollover legend focus map..
				$(".map-links a", obj).mouseenter( function(e) {
					e.preventDefault();
					var link = $(this).attr('href');
					$('area[href$="'+link+'"]', obj).trigger('mouseenter');
				});
				
				$(".map-links a", obj).mouseleave( function(e) {
					e.preventDefault();
					var link = $(this).attr('href');
					$('area[href$="'+link+'"]', obj).trigger('mouseleave');
				});
				
				$(".map-links a", obj).click( function(e) {
					e.preventDefault();
					var link = $(this).attr('href');
					$('area[href$="'+link+'"]', obj).trigger('click');
				});
				
				if(areas.length == 1){
					areas.trigger('click');
					$('.tooltip', obj).hide();
					$('.prompt-pointer', obj).hide();
					$('.description', obj).hide();
				}
				
				$.log('gui created [ '+$(this).attr('id')+', '+this+' ]');//+this.mapAreas.length+'].');
			});

		}
	});

	//Logging function - for debug purposes only.
	jQuery.log = function(message) {
		if (window.console && window.console.log) {
			console.debug(message);
		} else {
			window.status = 'Error: '+message;
		}
	};
		
})(jQuery); 