/**
* phpBB3 forum functions
*/

/**
* Window popup
*/
function popup(url, width, height, name)
{
	if (!name)
	{
		name = '_popup';
	}

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

/**
* Jump to page
*/
function jumpto()
{
	var page = prompt(jump_page, on_page);

	if (page !== null && !isNaN(page) && page > 0)
	{
		document.location.href = base_url.replace(/&amp;/g, '&') + '&start=' + ((page - 1) * per_page);
	}
}

/**
* 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;
		}
	}
}

/**
* Resize viewable area for attached image or topic review panel (possibly others to come)
* e = element
*/
function viewableArea(e, itself)
{
	if (!e) return;
	if (!itself)
	{
		e = e.parentNode;
	}
	
	if (!e.vaHeight)
	{
		// Store viewable area height before changing style to auto
		e.vaHeight = e.offsetHeight;
		e.vaMaxHeight = e.style.maxHeight;
		e.style.height = 'auto';
		e.style.maxHeight = 'none';
		e.style.overflow = 'visible';
	}
	else
	{
		// Restore viewable area height to the default
		e.style.height = e.vaHeight + 'px';
		e.style.overflow = 'auto';
		e.style.maxHeight = e.vaMaxHeight;
		e.vaHeight = false;
	}
}

/**
* Set display of page element
* s[-1,0,1] = hide,toggle display,show
*/
function dE(n, s)
{
	var e = document.getElementById(n);

	if (!s)
	{
		s = (e.style.display == '' || e.style.display == 'block') ? -1 : 1;
	}
	e.style.display = (s == 1) ? 'block' : 'none';
}

/**
* Alternate display of subPanels
*/
function subPanels(p)
{
	var i, e, t;

	if (typeof(p) == 'string')
	{
		show_panel = p;
	}

	for (i = 0; i < panels.length; i++)
	{
		e = document.getElementById(panels[i]);
		t = document.getElementById(panels[i] + '-tab');

		if (e)
		{
			if (panels[i] == show_panel)
			{
				e.style.display = 'block';
				if (t)
				{
					t.className = 'activetab';
				}
			}
			else
			{
				e.style.display = 'none';
				if (t)
				{
					t.className = '';
				}
			}
		}
	}
}

/**
* Call print preview
*/
function printPage()
{
	if (is_ie)
	{
		printPreview();
	}
	else
	{
		window.print();
	}
}

/**
* Show/hide groups of blocks
* c = CSS style name
* e = checkbox element
* t = toggle dispay state (used to show 'grip-show' image in the profile block when hiding the profiles) 
*/
function displayBlocks(c, e, t)
{
	var s = (e.checked == true) ?  1 : -1;

	if (t)
	{
		s *= -1;
	}

	var divs = document.getElementsByTagName("DIV");

	for (var d = 0; d < divs.length; d++)
	{
		if (divs[d].className.indexOf(c) == 0)
		{
			divs[d].style.display = (s == 1) ? 'none' : 'block';
		}
	}
}

function selectCode(a)
{
	// Get ID of code block
	var e = a.parentNode.parentNode.getElementsByTagName('CODE')[0];

	// Not IE
	if (window.getSelection)
	{
		var s = window.getSelection();
		// Safari
		if (s.setBaseAndExtent)
		{
			s.setBaseAndExtent(e, 0, e, e.innerText.length - 1);
		}
		// Firefox and Opera
		else
		{
			var r = document.createRange();
			r.selectNodeContents(e);
			s.removeAllRanges();
			s.addRange(r);
		}
	}
	// Some older browsers
	else if (document.getSelection)
	{
		var s = document.getSelection();
		var r = document.createRange();
		r.selectNodeContents(e);
		s.removeAllRanges();
		s.addRange(r);
	}
	// IE
	else if (document.selection)
	{
		var r = document.body.createTextRange();
		r.moveToElementText(e);
		r.select();
	}
}

/**
* Play quicktime file by determining it's width/height
* from the displayed rectangle area
*/
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();
}

var pod_fix_list = [];
var small_pod_fix_list = [];
var mini_pod_fix_list = [];

function pod_start() {
  //alert("starting pod_start");
  jQuery(".pod-plain")
	.removeClass("pod-plain")
	.wrap("<div class='pod'></div>")
	.each(function() {
	    jQuery(this).parent().addClass(jQuery(this).attr('class')); })
	.attr("class", "pod-content")
	.before('<div class="pod-ul"></div><div class="pod-ur"></div>' +
		'<div class="pod-left"></div><div class="pod-right"></div>' +
		'<div class="pod-ll"></div><div class="pod-lr"></div>')
	;
	
	jQuery(".pod-left,.pod-right").each(function() {
	  pod_fix_list[pod_fix_list.length] = jQuery(this);
  });
  pod_fix();
  //alert("finished pod_start");
}				   

function pod_fix() {
  //alert("starting pod_fix");
  //alert("pod_fix_list = " + pod_fix_list.length);
  jQuery.each(pod_fix_list, function(i) {
	var c = pod_fix_list[i];
	var h = Math.max(jQuery(c).parent().height() - jQuery(c).siblings(".pod-ul").height() - jQuery(c).siblings(".pod-ur").height(), 0) + 15;
	jQuery(c).height(h+"px");
	//alert("setting height to " + h + "px");
  });
  //alert("finished pod_fix");
}

function small_pod_start() {
  //alert("starting small_pod_start");
  jQuery(".pod-small-plain")
	.removeClass("pod-small-plain")
	.wrap("<div class='pod-small'></div>")
	.each(function() {
	    jQuery(this).parent().addClass(jQuery(this).attr('class')); })
	.attr("class", "pod-small-content")
	.before('<div class="pod-small-ul"></div><div class="pod-small-ur"></div>' +
		'<div class="pod-small-left"></div><div class="pod-small-right"></div>' +
		'<div class="pod-small-ll"></div><div class="pod-small-lr"></div>')
	;
	
	jQuery(".pod-small-left,.pod-small-right").each(function() {
	  small_pod_fix_list[small_pod_fix_list.length] = jQuery(this);
  });
  small_pod_fix();
  //alert("finished small_pod_start");
}				   

function small_pod_fix() {
  //alert("starting small_pod_fix");
  //alert("small_pod_fix_list = " + small_pod_fix_list.length);
  jQuery.each(small_pod_fix_list, function(i) {
	var c = small_pod_fix_list[i];
	var h = Math.max(jQuery(c).parent().height() - jQuery(c).siblings(".pod-small-ul").height() - jQuery(c).siblings(".pod-small-ur").height(), 0) + 15;
	jQuery(c).height(h+"px");
	//alert("setting height to " + h + "px");
  });
  //alert("finished small_pod_fix");
}

function mini_pod_start() {
  //alert("starting mini_pod_start");
  jQuery(".pod-mini-plain")
	.removeClass("pod-mini-plain")
	.wrap("<div class='pod-mini'></div>")
	.each(function() {
	    jQuery(this).parent().addClass(jQuery(this).attr('class')); })
	.attr("class", "pod-mini-content")
	.before('<div class="pod-mini-ul"></div><div class="pod-mini-ur"></div>' +
		'<div class="pod-mini-left"></div><div class="pod-mini-right"></div>' +
		'<div class="pod-mini-ll"></div><div class="pod-mini-lr"></div>')
	;
	
	jQuery(".pod-mini-left,.pod-mini-right").each(function() {
	  mini_pod_fix_list[mini_pod_fix_list.length] = jQuery(this);
  });
  mini_pod_fix();
  //alert("finished mini_pod_start");
}				   

function mini_pod_fix() {
  //alert("starting mini_pod_fix");
  //alert("mini_pod_fix_list = " + mini_pod_fix_list.length);
  jQuery.each(mini_pod_fix_list, function(i) {
	var c = mini_pod_fix_list[i];
	var h = Math.max(jQuery(c).parent().height() - jQuery(c).siblings(".pod-mini-ul").height() - jQuery(c).siblings(".pod-mini-ur").height(), 0) + 9;
	jQuery(c).height(h+"px");
	//alert("setting height to " + h + "px");
  });
  //alert("finished mini_pod_fix");
}

function initialize_pods() {
  pod_start();
  small_pod_start();
  mini_pod_start();
}

jQuery(window).resize(function() {
  pod_fix();
  small_pod_fix();
  mini_pod_fix();
});


