/*
ContextualTracker
http://ContextualTracker.wiadomosc.info/

Copyright (C) 2006 Tobiasz 'gosciu' Cudnik
gosc@wiadomosc.info

You can use ContextualTracker free of charge.
Distribution and code reuse NOT permitted.
*/

/* COMMON FUNCTIONS */

function toggle( idOrObj, explicit )
{
	if ( typeof idOrObj != 'object' ) {
		var obj = document.getElementById( idOrObj );

		if (! obj )
			return false;
	}
	else 
		obj = idOrObj;

	if ( typeof explicit !== 'undefined' ) {
		if ( explicit )
			obj.style.display = '';
		else
			obj.style.display = 'none';
		return;
	}

	if ( obj.style.display == 'none' )
		obj.style.display = '';
	else
		obj.style.display = 'none';
}

function debug( obj )
{
	var txt = '';
	for( var k in obj ) {
		txt += k+" = "+obj[k]+"\n";
	}

	alert( txt );
}

function getMouse(e)
{
	var pos = {x:0, y:0};

	if (e.pageX || e.pageY)
	{
		pos['x'] = e.pageX;
		pos['y'] = e.pageY;
	}
	else if (e.clientX || e.clientY)
	{
		pos['x'] = e.clientX + document.body.scrollLeft;
		pos['y'] = e.clientY + document.body.scrollTop;
	}

	return pos;
}

/* CONTEXT MENU */

var _contextMenuOpened = null;

function contextMenu( parent, event )
{
	var obj = null;
	for ( var i=0; i<parent.childNodes.length; i++ ) {
		if ( parent.childNodes[i].className == 'contextMenu' ) {
			obj = parent.childNodes[i];
			break;
		}
	}

	var minusTop = 0;
	var minusLeft = 0;

	while ( parent.parentNode ) {

		if ( getElementStyle( parent, 'position', 'position') == 'absolute' || getElementStyle( parent, 'position', 'position') == 'relative' ) {
			minusTop += parent.offsetTop;
			minusLeft += parent.offsetLeft;
		}

		parent = parent.parentNode;
	}

	if (! obj ) return false;

	var mouse = getMouse( event );

	obj.style.left = ( mouse.x + 10 - minusLeft ).toString()+'px';
	obj.style.top = ( mouse.y - minusTop ).toString()+'px';

	if ( _contextMenuOpened ) {
		if ( _contextMenuOpened != obj ) {
			toggle( _contextMenuOpened );
			toggle( obj );
			_contextMenuOpened = obj;
		}
		else {
			toggle( obj );
			_contextMenuOpened = null;
		}
	}
	else {
		toggle( obj );
		_contextMenuOpened = obj;
	}
}

function _contextMenuClose( e )
{
	src = window.event ? window.event.srcElement : e.target;

	if ( src.className != 'contextAnchor' && _contextMenuOpened ) {
		toggle( _contextMenuOpened );
		_contextMenuOpened = null;
	}
}

if ( document.attachEvent ) document.attachEvent('onclick',_contextMenuClose);
else document.addEventListener('click',_contextMenuClose,true);

// http://www.oreillynet.com/pub/a/javascript/excerpt/JSDHTMLCkbk_chap5/index5.html
function getElementStyle(elem, IEStyleProp, CSSStyleProp) {
    if ( typeof elem != 'object' )
		elem = document.getElementById(elem);
    if (elem.currentStyle) {
        return elem.currentStyle[IEStyleProp];
    } else if (window.getComputedStyle) {
        var compStyle = window.getComputedStyle(elem, "");
        return compStyle.getPropertyValue(CSSStyleProp);
    }
    return "";
}

/* GUI functions */

function changeChannelName( id, name )
{
	var new_name = prompt( lang['changeChannelName']+':', name );
	if ( typeof new_name == 'undefined' || ! new_name )
		return;

	url = './index.php?action=channel_change_name&channel_id='+id+'&new_name='+encodeURIComponent( new_name )+'&time='+(new Date()).getTime();

	var ajax = getAJAX();
	if ( ajax ) {
		ajax.open('HEAD', url, false);	// no async, wait for response
		ajax.send();
		window.location.reload( true );
		return;
	}

	bug = new Image();
	bug.src = url;
	setTimeout("window.location.reload( true );", 1500);
}

function submitSys()
{
	var url = removeUrlVar( document.location.toString(), 'limit_sys' );
	url = removeUrlVar( url, 'limit2_sys' );
	url = url.replace(/#$/, '' );

	if ( document.getElementById('sys_all').checked )
		document.location = url;
	else {
		var inputs = document.getElementById('sysForm').getElementsByTagName('input');

		for ( var i=0; i<inputs.length; i++ ) {
			if (! inputs[i].checked ) continue;
			
			if ( url.indexOf('?') > -1 ) url += '&limit_sys[]='+inputs[i].name;
			else url += '?limit_sys[]='+inputs[i].name;
		}

		document.location = url;
	}

	return false;
}

function removeUrlVar( url, varName )
{
	var pattern = new RegExp('(\\?|&amp;|&)'+varName+'(\\[\\])?=[^&#]*','gi');

	return url.replace( pattern, '$1' );
}

function refreshSys( obj )
{
	if ( typeof obj == 'undefined' )
		obj = {id:''};
	var all = true;
	var none = true;
	var inputs = document.getElementById('sysForm').getElementsByTagName('input');

	if ( obj.id != 'sys_all' ) {
		for ( var i=0; i<inputs.length; i++ ) {
			if ( inputs[i].id == 'sys_all' ) continue;
			if ( inputs[i].checked ) none = false;
			else all = false;
		}
	}

	if ( none || all || obj.id == 'sys_all' ) {
		for ( var i=0; i<inputs.length; i++ ) {
			if ( inputs[i].id == 'sys_all' ) inputs[i].checked = true;
			else inputs[i].checked = false
		}
	}
	else
		document.getElementById('sys_all').checked = false;
}

// from http://www.htmlgoodies.com/primers/jsp/article.php/3608046
if(typeof(XMLHttpRequest)!='undefined'){
	var getAJAX = function(){ return new XMLHttpRequest(); }
} else {
	var getAJAX = function(){
		var activeXObjects = ['Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.5.0', 'Msxml2.XMLHTTP.4.0',
		'Msxml2.XMLHTTP.3.0', 'Msxml2.XMLHTTP', 'Microsoft.XMLHTTP'];
		for(var i=0; i<activeXObjects.length; i++){
			try{
				return new ActiveXObject(activeXObjects[i]);
			}catch(err){}
		}
	}
}