/*
 * Katakomben Kaart - v1.0 - 2011-06-08
 * http://www.ifox.nl
 * Author - Tim de Vos
 */
 
 (function($) {
		  
	$.fn.katmap = function(options) {
		var opts = $.extend({}, $.fn.katmap.defaults, options);
		return this.each(function(i) {
			var $this 				= $(this);
			$this.opts 				= $.extend({}, opts);
			$this.opts.opened		= false;
			$this.opts.hovered		= false;
			$this.opts.automoveInterval		= null;
			$this.opts.dragged 		= false;
			
			$this.opts.automoveInterval = setInterval(function(){ $.fn.katmap.automove($this); }, 10);
			
			$($this.opts.openselect).click(function(e){ 	e.preventDefault(); $.fn.katmap.showmap($this); });
			$($this.opts.closeselect).click(function(e){ 	e.preventDefault();  $.fn.katmap.hidemap($this); });
			
			$this.bind('mouseover, mousemove', function(e)	{ 
				$this.opts.xperc = Math.round(e.pageX / $(window).width() * 100);
				$this.opts.yperc = Math.round(e.pageY / $this.opts.height * 100);
				$this.opts.hovered = true; 
			});
			
			$this.bind('mouseleave', function(e){
				$this.opts.hovered = false; 
			});
			
			$(document).bind('click', function(e){ 			($this.opts.hovered == false && !$("#fancybox-content").html() ? $.fn.katmap.hidemap($this) : false); });
			$(window).bind('resize', function(e){ 			$.fn.katmap.reposition($this); });
			
			$this.opts.dragwidth = $this.find('.drag').width();
			$this.opts.dragheight = $this.find('.drag').height();
			
			$this.find('.points > li').katslide();
			
			$.fn.katmap.reposition($this);
			
			//$.fn.katmap.showmap($this);
		});
	};
	
	$.fn.katmap.defaults = {
		closeselect	: '.closemap',
		openselect	: '.openmap',
		height : 650
	};
	
	$.fn.katmap.reposition = function($this){
		if($('body').width() != $this.opts.width){
			$this.opts.width = $('body').width();
			
			$this.opts.x1 = $this.opts.width - $this.opts.dragwidth;
			$this.opts.x2 = 0;
			$this.opts.y1 = $this.opts.height - $this.opts.dragheight;
			$this.opts.y2 = 0;
			
			if($this.opts.y1 > $this.opts.y2){
				temp = $this.opts.y2;
				$this.opts.y2 = $this.opts.y1;
				$this.opts.y1 = temp;
			}
			
			if($this.opts.x1 > $this.opts.x2){
				temp = $this.opts.x2;
				$this.opts.x2 = $this.opts.x1;
				$this.opts.x1 = temp;
			}
			
			xpos = ($this.opts.x1 + $this.opts.x2) / 2;
			ypos = ($this.opts.y1 + $this.opts.y2) / 2;
			$this.find('.drag').css({left:xpos, top:ypos});
			
			$this.find('.drag').draggable({	
				'containment'	: 	[$this.opts.x1, $this.opts.y1, $this.opts.x2, $this.opts.y2],
				'start' : function(){$this.opts.dragged = true;},
				'stop' : function(){$this.opts.dragged = false;} 
			});
		}
	};
	
	$.fn.katmap.showmap = function($this){
		if($this.opts.opened == false){
			$this.opts.hovered	= true;
			$this.opts.opened = true;
			$this.animate({top:'0px'}, 400);
			$("#menu").animate({marginTop:$this.opts.height+'px'}, 400);
			$('<div id="contentfade"></div>').insertAfter($("#menu")).css('height',$(document).height()+'px');
		}
	};
	
	$.fn.katmap.hidemap = function($this){
		if($this.opts.opened == true){
			$this.opts.opened = false;
			$this.animate({top:'-'+$this.opts.height+'px'}, 400);
			$("#menu").animate({marginTop:'0px'}, 400);
			$('#contentfade').remove();
			
		}
	};
	$.fn.katmap.automove = function($this){
		if($this.opts.dragged == false && $this.opts.hovered == true){
			newx = parseInt($('.drag').css('left'));
			if($this.opts.xperc <= 15) newx += 4;
			if($this.opts.xperc >= 85) newx -= 4;
			newy = parseInt($('.drag').css('top'));
			if($this.opts.yperc <= 15) newy += 4;
			if($this.opts.yperc >= 85) newy -= 4;
			if(newx <= $this.opts.x1) newx = $this.opts.x1;
			if(newx >= $this.opts.x2) newx = $this.opts.x2;
			if(newy <= $this.opts.y1) newy = $this.opts.y1;
			if(newy >= $this.opts.y2) newy = $this.opts.y2;
			
			$('.drag').css({'left':newx+'px','top':newy+'px'});
		}
	};
})(jQuery);


