function preview_button_click()
{ set_message(decode(encode($('input_field').getValue())),true);
  $('input').hide();
  $('message').show();
	if (animated)
	{	$('animation').show();
	  $('image').hide();
  }
}

function set_message(message,close_link)
{ message = '<div style="width: 160px; height: 377px; overflow-y: auto;">'+message+'</div>';
  if (close_link) message += '<br /><a href="javascript:end_preview();">sluit dit voorbeeld</a>';
  else message += '<br />&nbsp;';
  $('message').update(message);
}

function end_preview()
{ $('input').show();
  $('message').hide();
  if (animated) {
  	$('animation').hide();
  	$('image').show();
  }
}

function close_send_popup()
{ $('popup').hide();
}

function getURL()
{ return 'http://www.fliefd.nl/gift.html?i='+image+'&f='+format+'&m='+encode($('input_field').getValue());
}

function encode(url)
{ url=url.replace(/ /gi, '_');
  url=rot13(url);
  url=escape(url);
  return url;
}

function decode(url)
{ url=unescape(url);
  url=rot13(url);
  url=url.replace(/_/gi, ' ');
  url=url.replace(/\n/gi, "<br>\n");
  return url;
}

function calc_procent_full()
{ return Math.round(((getURL().length)*100)/2048);
}

function send_button_click()
{ $procent_full = calc_procent_full();
  if ($procent_full<100)
  { if ($('popup').visible())
    { new Ajax.Request('popup.html',      { method:'get',        onSuccess: function(transport){ popup_load_event(transport.responseText); }      });
    }
    else
    { $('popup').show();
      set_result_field_focus();
    }
  }
  else
  { alert('Foutmelding! Uw persoonlijke bericht kan niet verstuurd worden. Het bericht is te lang.');
  }
}

function urlencode(str)
{ str=escape(str);
  str=str.replace(/ /gi, '%20');
  return str;
}

function foo()
{
}

function popup_load_event(popup_text)
{ $('popup').update(popup_text);
  $('popup').show();
  url=getURL();
  $('popup_link').update('<input id="result_field" value="'+url+'" />');
  $('email_link').update('<a href="mailto:?SUBJECT='+urlencode('e-card van www.fliefd.nl')+'&BODY='+urlencode('Klik op de volgende link om de e-card te openen: '+url)+'" />Klik daarvoor op deze link.</a>');
  set_result_field_focus();
  Event.observe($('popup'), 'click', function(event){ set_result_field_focus(); });
}

function set_result_field_focus()
{ $('result_field').focus();
  $('result_field').select();
}

function rot13(str)
{ return str.replace(/[a-zA-Z]/g, function(c)
  { return String.fromCharCode((c <= "Z" ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26);
  });
}


function read_args()
{ var loc = new String(document.location);
  if (loc.indexOf('?')>=0)
  { loc = loc.split('?')[1]
    var tmp_args = loc.split('&');
    var args = new Object();
    args[0]=loc;
    for (var i=0;i<tmp_args.length;i++)
    { arg = tmp_args[i];
      split = arg.split('='); 
      args[split[0]]=split[1];
    }
  }
  return args;
}

var message=false
var image=new String();
var format=new String();
var animated = false;
var qstr=new String();
args = read_args();
if (args[0]!==undefined) { qstr = args[0]; }
if (args['m']!==undefined) { message = decode(args['m']); }
if (args['i']!==undefined) { image = args['i']; }
if (args['f']!==undefined) { format = args['f']; }
if (format == "swf") animated = true;

function input_keypress_event()
{ $procent_full = calc_procent_full();
  if ($procent_full<100)
  { $('size').update('<div style="width: 160px; border: 1px solid gray; margin-top: 4px;">'+
		'<div style="width: '+Math.round(($procent_full/100)*160)+'px; background: #c0c0c0; color: white; font-weight: bold; font-size: 30%; text-align: right;">&nbsp;</div>'+
		'</div>');
  }
  else
  { $('size').update('<div style="width: 160px; border: 1px solid gray; margin-top: 4px;">'+
		'<div style="width: 160px; background: #ff0000; color: white; font-weight: bold; font-size: 30%; text-align: right;">&nbsp;</div>'+
		'</div>');
  }
}

function document_load_event()
{ $('image').update('<a href="' + image + 'print.jpg" target="_blank"><img src="' + image + '.jpg" width="437" height="600" border="0" alt="Klik voor een hoge resolutie printversie" /></a>'); 
  if (animated)
  {	var FO = { movie: image+".swf", width:"437", height:"600", majorversion:"8", build:"0", xi:"true"};
    $('animation').hide();
	  UFO.create(FO, "animation");
  }
  if (message!==false)
  { if (animated)
	  { $('image').hide();
  	  $('animation').show();
    }	
	  set_message(message);
  }
  else
  { new Ajax.Request('input.html',    { method:'get',      onSuccess: function(transport){ input_load_event(transport.responseText); }    });
  }
}

function input_load_event(input_text)
{ $('input').update(input_text);
  Event.observe($('input_field'), 'keypress', function(event){ input_keypress_event(); });
  input_keypress_event();
}

