/*****************************************************************************
 * BlueCms - PHP Content Management System
 * Copyright (c) 2010 BlueCms
 *
 * @package core
 * @author blue 
 *
 * BlueCms JavaScript utilities
 *
 *****************************************************************************/

// used for opening popup windows
function wopen(url, name, w, h)
{
	w += 32; h += 96;
	var win = window.open(
		url,
		name,
		'width=' + w + ', height=' + h + ', ' + 'location=no, menubar=no, ' + 'status=no, toolbar=no, scrollbars=yes, resizable=no');
		win.resizeTo(w, h);
		win.focus();
}

// used for opening popup windows
/*function popupopen(url, name, w, h)
{
	wopen(url, name, w, h);
	window.location.reload();
}
*/

// used for closing popup windows
function refreshParent()
{
	/*window.opener.location.href = window.opener.location.href; */
	/*window.opener.location.refresh();*/

	window.close();

	var startTime = new Date().getTime(); // get the current time
	while (new Date().getTime() < startTime + 5000);

	if (window.opener && !window.opener.closed)
	{	
		window.opener.location.href = window.opener.location.href;
		window.opener.location.reload();
	}
}

// used as a substitution for target attribute of <a> tags,
// which is not part of XHTML 1.0 specification
function externalLinks()
{
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++)
	{
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
		      anchor.target = "_blank";
	}
}

// function used for dynamic change of textarea height
function resizeTextArea(id, rows)
{
	var textarea = document.getElementById(id);
	if (!textarea || (typeof(textarea.rows) == "undefined")) return;
	textarea.rows = rows;
}

//
// addLoadEvent()
// Adds event to window.onload without overwriting currently assigned onload functions.
// Function found at Simon Willison's weblog - http://simon.incutio.com/
function bcAddLoadEvent(func)
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function')
	{
		window.onload = func;
	} else {
		window.onload = function()
		{
			oldonload();
			func();
		}
	}
}

// Creates new div element and adds it as a child to body element
function bcCreateDiv(id, html, width, height, left, top)
{
	var newdiv = document.createElement('div');
	newdiv.setAttribute('id', id);

	if (width) { newdiv.style.width = 300; }
	if (height) { newdiv.style.height = 300; }

	if ((left || top) || (left && top)) {
		newdiv.style.position = "absolute";

		if (left) { newdiv.style.left = left; }

		if (top) { newdiv.style.top = top; }
	}

	newdiv.style.position = "absolute";
	//newdiv.style.z-index = "2";

	if (html)
	{
		newdiv.innerHTML = html;
	} else {
		newdiv.innerHTML = "nothing";
	}

	document.body.appendChild(newdiv);
} 


// Shows popup image for "obj" image object
function bcShowPopupImage(obj, url)
{
	objDiv = document.getElementById("bc_image_preview_popup");
	if (! objDiv)
	{
		bcCreateDiv("bc_image_preview_popup");
		objDiv = document.getElementById("bc_image_preview_popup");

	}

	if (!url) { url = obj.src }
	 
	objDiv.innerHTML = "<img src='" + url + "'+'width=\"250\"' >";
}

// Hides popup image 
function bcHidePopupImage(obj)
{
	objDiv = document.getElementById("bc_image_preview_popup");
	if (objDiv)
	{
		document.body.removeChild(objDiv);
	}
}

// Event handler for windows move over client area
function bcOnMoveArea(event)
{
	event = event || window.event;

	// popup image handling
	objDiv = document.getElementById("bc_image_preview_popup");
	if (objDiv)
	{	
		posLeft = event.clientX+document.body.scrollLeft+10;
		posTop = event.clientY+document.body.scrollTop+10;
		objDiv.style.left=posLeft+"px";
		objDiv.style.top=posTop+"px";
	}
}

document.onmousemove = bcOnMoveArea;



