// this file contains functions for the nb (newsbox)
// author:    Dominik Scholz, schlotzz@go4u.de
// changed:   2010-08-10

// global variables
	// create temporary data array
	var nba = new Array();

	// different settings
	nba.timeout = 15;
	nba.steps   = 25;
	nba.border  = 20;
	nba.width   = 500;
	nba.height  = 375;

	// destination iframe size
	nba.dst   = new Array();
	nba.dst.x = null;
	nba.dst.y = null;
	nba.dst.w = null;
	nba.dst.h = null;

	// iframe loaded
	nba.loaded = false;

// end of global variables



// start preloading
nb_start_preload();


// execute, if hyperlink is clicked
function nb(href)
{

	nba.loaded = false;

	if (!document.getElementById('nb_iframe'))
		nb_start();

	// get url of link
	nba.href = href;

	// show loading box
	var de = document.documentElement;
	var windowWidth  = window.innerWidth  || self.innerWidth  || (de&&de.clientWidth)  || document.body.clientWidth;
	var windowHeight = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
	var windowLeft   = window.pageXOffset || self.pageXOffset || (de&&de.scrollLeft)   || document.body.scrollLeft;
	var windowTop    = window.pageYOffset || self.pageYOffset || (de&&de.scrollTop)    || document.body.scrollTop;

	// get center of current page
	nba.dst.x = windowWidth/2  - parseInt(nba.width/2)  + windowLeft;
	nba.dst.y = windowHeight/2 - parseInt(nba.height/2) + windowTop;
	nba.dst.w = 500;
	nba.dst.h = 500;
	
	
	var loadbox = document.getElementById('nb_loadbox');
	loadbox.style.left = (parseInt(windowWidth/2) + windowLeft-25)+'px';
	loadbox.style.top  = (parseInt(windowHeight/2) + windowTop-25)+'px';
	loadbox.style.display = 'block';
	nb_loadani(0);

	var t = new Date();


	// set iframe container
	var container = document.getElementById('nb_div');
	container.style.left    = '-9999px';
	container.style.top     = '-9999px';
	container.style.width   = null;
	container.style.height  = null;
	container.style.display = 'block';

	// set iframe for preload
	var preload = document.getElementById('nb_iframe');
	preload.width  = nba.width+'px';
	preload.height = nba.height+'px';
	preload.src    = null;
	preload.src    = href + '?newsBox=true&time=' + t.getTime(); // prevent caching by using time as parameter

	return false;
}


// execute if image is preloaded
function nb_preloaded()
{
	var de = document.documentElement;
	var windowWidth  = window.innerWidth  || self.innerWidth  || (de&&de.clientWidth)  || document.body.clientWidth;
	var windowHeight = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
	var windowLeft   = window.pageXOffset || self.pageXOffset || (de&&de.scrollLeft)   || document.body.scrollLeft;
	var windowTop    = window.pageYOffset || self.pageYOffset || (de&&de.scrollTop)    || document.body.scrollTop;
	var preload = document.getElementById('nb_iframe');

	// get width and height from image
	var w = nba.width;
	var h = nba.height;

	// maximal width and height
	var maxw = windowWidth  - nba.border*2;
	var maxh = windowHeight - nba.border*2;

	if (h > maxh)
	{
		h = maxh;
	}

	// round width and height
	w = parseInt(w);
	h = parseInt(h);

	// get data of destination image
	nba.dst.x = windowWidth/2  - parseInt(w/2) + windowLeft;
	nba.dst.y = windowHeight/2 - parseInt(h/2) + windowTop;
	nba.dst.w = w;
	nba.dst.h = h;

	preload.style.width  = w+'px';
	preload.style.height = h+'px';

	nb_open();
}


// if page is loaded, insert images to preload preloading-animation
function nb_start_preload()
{
	// select body tag
	var insertBody = document.getElementsByTagName('body')[0];

	// if not loaded, try again later
	if (insertBody == null)
	{
		window.setTimeout("nb_start_preload();", 250);
		return;
	}

	// create divs for all preloading classes
	var insertDiv = document.createElement("div");
	insertDiv.style.position = 'absolute';
	insertDiv.style.left     = '-9999px';
	insertDiv.style.top      = '-9999px';

	insertBody.insertBefore(insertDiv, insertBody.firstChild);

	// create column
	for (var i=1; i<=8; i++)
	{
		var insertSubdiv = document.createElement("div");
		insertSubdiv.className = 'nb_loadstate'+i;
		insertDiv.appendChild(insertSubdiv);
	}

	// cycle through all anchors
	var insertAnchors = insertBody.getElementsByTagName('a');
	for (var i=0; i<insertAnchors.length; i++)
	{
		var href = insertAnchors[i].href;
		if (href.indexOf('-cb-') < 0)
			continue;
		insertAnchors[i].onclick = function() { return nb(this); };
	}

}


// insert html objects
function nb_start()
{
	// select body tag
	var insertBody = document.getElementsByTagName('body')[0];

	// loading bar
	var insertLoad = document.createElement('div');
	insertLoad.setAttribute('id', 'nb_loadbox');
	insertLoad.style.position   = 'absolute';
	insertLoad.style.left       = '10px';
	insertLoad.style.top        = '10px';
	insertLoad.style.display    = 'none';
	insertLoad.style.zIndex     = '1000';
	insertBody.insertBefore(insertLoad, insertBody.firstChild);

	// moving iframe container
	var insertDiv = document.createElement('div');
	insertDiv.setAttribute('id', 'nb_div');
	insertDiv.style.position  = 'absolute';
	insertDiv.style.left      = '10px';
	insertDiv.style.top       = '10px';
	insertDiv.style.zIndex    = '1000';
	insertDiv.style.overflow  = 'hidden';
	insertDiv.display         = 'none';
	insertBody.insertBefore(insertDiv, insertBody.firstChild);

	// moving iframe
	var insertIframe = document.createElement('iframe');
	insertIframe.setAttribute('id', 'nb_iframe');
	insertIframe.style.display = 'block';
	insertIframe.frameBorder = 'none';
	insertDiv.appendChild(insertIframe);
   	if (insertIframe.attachEvent)
		insertIframe.attachEvent("onload", function() { nba.loaded = true; }); // ie
	else if (insertIframe.addEventListener)
		insertIframe.addEventListener("load", function() { nba.loaded = true; }, true); // Safari, Firefox

	// close button
	var insertClose = document.createElement('a');
	insertClose.setAttribute('id', 'nb_closebox');
	insertClose.style.position  = 'absolute';
	insertClose.style.left      = '10px';
	insertClose.style.top       = '10px';
	insertClose.style.display   = 'none';
	insertClose.style.zIndex    = '1001';
	insertBody.insertBefore(insertClose, insertBody.firstChild);
	insertClose.href = 'javascript:nb_close();';

	// image shadow
	var insertShadow = document.createElement('table');
	insertShadow.setAttribute('id', 'nb_shadow');
	insertShadow.style.position = 'absolute';
	insertShadow.style.left     = '10px';
	insertShadow.style.top      = '10px';
	insertShadow.style.display  = 'none';
	insertShadow.style.zIndex   = '999';
	insertShadow.cellPadding    = '0';
	insertShadow.cellSpacing    = '0';
	insertBody.insertBefore(insertShadow, insertBody.firstChild);

	var insertShadowTbody = document.createElement("tbody");
	insertShadow.appendChild(insertShadowTbody);

	// create rows and columns
	for (var row=0; row<=2; row++)
	{
		// create rows
		var insertRow = document.createElement("tr");
		insertShadowTbody.appendChild(insertRow);

		// create column
		for (var col=1; col<=3; col++)
		{
			var insertCol = document.createElement("td");
			insertCol.id = 'nb_shadow'+(row*3+col);
			insertRow.appendChild(insertCol);
		}
	}
}


// cycle loading animation
function nb_loadani(s)
{
	if (nba.loaded)
	{
		nb_preloaded();
		return;
	}

	var o = document.getElementById('nb_loadbox');
	o.className  = 'nb_loadstate'+s;
	o.innerHTML = '<div style="display: block; width: 50px; height: 50px;">&nbsp;</div>';

	s = (s<8)? (s+1):1;

	if (o.style.display == 'block')
		window.setTimeout('nb_loadani('+s+');', 100);
}


// do open animation
function nb_open(p)
{
	var o = document.getElementById('nb_div');

	if (p==null)
	{
		// view image and hide the other stuff
		p = 0;
		nb_hide_controls();
		o.style.left   = parseInt(nba.dst.x)+'px';
		o.style.top    = parseInt(nba.dst.y)+'px';
		o.style.width  = parseInt(nba.dst.w)+'px';
		o.style.height = parseInt(nba.dst.h)+'px';
		o.style.display = 'block';
	}
	
	p += 100/nba.steps;
	var m = nb_magic(p/100);

	nb_opacity(o.style, p);

	if (p<100)
		window.setTimeout('nb_open('+p+');', nba.timeout);
	else
		nb_show_controls();
}




// do close animation
function nb_close(p)
{
    var o = document.getElementById('nb_div');

	if (p==null)
	{
		p = 0;
		nb_hide_controls();
	}

	p += 100/nba.steps;
	var m = nb_magic(p/100);

	nb_opacity(o.style, 100-p);

	if (p<100)
		window.setTimeout('nb_close('+p+');', nba.timeout);
	else
		o.style.display = 'none';
}


// show closebutton and shadow of image
function nb_show_controls()
{
    document.getElementById('nb_iframe').onclick = function() { nb_close(); };

    var o0 = document.getElementById('nb_closebox');
    o0.style.left    = parseInt(nba.dst.x-12)+'px';
    o0.style.top     = parseInt(nba.dst.y-14)+'px';
    o0.style.display = 'block';

    var o2 = document.getElementById('nb_shadow');
    o2.style.left    = parseInt(nba.dst.x-12)+'px';
    o2.style.top     = parseInt(nba.dst.y-7)+'px';
    o2.style.display = 'block';

    var o3 = document.getElementById('nb_shadow5');
    o3.style.width   = parseInt(nba.dst.w-32)+'px';
    o3.style.height  = parseInt(nba.dst.h-28)+'px';
}


// hide controls
function nb_hide_controls()
{
	document.getElementById('nb_closebox').style.display = 'none';
	document.getElementById('nb_loadbox').style.display  = 'none';
	document.getElementById('nb_shadow').style.display = 'none';
	document.getElementById('nb_iframe').onclick = null;
}


// get pixel position of any element on a page
function nb_position(o)
{
	var x = 0;
	var y = 0;
	do
	{
        x += o.offsetLeft;
        y += o.offsetTop;
	} while (o = o.offsetParent)

	return Array(x, y);
}


// set opacity of any element, use (element.style, 0-100)
function nb_opacity(o, p)
{
	var pInt       = Math.ceil(p);
	o.filter       = 'Alpha(opacity='+pInt+')';
	o.MozOpacity   = '' + pInt/100;
	o.KTHMLOpacity = '' + pInt/100;
	o.opacity      = '' + pInt/100;
}


// convert with sinus-function for smoother animation
function nb_magic(pos)
{
	return ((-Math.cos(pos*Math.PI)/2) + 0.5);
}

