/*
 * JQuery NavigMenu v.1.0
 * Copyright (c) 2009 Pawel 'lord_T' Maruszczyk (hrabstwo.net)
 * Date: 2009-01-18
 * Licensed under the MIT (MIT-LICENSE.txt)
 */

/*
linkDelay  //opoznienie linka
actionArea //obszar dzialania
cursorTop
cursorLeft
width
height
margin 	   //obszar dla myszy na ktorym menu nie znika
*/

// create closure
;(function($){

	var 
	timeouts=new Array(),
	count=0;

	// plugin definition
	$.fn.navigmenu = function(options){

		var 
		index=++count,//index of timeouts table

		refreshW = false,
		refreshH = false,

		opts = $.extend({},$.fn.navigmenu.defaults,options),
		container = this.eq(0),//map
		image = container.find('img:eq(0)');
		
		if(!opts.width){
			var imageW = image.width();
			refreshW = true;
		}
		else{
			var imageW = opts.width;
			image.css('width',opts.width);
		}
		
		if(!opts.height){
			var imageH = image.height();
			refreshH = true;
		}
		else{
			var imageH = opts.height;
			image.css('height',opts.height);
		}
		
		//corecting size of image
		image.one('load',function(){
			var $t=$(this);

			if(refreshW) imageW = $t.width();
			if(refreshH) imageH = $t.height();
			refresh();
			
			//support for png
			if(!$.support.opacity && $t.attr('src').match('png$','i')){
				var ieW=$t.width(),ieH=$t.height();
				image.css('filter','progid:DXImageTransform.Microsoft.AlphaImageLoader(src="'+$t.attr('src')+'",sizingMethod="crop"').attr('src','trans.gif');
				
				if(ieW) image.css('width',ieW);
				if(ieH) image.css('width',ieH);
			}
		});
		
		image.css({margin:opts.margin});
		refresh();
		
		//menu event
		$(opts.actionArea).dblclick(function(e){
			container.css({left:(e.pageX-opts.cursorLeft-opts.margin),top:(e.pageY-opts.cursorTop-opts.margin)}).fadeIn();

			clearTimeout(timeouts[index]-1);
			clearTimeout(timeouts[index]);
			return false;
		});
		
		//redefine link event
		$(opts.actionArea).find('a').unbind().click(function(){
			timeouts[index] = setTimeout('location.href="'+$(this).attr('href')+'";',opts.linkDelay);
			return false;
		});
			
		container.bind('mouseleave',hideCont);
		container.fadeOut();
		image.click(hideCont);

		function refresh(){
			container.css({width:imageW + (2*opts.margin), height:imageH + (2*opts.margin)});
			
			if(!opts.cursorTop || opts.cursorTop == 0 || refreshH)
				opts.cursorTop = imageH/2;
			if(!opts.cursorLeft || opts.cursorLeft == 0 || refreshW)
				opts.cursorLeft = imageW/2;
		}
		
		function hideCont(){
			container.fadeOut('fast');
		}
		
		//
		return this;
	}

	// plugin defaults
	$.fn.navigmenu.defaults ={
		linkDelay:200,
		actionArea:document,
		margin:10,
		cursorTop:0,
		cursorLeft:0,
		width:0,
		height:0
	};
  
// end of create closure
})(jQuery);
