//global vars

var notifier;
var snd={};
snd.level=0;

function includeJS(jsFile)
{
  document.write('<script type="text/javascript" src="' + jsFile + '"></scr' + 'ipt>'); 
}

includeJS('/js/jquery.hoverIntent.minified.js');

function initCore()
{
	enhanceUserLabels();
	enhanceMapLabels();
	
	notifier=new Notifier($('#NotifierContainer'));
	swfsound.embedSWF( '/swf/swfsound.swf' );
	if (USERDETAILS.sound>0)
  {
		swfsound.onload = function()
	  {
			snd.dran= swfsound.loadSound(USERDETAILS.soundfile, streaming = false );
	  }
  }
}

$(document).ready(function(){
	initCore();
});

function enhanceUserLabels()
{
    $(".userLabel").hoverIntent({
			sensitivity: 3, 
			interval: 200, 
			timeout: 50, 
    	    over: function () {
    	    	//cap "userLabel"
    	    	var id=$(this).attr('name').substring(9);
    	        $(this).append($('<div class="userTag" id="activeUserTag"><p style="text-align: center"><img src="/images/loading.gif"></p></div>'));
    	        var aut=$('#activeUserTag');
    	        aut.animate({
    	        		'marginLeft' : "+=10px"
    	        	},{ 'duration': 0 }
    	        );
    	        arrangeToScreen(aut);
    	        
    	        $('#activeUserTag').load('/user/showTag?id='+id);
    	      }, 
    	    out: function () {
    	    	$('#activeUserTag').remove();
   	        }
    });
}

function enhanceMapLabels()
{
    $(".mapLabel").hoverIntent({
			sensitivity: 3, 
			interval: 200, 
			timeout: 50, 
    	    over: function () {
    	    	//cap "mapLabel"
    	    	var id=$(this).attr('name').substring(8);
    	        $(this).append($('<div class="mapTag" id="activeMapTag"><p style="text-align: center"><img src="/images/loading.gif"></p></div>'));
    	        var amt=$('#activeMapTag');
    	        //console.log(amt);
    	        amt.animate({
    	        		'marginLeft' : "+=10px"
    	        	},{ 'duration': 0 }
    	        );
    	        arrangeToScreen(amt);
    	        $('#activeMapTag').load('/map/showTag?id='+id);
    	      }, 
    	    out: function () {
    	    	$('#activeMapTag').remove();
   	        }
    });
}

function arrangeToScreen(item)
{
    //Check if inside the screen
    var o=item.offset();
    var x=o.left;
    var y=o.top;
    var diffX=0;
    var diffY=0;
    	        
    var maxX=$(window).width()-item.width();
    var maxY=$(window).height()-item.height();
    	        
    if((x<0)||(x>maxX))
    {
    	diffX=maxX-x;
    }
    	        
    if((y<0)||(y>maxY))
    {
    	diffY=maxY-y;
    }
    	        
    item.animate({
    		'marginLeft' : "+="+diffX+"px",
    		'marginTop' : "+="+diffY+"px"
    	},{ 'duration': 0 }
    );
}
	        
function linkify(text)
{
	if( !text ) return text;
	text = text.replace(/((https?\:\/\/|ftp\:\/\/)|(www\.))(\S+)(\w{2,4})(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/gi,function(url){
		nice = url;
		if( url.match('^https?:\/\/') )
		{
			nice = nice.replace(/^https?:\/\//i,'')
		}
		else
			url = 'http://'+url;
 
		return '<a target="_blank" rel="nofollow" href="'+ url +'">'+ nice.replace(/^www./i,'') +'</a>';
	});
	return text;
}

function gidLinkify(text)
{
	if( !text ) return text;
	/<B>(.*?)<\/B>.*/.exec(text);
	var speaker=RegExp.$1;
	text=text.replace('/me',speaker);
	text = text.replace(/GID[ =]([0-9]{3,6})/gi,function(){
		var gid=RegExp.$1;
		$.getJSON('/api/game/'+gid+'/info.json?callback=?',function(gameInfo){
			$('a.GidLink'+gid).text(gid +' - '+gameInfo.game.name);
		});
		return '<a class="GidLink'+gid+'" href="http://www.karopapier.de/showmap.php?GID='+gid+'" target="_blank">'+gid+'</a>';
	});
	return text;
}

