var isOpera = false;
if(navigator.userAgent.match(/opera/i)){ isOpera = true; }

//{{{$() : ディスプレイにあわせて高さを設定する
$(function()
{
  var displayHeight;
  if(window.innerHeight){
    displayHeight = window.innerHeight;
  }
  else if(document.documentElement.clientHeight){
    displayHeight = document.documentElement.clientHeight;
  }
  else if(document.body.clientHeight){
    displayHeight = document.body.clientHeight;
  }
  else{
    displayHeight = screen.availHeight;
  }

  if(true || $("#Container").outerHeight(true) < displayHeight){
    var $main = $("#Main");
    var mainMarginPadding = $main.outerHeight(true) - $main.height();
    var newMinHeight = displayHeight - mainMarginPadding - $("#Header").outerHeight(true) - $("#Footer").outerHeight(true);

    //webkitではouterHeightが正常に動作しないので、手動で調整必要
    if(navigator.userAgent.match(/Webkit/i)){
      //newMinHeight -= 15;
    }
    $main.css("min-height" , newMinHeight);
  }

});
//}}}
//{{{$() : 現在のページのNaviMenuの色を設定する
$(function()
{
  var url = document.URL;
  var elm;
  if(url.match(/mynote/)){
    $("#MyNote").addClass("NaviMenuHOver");
  }
  else if(url.match(/linkstock/)){
    $("#LinkStock").addClass("NaviMenuHOver");
  }
  else if(url.match(/moneytag/)){
    elm = $("#MoneyTag").addClass("NaviMenuHOver");
  }
});
//}}}
//{{{$() : 翻訳を設定する
$(function()
{
  var $t = $(".Translate");
  $t.click(function(){
    var $this = $(this);
    var temp = $this.html();
    $this.html($this.attr("translate"));
    $this.attr("translate" , temp);
  });

  $t.attr("title" , "クリックで日本語を表示します");
  $t.before("<img src='/image/book_open.png' style='margin:0 5px;' />");
});
//}}}
//{{{$() : helpButtonを登録する
$(function()
{
  $(".HelpButton").attr("title" , "ヘルプ").click(helpButton);
});
//}}}
//{{{function helpButton()
function helpButton()
{
  $("#HelpDialog").dialog({modal:true , resizable:false ,buttons:{"OK" : ok} , width:600 , position:["center" , "middle"] , dialogClass:"HelpDialog"});
  $("#HelpDialog").dialog("open");

  function ok(){
    $(this).dialog("close");
  }
 
}
//}}}
//{{{function shield(on , cursorWait)
function shield(on , cursorWait)
{
  if(on) {
    if(cursorWait) { $("body").append("<div class='Shield' style='cursor:wait;'>"); }
    else { $("body").append("<div class='Shield'>"); }
  }
  else {
    $(".Shield").remove();
  }
}
//}}}
//{{{function messageDialog(msg , icon)
function messageDialog(msg , icon)
{

  var img = "";
  if(icon == "info"){
    img = "<img src='/image/information.png' style='margin-right:5px;'>";
  }
  else if(icon == "error"){
    img = "<img src='/image/cancel.png' style='margin-right:5px;'>";
  }

  $("<div>" + img + msg + "</div>").dialog({modal:true , resizable:false ,buttons:{"OK" : ok}});

  function ok(){
    $(this).dialog("close");
  }
}
//}}}
//{{{function getByteUTF8(str)
function getByteUTF8(str)
{
  var num;
  for(var i = 0, len = str.length, num, res = 0; i < len; i++) {
    num = str.charCodeAt(i);
    if(num < 0x80) {
      res++;
    } else if(num < 0x800 || (num > 0xd7ff && num < 0xe000)) {
      res += 2;
    } else {
      res += 3;
    }
  }
  return res;
}
//}}}
//{{{function getDomain(url)
function getDomain(url)
{
  return url.replace(/^http[s]*:[/][/]/ , "").replace(/[/].*/, "");
}
//}}}
//{{{function sortMap(map , index)
/*
mapのvalueが配列であるとき、その配列のindex番目の要素で
ソートした結果を配列として返す
*/
function sortMap(map , index)
{
  var _array = [];
  var key;
  for(key in map){ _array.push(map[key]); }
  _array.sort(function(a , b)
  {
    switch(typeof(a))
    {
      case "num":
        return a[index] - b[index]
        break;
      default:
        if(a[index] == b[index]){ return 0;}
        else if(a[index] < b[index]){ return -1;}
        else if(a[index] > b[index]){ return 1;}
        break;
    }
  });
  return _array;
}
//}}}
//{{{function setInput()
function setInput()
{
  function init()
  {
    $(this).css("color" , "#aaaaaa").val($(this).attr("default")).attr("isDefault" , "true");
  }

  function onBlur()
  {
    var $this = $(this);
    if($this.val() == "")
    {
      $this.val($this.attr("default")).css("color" , "#aaaaaa");
      $this.attr("isDefault" , "true");
    }
  }

  function onFocus()
  {
    var $this = $(this);
    if($this.attr("isDefault") == "true")
    {
      $this.val("").css("color" , "#000000");
      $this.attr("isDefault" , "false");
    }
  }

  $("input[default]").each(init).focus(onFocus).blur(onBlur);
}
//}}}
$(function()
{
  setInput();
});
