function toggle_option(option)
{
  if(document.getElementById(option).style.display == 'none')
  {
    document.getElementById(option).style.display = '';
	document.getElementById(option+'1').style.display = '';
  }
  else
  {
    document.getElementById(option).style.display = 'none';
	document.getElementById(option+'1').style.display = 'none';
  }
}

var uploadfields = 1;
function add_uploadfield()
{
	if(uploadfields >= 15)
		return alert('Maximal 15 Uploads gleichzeitig!');
	var fileinput = document.createElement('input');
	if(uploadmethod == 'local')
	{
		fileinput.type = 'file';
		fileinput.name = 'files'+uploadfields;
		fileinput.size = 40;
	}
	else
	{
		fileinput.type = 'text';
		fileinput.name = 'files[]';
		fileinput.style.width = '400px';
	}
	document.getElementById('uploadfields').appendChild(document.createElement('br'));
	document.getElementById('uploadfields').appendChild(fileinput);
	uploadfields++;
}

var uploadmethod = 'local';
function upload(method)
{
	if(method == uploadmethod)
		return;
	document.getElementById('localupload').style.display = 'none';
	document.getElementById('remoteupload').style.display = 'none';
	document.getElementById(method+'upload').style.display = '';
	document.getElementById('uploadfields').innerHTML = '';
	document.getElementById('method').value = method;
	uploadfields = 1;
	uploadmethod = method;
}

var curpage = -1;
function gal_update_page(page)
{
  if(curpage != page) {
	$('flip_prev').style.display = 'none';
    if(page > 0)
	  $('flip_prev').style.display = '';
	$('flip_next').style.display = 'none';
	if(page < Math.ceil(files.length/6)-1)
	  $('flip_next').style.display = '';
  }
  curpage = page;
  $('gallery').update();
  for(var i=page*6;i<(page+1)*6 && i<files.length;i++) {
    if(i == page*6)
	  $('gallery').insert(new Element('div', { 'style': 'float:left'}).update(new Element('img', { 'src': files[i].img, 'style': 'width:100px;height:100px;cursor:pointer', 'onclick': 'gal_update_image('+i+')' })));
	else
	  $('gallery').insert(new Element('div', { 'style': 'float:left;margin-left:5px'}).update(new Element('img', { 'src': files[i].img, 'style': 'width:100px;height:100px;cursor:pointer', 'onclick': 'gal_update_image('+i+')' })));
  }
}

function gal_update_image(index, auto)
{
//  if(auto == true && dia)
//    dia = false;
  $('img_next').style.display = 'none';
  $('img_prev').style.display = 'none';
  $('img_dia').style.display = 'none';
  if(index > 0)
    $('img_prev').style.display = '';
  if(index < files.length-1) {
    $('img_next').style.display = '';
	$('img_dia').style.display = '';
  }
  if(Math.floor(index/6) != curpage)
    gal_update_page(Math.floor(index/6));
  curimg = index;
  new Effect.Opacity($('img'), { from:1, to: 0, duration: 1.0, afterFinish: function() {
    $('img').update(new Element('img', { 'src': files[curimg].img})); resize_img($('img').childNodes[0], 680, 480);
    new Effect.Opacity($('img'), { from: 0, to: 1, duration: 1.0 });
  }});
}

var dia = false;
function gal_start_dia()
{
  dia = true;
  new PeriodicalExecuter(function(pe) {
    if(curimg == files.length-1 || !dia) {
	  pe.stop();
	  return;
	}
	gal_update_image(curimg+1, true);
  }, 5);
}

function gal_read_cookie()
{
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
	while (c.charAt(0)==' ')
	  c = c.substring(1,c.length);
	if (c.indexOf('markedfiles=') == 0)
	{
	  var files = c.substring('markedfiles='.length,c.length);
	  break;
	}
  }
  if(typeof files == 'undefined')
    return;
  files = files.split('-');
  for(i=0;i < files.length;i++) {
    if($('file_c'+files[i])) {
	  $('file_c'+files[i]).checked = 'checked';
	  $('file_c'+files[i]).parentNode.style.background = 'yellow';
	}
  }
}

function gal_mark(all)
{
  if(all)
    Form.getInputs('gallery', 'checkbox').each(function(input) { input.checked = 'checked'; input.parentNode.style.background = 'yellow'; });
  else
    Form.getInputs('gallery', 'checkbox').each(function(input) { input.checked = ''; input.parentNode.style.background = ''; });
}

function resize_img(img, maxwidth, maxheight)
{
  if(maxwidth && img.width > maxwidth) {
	img.height = (img.height / img.width) * maxwidth;
	img.width = maxwidth;
  }
  if(maxheight && img.height > maxheight) {
    img.width = (img.width / img.height) * maxheight;
    img.height = maxheight;
  }
}

document.observe('dom:loaded', function()
{
  Event.observe('box-open', 'click', function(e) { Effect.toggle('language_box', 'blind', {duration: 0.5}); });

  $$('div.bb_spoiler').each(function(tag)
  {
    var openerTag = tag.down('span.spoiler_link');
    var contentTag = tag.down('div.spoiler_content');

    contentTag.style.display = 'none';
    Event.observe(openerTag, 'click', function() { Effect.toggle(contentTag, 'blind', {duration:0.5}); });

  });

});