(function($){

	$.fn.imagemap = function(customOptions) {
		
		var options = $.extend({},$.fn.imagemap.defaultOptions, customOptions);
		
		hover_status = false;

		//// hide popups and its content
		$('.popup').hide();
		$('.popup p, .popup h3, .popup img').hide();
		
		//// mouse position
		$('#main').mousemove(function(e){
			var xpos = e.pageX - this.offsetLeft-420;// 
			var ypos = e.pageY - this.offsetTop-510;//
      		$('#xpos').val(xpos);
			$('#ypos').val(ypos);
   		});
   		
   		//// hover over popup
		$('.popup').hoverIntent(function() {
			hover_status = true;
			timer();
		}, function() {
			hover_status = false;
			timer();
			setTimeout(function() {
				checkme();
			},1000);
		});
   		
		//// area
		$(this).find('area').each(function(i) {
			
			$(this).hoverIntent(function() {
			 
										 
				$('.popup').fadeOut('fast');
				hover_status = true;
				
				//// animate outwards then upwards

				$('.popup').eq(i).css("width", "2px");
				$('.popup').eq(i).css("height", "2px");
				var xpos = $('#xpos').val();
				var ypos = $('#ypos').val();
				
				if(i == '0' || i == '1' || i == '14' || i == '15' || i == '13'){
					xpos = parseFloat(xpos)+400;
				}
				
				$('.popup').eq(i).animate({ width: "400px"}, function() {
					$('.popup').eq(i).animate({ height: "267px", top : '-=267px'}, function() {
						$(this).find('img').fadeIn('slow');
						$(this).find('p').fadeIn('slow', function() {
							$(this).find('p').find('a').show();
						});
						$(this).find('h3').fadeIn('slow');
					});
				});
				//alert(i);
				$('.popup').eq(i).css("top",ypos+"px");
				$('.popup').eq(i).css("left", xpos+"px");
				
				
				
			}, function() {
				hover_status = false;			
			});
			
		});
		
		//// set interval, if false after 2 seconds hide the particular popup
		setInterval(function() {
			$('#hover_status').val(hover_status);
			if(hover_status == false) {
				$('#inter').val('countdown to fade out');
				setTimeout(function() {
					if(hover_status == false) {
						$('.popup').fadeOut('fast');
					};
				},1000);
			} else {
				$('#inter').val('animate');
			}
		}, 100);
		
	};
	
	//// options
	$.fn.imagemap.defaultOptions = {
		mycount:100,
		offleft: 0,
		offtop: 0
	};

})(jQuery);
