
button = 0;
Hi     = 1280 / 800;

/*********************************** getPoint ****************************************************/
function getPoint(theEvent){
  var obj = document.form1;
  var msg = document.getElementById('str'); var shadow = document.getElementById('shadow');
  var xHi = document.form1.tWidth.value / 800; 
  var yHi = document.form1.tHeight.value / 600;
  
  if (document.all)
    theEvent = event;

  if (document.all){
    var x = theEvent.offsetX;//theEvent.clientX;
    if (x < 0) return 1; else if (x > 800 * xHi) return 1;
    obj.x.value = Math.floor(theEvent.offsetX * xHi);//Math.floor(theEvent.clientX * xHi);
    var y = theEvent.offsetY;//theEvent.clientY;
    if (y < 0) return 1; else if (y  > 600 * yHi) {alert(y);return 1;}
    obj.y.value = Math.floor(theEvent.offsetY * yHi); //Math.floor(theEvent.clientY * yHi) - 350;
    if (button)
      return 1;
  }else{
    if (button)
      return 1;
    // Y軸を先に設定しないと、送信ボタンを押した時にずれる
    var y = theEvent.pageY;//theEvent.clientY;
    if ((Math.floor(theEvent.pageY * yHi)) < 0) return 1; else if (y > 600) return 1;
    obj.y.value = Math.floor(theEvent.pageY * yHi);
    var x = theEvent.pageX;

    if (x < 0) return 1; else if (x  > 800) return 1;
    obj.x.value = Math.floor(theEvent.pageX * xHi);
  }
  msg.style.left = x + "px"; shadow.style.left = (x + 1) + "px"; // 表示位置の移動
  msg.style.top  = y + "px"; shadow.style.top  = (y + 1) + "px"; // 影の表示位置の移動
}
// Firefox用
if (!document.all){
  window.document.captureEvents(Event.MOUSEDOWN);
  window.document.onmousedown = getPoint;
}

/***********************************  setStr ****************************************************/
function setStr(obj){
  document.getElementById("str").innerHTML = obj.value.replace(/\n/gm , "<br>");
  document.getElementById("shadow").innerHTML = obj.value.replace(/\n/gm , "<br>");
  document.getElementById('comment').value = obj.value;
}

/*********************************** setFontSize ***********************************************/

function setFontSize(targ , selObj , restore){ //v3.0
  var obj = document.getElementById('str'); var shadow = document.getElementById('shadow');
  obj.style.fontSize = selObj.options[selObj.selectedIndex].value + "px";
  shadow.style.fontSize = selObj.options[selObj.selectedIndex].value + "px";
  document.form1.fontSize.value = selObj.options[selObj.selectedIndex].value;
  if (restore) selObj.selectedIndex=0;
}

/********************************** chengFont *******************************************/

function chengFont(targ , selObj , restore){ //v3.0
  var obj = document.getElementById('str'); var shadow = document.getElementById('shadow');
  obj.style.fontFamily = selObj.options[selObj.selectedIndex].value;
  shadow.style.fontFamily = selObj.options[selObj.selectedIndex].value;
  document.form1.font.value = selObj.selectedIndex;
  
  if (restore) selObj.selectedIndex=0;
}

/**************************************** imgCheng ********************************************/

function imgCheng(img){
  var obj = document.getElementById("screen");
  obj.style.backgroundImage = "url(../img/" + img + "-800x600.jpg)";
  document.form1.id.value = img;
}

/***************************************** showHelp ******************************************/

function showHelp(x , y){
  var msg = "<p" + ">選択されたお好きな壁紙に、自由に文章を入れることが出来るツールです。<" + "br>";
  msg += "※ クリックした位置に文字を移動することが出来ます。<" + "br>";
  msg += "※ フォントサイズ、フォントの種類を変更できます。<" + "br>";
  msg += "※ 下のサムネイルをクリックすると、壁紙の画像を変更できます。<" + "br>";
  msg += "※ メッセージに入力してある文章を、壁紙に組み込むことが出来ます。自由に変更できます。<" + "/p>";
  msg += "<p" + ">※ 表示されている文字の位置・大きさ・フォントの種類は、あくまで目安です。";
  msg += "<br><span style=\"color: #FF6F2F; font-size: 14px;\">※ 画像は必ずご自分のパソコン内に保存してご使用ください。";
  msg += "</" + "p>";
  msg += "<p style=\"text-align: right;\"" + "><a " + "href=\"javascript:showHelp(-500 , -500)\">close</a" + "><" + "/p>";
  var obj = document.getElementById("help");
  obj.style.left = x + "px";
  obj.style.top = y + "px";
  obj.innerHTML = msg;
}

/******************************************* ini *********************************************/

function ini(){
  //document.form1.msg.value = "近頃の若い奴らとはよくゆうけれど\n自分の頃よりだいぶマシ。\n";
  //document.form1.msg.value += "焦ることないさ。\n焦ることないさ。\n自分に言い聞かす\n";
 
  document.form1.fontSizeT.selectedIndex = 3;
  document.form1.menu2.selectedIndex = 0;
  document.form1.fontColor.selectedIndex = 0;
  document.form1.shadowColor.selectedIndex = 1;
  document.form1.cSize.selectedIndex = 0;
  
  var fSize = document.form1.fontSize.value = document.form1.fontSizeT.options[3].value;
  
  
  var msg = document.form1.msg.value;
  var img = "20060710170009-1";
  //document.getElementById('x').disabled = false;
  //document.getElementById('y').disabled= false;
  document.form1.comment.value    = msg;
  var str = document.getElementById('str');
  var shadow = document.getElementById('shadow');
  str.style.fontSize = fSize + "px";
  document.getElementById('shadow').style.fontSize = fSize + "px";
  //str.style.fontSize = Hi * 36; 
  str.innerHTML = msg.replace(/\n/gm , "<br>");
  
  document.getElementById('shadow').innerHTML = msg.replace(/\n/gm , "<br>");
  document.getElementById('msgForm').style.top = "-1500px";
  document.getElementById('screen').style.backgroundImage = "url(../img/" + img + "-800x600.jpg)";
  document.form1.id.value = img;
  document.form1.x.value = 500;
  str.style.left = 500 * 800 / 1280; shadow.style.left = 500 * 800 / 1280 + 1;
  document.form1.y.value = 500;
  str.style.top = 500 * 600 / 1024; shadow.style.top = 500 * 600 / 1024 + 1;
  document.form1.tWidth.value = 1280;
  document.form1.tHeight.value = 1024;
  
  
}

/*********************************************** showMsgForm ********************************/

function showMsgForm(x , y){
  if (x == "" && y == ""){
    if (document.all){
      theEvent = event;
      x = theEvent.clientX; y = theEvent.clientY;
    }else{
      x = theEvent.clientX; y = theEvent.clientY;
    }
  }
  
  var obj = document.getElementById('msgForm');
  obj.style.zIndex = 50;
  obj.style.top  = y + "px";
  obj.style.left = x + "px";
//  var cmt   = document.form1.cmt;
  var cmt   = document.getElementById("cmt");
  cmt.value = document.form1.comment.value;
  button = 1;
  cmt.focus();
}

/*********************************************** closeMsgForm *******************************/

function closeMsgForm(x , y){
  var obj = document.getElementById('msgForm');
  var str = document.getElementById('str');
  var sha = document.getElementById('shadow');
  var hid = document.form1.comment;
  var msg = document.form1.msg;
//  var cmt = document.form1.cmt;
  var cmt = document.getElementById('cmt');
  obj.style.top = y + "px"; obj.style.left = x + "px";
  obj.style.zIndex = 0;
  hid.value = msg.value = cmt.value;
  str.innerHTML = sha.innerHTML = cmt.value.replace(/\n/gm , "<br>");
  button = 0;
}

/********************************************* chengFontColor *******************************/

function chengFontColor(targ , selObj , restore , ele){
  var obj = document.getElementById(ele);
  var color;
  if (selObj.selectIndex == 0)
    color = "#FFFFFF";
  else if (selObj.selectIndex == 1)
    color = "#555555";
  else if (selObj.selectIndex == 2)
    color = "#CCCCCC";
  obj.style.color = color;
}


/****************************************** check ******************************/

function check(){
  var obj = document.form1.comment;
  if (obj.value.length == 0){
    alert("メッセージを入力してください");
    return false;
  }
}

/***************************** chengScreenSize ***********************************************/
function chengScreenSize(targ , selObj , restore){ //v3.0
  var size = selObj.options[selObj.selectedIndex].value;
  var arr  = size.split(",");
  arr[0]   = parseInt(arr[0]);
  arr[1]   = parseInt(arr[1]);
  var oldWidth  = parseInt(document.form1.tWidth.value);
  var oldHeight = parseInt(document.form1.tHeight.value);
  var msg    = document.getElementById('str');
  var shadow = document.getElementById('shadow');


  var oldX    = document.form1.x.value;
  var oldY    = document.form1.y.value;
  
  document.form1.tWidth.value = arr[0];
  document.form1.tHeight.value = arr[1];
  var tmp = eval(oldWidth / arr[0]);
  document.form1.x.value = Math.floor(oldX * arr[0] / oldWidth);//Math.floor(document.form1.x.value * (arr[0] / oldWidth));
  document.form1.y.value = Math.floor(oldY * arr[1] / oldHeight);//Math.floor(document.form1.y.value * (arr[0] / oldHeight));
  
  ////alert("壁紙のサイズを変更したら、もう一度文字の位置を\n確定してください"); 1200:10 = 800:x
}

/***************************************** showMenu *******************************************/
function showMenu(){
  document.getElementById('menu').style.top = "0px";
}
function closeMenu(){
  document.getElementById('menu').style.top = "-15px";
}

/**************************************** onMenu ***********************************************/
function onMenu(n){
  // Firefox用に閉じる、開くボタンにマウスを載せた時に、「詩」を移動させないようにする
  
  if (document.all)
    return;
  button = n;
}