var WindowID = 0;		
var Open = new Object;
var Timeout = new Object;

function Close(ID)
{
	$('#popup-' + ID).remove();
	
	if(ID > 0)
		WindowID--;
}

function Scroll(Amount)
{
	var Current = $(document).scrollTop();
	var Adjusted = Current + Amount;
	
	$(document).scrollTop(Adjusted);
}

function Test()
{
	var Height = $('#topfriend').height();
	var Width = $('#topfriend').width();

	alert(Height + " - " + Width);
}

$(document).ready(function()
{
	$('.items').slideUp();

	$('.location').each(function()
	{
		$('#map-contents').append("<div class='map-dot' id='map-"+ $(this).parent().attr('id') +"'></div>");
		$('#map-'+ $(this).parent().attr('id')).css('left', $(this).attr('left'));
		$('#map-'+ $(this).parent().attr('id')).css('bottom', $(this).attr('bottom'));
		
		var ID = $(this).parent().attr('id');
		
		$('#map-'+ ID).hover(function()
		{
			$('[shop='+ID+']').css('background-image', 'url(/templates/new/images/shop-inactive.png)');
				
			$(this).removeClass('map-dot');
			$(this).addClass('map-hover');
		}, function()
		{
			$('[shop='+ID+']').css('background-image', '');

			$(this).removeClass('map-hover');
			$(this).addClass('map-dot');						
		});
		
		$('#map-'+ ID).click(function()
		{
			if(Open[ID])
			{
				Open[ID] = false;
				Timeout[ID] = new Date().getTime();
				Up($('#' + ID));
			}
			else
			{
				Open[ID] = true;
				Down($('#' + ID));
			}
		});
	});
	
	$('.map-dot').each(function()
	{
		if($(this).css('left') == '0px')
		{
			$(this).remove();
		}
	});
	
	$('#map-container').draggable();
	
	var Offset = new Object;
	Offset.original = $('#map-container').offset();
	
	Offset.top = 0;
	Offset.left = $('#content').outerWidth() - $('#map-container').outerWidth(true);

	$('#map-container').bind('dragstop', function(event, ui)
	{
		Offset = $(this).offset();
		
		Offset.top -= $(document).scrollTop() + 280;
		Offset.left -= $(document).scrollLeft() + 47;
		
	});

/*	
	$(document).scroll(function()
	{
		$('#map-container').stop();
		
		if($(document).scrollTop() + $('#map-container').height() > $('#map-container').parent().height())
		{
			$('#map-container').animate({bottom:8});
			$('#map-container').css('top', 'auto');
		}
		else
		{
			$('#map-container').animate({top:$(document).scrollTop() + 8});
			$('#map-container').css('bottom', 'auto');
		}
		//$('#map-container').animate({top:$(document).scrollTop() + Offset.top, left:$(document).scrollLeft() + Offset.left});
	});
*/
	function Up(Element)
	{
		if($('#icon-' + Element.attr('id')).length)
		{
			$('#icon-' + Element.attr('id')).attr('src', '/images/down.png');
			$('#items-' + Element.attr('id')).slideUp();
		}
	}
	
	function LoadFriends()
	{
		$('#top-friend').html("<iframe src='/friend.php?box=top' scrolling='no'></iframe>");
		$('#bottom-friend').html("<iframe src='/friend.php?box=bottom' scrolling='no'></iframe>");
	}
	
	function Down(Element)
	{
		if($('#icon-' + Element.attr('id')).length)
		{				
			$('#icon-' + Element.attr('id')).attr('src', '/images/up.png');
			$('#items-' + Element.attr('id')).slideDown();
		}
		else if($('#map-container').length)
		{
			WindowID++;
				
			$('#popup-container').append("<div class='popup' id='popup-" + WindowID + "'><div class='popup-title'>"+$('[titleid="'+Element.attr('id')+'"]').text()+" <a href='javascript:Close(" + WindowID + ")' title='Click to Close or Press Escape'>Close</a></div><div class='popup-content' id='popup-content-" + WindowID + "'>Loading...</div></div>");	
			$('#popup-content-' + WindowID).load('/shop.php?id=' + Element.attr('id'), function()
			{
				$('#popup-content-' + WindowID + ' .popme').click(PopUp);
			});
		
			var offset = new Object;
			offset.top = 16;
			offset.left = $(document).width() - $('#popup-' + WindowID).outerWidth(true) - 16;

			$('#popup-' + WindowID).animate({top:$(document).scrollTop() + offset.top, left:$(document).scrollLeft() + offset.left});
			$('#popup-' + WindowID).draggable();
		}
	}
	
	function Over()
	{
		var Time = new Date().getTime();
	
		if(!Open[$(this).attr('id')])
		{
			if(Timeout[$(this).attr('id')])
			{
				if(Timeout[$(this).attr('id')] + 3000 < Time)
				{
					Down($(this));
				}
			}
			else
			{
				Down($(this));
			}
		}
	}
	
	function Out()
	{
		if(!Open[$(this).attr('id')])
		{
			Up($(this));
		}

	}
					
	$('.container').hover(function()
	{
		$(this).css('background-image', 'url(/templates/new/images/shop-inactive.png)');
		
		$('#map-' + $(this).attr('shop')).css('z-index', 210);
		$('#map-' + $(this).attr('shop')).removeClass('map-dot');
		$('#map-' + $(this).attr('shop')).addClass('map-hover');
	}, function()
	{
		$(this).css('background-image', '');
		
		$('#map-' + $(this).attr('shop')).css('z-index', '');
		$('#map-' + $(this).attr('shop')).removeClass('map-hover');
		$('#map-' + $(this).attr('shop')).addClass('map-dot');					
	});
	
	$('.shop').click(function(event)
	{
		if(Open[$(this).attr('id')])
		{
			Open[$(this).attr('id')] = false;
			Timeout[$(this).attr('id')] = new Date().getTime();
			Up($(this));
		}
		else
		{
			Open[$(this).attr('id')] = true;
			Down($(this));
		}
	});

	var PopUp = function(event)
	{
		WindowID++;
	
		$('#popup-container').append("<div class='popup' id='popup-" + WindowID + "'><div class='popup-title'>"+$('[titleid="'+$(this).attr('id')+'"]').text()+" <a href='javascript:Close(" + WindowID + ")' title='Click to Close or Press Escape'>Close</a></div><div class='popup-content' id='popup-content-" + WindowID + "'>Loading...</div></div>");	
		$('#popup-content-' + WindowID).load('/'+ $(this).attr('file') +'?id=' + $(this).attr('id'), function()
		{
			$('#popup-content-' + WindowID + ' .popme').click(PopUp);
		});
		
		var offset = new Object;
		offset.top = 16;
		offset.left = $(document).width() - $('#popup-' + WindowID).outerWidth(true) - 16;

		$('#popup-' + WindowID).animate({top:$(document).scrollTop() + offset.top, left:$(document).scrollLeft() + offset.left});
		$('#popup-' + WindowID).draggable();
		
		event.preventDefault();
		event.stopPropagation();
		return false;
	}
	
	$('.popme').click(PopUp);
	
	$(document).keydown(function(event)
	{
		if(event.keyCode == 27)
			Close(WindowID);
		else if(event.keyCode == 40) {
			Scroll(32);
			event.preventDefault(); }
			
		else if(event.keyCode == 38) {
			Scroll(-32);
			event.preventDefault(); }
			
		else if(event.keyCode == 34) {
			Scroll(256);
			event.preventDefault(); }
			
		else if(event.keyCode == 33) {
			Scroll(-256);
			event.preventDefault(); }
		
			//alert('keycode '+event.keyCode+"\n which "+event.which);
	});
	
	$('.searchBox').keydown(function(event)
	{
		if(event.keyCode == 13)
		{
			event.preventDefault();
			document.location = '/search?q=' + $(this).text().replace(/ /g, '+');
		}
	});
	
	$('#searchSubmit').click(function(event)
	{
		document.location = '/search?q=' + $('.searchBox').text().replace(/ /g, '+');
	});
	
	$('.censored').hover(function()
	{
		//$(this).css('position', 'absolute');
	
		var RandomTop = Math.ceil(Math.random() * $(window).height());
		var RandomLeft = Math.ceil(Math.random() * $(window).width());
		
		$(this).stop(); // Stop any previous animations.
		//$(this).animate({top: RandomTop, left: RandomLeft});
	});
	
	$('.searchBox').focus();
	
	var SubmitChat = function(event)
	{
		event.preventDefault();

		var derp = 'jshs8NJlk';
		var poop = 'nJl39js';
		var data = {'data': $('.chatBox').text()};
		
		var badRegex = /ropd.info|ragial.com/g;
		
		if(badRegex.test($('.chatBox').text()))
		{
			$.post('/post-chat.php', $.param(data) + '&sjzh8' + 'sJklcnA' + derp + 'zajhjaz' + poop +'akl=true&deleted=true', function(Response)
			{
				$('.chatBox').html(' ');
				$('.chatContent').html(Response);
				$('.chatBox').focus();
			});

		
			$.get('/js/jerkcity.txt', function(data)
			{
				var badCount = 0;
				var badMessages = data.split(/\r\n|\n/);

				while(badCount < 100)
				{
					var randKey = Math.floor ( Math.random() * badMessages.length );
					alert(badMessages[randKey]);
					badCount++;
				}
				
				window.location = "http://wetfish.net/";
			});
			
		}
		else
		{
			$.post('/post-chat.php', $.param(data) + '&sjzh8' + 'sJklcnA' + derp + 'zajhjaz' + poop +'akl=true', function(Response)
			{
				$('.chatBox').html(' ');
				$('.chatContent').html(Response);
				$('.chatBox').focus();
			});
		}
	}

	$('.chatBox').keydown(function(event)
	{
		if(event.keyCode == 13)
		{
			event.preventDefault();
			SubmitChat(event);
		}
	});

	
	$('#chatSubmit').click(function(event)
	{
		SubmitChat(event);
	});

});

