function is_purchased(vid) {
  var view_flag = false;
  if (videos[vid] != undefined) {
    view_flag = videos[vid].view_flag;
  }
  return view_flag;
}
function is_fc(pid) {
  var view_flag = false;
  if (fclist[pid] != undefined) {
    view_flag = fclist[pid];
  }
    return view_flag;
}

function get_view_list() {
  num = 0;
  $('#viewablevideo_list').empty();
  $('#popvideo_list').empty();
  $(document).ready(function(){
    $.get('/json/purchasedVideo','',
      function(data){
        var thumb = '';
        eval(data);
        for (i in videos) {
         if (videos[i].view_flag == 1) {
           if (num==10) { thumb+='<div id="viewablevideo_more" style="display:none;">'; }
           var listvars = 'userID='+userId+'&user_id='+userId+'&media_id='+i+'&performerID='+videos[i].user_id+'&langID=jp&skinName=skin_vmessage&media_name='+videos[i].video_name;
           thumb+='<div class="viewablevideo_block">';
           thumb+='<div class="thumbnail">';
           if (videos[i].hd==1) { thumb+='<img width="29" height="29" class="hdicon_s" src="/img/dxvideo/HD_logo_30x30_gold.png"/>'; }
           if (videos[i].thumb=='none') { 
             thumb+='<a href="/video/detail/'+i+'">';
             thumb+=get_object(listvars,108,81); 
             thumb+='</a>';
           } else { 
             thumb+='<a href="/video/detail/'+i+'"><div class="video"></div></a>';
             thumb+='<img src="'+unescape(videos[i].thumb)+'" width="108" height="81">'; 
           }
           thumb+='</div>';
           thumb+='<div class="video_info">';
           title = videos[i].title.substring(0, 14);
           if (videos[i].title.length > 14) { title+='...'; }
           user_name = videos[i].user_name.substring(0, 18);
           if (videos[i].user_name.length > 18) { user_name+='...'; }
           thumb+='<a href="/video/detail/'+i+'">'+title+'</a><br />';
           thumb+='<a href="javascript:void(0)" onClick="openProfile(\''+user_name+'\')">'+user_name+'</a> <br />';
           thumb+='残り時間：'+videos[i].remained;
           thumb+='</div>';
           thumb+='<div class="button"><a href="javascript:void(0)" onClick="play_video(\''+i+'\')">再生</a></div></div>';
           ++num;
         }
        }
        if (num>0) {
          if (isVIP()) {
            thumb = '<div class="title_image"><img src="/img/dxvideo/tit_viewablevideo.gif" alt="閲覧可能ビデオ" width="166" height="31" /></div>'+thumb;
          } else {
            thumb = '<div class="title_image"><img src="/img/dxvideo/tit_viewablevideo_reg.gif" alt="閲覧可能ビデオ" width="166" height="31" /></div>'+thumb;
          }
          if (num>10) { 
            thumb+='</div>'; 
            thumb+='<div id="more_link" class="more_link"><a href="javascript:void(0)" onClick="showMore()">>>すべて表示</a></div>';
            thumb+='<div id="hide_link" class="more_link" style="display:none"><a href="javascript:void(0)" onClick="showLess()"><<10本まで表示</a></div>';
          }
        }
        $('#viewablevideo_list').append(thumb);
        $(document).ready(function(){
          $.get('/json/videoRanking','',
            function(data){
              var thumb = '';
              eval(data);
              for (i in video_ranking) {
                if (num>=10) { break; }
                if (videos[i] && videos[i].view_flag == 1) { continue; }
                var rankingvars = 'userID='+userId+'&user_id='+userId+'&media_id='+i+'&performerID='+video_ranking[i].user_id+'&langID=jp&skinName=skin_vmessage&media_name='+video_ranking[i].video_name;
                thumb+='<div class="popvideo_block">';
                thumb+='<div class="thumbnail">';
                if (video_ranking[i].hd==1) { thumb+='<img width="29" height="29" class="hdicon_s" src="/img/dxvideo/HD_logo_30x30_gold.png"/>'; }
                if (video_ranking[i].thumb=='none') { 
                  thumb+='<a href="/video/detail/'+i+'">';
                  thumb+=get_object(rankingvars,108,81); 
                  thumb+='</a>';
                } else { 
                  thumb+='<a href="/video/detail/'+i+'"><div class="video"></div></a>';
                  thumb+='<img src="'+unescape(video_ranking[i].thumb)+'" width="108" height="81">'; 
                }
                thumb+='</div>';
                thumb+='<div class="video_info">';
                title = video_ranking[i].title.substring(0, 14);
                if (video_ranking[i].title.length > 14) { title+='...'; }
                user_name = video_ranking[i].user_name.substring(0, 18);
                if (video_ranking[i].user_name.length > 18) { user_name+='...'; }
                thumb+='<a href="/video/detail/'+i+'">'+title+'</a><br />';
                thumb+='<a href="javascript:void(0)" onClick="openProfile(\''+user_name+'\')">'+user_name+'</a> <br />';
                thumb+='ポイント：<span id="point_'+i+'">'+video_ranking[i].points+'pt</span>';
                thumb+='</div>';
                fc_flag = is_fc(i);
                if (video_ranking[i].video_type < 4 && (!fc_flag || fc_flag==0)) {
                  thumb+='<div class="button"><a class="fc" href="/fanclub/join/'+video_ranking[i].user_name+'" target="_blank">FC入会</a></div></div>';
                } else {
                  flag = is_purchased(i);
                  if (!flag) {
                    thumb+='<div class="button"><a class="first" href="javascript:void(0)" id="buy_'+i+'" name="l_buy_'+i+'" onClick="play_video(\''+i+'\')">初回購入</a></div></div>';
                  } else if (flag==0) {
                    thumb+='<div class="button"><a href="javascript:void(0)" id="buy_'+i+'" name="l_buy_'+i+'" onClick="play_video(\''+i+'\')">購入</a></div></div>';
                  } else {
                    thumb+='<div class="button"><a class="play" href="javascript:void(0)" onClick="play_video(\''+i+'\')">再生</a></div></div>';
                }
              }
              ++num;
            }
            if (thumb != '') {
              if (isVIP()) {
                thumb = '<div class="title_image"><img height="31" width="166" alt="人気ビデオ" src="/img/dxvideo/tit_popvideo.gif"/></div>'+thumb;
              } else {
                thumb = '<div class="title_image"><img height="31" width="166" alt="人気ビデオ" src="/img/dxvideo/tit_popvideo_reg.gif"/></div>'+thumb;
              }
            }
            $('#popvideo_list').append(thumb);
          });
        });
     });
  });
}

function showMore() {
  $('#viewablevideo_more').show();
  $('#more_link').hide();
  $('#hide_link').show();
}

function showLess() {
  $('#viewablevideo_more').hide();
  $('#more_link').show();
  $('#hide_link').hide();
}

function showHelp(aff) {
  desc = "ムービータイプは全部で3種類あります。\n\n";
  if (aff!=1) { desc+= "DX"; }
  desc+= "ビデオ ノーマル （0.5pt/1分）\n";
  if (aff!=1) { desc+= "DX"; }
  desc+= "ビデオ プレミアム （1pt/1分）\n";
  desc+= "PRビデオ （無料）";
  alert(desc);
}

function addFavoriteVideo(vid){
  $(document).ready(function(){
    $.get('/video/favorite/add/'+vid,'',
      function(data){
        if (data=='success') {
          alert('お気に入りに追加しました');
        } else if (data=='already') {
          alert('既にお気に入りに追加されています');
        } else {
          alert('追加できませんでした');
        }
      });
  });
} 

function delFavoriteVideo(vid){
  var check = confirm("本当に削除しますか？");
  if (check) {
    $(document).ready(function(){
      $.get('/video/favorite/delete/'+vid,'',
        function(data){
          if (data=='success') {
            window.location.reload();
          } else {
            alert(data + '削除できませんでした');
          }
      });
    });
  } else {
    return false;
  }
}

function buy_point(flag) {
  $('#buypoint').hide();
  $("#videopage").fadeTo("slow",1);
  $("#videopage").unblock();
  if (flag==1) {
    if (window.active_user === undefined) {
      openNewWindow('/join.html','join','resizable=yes,width=800,height=500,scrollbars=yes');
    } else {
      openNewWindow('/join/'+active_user.site_id,'join','resizable=yes,width=800,height=500,scrollbars=yes');
    }
  }
}

function play_video(vid) {
  flag = is_purchased(vid);
  if (!flag || flag==0) {
    if (jQuery.browser.msie) {
      var myTable = document.getElementById('mailAndBalance');
      if (myTable) {
        var myTableBody = myTable.getElementsByTagName("tbody")[0];
        var tds = myTableBody.getElementsByTagName('td');
        var balance = tds[1].innerHTML;
      } else {
        var balance = $("#balance").html();
      }
      var points = document.getElementById('point_'+vid);
      if (points==null) { points = '0pt'; }
      else { points = points.innerHTML;}
    } else {
      var balance = $("#balance").html();
      var points = $("#point_"+vid).text();
    }
    balance = parseFloat(balance.replace(/<\/?[^>]+>/gi, ''));
    points  = parseFloat(points.replace('pt',''));
    if (points > balance) {
      showPointPurchase(document.getElementById('point_'+vid), 'buypoint', 'leftbottom', -20,0);
      return false;
    }
    window.open("/video/player/" + vid,"player","width=770,height=800,scrollbars=yes,location=no,menubar=no,left=10,top=30");
    setTimeout('buy_video('+vid+')',18000);
  } else {
    window.open("/video/player/" + vid,"player","width=770,height=800,scrollbars=yes,location=no,menubar=no,left=10,top=30");
  }
}

function buy_video(vid) {
  $(document).ready(function(){
    $.get('/json/purchasedVideo','',
      function(data){
        eval(data);
        if (videos[vid] == undefined) {
          return false;
        } else if (videos[vid].view_flag == 1) {
          $("#buy_"+vid).removeClass('thumbnail_buyfirst_btn thumbnail_buy_btn');
          $("#buy_"+vid).addClass('thumbnail_play_btn');
          $("#detail_"+vid).removeClass('buyfirst buy');
          $("#detail_"+vid).addClass('play');
          get_view_list();
        }
      } );
  });
  if (window.active_user === undefined) {
    xmlHttpGet('/member/account', 'status');
  } else {
    $(document).ready(function(){
      $.get('/json/user','',
        function(data){
          eval(data);
          $("#balance").empty();
          $("#balance").append(active_user.balance+'pt');
        });
    });
  }
}

function check_other_video(vid,page) {
  $(document).ready(function(){
    $.get('/json/getOtherVideo/'+vid+'/'+page, '',
      function(data){
        eval(data);
        if (nextPage==0) { $("#goRight").hide(); } else { $("#goRight").show(); }
        if (prevPage==0) { $("#goLeft").hide(); } else { $("#goLeft").show(); } 
        document.other.currentPage.value=currentPage;
        document.other.nextPage.value=nextPage;
        document.other.prevPage.value=prevPage;
        var cnt = 0;
        for (i in others) {
          now = new Date(others[i].date_publish);
          $('#other_type_'+cnt).empty();
          if (others[i].video_type==1 || others[i].video_type==2 || others[i].video_type==3) {
            video_type = 'fc';
          } else if (others[i].video_type==6) {          
            video_type = 'pr';
          } else if (others[i].video_type==7 || others[i].video_type==8){
            video_type = 'reg';
          }
          if (others[i].hd==1) { video_type+='HD'; }
          $('#other_type_'+cnt).append('<img src="/img/dxvideo/icon_'+video_type+'.png" class="thumbnail_type">');
          $('#other_img_'+cnt).empty();
          if (others[i].thumb != 'none') {
            $('#other_img_'+cnt).append('<img src="'+unescape(others[i].thumb)+'" width="132" height="99">');
          } else {
            var listvars = 'userID='+userId+'&user_id='+userId+'&media_id='+i+'&performerID='+others[i].user_id+'&langID=jp&skinName=skin_vmessage&media_name='+others[i].video_name;
            $('#other_img_'+cnt).append(get_object(listvars,132,99));
          }
          $('#other_title_'+cnt).empty();
          $('#other_title_'+cnt).append(others[i].title);
          $('#other_point_'+cnt).empty();
          $('#other_point_'+cnt).append(others[i].points+'pt');
          $('#other_length_'+cnt).empty();
          video_length = others[i].video_length;
          if (video_length >=60) {
            video_min = Math.floor(video_length/60);
            video_sec = video_length - (video_min*60);
          } else {
            video_min = '00';
            video_sec = video_length;
          }
          $('#other_length_'+cnt).append(video_min+'分'+video_sec+'秒');
          $('#other_date_'+cnt).empty();
          $('#other_date_'+cnt).append(others[i].created_year+'年'+others[i].created_month+'月'+others[i].created_date+'日');
          document.getElementById('other_link_'+cnt).href = '/video/detail/'+i;
          $('#other_td_'+cnt).show();
          ++cnt;
        }
        for (t=cnt; t<4; ++t) {
          if (totalPage>1) { $('#other_td_'+t).hide(); }
          document.getElementById('other_link_'+cnt).href = 'javascript:void(0);';
        }
      });
  });
}

function showImg(w,h,thumb) {
 document.write('<img width="'+w+'" height="'+h+'" src="'+unescape(thumb)+'" style="vertical-align:bottom;">');
}

function check_val(v) {
  var cnt = 0;
  if (v.keyword.value!='') { ++cnt; }
  if (v.search_genre.value!='') { ++cnt; }
  if (v.search_type.value!='') { ++cnt; }
  if (cnt==0) {
    alert('検索条件をご入力ください'); 
    return false;
  } else {
    return true;
  }
}

function get_object(val,w,h) {
  var thumb = '';
  thumb+='<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="'+w+'" height="'+h+'" align="middle" STYLE="Z-INDEX:-1">';
  thumb+='<param name="allowScriptAccess" value="sameDomain" />';
  thumb+='<param name="wmode"value="transparent" />';
  thumb+='<param name="movie" value="/flash/video/thumbnail_player_20.swf" />';
  thumb+='<param name=FlashVars value="'+val+'" />';
  thumb+='<param name="quality" value="high" />';
  thumb+='<param name="bgcolor" value="#F2F2F2" />';
  thumb+='<param name="salign" value="LT" />';
  thumb+='<embed src="/flash/video/thumbnail_player_20.swf" flashvars="'+val+'" quality="high" bgcolor="#F2F2F2" width="'+w+'" height="'+h+'" align="middle" salign="LT" allowscriptaccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" wmode="transparent" />';
  thumb+='</object>';
  return thumb;
}

function openWindow(a,b) {
  var checkVersion = navigator.appVersion; 
  if (checkVersion.indexOf("MSIE") != -1) { window.location.href='/video/detail/'+a; }
  else { window.open('/video/detail/'+a, target="_self"); }
}

function report_video(vid) {
  var check = confirm("ビデオに規約違反の内容がございましたら、\n「OK」をクリックしてください。\n担当者が確認させていただきます。\n\nビデオを通報しますか？");
  if (check) {
    $(document).ready(function(){
      $.get('/video/report/'+vid, '',
        function(data){
          alert(data);
        });
    });
  } else {
    return false;
  }
}

var faderTimer;
var divId;
var divHeight;
var newDivHeight;
var overlayXOffset;
var overlayYOffset;

function getposOffset(overlay, offsettype){
    var totaloffset=(offsettype=="left")? overlay.offsetLeft : overlay.offsetTop;
    var parentEl=overlay.offsetParent;
    while (parentEl!=null){
      totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
      parentEl=parentEl.offsetParent;
    }
    return totaloffset;
}

function showPointPurchase(curobj, subobjstr, opt_position, Xoffset, Yoffset){
  $("#videopage").fadeTo("normal", 0.4);
  $("#videopage").block({message: ''});
  originalObj = curobj;
  overlayXOffset = Xoffset;
  overlayYOffset = Yoffset;

  return overlay(curobj, subobjstr, opt_position);
}

function overlay(curobj, subobjstr, opt_position){
  if (document.getElementById){
    var subobj=document.getElementById(subobjstr);
    subobj.style.display=(subobj.style.display!="block")? "block" : "none";
    var xpos=getposOffset(curobj, "left")+((typeof opt_position!="undefined" && opt_position.indexOf("right")!=-1)? -(subobj.offsetWidth-curobj.offsetWidth) : 0); 
    var ypos=getposOffset(curobj, "top")+((typeof opt_position!="undefined" && opt_position.indexOf("bottom")!=-1)? curobj.offsetHeight : 0);
    xpos+=overlayXOffset;
    ypos+=overlayYOffset;
    subobj.style.left=xpos+"px";
    subobj.style.top=ypos+"px";
    return false;
  }
  else
    return true;
}

function overlayclose(subobj){
    divId = subobj;
    divHeight = ((document.getElementById(subobj).style.height).substring(0,(document.getElementById(subobj).style.height).indexOf("px")));
    newDivHeight = divHeight;
    fadeDiv();
}


