//  +------------------------------------------------------------------------+
//  | netjukebox, Copyright © 2001-2010 Willem Bartels                       |
//  |                                                                        |
//  | http://www.netjukebox.nl                                               |
//  | http://forum.netjukebox.nl                                             |
//  |                                                                        |
//  | This program is free software: you can redistribute it and/or modify   |
//  | it under the terms of the GNU General Public License as published by   |
//  | the Free Software Foundation, either version 3 of the License, or      |
//  | (at your option) any later version.                                    |
//  |                                                                        |
//  | This program is distributed in the hope that it will be useful,        |
//  | but WITHOUT ANY WARRANTY; without even the implied warranty of         |
//  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          |
//  | GNU General Public License for more details.                           |
//  |                                                                        |
//  | You should have received a copy of the GNU General Public License      |
//  | along with this program.  If not, see <http://www.gnu.org/licenses/>.  |
//  +------------------------------------------------------------------------+



//  +------------------------------------------------------------------------+
//  | overLIB initialize (overlib.js & overlib_cssstyle.js)                  |
//  +------------------------------------------------------------------------+
var ol_width			= 0;
var ol_vauto			= 1;
var ol_fgclass			= 'ol_foreground';
var ol_bgclass			= 'ol_background';
var ol_textfontclass	= 'ol_text';
var ol_captionfontclass	= 'ol_caption';



//  +------------------------------------------------------------------------+
//  | SHA1 (sha1.js)                                                         |
//  +------------------------------------------------------------------------+
function sha1(data)
{
return rstr2hex(rstr_sha1(str2rstr_utf8(data)));
}



//  +------------------------------------------------------------------------+
//  | HMAC-SHA1 (sha1.js)                                                    |
//  +------------------------------------------------------------------------+
function hmacsha1(key, data)
{
return rstr2hex(rstr_hmac_sha1(str2rstr_utf8(key), str2rstr_utf8(data)));
}



//  +------------------------------------------------------------------------+
//  | Set screen width in a session cookie for server side PHP script.       |
//  +------------------------------------------------------------------------+
function cookie()
{
var width;
if		(self.innerWidth > 0)						width = self.innerWidth; // All except IE
else if	(document.documentElement.clientWidth > 0)	width = document.documentElement.clientWidth; // IE 6 strict mode
else if	(document.body.clientWidth > 0)				width = document.body.clientWidth; // All other IE versions
else if	(screen.width > 0)							width = screen.width; // Fallback
if		(width < 320) 								width = 320;
document.cookie = 'netjukebox_width=' + width;
}



//  +------------------------------------------------------------------------+
//  | Formatted time                                                         |
//  +------------------------------------------------------------------------+
function formattedTime(miliseconds)
{
var seconds 	= Math.round(miliseconds / 1000);
var hour 		= Math.floor(seconds / 3600);
var minutes 	= Math.floor(seconds / 60) % 60;
seconds 		= seconds % 60;
	
if (hour > 0)	return hour + ':' + zeroPad(minutes, 2) + ':' + zeroPad(seconds, 2);
else			return minutes + ':' + zeroPad(seconds, 2);
}



//  +------------------------------------------------------------------------+
//  | Zero pad                                                               |
//  +------------------------------------------------------------------------+
function zeroPad(number, n)
{ 
var zeroPad = '' + number;

while(zeroPad.length < n)
	zeroPad = '0' + zeroPad; 

return zeroPad;
}



//  +------------------------------------------------------------------------+
//  | Show hide                                                              |
//  +------------------------------------------------------------------------+
function showHide(a, b)
{
document.getElementById(a).style.display = (document.getElementById(a).style.display == 'none') ? 'block' : 'none';
document.getElementById(b).style.display = (document.getElementById(b).style.display == 'none') ? 'block' : 'none';
}



//  +------------------------------------------------------------------------+
//  | Inverse checkbox                                                       |
//  +------------------------------------------------------------------------+
function inverseCheckbox(frm) 
{
for (var i = 0; i < frm.elements.length; i++) 
	{
	if (frm.elements[i].type == 'checkbox') 
		frm.elements[i].checked = !frm.elements[i].checked;
	}
}



//  +------------------------------------------------------------------------+
//  | Get ralative X                                                         |
//  +------------------------------------------------------------------------+
function getRelativeX(event, obj) 
{
var currentObjLeft = 0;
if (typeof(event.offsetX) != 'undefined')
	return event.offsetX;
else if (obj.offsetParent) 
	{ 
	while (obj.offsetParent) 
		{ 
		currentObjLeft += obj.offsetLeft;
		obj = obj.offsetParent; 
		} 
	}
else if (obj.x) 
	currentObjLeft += obj.x;

// todo scroll offset
// return event.pageX - currentObjLeft + parent.document.body.scrollLeft;
return event.clientX - currentObjLeft;
}



//  +------------------------------------------------------------------------+
//  | AJAX global cache                                                      |
//  +------------------------------------------------------------------------+
var ajaxCache = new Array();



//  +------------------------------------------------------------------------+
//  | AJAX ActiveX XMLHttpRequest wrapper                                    |
//  +------------------------------------------------------------------------+
if (typeof XMLHttpRequest == 'undefined' && typeof ActiveXObject != 'undefined')
{
var error;
var axObjects = ['MSXML2.XMLHTTP.6.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP'];
for (var axIndex in axObjects)
	{
	try
		{
		new ActiveXObject(axObjects[axIndex]);
		XMLHttpRequest = function() { return new ActiveXObject(axObjects[axIndex]); }
		break;
		}
	catch(error)
		{
		// On error do nothing, try next one
		}
	}
error = null;
}



//  +------------------------------------------------------------------------+
//  | AJAX request                                                           |
//  +------------------------------------------------------------------------+
function ajaxRequest(url, cache)
{
if (typeof timer_id == 'number' && typeof timer_function == 'string' && typeof timer_delay == 'number')
	{
	clearTimeout(timer_id);
	timer_id = setTimeout(timer_function, timer_delay);
	}
if (cache == true && typeof ajaxCache[url] == 'string')
	eval(ajaxCache[url]);
else if (typeof XMLHttpRequest != 'undefined')
	{
	var http = new XMLHttpRequest();
	http.onreadystatechange = function()
		{
		if (http.readyState == 4 && http.status == 200)
			{
			if (cache == true)
				ajaxCache[url] = http.responseText;
			eval(http.responseText);
			}
		}
	http.open('get', url + '&ajax=1', true);
	http.send(null);
	}
}
