function popup(url, width, height, name)
{
	name = (!name) ? '_popup' : name;

	window.open(url.replace(/&amp;/g, '&'), name, 'height=' + height + ',resizable=yes,scrollbars=yes,width=' + width);
	return false;
}

function jumpto() {
	$("#pagination-dialog").show();
	$("#pageNumber").keydown(function(event) {
		if(event.which == 13) {
			$(this).dialog("close");
			goToPage($("#pageNumber").val());
		}
	});
	$("#pagination-dialog").dialog({
		modal: true,
		buttons: {
			Ok: function() {
				goToPage($("#pageNumber").val());
			},
			Cancel: function() {
				$(this).dialog("close");
			}
		}
	});
}

function goToPage(page) {
	if (page !== null && !isNaN(page) && page == Math.floor(page) && page > 0) {
		if (config.base_url.indexOf('?') == -1) {
			document.location.href = config.base_url + '?start=' + ((page - 1) * config.per_page);
		} else {
			document.location.href = config.base_url.replace(/&amp;/g, '&') + '&start=' + ((page - 1) * config.per_page);
		}
	}
}

function toggleBots() {
	var botLinks = $(".bot");

	if(botLinks.is(":visible")) {
		botLinks.hide();
		$("#toggleBots").text("Show Bots");
	} else {
		botLinks.show();
		$("#toggleBots").text("Hide Bots");
	}
}

function displayPMDialog() {
	$( "#newPrivateMessages_dialog" ).show();
	$( "#newPrivateMessages_dialog" ).dialog({
		modal: true,
		buttons: {
			Ok: function() {
				document.location.href = config.inbox;
			},
			Cancel: function() {
				$(this).dialog("close");
			}
		}
	});
}

/**
* Find a member
*/
function find_username(url)
{
	popup(url, 760, 570, '_usersearch');
	return false;
}

/**
* Mark/unmark checklist
* id = ID of parent container, name = name prefix, state = state [true/false]
*/
function marklist(id, name, state)
{
	var parent = document.getElementById(id);
	if (!parent)
	{
		eval('parent = document.' + id);
	}

	if (!parent)
	{
		return;
	}

	var rb = parent.getElementsByTagName('input');
	
	for (var r = 0; r < rb.length; r++)
	{
		if (rb[r].name.substr(0, name.length) == name)
		{
			rb[r].checked = state;
		}
	}
}

/**
* Play quicktime file by determining it's width/height
* from the displayed rectangle area
*
* Only defined if there is a file block present.
*/
function play_qt_file(obj)
{
	var rectangle = obj.GetRectangle();

	if (rectangle)
	{
		rectangle = rectangle.split(',')
		var x1 = parseInt(rectangle[0]);
		var x2 = parseInt(rectangle[2]);
		var y1 = parseInt(rectangle[1]);
		var y2 = parseInt(rectangle[3]);

		var width = (x1 < 0) ? (x1 * -1) + x2 : x2 - x1;
		var height = (y1 < 0) ? (y1 * -1) + y2 : y2 - y1;
	}
	else
	{
		var width = 200;
		var height = 0;
	}

	obj.width = width;
	obj.height = height + 16;

	obj.SetControllerVisible(true);

	obj.Play();
}

/**
* New function for handling multiple calls to window.onload and window.unload by pentapenguin
*/
window.onload = function()
{
	for (var i = 0; i < onload_functions.length; i++)
	{
		eval(onload_functions[i]);
	}
}

window.onunload = function()
{
	for (var i = 0; i < onunload_functions.length; i++)
	{
		eval(onunload_functions[i]);
	}
}
if(navigator.userAgent && navigator.userAgent.indexOf('Mac OS X') > 0)
{
	document.write('<link rel="stylesheet" href="' + config.theme_path + '/mac.css" type="text/css" />');
}

/*
 * @TODO: This is fucking horrible...................
 */

$(document).ready(function(){
	$('.poptip').mouseenter(function(){
		var text = $(this).attr('title');
		var posi = $(this).position();
		var top  = posi.top;
		var left = posi.left+5;
		var wid	 = $(this).width();
		$(this).attr('title','');
		$(this).parent().append('<div class="poptext">'+text.replace('|','<br />')+'</div>');
		$('.poptext').css({'left':(left+wid)+'px',
					'top':(top-$('.poptext').height())+'px',
		});
		$('.poptext').fadeIn('slow');
	});

	$('.poptip').mouseleave(function(){
		var title = $(this).parent().find('.poptext').html();
		$(this).attr('title',title.replace('<br />','|'));
		$(this).parent().find('.poptext').fadeOut('slow');
		$(this).parent().find('.poptext').remove();
	});

	$("a.cross").click(function() {
        $(".global-notice").fadeOut();
        $.cookie('noticeState', 'hidden', { expires: 3, path: '/'});
    });

    if($.cookie('noticeState') == 'hidden') {
        $(".global-notice").hide();
    };

	if (user.new_pm)
	{
		displayPMDialog();
	}

	$(".row1 > .socialLinks").hide();
	$(".row1").hover(function() { 
			$(this).find(".socialLinks").show();
		},
		function() {
			$(this).find(".socialLinks").hide();
		}
	);
});

if(config.use_ga)
{
	/*
	 * Google Analytics
	 */
	var _gaq = _gaq || [];
	_gaq.push(['_setAccount', 'UA-9246967-30']);
	_gaq.push(['_trackPageview']);

	(function() {
		var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
		ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
		var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
	})();	
}
