
function getComment() {
  var url ;
  var id = '';
  var status = '';
  urls = '/article/comment/';
  var divcmt = document.getElementById('comment');

  if (divcmt.style.display == "block"){
    divcmt.style.display = "none";
    status = "none";
    return;
  }else{
    status = "block";
  }
  divcmt.style.display = "block";
  var str = location.pathname;
  var url = /^\/article\/detail\/(\d+)/;
  if (str.match(url)) {
      var m = str.match(url);
      id = m[1];
  }
  url = /^\/(%|[A-Za-z0-9])*\/article\/detail\/(\d+)/;
  if (str.match(url)) {
      var m = str.match(url);
      id = m[2];
      
  }
  urls = urls + id;
  id += '&rand=' + Math.random();
  $("#comment").load(urls + '?' + id);
}


function addComment(f) {
  if(!confirm('コメントを投稿しますか？'))
    return false;

  var article_id = f.article_id.value;
  var comment = encodeURIComponent(f.cmt_comment.value);
  var is_member = f.is_member.value;
  var cmt_name = encodeURIComponent(f.cmt_name.value);
  var cmt_guid = f.cmt_guid.value;
  var urlc = '/article/comment/' + article_id;
  var email = encodeURIComponent(f.cmt_email.value);
  var cmt_title = encodeURIComponent(f.cmt_title.value);
  var urlc = '/article/comment/' + article_id;

  var title_icon = '';
  for(var i=0;i< f.cmt_title_icon.length;i++) {
    var r = f.cmt_title_icon[i];
    if (r.checked) {
      title_icon = r.value;
    }
  }
  if (cmt_name == ''){
    alert('ニックネームを入力してください。');
    return false;
  }
  if (comment == ''){
    alert('コメントを入力してください。');
    return false;
  }
  if (cmt_title == ''){
    alert('タイトルを入力してください。');
    return false;
  }

  var fdata = 'comment=' + comment  + 
              '&article_id=' + article_id +
              '&comment_title=' + cmt_title +
              '&is_member=' + is_member + 
              '&name=' + cmt_name + 
              '&title_icon=' + title_icon + 
              '&comment_guid=' + cmt_guid +
              '&user_agent=' + encodeURIComponent(navigator.userAgent) +
              '&open_flag=1' +
              '&email=' + email 
              ;
  jQuery.ajax({
    type: "POST",
    url: urlc,
    data: fdata,
    success: function(res){
      if (res ==''){
        alert('コメントありがとうございました。');
      }else{
        alert('コメント登録に失敗しました');
      }
    }
  });
  return false;
}


function getTrackBack() {
  var url ;
  var id = '';
  var status = '';
  urls = '/article/trackback/';
  var divcmt = document.getElementById('tblist');

  if (divcmt.style.display == "block"){
    divcmt.style.display = "none";
    status = "none";
    return;
  }else{
    status = "block";
  }
  divcmt.style.display = "block";
  var str = location.pathname;
  var url = /^\/article\/detail\/(\d+)/;
  if (str.match(url)) {
      var m = str.match(url);
      id = m[1];
  }
  url = /^\/(%|[A-Za-z0-9])*\/article\/detail\/(\d+)/;
  if (str.match(url)) {
      var m = str.match(url);
      id = m[2];
      
  }
  urls = urls + id;
  id += '&rand=' + Math.random();
  $("#tblist").load(urls + '?' + id);
}


function delFile(id,filename) {
  if(!confirm('ファイルを削除しますか？'))
    return false;
  var data = 'id=' + id + '&file_name=' + filename + '&mode=del';
  jQuery.ajax({
    type: "POST",
    url: '/user/author/upload',
    data: data,
    success: function(msg){
      alert('削除しました');
      location.reload();
    }
  });
}


    $(document).ready(function () {
			var strSearch = location.href;

			if ( strSearch.indexOf( "img=" ) != -1 ) {
			  var current = getParams()['img'];
  			current = current.replace(/\\/g,'/').replace(/.*\//, '');;

        var caption = '';
        $("#article a").each(function(){
          if($(this).children('img').length != 0){
            var fileName = $(this).attr("href");
            fileName = fileName.replace(/\\/g,'/').replace(/.*\//, '');
            if (fileName == current){
                caption = $(this).closest('div.centerimg').children('div.caption').text();
//                caption = $(this).find('div.caption').text();
            }
            
          }
        });

        //記事初期化
				$("#article").empty();
				$("#article_bottom").empty();
				$("#lead").empty();
				$(".linkmenu_sub").empty();
				$(".profile").empty();
				$(".ad_info_list").empty();
        
  			var page = getParams()['p'] ;
  			var curentIndex = 0;
  			var nextImg;
  			var preImg;
  			var thumbnail  = new Array();
  			var article_id;
  			var str = location.pathname;
  		  var url = /^\/article\/detail\/(\d+)/;
  		  var alias = 'detail';
  		  if (str.match(url)) {
  		      var m = str.match(url);
  		      article_id = m[1];
  		  }
  		  url = /^\/article\/proof\/(\d+)/;
  		  if (str.match(url)) {
  		      var m = str.match(url);
  		      article_id = m[1];
  		      alias = 'proof';
  		  }
  		  

        //テンプレート定義
        $.template( "thumbnailTemplate", '\
        <div class="article_image">\
          <div class="centerimg">\
            <img src="/static/images/article/${ article_id }/${ current }">\
            {{if caption }}\
              <center>\
              <div class="caption">${caption}</div>\
              </center>\
            {{/if}}\
          </div>\
        </div>\
          \
          <div id="bo">\
            <ul>\
              {{if curentIndex > 1 }}\
              <li id="bo-01">\
                <a href="/article/'+ alias + '/${ article_id }{{if page }}${ page }&{{else}}?{{/if}}img=${ preImg }">\
                  <span>≪</span>\
                </a>\
              </li>\
              {{else}}\
              <li id="bo-none"></li>\
              <li>\
              {{/if}}\
              <li>\
                  <div align="center" style=" width:538px; float:left;">\
                  <input type="button" class="back_article" \
                  onclick="location.href=\'/article/'+ alias + '/${ article_id }${page}\';return true;" \
                  value="記事に戻る">\
                  </div>\
              </li>\
              {{if maxIndex > curentIndex }}\
              <li id="bo-02">\
                <a href="/article/'+ alias + '/${ article_id }{{if page }}${ page }&{{else}}?{{/if}}img=${ nextImg }"><span>≫</span></a>\
              </li>\
              {{else}}\
              <li id="bo-none"></li>\
              {{/if}}\
              </ul>\
          </div>\
          \
          <div id="thumbnail">\
          {{each(i,tmp) thumbnail }}\
            <a href="/article/'+ alias +'/${ article_id }?img=${ tmp }">\
              <img width="80" src="/static/images/article/${ article_id }/${ tmp }">\
            </a>\
          {{/each}}\
          </div>\
        ');
        
        
        //データ生成
        jQuery.each(images, function(i, val) {
           var fileName = val.replace(/\\/g,'/').replace(/.*\//, '');
           thumbnail[i] = fileName;
           if ( current  == fileName  ){
              curentIndex = i + 1;
           }
        });
        

        if ( (curentIndex + 1) <= images.length){
            nextImg = images[curentIndex];
            nextImg = nextImg.replace(/\\/g,'/').replace(/.*\//, '');
        }
        if ( curentIndex > 1){
            preImg = images[curentIndex -2];
            preImg = preImg.replace(/\\/g,'/').replace(/.*\//, '');
        }
        
        
        var paramImage= [{
          'maxIndex'   : thumbnail.length,
          'current'    : current,
          'curentIndex': curentIndex,
          'thumbnail'  : thumbnail,
          'article_id' : article_id,
          'nextImg'    : nextImg,
          'preImg'     : preImg,
          'page'       : page ? '?p=' + page : '',
          'caption'    : caption
        }];
        $.tmpl( "thumbnailTemplate", paramImage ).appendTo( "#article" );
				
			}
			
			
	    $("#article a").click(function () {
					if ( strSearch.indexOf( "img=" ) == -1 ) {
							if($(this).children('img').length != 0){
										var file_url = $(this).attr('href');
										
										file_url = file_url.substring(file_url.lastIndexOf("/")+1,file_url.length)
										if ( strSearch.indexOf( "?" ) == -1 ) {
											location.href= location.href + '?img=' + file_url;
										}	else {
											location.href= location.href + '&img=' + file_url;
										}
									return false;
							}
					}
					return true;
	    });
	    
	    
    });
    
function getParams()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i <hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}    
    



