$(document).ready(function(){
	// This function is executed once the document is loaded
	
	// Caching the jQuery selectors:
	var count = $('.onlineWidget .count');
	var ncount = $('.onlineWidget .ncount');
	var panel = $('.onlineWidget .panel');
	var timeout;
 var h=$('.body').height();
	// Loading the number of users online into the count div:
	count.load('counter/online.php?uid='+$('#ud').val());
	ncount.load('counter/online.php?nuser=1&uid='+$('#ud').val());
	$('.onlineWidget').hover(
		function(){
			// Setting a custom 'open' event on the sliding panel:
			
			clearTimeout(timeout);
			timeout = setTimeout(function(){panel.trigger('open');},500);
		},
		function(){
			// Custom 'close' event:
			
			clearTimeout(timeout);
			timeout = setTimeout(function(){panel.trigger('close');},500);
		}
	);
	
	var loaded=false;	// A flag which prevents multiple ajax calls to geodata.php;
	
	// Binding functions to custom events:
	
	 panel.bind('open',function(){
			
			
		 panel.slideDown(function(){
			 
			if(!loaded)
			{
				// Loading the countries and the flags once the sliding panel is shown:
				panel.load('counter/geodata.php?uid='+$('#ud').val(), {limit: 25}, function(){ 
					if($(".onlineWidget .geoRow ").length!=0)	 {
						$("#newd").animate( { height:($(".onlineWidget .geoRow ").length*20)+"px" }, 500 );
					}
						$("body", top.document).find("iframe").animate( { height:($('.body').height()*$(".onlineWidget .geoRow ").length*10)+"px" }, 500 );
 				 });
				loaded=true; 
 			}


		});
 			if($(".onlineWidget .geoRow ").length!=0)	  
				$("#newd").animate( { height:($(".onlineWidget .geoRow ").length*20)+"px" }, 500 );
				$("body", top.document).find("iframe").animate( { height:($('.body').height()*$(".onlineWidget .geoRow ").length*10)+"px" }, 500 );
	}).bind('close',function(){

		panel.slideUp();
		$("#newd").animate( { height:"10px" }, 500 );
		 
		$("body", top.document).find("iframe").animate( { height:"150px" }, 500 );
	}); 
	
});
