var current_color = '000000';
var _timeout = new Array();

function start_scroll(step,div)
{
	document.getElementById(div).scrollTop+=step;
	// accelerate the scroll speed as we keep scrolling
	if (step>0)
		step++;
	else
		step--;

	_timeout[div] = setTimeout('start_scroll('+step+',"'+div+'")', 50);
}

function stop_scroll(div)
{
	if (_timeout[div]) clearTimeout(_timeout[div]);
	_timeout[div] =false;
}

function scroll(n,div)
{
	vrooom(n,div);
}


//-------------------
// init
window.onload =  function()
{
	if ($('container_up'))
	{
		$('container_up').onmousedown = function() { start_scroll(-5,'container'); }
		$('container_up').onmouseup = function() { stop_scroll('container'); }

		$('container_dn').onmousedown = function() { start_scroll(5,'container'); }
		$('container_dn').onmouseup = function() { stop_scroll('container'); }
	}
}

function load_preview(id,a,s)
{
	if (a=='thumbnails')
		$(a).innerHTML = '<img id=loading src=/ui/loading1.gif style=border:none> Loading...';
	else
  new Effect.Opacity(a, {to:0.1,duration:0.1});

  	new Ajax.Updater(
      	a,
        "gallery.php",
        {
          parameters: 'm='+a+'&id='+id+'&s='+s,
		  evalScripts: true
        }
      );
}



function _rollover(obj,r) { _assign(obj,obj.src,r); } function _assign(obj,s,r) { obj.onmouseover = function() { obj.src = r; }; obj.onmouseout = function() { obj.src = s; }; obj.border=0; obj.onload=''; }

function ct(k)
{
	document.getElementById("fb").style.position = 'absolute';
	if (k==undefined) k = '';
	document.getElementById("fb").style.display = k;
}

/* postcard */

function set_size(s,obj)
{	
	$('fs').value = s;
		
	var div = document.getElementById('dtextsize');
	var a = div.getElementsByTagName("a");
	for (i=0;i<a.length;i++)
	{
	    a[i].className = "def";
	}

	obj.className = "sel";
	
	load_text();
}

function load_text()
{
	var str = $('m').value;
	$('message').innerHTML = str.replace(/\n/g, '<br>').replace(/\s/g, '&nbsp;');
	$('message').style.color = '#' + current_color;
	$('message').style.font = $('fs').value+ "px " + $('textfont').value;
}

function setc(value,obj,targetobj)
{
//	document.elements[''].value = value;
	
	var div = document.getElementById('dfont');
	var imgs = div.getElementsByTagName("img");
	for (i=0;i<imgs.length;i++)
	{
	    imgs[i].className = "def";
	}
	current_color = obj.name;
	$('fc').value = '#' + obj.name;
	obj.className = "sel";
	load_text();
}

function set_alignment(value,obj)
{
	
		
	var div = document.getElementById('dalign');
	var a = div.getElementsByTagName("a");
	for (i=0;i<a.length;i++)
	{
	    a[i].className = "def";
	}

	obj.className = "sel";
	
	load_text();
	$('message').style.textAlign = value;
	$('fa').value = value;
}

function make_drag(obj)
{
	new Draggable(obj, {starteffect:undefined, endeffect:undefined});
}

function validate_form()
{
	if (document.postcard.sender_name.value == '')
	{
		document.postcard.sender_name.focus();
		alert('Please insert your name.');
		return false;
	}
	
	if (document.postcard.sender_email.value == '')
	{
		document.postcard.sender_email.focus();
		alert('Please insert your email.');
		return false;
	}
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.postcard.sender_email.value) != true)
	{
	    document.postcard.sender_email.focus();
		alert("Your email is invalid.")
	    return false;
	}
	
	if (document.postcard.receiver_name.value == '')
	{
		document.postcard.receiver_name.focus();
		alert('Please insert receiver name.');
		return false;
	}
	
	if (document.postcard.receiver_email.value == '')
	{
		document.postcard.receiver_email.focus();
		alert('Please insert receiver email.');
		return false;
	}
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.postcard.receiver_email.value) != true)
	{
	    document.postcard.receiver_email.focus();
		alert("Your friend email is invalid.")
	    return false;
	}

	if (document.postcard.msg.value == '')
	{
		document.postcard.msg.focus();
		alert('Please enter your message.');
		return false;
	}

	
	return true;
}

function adjust_pageheight(el)
{
	var hh = el.naturalHeight || el.height;
	$('hpad').style.height = (100+hh) + 'px';
}

/* view postcard */

function showmsg()
{
	new Effect.Appear('picholder');

	var pos = Position.cumulativeOffset($('pic'));
	$('msg').style.left = pos[0] + 'px';
	$('msg').style.top = pos[1] + 'px';
	new Effect.Appear('msg',{queue:'end'})
}

/* album */

function sel(fn)
{
	new Effect.Opacity('pvc',{duration:0.1,to:0.1});$('actual').src='/thumb.php?img='+fn+'&w=450&h=450&fit=1&cache=1';
}

function onld()
{
	new Effect.Appear('pvc',{duration:0.5,queue:'end'});
}
