// JavaScript Document
var FlagIntervals = Array();
var FlagIntervals2 =  Array();
(function($) {
	$.fn.vhaflag = function(options) {
		var opts = $.extend({}, $.fn.vhaflag.defaults, options);
		return this.each(function(i) {
								  
			var $this 					= $(this);
			$this.opts 					= $.extend({}, opts);
			$this.opts.interval 		= null;
			$this.opts.flagreference 	= false;
			
			FlagIntervals2[FlagIntervals2.length] = setTimeout(function(){
				$this.bind('mouseover',function(event){
					$.fn.vhaflag.showflag($this,$this.attr('flag'), event.pageX, event.pageY);
				});
				
				$this.bind('mousemove',function(event){
					$.fn.vhaflag.reposflag($this,event.pageX, event.pageY);
				});
				
				$this.bind('mouseout',function(){
					$.fn.vhaflag.hideflag($this);
				});
				
				FlagIntervals[FlagIntervals.length] = setInterval(function(){
					$.fn.vhaflag.checkPosition($this);
				},250);
				
			},300);
		});
	};
	
	$.fn.vhaflag.defaults = {
	};
	
	$.fn.vhaflag.checkPosition = function($this){
		show = false;
		if(mousex >= parseInt($this.offset().left)){
			if(mousex <= parseInt($this.offset().left) + parseInt($this.outerWidth())){
				if(mousey >= parseInt($this.offset().top)){
					if(mousey <= parseInt($this.offset().top) + parseInt($this.outerHeight())){
						show = true;
		}}}}
		
		(show ? $.fn.vhaflag.showflag($this, $this.attr('flag'), mousex, mousey) : $.fn.vhaflag.hideflag($this));
	};
	$.fn.vhaflag.showflag = function($this, content, mousex2, mousey2){
		if($this.opts.flagreference == false){
			var html = '<div class="flag" id="newflag"><div class="content">' + content + '</div><div class="corner"></div></div>';
			$('#container').prepend(html);
			$this.opts.flagreference = $("#newflag");
			$("#newflag").removeAttr('id')
			$this.opts.flagreference.stop().fadeTo(0,0);
			$.fn.vhaflag.reposflag($this, mousex2, mousey2);
		}
		$this.opts.flagreference.stop().fadeTo(200,1);
	};
	
	$.fn.vhaflag.reposflag = function($this, mousex2, mousey2){
		if($this.opts.flagreference){
			toppos = $("#top").height() - $this.opts.flagreference.height() + 17;
			$this.opts.flagreference.css({'left':mousex2+'px','top':toppos+'px'})
		}
	};
	
	$.fn.vhaflag.hideflag = function($this){
		if($this.opts.flagreference){
			$this.opts.flagreference.stop().fadeTo(200,0,function(){ 
				$(this).remove(); 
				$this.opts.flagreference = false;
			});
			
		}
	};
	
})(jQuery);
