
/**
 *
 * @access public
 * @return void
 **/

/**
 *
 * @access public
 * @return void
 **/
function ltrim( string, char){
	if (char == null ) {
		char = ' ';
	}
	while (string.charAt(0)==char) string = string.substring(1);
	return string;
}

function rtrim( string, char){
	if (char == null ) {
		char = ' ';
	}
	while (string.charAt(string.length-1)==char) string = string.substring(0, string.length-1);
	return string;
}

/**
 *
 * @access public
 * @return void
 **/
function trim( string, char){
	return ltrim(rtrim(string, char), char);
}
/**
 *
 * @access public
 * @return void
 **/
function setCookie( name, value)
{
	var expiry = new Date();
	var domain = document.domain.split('.');
	if (domain.length > 2) { // remove first part of domain name, but only if at least 2 parts are left
		domain = domain.slice(1);
	}

	expiry.setTime( expiry.getTime()+365*24*60*60*1000); 	// let it expire in 1 year

	var cookie = name + '=' + value + '; expires=' + expiry.toUTCString() + '; path=/'; // ; domain=' + domain.join('.');
	document.cookie = cookie;
}

/**
 *
 * @access public
 * @return void
 **/
function readCookie( name){
	var cookie = document.cookie;
	var value = '';
	cookie = cookie.split( ';');
	for ( var i = 0; i < cookie.length; i++)
	{

		if ( ltrim(cookie[i]).indexOf(name+'=') == 0)
		{
			value = cookie[i].substring( cookie[i].indexOf('=')+1);
			break;
		}
	}

	return value;
}

var timer = null;

function GetAjax( )
{

	var xmlHttp;

	try
	{
	  // Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	}
	catch (e)
  	{
  // Internet Explorer
  		try
    	{
    		xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    	}
  		catch (e)
    	{
    		try
      		{
      			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      		}
    		catch (e)
      		{
				return false;
      		}
    	}
  	}

  	return xmlHttp;
}

/**
 *
 * @access public
 * @return void
 **/
var XmlHttp;
function GetRandomPicture()
{

	XmlHttp = GetAjax();
	if (XmlHttp)
	{
		XmlHttp.onreadystatechange = XmlRequestHandler;
		var div = document.getElementById('randompicturehost');
		var width = div.clientWidth;
		if (!width) {	// IE does not always provide clientWidth
			width = div.offsetWidth;
		}
		if (!width) {	// default to smallest thumbnails
			width = 100;
		}
		var url = 'SimpleGallery/randompicture.php?q=0&width=' + width;
		XmlHttp.open('GET', url, true);
		XmlHttp.send(null);
	}
}

/**
 *
 * @access public
 * @return void
 **/
function ShowPreview( show)
{
	document.getElementById('gallerypreview').style.display = (show ? 'block' : 'none');
}

/**
 *
 * @access public
 * @return void
 **/
function UpdateSize()
{
	var img = document.getElementById('randompicture');
	var width = img.width;
	var height = img.height;
	var aspect = width/height;
	var div = document.getElementById('randompicturehost');
	var availwidth = div.clientWidth;
	var availheight = div.clientHeight;
	var newheight = availwidth / aspect;
	var newwidth = availheight * aspect;
	if ( newheight < availheight)
	{
		img.height = newheight;
		img.width = newheight * aspect;
	}
	else if ( newwidth < availwidth)
	{
		img.height = newwidth / aspect;
		img.width = newwidth;
	}
	img.style.visibility = 'visible';
	preview_start();
}

function XmlRequestHandler()
{
  	switch(XmlHttp.readyState)
	{
  		case 0:	// not initialized
  			break;
  		case 1: // request set up
  			break;
  		case 2: // request sent
  			break;
  		case 3: // request in process
  			break;
  		case 4: // request complete
  		{
  			var img = document.getElementById('randompicture');
  			var xmlResponse = XmlHttp.responseXML;
  			var data = xmlResponse.getElementsByTagName('responseData');
  			if ( data && data.length) // if responseData does not exist, generating a preview image failed
			{
  				img.style.visibility = 'hidden';
			  	img.src = decodeURI(xmlResponse.getElementsByTagName('thumbURL')[0].firstChild.nodeValue);
			  	var desc = xmlResponse.getElementsByTagName('description')[0].firstChild;
	  			img.setAttribute( "alt", xmlResponse.getElementsByTagName('description')[0].firstChild.nodeValue);
	  			img.setAttribute( "title", xmlResponse.getElementsByTagName('description')[0].firstChild.nodeValue);
	  			img.width = xmlResponse.getElementsByTagName('thumbWidth')[0].firstChild.nodeValue;
	  			img.height = xmlResponse.getElementsByTagName('thumbHeight')[0].firstChild.nodeValue;
	  			var link = document.getElementById('randompicturelink');
	  			link.href = decodeURIComponent(xmlResponse.getElementsByTagName('galleryURL')[0].firstChild.nodeValue);
				img.style.visibility = 'visible';
			}
			if (readCookie('gallerypreview')!='no')
				preview_start();
  			break;
  		}
  	} // switch
}

/**
 *
 * @access public
 * @return void
 **/
function init()
{
	ShowPreview( true);
	GetRandomPicture();
	if (readCookie('gallerypreview')!='no')
	{
		preview_start();
	}
	else
	{
		preview_stop();
	}
}

function preview_start()
{
	clearTimeout( timer);

	setCookie('gallerypreview', 'yes');
	timer=setTimeout('GetRandomPicture();', 10000);
	document.getElementById('preview_toggle').innerHTML = 'Vorschau stoppen';
	document.getElementById('preview_toggle').onclick = preview_stop;
	return false;
}

function preview_stop()
{
	clearTimeout( timer);
	setCookie('gallerypreview', 'no');
	document.getElementById('preview_toggle').innerHTML = 'Vorschau starten';
	document.getElementById('preview_toggle').onclick = preview_start;
	return false;
}

