function cookie(name, value, options){
  if(typeof value != 'undefined'){
    var options = options || {};
    if(value === null){
      value = '';
      options.expires = -1;
    }
    var expires = '';
    if(options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)){
      var date;
      if(typeof options.expires == 'number'){
        date = new Date();
        date.setTime(date.getTime() + options.expires * 24 * 60 * 60 * 1000);
      }else{
        date = options.expires;
      }
      expires = '; expires=' + date.toUTCString();
    }
    var path = options.path ? '; path=' + options.path : '';
    var domain = options.domain ? '; domain=' + options.domain : '';
    var secure = options.secure ? '; secure' : '';
    document.cookie = [name, '=', escape(Object.toJSON(value)), expires, path, domain, secure].join('');
  }else{
    if(document.cookie && document.cookie != ''){
      var value = document.cookie.match(name + '=(.*?)(;|$)');
      if(value){
        return (unescape(value[1].replace(/\+/g, ' '))).evalJSON();
      } else {
        return null;
      }
    }
  }
}

function writeFlash() {
  var flash = cookie('flash');
  var flag = true;
  if(flash && flash != '' && flash.length > 0){
    var content = '';
    for(var i = 0; i < flash.length; i ++){
      for(var j in flash[i]){
        if(j == 'error' || j == 'failure'){flag = false;}
        content += flash[i][j]
      }
    }
    var obj = {rel: 'inline', title: 'Message', message: content};
    if(flag){Object.extend(obj, {autoHideDelay: 1500});}
    modalview.show(obj);
    cookie("flash", null, {path:'/'});
  }
}

function writeAnnouncements(){
  var elem = $('announcement');
  if(elem){
    var data = cookie('announcement');
    if(data){
      elem.innerHTML = "<p class='wrap'>" + data + "</p>";
      elem.insert("<a onclick=\"new Ajax.Request('/javascripts/hide_announcement.js', {asynchronous:true, evalScripts:true}); return false;\" href=\"#\">隠す</a>");
    }
  }
}

function writeUserNavi() {
  var elem = $('userNavigation');
  if(elem){
    var data = cookie('login');
    var content = '';
    if(data == null){
      content += '<a href="/login">ログイン</a> | <a href="/signup">ユーザー登録</a>';
    }else{
      content += 'ようこそ ' + data + ' ( <a href="/user">ダッシュボード</a> | <a href="/logout">ログアウト</a> )'
    }
    elem.innerHTML = content;
  }  
}

writeUserNavi();
writeFlash();
writeAnnouncements();
// if(!cookie("timezone")){cookie("timezone", (new Date().getTimezoneOffset())*(-1), {path: '/'})}

document.observe("dom:loaded", function(){
  if($('keyword')){
    $('keyword').observe('focus', function(){
      this.value = '';
      this.setStyle({color:'#000'});
    });
  }
  if($('announcement') && !Element.empty('announcement')){
    Effect.BlindDown('announcement', {duration:0.5});
  }
  if(window.XMLHttpRequest){
    $$('.linkbox').each(function(elem){
      elem.observe('click', function(){
        location.href = this.select('a')[0];
      });
    });
  } else {
    $$('.linkbox').each(function(elem){
      elem.observe('click', function(){
        location.href = this.select('a')[0];
      });
      elem.observe('mouseover', function(){
        this.addClassName('IE6linkboxHover');
      });
      elem.observe('mouseout', function(){
        this.removeClassName('IE6linkboxHover');
      });
    });
  }
});