var scrollCounter;
var scrollId = 'body-login';

function handleBodyClick(ev)
{
	var str = '';
	ev = window.event ? window.event : ev;
	var inLogin = false;

	var targ;
	if (ev.target) targ = ev.target;
	else if (ev.srcElement) targ = ev.srcElement;

	node = targ;
	while (true)
	{
		if (!node)
			break;

		if (node.id && node.id == scrollId)
		{
			inLogin = true;
			break;
		} else {

			if (node)
				str += node.tagName + "-" + node.id + "\n";

			node = node.parentNode;
		}
	}

	if (!node)
	{
		document.getElementById('body2').onclick = null;
		loginBar_ScrollOut();

	}

}

function scrollDiv_Interval(direction, divId)
{
	var subjectDiv     = document.getElementById(divId);

	if (!subjectDiv)
	{
		window.clearInterval(scrollCounter);
		return;
	}

	var thresholdPropertyValue = direction > 0 ? subjectDiv.offsetTop : (subjectDiv.offsetTop + subjectDiv.offsetHeight);
	var scrollAmount = 30 * direction;

	var curPos = parseInt(subjectDiv.offsetTop);

	var nextPos = curPos + scrollAmount;

	//console.log ( direction + " - " + ( thresholdPropertyValue + scrollAmount)  );

	if ( direction == 1 && thresholdPropertyValue + scrollAmount >= 0)
	{
		window.clearInterval(scrollCounter);
		//console.log("Direction = 1, canceling timer");
		document.getElementById('body2').onclick = handleBodyClick;

		subjectDiv.style.top = "0px";


		return;
	} else if (direction == -1 && (thresholdPropertyValue + scrollAmount) <= 0) {
		document.getElementById('body2').onclick = null;
		//console.log("Direction = -1, canceling timer");
		subjectDiv.style.top = "-" + (subjectDiv.offsetHeight) + "px";
		window.clearInterval(scrollCounter);
	}

	subjectDiv.style.top = (curPos + scrollAmount) + "px";
}


function loginBar_ScrollOut()
{
	var bar = document.getElementById('body-login');

	if (scrollCounter)
	{
		window.clearInterval(scrollCounter);
	}

	scrollCounter = window.setInterval (
		"scrollDiv_Interval(-1, 'body-login')",
		20
	);
}

function loginBar_ScrollIn()
{
	var bar = document.getElementById('body-login');

	scrollCounter = window.setInterval(
		"scrollDiv_Interval(1, 'body-login')",
		20
	);

	return false;
}


function getPageHeight()
{
		return window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ?  document.documentElement.clientHeight : document.body != null? document.body.clientHeight : null;
}

function pp_fixSmallPages()
{
	var height = getPageHeight();

	if (height >document.getElementById('body2').scrollHeight )
	{

		var new_height = height - 10;

		document.getElementById('body2').style.height = height +"px";

		document.getElementById('body-border-left').style.height = new_height  + "px";
		document.getElementById('body-border-right').style.height = new_height  + "px";
		document.getElementById('body-content').style.height = new_height  + "px";
	}
}

function pp_attachLoginEvents()
{
	var el = document.getElementById('nav-login-link');

	if (el)
		el.onclick=loginBar_ScrollIn;
}

function pp_fixBrowserIssues()
{
	if(navigator.userAgent.indexOf('MSIE') != -1 && navigator.userAgent.indexOf('Opera') == -1)
	{
		var items = new DOMQuery("ul#top-menu li:last-child");

		for (var i=0;i<items.length;i++)
			jscss('add', items.get(i), 'last-child');

	}
}

function makeBlocksEditable(targets)
{
	var editLink = 'ul#admin-links li.edit-blocks a';

	var items = new DOMQuery(editLink);

	for (var i=0;i<items.length;i++)
	{
		items.get(i).onclick = function() { _makeBlocksEditable(); return false; }
	}
/*
	addEventToTargets(targets, 'click', _makeBlocksEditable, editLink);

	if(getCookie('edit_blocks') == '1' && new DOMQuery(editLink).get(0))
		_makeBlocksEditable(); */
}

function _makeBlocksEditable(e)
{
	/* if(e)
		e.cancelDefault(); */

	var blocks = new DOMQuery('div.cms_block[id]');
	var block, blockId, editLink;
	var editable = true;

	jscss('toggle', new DOMQuery('ul#admin-links li.edit-blocks a').get(0), 'active');

	for(var i = 0; i < blocks.length; i++)
	{
		var block = blocks.get(i);
		if(editLink = new DOMQuery('a[rel=edit]', block).get(0))
		{
			editLink.parentNode.removeChild(editLink);
			editable = false;
		}
		else
		{
			var matches = block.id.match(/block\-([0-9]+)/i);

			if (matches && matches.length > 0)
			{
				blockId = matches[1];

				var a = document.createElement('a');
				a.setAttribute('href', board_script_url + '/../admin/admin.php?action=update_block&data[block_id]=' + blockId);
				a.rel   = 'edit';
				a.className = "editable_block";
				var txt = document.createTextNode('Edit block');
				a.appendChild(txt);
				block.appendChild(a);
			}
		}
	}

	if(editable)
		setCookie('edit_blocks', '1')
	else
		setCookie('edit_blocks', '0')
}

function createDOMNode(tagname, options, children)
{
    var option, child, me;

    if(tagname != null)
    {
        me = document.createElement(tagname);
    }
    else
    {
        me = document.createDocumentFragment();
    }

    for(option in options)
    {
        if(!options.hasOwnProperty(option))
            continue;

        if(option == 'event')
        {
            addEvent(me, options[option][0], options[option][1]);
        }
        else
        {
            if (option == 'class')
            {
                me.setAttribute('className', options[option]);
            }

            me.setAttribute(option, options[option]);
        }
    }

    for(var i = 0; (child=children[i]); i++)
    {
        if( ["string", "number"].indexOf(typeof child) > -1 )
        {
            child = document.createTextNode(child);
        }
        me.appendChild(child);
    }

    return me;
};

