/**
* phpBB3 forum functions
* for SigilTheme3 (3.2)
*/


/* http://www.quirksmode.org/js/cookies.html */

function createCookie(name,value,days) { if (days) { var date = new Date(); date.setTime(date.getTime()+(days*24*60*60*1000)); var expires = "; expires="+date.toGMTString(); } else var expires = ""; document.cookie = name+"="+value+expires+"; path=/"; }
function readCookie(name) { var nameEQ = name + "="; var ca = document.cookie.split(';'); for(var i=0;i < ca.length;i++) { var c = ca[i]; while (c.charAt(0)==' ') c = c.substring(1,c.length); if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); } return null; }
function eraseCookie(name) { createCookie(name,"",-1); }


/**
* 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 == Math.floor(page) && page > 0) {
		if (base_url.indexOf('?') == -1) {
			document.location.href = base_url + '?start=' + ((page - 1) * per_page);
		}
		else {
			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 {
			// workaround for bug # 42885
			if (window.opera && e.innerHTML.substring(e.innerHTML.length - 4) == '<BR>') {
				e.innerHTML = e.innerHTML + '&nbsp;';
			}

			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();
}


/**
* Check if the nodeName of elem is name
* @author jQuery
*/
function is_node_name(elem, name)
{
	return elem.nodeName && elem.nodeName.toUpperCase() == name.toUpperCase();
}

/**
* Check if elem is in array, return position
* @author jQuery
*/
function is_in_array(elem, array) {
	for (var i = 0, length = array.length; i < length; i++)
		// === is correct (IE)
		if (array[i] === elem) {
			return i;
		}

	return -1;
}

/**
* Find Element, type and class in tree
* Not used, but may come in handy for those not using JQuery
* @author jQuery.find, Meik Sievertsen
*/
function find_in_tree(node, tag, type, class_name) {
	var result, element, i = 0, length = node.childNodes.length;

	for (element = node.childNodes[0]; i < length; element = node.childNodes[++i]) {
		if (!element || element.nodeType != 1) continue;

		if ((!tag || is_node_name(element, tag)) && (!type || element.type == type) && (!class_name || is_in_array(class_name, (element.className || element).toString().split(/\s+/)) > -1)) {
			return element;
		}

		if (element.childNodes.length) {
			result = find_in_tree(element, tag, type, class_name);
		}

		if (result) return result;
	}
}

/**
* Usually used for onkeypress event, to submit a form on enter
*/
function submit_default_button(event, selector, class_name) {
	// Add which for key events
	if (!event.which && ((event.charCode || event.charCode === 0) ? event.charCode : event.keyCode)) {
		event.which = event.charCode || event.keyCode;
	}

	// Keycode is not return, then return. ;)
	if (event.which != 13) return true;

	var current = selector['parentNode'];

	// Search parent form element
	while (current && (!current.nodeName || current.nodeType != 1 || !is_node_name(current, 'form')) && current != document)
		current = current['parentNode'];

	// Find the input submit button with the class name
	//current = find_in_tree(current, 'input', 'submit', class_name);
	var input_tags = current.getElementsByTagName('input');
	current = false;

	for (var i = 0, element = input_tags[0]; i < input_tags.length; element = input_tags[++i]) {
		if (element.type == 'submit' && is_in_array(class_name, (element.className || element).toString().split(/\s+/)) > -1) {
			current = element;
		}
	}

	if (!current) return true;

	// Submit form
	current.focus();
	current.click();
	return false;
}

/**
* Apply onkeypress event for forcing default submit button on ENTER key press
* The jQuery snippet used is based on http://greatwebguy.com/programming/dom/default-html-button-submit-on-enter-with-jquery/
* The non-jQuery code is a mimick of the jQuery code ;)
*/
function apply_onkeypress_event() {
	// jQuery code in case jQuery is used
	if (jquery_present) {
		$('form input').live('keypress', function (e) {
			var default_button = $(this).parents('form').find('input[type=submit].default-submit-action');
			
			if (!default_button || default_button.length <= 0)
				return true;

			if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
				default_button.click();
				return false;
			}

			return true;
		});

		return;
	}

	var input_tags = document.getElementsByTagName('input');

	for (var i = 0, element = input_tags[0]; i < input_tags.length ; element = input_tags[++i]) {
		if (element.type == 'hidden') continue;

		// onkeydown is possible too
		element.onkeypress = function (evt) { submit_default_button((evt || window.event), this, 'default-submit-action'); }; 
	}
}

/**
* Detect JQuery existance. We currently do not deliver it, but some styles do, so why not benefit from it. ;)
*/
var jquery_present = typeof jQuery == 'function';


/**
* SigilTheme3 functions
*/
function st3autoselect(option) {
	document.getElementById(option).selected = true;
}


function st3mcpShowHide(review, type) {
	var e = review.getElementsByTagName(type);

	switch (type) {
		case 'img' :
			for (var i = 0; i < e.length; i++)
				if ( ( (e[i].parentNode.className == 'content') || (e[i].parentNode.parentNode.className == 'attach-image') || (e[i].parentNode.parentNode.tagName == 'BLOCKQUOTE') )
						&& (e[i].offsetWidth > '40') && (e[i].offsetHeight > '40') ) {
					if (!e[i].vaSrc) {
						e[i].vaSrc = e[i].src;
						e[i].vaOnclick = e[i].onclick;
						e[i].vaClassName = e[i].className;
						e[i].className = 'hidden-image';
						e[i].style.width = e[i].offsetWidth-4 + 'px';
						e[i].style.height = e[i].offsetHeight-4 + 'px';
						e[i].src = './images/spacer.gif';
						e[i].onclick = 'return false;';
					}
					else {
						e[i].src = e[i].vaSrc;
						e[i].onclick = e[i].vaOnclick;
						e[i].className = e[i].vaClassName;
						e[i].style.width = '';
						e[i].style.height = '';
						e[i].vaSrc = false;
					}
				}
			break;
		case 'code' :
			for (var i = 0; i < e.length; i++)
				if ((e[i].parentNode.parentNode.className) == 'codebox') {
					if (e[i].style.display == 'none') {
						e[i].style.display = '';
						e[i].parentNode.parentNode.getElementsByTagName('DT')[0].style.paddingBottom = '';
						e[i].parentNode.parentNode.getElementsByTagName('DT')[0].style.borderBottomWidth = '1px';
					}
					else {
						e[i].style.display = 'none';
						e[i].parentNode.parentNode.getElementsByTagName('DT')[0].style.paddingBottom = '0';
						e[i].parentNode.parentNode.getElementsByTagName('DT')[0].style.borderBottomWidth = '0';
					}
				}
			break;
		case 'blockquote' :
			for (var i = 0; i < e.length; i++)
				if ((e[i].parentNode.className) == 'content') {
					var v = e[i].getElementsByTagName('DIV')[0];

					if (!v.vaHeight) {
						v.vaHeight = v.getElementsByTagName('CITE')[0].offsetHeight;
						v.style.height = v.vaHeight + 'px';
						v.style.overflow = 'hidden';
					}
					else {
						v.style.height = '';
						v.style.overflow = '';
						v.vaHeight = false;
					}
				}
			break;
	}
}


function st3viewableCodeArea(ce, mh, cs1, cs2) {
	le = ce;
	ce = ce.parentNode.parentNode.getElementsByTagName('CODE')[0];

	if (ce.offsetHeight < mh) return;

	// Opera selection fix
	if (window.opera) {
		var s = window.getSelection();
		var r = document.createRange();
		r.selectNodeContents(ce);
		s.removeAllRanges();
	}

	viewableArea(ce, 'True');
	st3replaceString(le, cs1, cs2);
}


function st3replaceString(e, s1, s2) {
	if (e.innerHTML == s1) e.innerHTML = s2;
	else e.innerHTML = s1;
}


function st3gtpInput(e, hint) {
	// browser and version detection - http://www.javascriptkit.com/javatutors/navigator.shtml

	if ((/MSIE (\d+\.\d+);/.test(navigator.userAgent)) && (Number(RegExp.$1) <= 6)) {
		jumpto();
	}
	else {
		eId = e.getAttribute('id');

		if (document.getElementById('gtpinput') != null) {
			es = document.getElementById('gtpspan');
			ei = document.getElementById('gtpinput');

			es.removeChild(ei);
			es.parentNode.removeChild(es);

			if (ei.getAttribute('name') != eId + 'page') {
				st3gtpInput(e, hint);
			}
		}
		else {
			es = document.createElement('span');
			es.setAttribute('id', 'gtpspan');

			ei = document.createElement('input');
			ei.setAttribute('name', eId + 'page');
			ei.setAttribute('id', 'gtpinput');
			ei.setAttribute('type', 'text');
			ei.setAttribute('value', on_page);
			ei.setAttribute('title', hint);
			ei.onkeypress = function(event) {
				// http://www.phpfreaks.com/forums/index.php?topic=86160
				// http://gosnip.net/blog/2008/04/01/onkeypress-ie-and-javascript/
				var Key = window.event ? window.event.keyCode : event.which ? event.which : event.charCode;

				if (Key == 13) st3jumpto(ei);
				else if (Key == 27 || (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent) && event.keyCode == event.DOM_VK_ESCAPE)) st3gtpInput(e, hint);
				else if (Key == 0 || Key == 8 || (Key >= 48 && Key <= 57)) return true;
				else return false;
			}

			e.parentNode.appendChild(es);
			es.appendChild(ei);

			ei.focus();
			ei.select();
		}
	}
}


function st3jumpto(e) {
	var page = e.value;

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


function st3gotourl(url) {
	if (window.event.ctrlKey == 1) window.open(url);
	else document.location.href = url;
}


function st3setHeight(p) {
	for (i = 0; i < panels.length; i++) {
		pe = document.getElementById(panels[i]);
		if (pe != null) break;
	}

	if (pe.parentNode.className != 'tabs-fixed-height') return;
	else {
		pe = pe.parentNode;

		if (!pe.vaHeight) pe.vaHeight = 0;

		for (i = 0; i < panels.length; i++) {
			var e = document.getElementById(panels[i]);

			if (e) {
				if (pe.vaHeight < e.offsetHeight) pe.vaHeight = e.offsetHeight;
			}
		}

		pe.style.height = pe.vaHeight + 'px';
	}
	subPanels(p);
}


function st3expandCollapseCategory(cid) {
	var cat = document.getElementById('category-' + cid);

	if (cat.className == 'forabg bg1') {
		cat.className = 'forabg bg1 collapsed';
		createCookie('collapsed-category-' + cid, 1, '180');
	}
	else {
		cat.className = 'forabg bg1';
		eraseCookie('collapsed-category-' + cid);
	}
}


function st3checkCategory(cid) {
	if (readCookie('collapsed-category-' + cid)) {
		st3expandCollapseCategory(cid);
	}
}


function st3autoFillSubject(subj, strD, strE) {
	var e = document.getElementById('autofill-switch');

	if (e.checked) {
		e.title = strD;
		document.getElementById('subject').value = subj;
		eraseCookie('disable-subject-autofill');
	}
	else {
		e.title = strE;
		document.getElementById('subject').value = '';
		createCookie('disable-subject-autofill', 1, '180');
	}
}


function st3checkAutoFill(subj, strD, strE) {
	if (readCookie('disable-subject-autofill')) {
		var e = document.getElementById('autofill-switch');

		e.checked = false;
		st3autoFillSubject(subj, strD, strE);
	}
}


function st3quickLoginLabels(inpUsername, inpPassword) {
	var inpUsername = document.getElementById(inpUsername);
	var inpPassword = document.getElementById(inpPassword);

	var vTimer = setTimeout(function() {
		if (inpUsername.value == '' && inpPassword.value == '') {
			inpUsername.className = inpUsername.className + ' empty';
			inpPassword.className = inpPassword.className + ' empty';
		}
	}, 1000);
}


function st3staticBoxAction(id, idn) {
	var box = (id == false) ? false : document.getElementById(id).style;
	var idn = idn || false;
	var ie = /*@cc_on!@*/false;

	if (ie) {
		if (box) box.display = 'none';
		if (idn) document.getElementById(idn).style.display = 'block';
	}
	else {
		if (idn && (box.display == 'none' || id == false)) {
			box = document.getElementById(idn).style;
	
			setTimeout(function() {
				if (box.display == 'none') {
					box.display = 'block';
					box.opacity = '0.0';
					var o = 2;
				}
				else {
					var o = parseInt(box.opacity.split('.')[1]) + 2;
				}
		
				if (o < 10) {
					box.opacity = '0.' + o;
					st3staticBoxAction(id, idn);
				}
				else {
					box.opacity = '1.0';
					st3staticBoxHighlight(idn, 111, 0);
				}
			}, 1);
		}
		else {
			setTimeout(function() {
				var o = box.opacity.split('.')[1];
				o = (typeof(o) == 'undefined' || box.opacity == '1.0') ? 9 : parseInt(o)-1;
	
				if (o > 0) {
					box.opacity = '0.' + o;
					st3staticBoxAction(id, idn);
				}
				else {
					box.display = 'none';
					box.opacity = '1.0';
					if (idn) st3staticBoxAction(id, idn);
				}
			}, 10);
		}
	}
}


function st3staticBoxHighlight(id, c, s) {
	var box = document.getElementById(id);
	var s = (c == 999) ? 1 : (c == 111 && s != 0) ? 3 : s;

	setTimeout(function() {
		box.style.borderColor = '#' + c;

		switch (s) {
			case 0 : {
				st3staticBoxHighlight(id, c+111, 0);
				break;
			}
			case 1 : {
				setTimeout(function() { st3staticBoxHighlight(id, c-111, 2); }, 200);
				break;
			}
			case 2 : {
				st3staticBoxHighlight(id, c-111, 2);
				break;
			}
			case 3 : {
				setTimeout(function() { box.style.borderColor = ''; }, 50);
				break;
			}
		}
	}, 50);
}


function st3quickOptions(o, c) {
	var bClassName = document.body.className;
	var bSplitedCn = bClassName.split(' ');

	var c = c || false;
	var v = parseInt(readCookie(o)) || 0;
	if (!c) v += 1;

	var n = '';
	for (var i = 0; i < bSplitedCn.length; i++) {
		if (bSplitedCn[i].split('_')[0] != o) {
			n += bSplitedCn[i];
			n += ' ';
		}
	}

	switch (o) {
		case 'font-size' : {
			n += (v == 1) ? o + '_medium' : (v == 2) ? o + '_large' : o + '_small';
			if (v > 2) v = 0;
			break;
		}
		case 'text-brightness' : {
			n += (v == 1) ? o + '_high' : (v == 2) ? o + '_highest' : o + '_standard';
			if (v > 2) v = 0;
			break;
		}
		case 'page-width' : {
			var w = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
			var s = (w > 1400) ? 2 : 1;
	
			n += (v == 1) ? o + '_narrow' : (v == 2 && s == 2) ? o + '_wide' : o + '_normal';
			if (v > s) v = 0;
			break;
		}
	}

	document.body.className = n;

	if (!c) {
		eraseCookie(o);
		createCookie(o, v, '180');
	}
}


function st3quickOptionsCheck() {
	if (readCookie('font-size')) {
		st3quickOptions('font-size', true);
	}
	if (readCookie('text-brightness')) {
		st3quickOptions('text-brightness', true);
	}
	if (readCookie('page-width')) {
		st3quickOptions('page-width', true);
	}
}

