// Some globals
var _result;
var _filename;
var _datadir;
var _downloadname;
var _animtype;
var _currentsize,_currentsizebutton;
var _fontcol0,_fontcol1,_fontcol2,_fontcol3,_fontcol4,_fontcol5,_fontcol6,_fontcol7;
var _thisstyle,_laststyle;

_currentsize="l";
//_currentsizebutton=document.getElementById('optionwidth3a_default');
_laststyle="";
_thisstyle="flutter";
_animtype="none";

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

function pageInit() {
//updateBGColour();
}

function bgPreview() {
 if (_animtype=="none")
  preview_url="preview.php?image="+_datadir+"/"+_filename+".png";
 else
  preview_url="preview.php?image="+_datadir+"/"+_filename+".gif";
// window.location=preview_url;
 window.open(preview_url,"_preview");
}

function GetCodes() {
 
 if (_animtype=="none")
  codes_url="get-font-image.php?result="+_result+"&ref="+_datadir+"/"+_filename+".png";
 else
  codes_url="get-font-image.php?result="+_result+"&ref="+_datadir+"/"+_filename+".gif";
// window.location=codes_url;
 window.open(codes_url,"_codes");
}

function GetCodesManual() {
 
 if (_animtype=="none")
  codes_url="hosting-unavailable.php?ref1="+_datadir+"/"+_filename+".png";
 else
  codes_url="hosting-unavailable.php?ref1="+_datadir+"/"+_filename+".gif";

 codes_url+="&ref2="+_datadir+"&ref3="+_downloadname+"&ref4="+_animtype;
// window.location=codes_url;
 window.open(codes_url,"_codes");
}

/*
// Set the form bgcolour
function updateBGColour() {
var bgcolour_sel=document.getElementById('gen_fields').bgcolour_sel;

// bgcolour_sel dropdown
len = bgcolour_sel.length;
for (i = 0; i <len; i++) {
if (bgcolour_sel[i].selected) {
bgcolour_val = bgcolour_sel[i].value;
}
}

document.getElementById('gen_fields').bgcolour.value=bgcolour_val;
}
*/

function setStyle(style) {
// Set the form style
document.getElementById('gen_fields').font_style.value=style;
//SelectFontColour(0);
ProcessGen();
////updateFontColour();
}

function updateFontColour() {
var i0,i1,i2,i3,i4,i5,i6,i7;
var curstyle;

curstyle=document.getElementById('gen_fields').font_style.value;
base_url="http://static1.glowtxt.com/backgrounds/";

// Get each image 0-7 for this font style
i0=textures[curstyle][0]+".jpg"; 
i1=textures[curstyle][1]+".jpg"; 
i2=textures[curstyle][2]+".jpg"; 
i3=textures[curstyle][3]+".jpg"; 
i4=textures[curstyle][4]+".jpg"; 
i5=textures[curstyle][5]+".jpg"; 
i6=textures[curstyle][6]+".jpg"; 
i7=textures[curstyle][7]+".jpg"; 

// Update the font colour divs with the new images
//alert(base_url+i0);
_fontcol0.style.backgroundImage="url("+base_url+i0+")";
_fontcol1.style.backgroundImage="url("+base_url+i1+")";
_fontcol2.style.backgroundImage="url("+base_url+i2+")";
_fontcol3.style.backgroundImage="url("+base_url+i3+")";
_fontcol4.style.backgroundImage="url("+base_url+i4+")";
_fontcol5.style.backgroundImage="url("+base_url+i5+")";
_fontcol6.style.backgroundImage="url("+base_url+i6+")";
_fontcol7.style.backgroundImage="url("+base_url+i7+")";
}

function ProcessGenCallback() {
 if(Ajax.CheckReadyState(Ajax.request))
 {
//  alert("callback done");
  var response = Ajax.request.responseXML.documentElement;
  var _image = response.getElementsByTagName('image');
  _result = response.getElementsByTagName('result')[0].firstChild.data;
  _filename = response.getElementsByTagName('filename')[0].firstChild.data;
  _datadir = response.getElementsByTagName('datadir')[0].firstChild.data;
  _downloadname = response.getElementsByTagName('downloadname')[0].firstChild.data;
  _imageheight = response.getElementsByTagName('imageheight')[0].firstChild.data;
  _animtype = response.getElementsByTagName('animtype')[0].firstChild.data;
  _result=RTrim(_result);
  _downloadname=RTrim(_downloadname);

  var html_output;
  if (_animtype=="none")
   html_output="<img src='http://static1.glowtxt.com/"+_datadir+"/"+_filename+".png' onload='ShowOutput()'>";
  else
   html_output="<img src='http://static1.glowtxt.com/"+_datadir+"/"+_filename+".gif' onload='ShowOutput()'>";
/*
  var html_output="<img src='"+_datadir+"/"+_filename+".gif' onload='ShowOutput()'>"; 
*/
  document.getElementById('hidden_output').innerHTML=html_output;
/*
  alert("html_output is "+html_output);
  alert("result is "+_result);
  alert("filename is "+_filename);
*/
  //window.location="ecard-sent.php";
 }
}

function ShowOutput() {

  var html_output;
  if (_animtype=="none")
   html_output="<table height='100%'><tr valign='center'><td><img src='http://static1.glowtxt.com/"+_datadir+"/"+_filename+".png'></td></tr></table>";
  else
   html_output="<table height='100%'><tr valign='center'><td><img src='http://static1.glowtxt.com/"+_datadir+"/"+_filename+".gif'></td></tr></table>";

/*
  var html_output="<table height='100%'><tr valign='center'><td><img src='"+_datadir+"/"+_filename+".gif'></td></tr></table>";
*/
  document.getElementById('hidden_output').innerHTML="";
  var mod_height=_imageheight;
  if (mod_height<100) mod_height=100;
  document.getElementById('output').style.height=parseInt(mod_height)+18;
  document.getElementById('output-wrapper').style.height=parseInt(mod_height)+18;

  //document.getElementById('fontbox').style.marginTop=(parseInt(mod_height)-118)+43;
  document.getElementById('fontouterbox').style.marginTop=(parseInt(mod_height)-118)+43-25;
  document.getElementById('output').innerHTML=html_output;

  // Hide the progress indicator
  document.getElementById('output-overlay').style.display="none";

  updateFontColour();

 // Reset colour selection first if changing font style
 if (_thisstyle!=_laststyle) {
 var fc,oldfc,oldfcnum;
 oldfcnum=document.getElementById('gen_fields').font_colour.value;
 fc=_fontcol0;

 switch(oldfcnum) {
  case "0": oldfc=_fontcol0; break;
  case "1": oldfc=_fontcol1; break;
  case "2": oldfc=_fontcol2; break;
  case "3": oldfc=_fontcol3; break;
  case "4": oldfc=_fontcol4; break;
  case "5": oldfc=_fontcol5; break;
  case "6": oldfc=_fontcol6; break;
  case "7": oldfc=_fontcol7; break;
 }

document.getElementById('gen_fields').font_colour.value=0;

oldfc.style.borderTop="2px solid #000";
//oldfc.style.borderBottom="1px solid #000";
//fc.style.borderTop="2px solid #fff";
fc.style.borderTop="2px solid #c7c";
//fc.style.borderBottom="1px solid #b6b";
}



//  alert("image height is "+_imageheight);
//515,25
  // Write the embedding code
  var html_output="";
  if (_result=="ok") {
   html_output="<textarea id='embed_textarea' onClick='this.focus();this.select()'>";

   html_output+="<a href='http://glowtxt.com/'>";
   if (_animtype=="none")
    html_output+="<img src='http://static1.glowtxt.com/"+_datadir+"/"+_filename+".png' border='0' alt='glowtxt.com image'>";
   else
    html_output+="<img src='http://static1.glowtxt.com/"+_datadir+"/"+_filename+".gif' border='0' alt='glowtxt.com image'>";

   html_output+="</a>";

//html_output+="Sorry! Link codes are disabled right now until the site move is complete!";
   html_output+="</textarea>";
  } 
  else if (_result=="toobigtolink") {
   html_output="<textarea id='embed_textarea'>";
   html_output+="\n\nImage is too big to link to - please download instead, or try a smaller font size or non-animated version .";
   html_output+="</textarea>";
  }
  else if (_result=="busy") {
   html_output="<textarea id='embed_textarea'>";
   html_output+="Sorry, the server is busy at the moment- please try again in a few minutes.";
   html_output+="</textarea>";
  }
//  document.getElementById('embed_code').innerHTML=html_output;

  // Submit type note
  var modenote;
  var submit_type = document.getElementById('gen_fields').submittype.value;
  if (submit_type!="comment")
   modenote="Text mode - Image can be any width";
  else
   modenote="Comment mode - Image width max. 400 pixels";
  document.getElementById('mode-status').innerHTML=modenote;

  html_output="<form name='download_fields' action='download.php' method='POST' style='display:inline'>";
  html_output+="<input type='hidden' name='filename' id='filename' value='";
  html_output+=_filename;
  html_output+="'>";
  html_output+="<input type='hidden' name='downloadname' id='downloadname' value='";
  html_output+=_downloadname;
  html_output+="'>";
  html_output+="<input type='hidden' name='animtype' id='animtype' value='";
  html_output+=_animtype;
  html_output+="'>";
//  html_output+="<input type='submit' value=' Get\r\nWeb code' id='codesbutton' onclick='GetCodesManual(); return false;'>";
  html_output+="<input type='submit' value=' Get\r\nWeb code' id='codesbutton' onclick='GetCodes(); return false;'>";
  html_output+="<input type='submit' value='  Download\r\n Image  ' id='downloadbutton'>";
  html_output+="<input type='submit' value=' Preview\r\nBackground' id='previewbutton' onclick='bgPreview(); return false;'> ";
  html_output+="</form>";
  html_output+="<div style='line-height:1px; height:1px; font-size: 0em; clear: both; margin-bottom:5px'>"; 
  html_output+="</div>";
  html_output+="<center><font size='-2' color='#bb99ee'>Note: Hotlinking not supported, use URL from 'Get web code'.</font></center>";

  document.getElementById('embed_code_text').innerHTML=html_output;
}

function ProcessGen() {
var font_style = document.getElementById('gen_fields').font_style;
var font_size = document.getElementById('gen_fields').font_size.value;
var font_colour = document.getElementById('gen_fields').font_colour.value;
var bgcolour = document.getElementById('gen_fields').color0.value;
var text = document.getElementById('gen_fields').text.value;
var text2 = document.getElementById('gen_fields').text2.value;
var text3 = document.getElementById('gen_fields').text3.value;
var glow_halo = document.getElementById('gen_fields').glow_halo;
var non_trans = document.getElementById('gen_fields').non_trans.checked;
var glitter_border = document.getElementById('gen_fields').glitter_border.checked;
var anim_type = document.getElementById('gen_fields').anim_type;
var submit_type = document.getElementById('gen_fields').submittype.value;

// fontstyle dropdown
len = font_style.length
for (i = 0; i <len; i++) {
if (font_style[i].selected) {
font_style_val = font_style[i].value
}
}

_laststyle=_thisstyle;
_thisstyle=font_style_val;

// Reset colour selection first if changing font style
if (_thisstyle!=_laststyle) {
/*
var fc,oldfc,oldfcnum;
oldfcnum=document.getElementById('gen_fields').font_colour.value;
fc=_fontcol0;

switch(oldfcnum) {
case "0": oldfc=_fontcol0; break;
case "1": oldfc=_fontcol1; break;
case "2": oldfc=_fontcol2; break;
case "3": oldfc=_fontcol3; break;
case "4": oldfc=_fontcol4; break;
case "5": oldfc=_fontcol5; break;
case "6": oldfc=_fontcol6; break;
case "7": oldfc=_fontcol7; break;
}

document.getElementById('gen_fields').font_colour.value=0;

oldfc.style.borderTop="2px solid #000";
//oldfc.style.borderBottom="1px solid #000";
//fc.style.borderTop="2px solid #fff";
fc.style.borderTop="2px solid #c7c";
//fc.style.borderBottom="1px solid #b6b";
*/

 font_colour=0;
}

/*
// size radio buttons
len = font_size.length
for (i = 0; i <len; i++) {
if (font_size[i].checked) {
font_size_val = font_size[i].value
}
}
*/
font_size_val = font_size;

// anim type radio buttons
len = anim_type.length
for (i = 0; i <len; i++) {
if (anim_type[i].checked) {
anim_type_val = anim_type[i].value
}
}

// glowhalo radio buttons
len = glow_halo.length
for (i = 0; i <len; i++) {
if (glow_halo[i].checked) {
glow_halo_val = glow_halo[i].value
}
}

var reqstring = 'gentext2.php?text='+encodeURIComponent(text);
reqstring += '&text2='+encodeURIComponent(text2);
reqstring += '&text3='+encodeURIComponent(text3);
reqstring += '&font_style='+encodeURIComponent(font_style_val);
reqstring += '&font_size='+font_size_val;
reqstring += '&font_colour='+font_colour;
reqstring += '&bgcolour='+encodeURIComponent(bgcolour);
reqstring += '&glow_halo='+glow_halo_val;
reqstring += '&non_trans='+non_trans;
reqstring += '&glitter_border='+glitter_border;
reqstring += '&anim_type='+anim_type_val;
reqstring += '&submit_type='+submit_type;

//alert("calling "+reqstring);

// Ajax animation
//var html_output="<img src='ajax-loader2.gif' style='margin:40px 0 0 0'>";
//document.getElementById('output').innerHTML=html_output;
document.getElementById('output-overlay').style.display="";

Ajax.Request(reqstring, ProcessGenCallback);
}

function SetSizeHigh(element,size) {
 if (_currentsize==size) return;
 element.style.backgroundPosition="-40px 0px";
}
function SetSizeLow(element,size) {
 if (_currentsize==size) return;
 element.style.backgroundPosition="0px 0px";
}
function SetSizeSelected(element,size) {
 if (_currentsize==size) return;

 _currentsizebutton.style.backgroundPosition="0px 0px";
 element.style.backgroundPosition="-80px 0px";
 _currentsize=size;
 _currentsizebutton=element;
 document.getElementById('font_size').value=size;
 ProcessGen();
}

function SelectFontColour(num,skip) {
var fc,oldfc,oldfcnum;
oldfcnum=document.getElementById('gen_fields').font_colour.value;

//alert("num is "+num+", oldfcnum is "+oldfcnum);

if (num==oldfcnum) return;

switch(num) {
case "0": fc=_fontcol0; break;
case "1": fc=_fontcol1; break;
case "2": fc=_fontcol2; break;
case "3": fc=_fontcol3; break;
case "4": fc=_fontcol4; break;
case "5": fc=_fontcol5; break;
case "6": fc=_fontcol6; break;
case "7": fc=_fontcol7; break;
}

switch(oldfcnum) {
case "0": oldfc=_fontcol0; break;
case "1": oldfc=_fontcol1; break;
case "2": oldfc=_fontcol2; break;
case "3": oldfc=_fontcol3; break;
case "4": oldfc=_fontcol4; break;
case "5": oldfc=_fontcol5; break;
case "6": oldfc=_fontcol6; break;
case "7": oldfc=_fontcol7; break;
}

document.getElementById('gen_fields').font_colour.value=num;
oldfcnum=num;

oldfc.style.borderTop="2px solid #000";
//oldfc.style.borderBottom="1px solid #000";

//fc.style.borderTop="2px solid #fff";
fc.style.borderTop="2px solid #c7c";
//fc.style.borderBottom="1px solid #b6b";

if (skip!=1)
 ProcessGen();
}


