$(document).ready(function(){
	$(".showUser").click(function(){
		window.startOverlay($(this).attr("id"));
		return false;
	});
	
	$(".nosudzi").click(function(){
		//getMousePos();
		window.pasudzi($(this).attr("id"));
		return false;
	});	

});

function pasudzi(id) {
	var arr = id.split("_");
	var bad = arr[1];
	var good = arr[2];
	var url = window.location;
	//alert(mX+' '+mY);
	$("body").append('<div class="sudz"></div><div class="pasudzies">Ielāde...</div>');		
	$(".sudz").css({"height":document.body.clientHeight+'px'}).animate({"opacity":"0.6"}, 400, "linear");
	

	$(".pasudzies").load("/ajax/userInfo/pasudzies/").css({"margin-top": 0,"top":mY-50+'px', "left": mX+'px'});
	
	$(".pasudzies").ajaxComplete(
		function(request, settings){
   			$("#sudzambibel").click(
				function() {
					$.post(
							"/ajax/userInfo/pasudzies/",
							{
								"g":good,
								"b":bad,
								"u":url,
								"i":$("#sudziba").val()
							},
							$(".pasudzies").html('Sūdzība nosūtīta<br /><a onclick="aizvert(); return false;" href="#">Aizvērt</a>'),
							"text"
						);
				}
			);
			
			$("#nesudzambibel").click(
				function () {
					aizvert();
				}
			);
 		}
	);

	//$(".pasudzies").css({"margin-top": 0,"top":mY-50+'px', "left": mX+'px'});
	window.removeOverlay3();		 
}

function startOverlay(user_id) {
	//$("body").append('<div class="userInfo"></div><div class="userContent"><div class="loading"><img src="/img/ajax-loader.gif" border="0" alt="" /><br /><small>ielāde</small></div></div>');		
	$("body").append('<div class="userInfo"></div><div class="userContent"><div class="loading"><small>ielāde</small></div></div><img style="position: absolute;" class="close" src="/images/close.gif" border="0" alt="Aizvērt">');		
	
	$(".userInfo").css({"height":document.body.clientHeight+'px'}).animate({"opacity":"0.6"}, 400, "linear");
	
	$(".loading img").load(function() {
		
		var left = (myWidth()-$(".loading img").width())/2;
		var top = getScrollXY()+300;
				
		$(".loading")
			.css({
				"top":		top+'px',
				"left":		left+'px',
				"width":      $(".loading img").width(),
				"height":     $(".loading img").height()+10,
				"margin-top": -($(".loading img").height()/2),
				"margin-left":-($(".loading img").width()/2) //to position it in the middle
			});
	});
	
	
	$(".userContent").load("/ajax/userInfo/getData/?id="+user_id+"&r="+Math.random()).css({"top":getScrollXY()+'px', "left":"200px"});
	
	$(".close")
		.css({
				"top":getScrollXY()+4+'px', 
				"left":"778px", 
				"z-index":"10002",
				"cursor":"pointer"
				})
		.click(function(){
			$(".userContent, .userInfo, .userProfile, .close").remove();
		});
	
	window.removeOverlay();	
}

function removeOverlay() {
	$(".userInfo").click(function(){
		$(".userContent, .userInfo, .userProfile, .close").remove();	
	});
}

function removeOverlay2() {
	$(".userInfo").click(function(){
		$(".userInfo, .pasudzies, .close, .userContent, .userInfo, .userProfile").remove();	
	});
}

function removeOverlay3() {
	$(".usudz").click(function(){
		$(".sudz, .pasudzies").remove();
	});
}

function aizvert() {
	$(".sudz, .pasudzies").remove();	
}

function getScrollXY() {
	var scrOfX = 0, scrOfY = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
	return scrOfY;
}

function myWidth() {
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	return myWidth;
}

function myHeight() {
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	return myHeight;
}


var IE = document.all?true:false
if (!IE) {
	document.captureEvents(Event.MOUSEMOVE)
}
document.onmousemove = getMouseXY;
var mX = 0;
var mY = 0;

// Main function to retrieve mouse x-y pos.s

function getMouseXY(e) {
  if (IE) {
    mX = event.clientX + document.body.scrollLeft;
    mY = event.clientY + document.body.scrollTop;
  } else {  // grab the x-y pos.s if browser is NS
    mX = e.pageX;
    mY = e.pageY;
  }  
  if (mX < 0){mX = 0}
  if (mY < 0){mY = 0}  
  return true;
}
