$(document).ready(function() {
	$('.profile').live('click', function(e) {
 		var url = '/forum/profilecard.php?u='+$(this).attr('profile_id')+'&r='+Math.random();
		showHint(url, $(this));
		return false;
	});
	$('.mini-post').live('click', function(e) {
		var url= "/forum/viewpost.php?p="+$(this).attr('post_id');
		showHint(url, $(this));
		return false;
    });	
	$('.mini-privmsg').click(function(e) {

		var url= "/forum/viewprivmsg.php?view=1&rnd="+Math.round(Math.random()*1000);
		showHint(url, $(this));
    });	
	$('.mini-post_menu').mouseover(function(e) {
		var menu = $('#postmenu'+$(this).attr('post_id'));
		menu.css({ 'top' : ($(this).offset().top + $(this).height()+'px'), 'left' : ($(this).offset().left + $(this).width()- menu.width() +'px')});
		menu.show();
		menu.mouseleave(function(e) { $(this).hide()});
    });	
	$('.mainmenu li ul li, .mainmenu li').hover( function() { $(this).addClass('ie_hover') }, function() { $(this).removeClass('ie_hover') });
	$(document).keydown(function(e) { 
		if (!(e.ctrlKey &&  (e.keyCode == 37 || e.keyCode == 39)) || e.target.tagName == "TEXTAREA" || e.target.tagName == "INPUT") return;
		if (e.keyCode == 39 && $("link[rel='next']").length)
		{
			document.location= $("link[rel='next']").attr('href');
		}
		else if ($('link[rel="prev"]').length)
		{
			document.location= $('link[rel="prev"]').attr('href');
		};
	});	
	
});

function showHint(url, obj)
{
	if ($('#hint-container').length == 0) $('BODY').append($('<div id="hint-container"></div>'));

	$('#hint-container').css({ 'top' : (obj.offset().top+obj.outerHeight() +'px'), 'left' : (obj.offset().left+'px'), width : 'auto'}).
		html('<img src="/forum/images/loading.gif" border="0"/>');
	$('#hint-container').show('fast', function(){
		$(this).bind('mouseleave click touchstart', function() {
			$(this).hide('fast', function() {
				$(this).unbind()
			});
		});
	});					
	$.get(url, function(data) { 		
		$('#hint-container').html(data);		
		if ( ($('#hint-container').offset().left + $('#hint-container').outerWidth()) > $('BODY').width()- $('BODY').position().left)
			$('#hint-container').animate({left : ($('BODY').width() - $('BODY').position().left -  $('#hint-container').outerWidth()-25+'px')});
	});
	return false;	
};

var priv_page_url ='';
var next_page_url ='';

var postImageEl = null;
var postImageIm = null;
var postImageTimerID = null;

function postImageOver(e)
{
	e = e || window.event;
	postImageEl = (e.target||e.srcElement);
	if (postImageEl)
	{		
		postImageIm = new Image();
		postImageIm.src = postImageEl.src;
		if ((postImageIm.complete == true) && ((postImageIm.width > postImageEl.width) || (postImageIm.height > postImageEl.height)))
		{
			postImageEl.setAttribute('oldWidth', postImageEl.width);
			postImageEl.setAttribute('oldHeight', postImageEl.height);
			postImageTimerID = window.setTimeout('postImageShow(\'enlarge\');',500);
		};
	};
}

function postImageShow(direction)
{
	if (direction == 'enlarge')
	{
		if ((postImageIm.width > postImageEl.width) || (postImageIm.height > postImageEl.height))
		{
			postImageEl.style.width = Math.ceil(postImageEl.width + (postImageIm.width - postImageEl.width) / 2);	
			postImageEl.style.maxWidth = Math.ceil(postImageEl.width + (postImageIm.width - postImageEl.width) / 2) + 'px';
			postImageTimerID = window.setTimeout('postImageShow(\'enlarge\');',20);		
		};
	}
	else if ((postImageEl.getAttribute('oldWidth') > postImageEl.width) || (postImageEl.getAttribute('oldHeight') > postImageEl.height))
	{
			postImageEl.style.width = Math.ceil(postImageIm.getAttribute('oldWidth') - (postImageEl.width - postImageIm.getAttribute('oldWidth')) / 2);	
			postImageEl.style.maxWidth = Math.ceil(postImageIm.getAttribute('oldWidth') - (postImageEl.width - postImageIm.getAttribute('oldWidth')) / 2) + 'px';
			postImageTimerID = window.setTimeout('postImageShow(\'shrink\');',200);					
	};	
}

function postImageOut(e)
{
	e = e || window.event;
	postImageEl = (e.target||e.srcElement);
	if (postImageEl)
	{
		if (postImageTimerID)
		{
			window.clearTimeout(postImageTimerID);
			postImageTimerID = null;
		};
		postImageEl.style.maxWidth = '';
		postImageEl.style.width = '';			
	};
}


function hitTest(el)
{
	alert('hittest');
  var p = elMouseOver;
  while (p) {
	  if (p == el)
	  {
		  return true;
	  };
	  p = p.parentElement || p.parentNode;
  };
  return false; 
}

function getAbsPos(p) {
	var s = { x:0, y:0 };
	while (p.offsetParent) {
			s.x += p.offsetLeft;
			s.y += p.offsetTop;
			p = p.offsetParent;
	}
	return s;
};

function getById(id) {
	alert('getByid');
	if(document.getElementById) {
		return document.getElementById(id);
	}else if(document.all) {
		return document.all[id];
	}else if(document.layers) {
		return document.layers[id];
	}
	return null;
};


function getBookPreview(container, url, start)
{
	$(container).load(url+'&mode=preview&start='+start+'&cnt='+container+'&rnd='+Math.round(Math.random()*1000));
};

