var photo_current = 0;

function browse_to (url)
{
	window.location.assign (url);
}

function check_length (ta_id, max)
{
	var len = document.getElementById (ta_id).value.length;
	if (len > max)
	{
		window.alert ('Uw reactie is ' + len + ' tekens lang. Beperkt aub uw tekst tot ' + max + ' tekens of minder.');
		return false;
	}
	return true;
}

function close_overlays ()
{
	$ ('.overlay').remove ();
	$ ('iframe.adv').css ('display', 'block');
	return false;
}

function event_toggle (id)
{
	var e = document.getElementById ('event_' + id);
	e.style.display = (e.style.display == 'block') ? 'none' : 'block';
	return false;
}

function hide_overlays ()
{
	$ ('.overlay').css ('display', 'none');
	$ ('iframe.adv').css ('display', 'block');
	return true;
}

function other_captcha ()
{
	var e = document.getElementById ('captcha_img');
	var s = e.src.replace (/\?ts=\d+/, '');
	e.src = s + '?ts=' + new Date ().getTime ();
	return false;
}

function pageswitch (id, idx)
{
	$ ('#' + id + ' .feedpage').removeClass ('current').eq (idx).addClass ('current');
	$ ('#' + id + ' .feedpage').addClass ('inactive').eq (idx).removeClass ('inactive');
	return false;
}

function photo_activate (idx)
{
	if (idx == photo_current) return;
	
	var e = document.getElementById ('photo_' + idx);
	if (e)
	{
		var o = document.getElementById ('photo_' + photo_current);
		if (o)
		{
			var l = document.getElementById ('photo_' + (photo_current - 1));
			if (!l) l = document.getElementById ('photo_' + (photo_current + 2));
			if (l) l.style.display = 'none';
			o.style.display = 'none';
			var r = document.getElementById ('photo_' + (photo_current + 1));
			if (!r) r = document.getElementById ('photo_' + (photo_current - 2));
			if (r) r.style.display = 'none';
		}
	
		var l = document.getElementById ('photo_' + (idx - 1));
		if (!l) l = document.getElementById ('photo_' + (idx + 2));
		if (l)
		{
			l.className = 'thumb inactive';
			l.style.display = '';
		}
		e.className = 'thumb active';
		e.style.display = '';
		var r = document.getElementById ('photo_' + (idx + 1));
		if (!r) r = document.getElementById ('photo_' + (idx - 2));
		if (r)
		{
			r.className = 'thumb inactive';
			r.style.display = '';
		}
		photo_current = idx;
		
		document.getElementById ('input_photo').value = e.src.match (/(\w+)_\w+?_s\.jpg/) [1];
		document.getElementById ('photo_large').src = e.src.replace ('_s.jpg', '_m.jpg');
	}
	return false;
}

function photo_select (evt)
{
	if (!evt) var evt = window.event;
	if (!evt) return;
	var e = evt.target ? evt.target : evt.srcElement;
	switch (e.id)
	{
	case 'photo_left' :
		photo_activate (photo_current - 1);
		break;
	case 'photo_right' :
		photo_activate (photo_current + 1);
		break;
	default :
		photo_activate (parseInt (e.id.substr (6)));
	}
	return false;
}

function rating_preview (n)
{
	var e = document.getElementById ('rating');
	e.className = 'spacer rating' + n;
}

function rating_vote (n)
{
	var url = window.location.href;
	url = url.replace (/([\?&])rating=\d*/, '$1');
	url = url.replace ('?&', '?');
	url = url.replace (/&$/, '');
	url = url + ((url.indexOf ('?') >= 0) ? '&' : '?') + 'rating=' + n;
	window.location = url;
}

function tabswitch (id, idx)
{
	$ ('#' + id + ' .tabs li').removeClass ('current').eq (idx).addClass ('current');
	$ ('#' + id + ' .tab').addClass ('inactive').eq (idx).removeClass ('inactive');
	return false;
}

function update_length (ta_id, count_id, max)
{
	var len = document.getElementById (ta_id).value.length;
	var remaining = max - len;
	document.getElementById (count_id).value = remaining;
	$ ('#' + count_id).toggleClass ('overflow', remaining < 0);	
}

$ (document).ready (function ()
{
	var dummy = document.createElement ('input');
	if (!('autofocus' in dummy))
	{
		$ ('input[autofocus]').first ().focus ();
		$ ('textarea[autofocus]').first ().focus ();
	}
	if (!('placeholder' in dummy))
	{
		$ ('input[placeholder]')
		.bind
		(
			'focus',
			function ()
			{
				if (this.value == this.getAttribute ('placeholder'))
				{
					this.value = '';
				}
			}
		)
		.bind
		(
			'blur',
			function ()
			{
				if (this.value == '')
				{
					this.value = this.getAttribute ('placeholder');
				}
			}
		)
		.each
		(
			function ()
			{
				$ (this).val ($ (this).attr ('placeholder'));
			}
		);
	}
});

