$(function() {
    $("#tabs").tabs();
});
  
function readyDocument (uri) {
  $(document).ready(function($){
    
    //
    
    //$("#comment").load(uri);
    $("#comment").load(uri, "", function(res, status, req){
      if(status == "success") {
        //jAlert(req, status);
        //var arr = $.makeArray(res);
        //jAlert("", $(res).html());
        
      }else {
        //jAlert('ないです。');
      }
    });
    

    $("#relate_search").jcarousel({
      //wrap: 'circular',
      vertical: false,
      visible: 6,
      itemLoadCallback: relate_search_itemLoadCallback
    });
    
    
    $("#seen_apps").jcarousel({
      //wrap: 'circular',
      visible: 6,
    });
    
    mouseOverPopup();
    
    //$("#comment").before(comment);
  });
  
  
  
}



// for jCarousel 
function relate_search_itemLoadCallback (carousel, state) {
  if (state != "init") {
    return ;
  }
  
  $.getJSON('/search/json?q=' + relate_search_query, function(data){
    relate_search_itemAddCallback(carousel, carousel.first, carousel.last, data);
  });
}

function relate_search_itemAddCallback (carousel, first, last, data) {
  // Simply add all items at once and set the size accordingly.
  //var items = data.split('|');
  var size = data.size;
  //jAlert(size);
  for (i = 0; i < size; i++) {
    //jAlert(data[i].packagename, data[i].title);
    carousel.add(i+1, relate_search_getItemHTML(data[i].packagename, data[i].title, data[i].creator));
  }
  carousel.size(size);
  
  carousel.unlock();
  //carousel.setup();
}

//function relate_search_getItemHTML(pname, title)
//{
//    return '<a href="/detail/app/'+pname+'"><img src="/image.php?pname=' + pname + '&iu=0&iid=0" width="85" height="75" alt="' + title + '" onmouseover="mouseOverPopup()" /></a>';
//};

function relate_search_getItemHTML(pname, title)
{
    return '<div class="imlBox"><div class="img"><a href="http://applizoo.com/detail/app/'+pname+'"><img class="trigger" src="http://applizoo.com/image.php?pname='+pname+'&iu=0&iid=0" width="85" height="75" alt="イメージ" /></a></div><div class="txt"><p class="marBottom5"><a href="http://applizoo.com/detail/app/'+pname+'">'+ title +'</a></p></div></div>';
};


function delWatchList (package_name) {
  jConfirm(package_name + 'をウォッチリストから削除します。<br />よろしいですか?', "アイテム削除", function(r){
    if (r) {
      //$.getJSON("/user_mypage/watchlist/delete", {pname:package_name}, function(json){
      $.getJSON("/user_mypage/deletewatchlist", {pname:package_name}, function(json){
        if(json.status == 'success'){
          window.location.reload();
        }　else{
          jAlert('削除出来ませんでした。');
        }
      });
    }
  });

}

function addWatchList (app_name, package_name, category, isfree) {
  jConfirm(app_name + 'をウォッチリストに追加します。<br />よろしいですか?', "アイテム追加", function(r){
    if (r) {
      $.getJSON("/user_mypage/watchlist/add", {pname:package_name, aname:app_name, cat:category, isfree:isfree}, function(json){
        if(json.status == 'success'){
          jAlert(app_name + "をウォッチリストに追加しました。", 'ウォッチリスト');
          //window.location.reload();
        }else if (json.status == 'not_login'){
          $('<div>'+json.message+'</div>').dialog({
            title:'ウィッシュリスト',
            width:330,
            buttons: {
              '新規登録': function(){
                location.href = "/regist/Preregist";
              },
              'ログイン': function(){
                location.href = "https://applizoo.com/login" + "?refer=http://applizoo.com/detail/app/" + package_name + "&pname=" + package_name + "&aname=" + app_name + "&cat=" + category + "&isfree=" + isfree; 
              },
              'Cancel': function(){
                $(this).dialog('close');
              }
            }
          });
        }　else if (json.status == 'fail') {
          jAlert(json.message, "エラー");
        }
      });
    }
  });
}

function delOriginalReview (package_name) {
  jConfirm(package_name + 'をレビューから削除します。<br />よろしいですか?', "アイテム削除", function(r){
    if (r) {
      $.getJSON("/user_mypage/deletereview", {pname:package_name}, function(json){
        if(json.status == 'success'){
          window.location.reload();
        }　else{
          jAlert('削除出来ませんでした。');
        }
      });
    }
  });

}

function mouseOverPopup() {
  $(function () {
    $('.bubbleInfo').each(function () {
      // options
      var distance = 10;
      var time = 250;
      var hideDelay = 500;
      
      var hideDelayTimer = null;
      
      // tracker
      var beingShown = false;
      var shown = false;
      
      var trigger = $('.trigger', this);
      var popup = $('.popup', this).css('opacity', 0);
      
      // set the mouseover and mouseout on both element
      $([trigger.get(0), popup.get(0)]).mouseover(function () {
        // stops the hide event if we move from the trigger to the popup element
        if (hideDelayTimer) clearTimeout(hideDelayTimer);
        
        // don't trigger the animation again if we're being shown, or already visible
        if (beingShown || shown) {
          return;
        } else {
          beingShown = true;
        
          // reset position of popup box
          popup.css({
            top: -100,
            left: -33,
            display: 'block' // brings the popup back in to view
          })

          // (we're using chaining on the popup) now animate it's opacity and position
          .animate({
            top: '-=' + distance + 'px',
            opacity: 1
          }, time, 'swing', function() {
            // once the animation is complete, set the tracker variables
            beingShown = false;
            shown = true;
          });
        }
      }).mouseout(function () {
        // reset the timer if we get fired again - avoids double animations
        if (hideDelayTimer) clearTimeout(hideDelayTimer);
        
        // store the timer so that it can be cleared in the mouseover if required
        hideDelayTimer = setTimeout(function () {
          hideDelayTimer = null;
          popup.animate({
            top: '-=' + distance + 'px',
            opacity: 0
          }, time, 'swing', function () {
            // once the animate is complete, set the tracker variables
            shown = false;
            // hide the popup entirely after the effect (opacity alone doesn't do the job)
            popup.css('display', 'none');
          });
        }, hideDelay);
      });
    });
  });
}


function popup(obj, id) {
    var elm = document.getElementById(id);
    elm.style.display='block';
    elm.style.position='absolute';
    /* 吹き出しの横軸を設定 */
    elm.style.left = obj.offsetLeft + "px";
    /* 吹き出しの縦軸を設定, 吹き出し自体の高さを引いて設定 */
    elm.style.top = (obj.offsetTop - elm.style.height.substring(0,elm.style.height.length-2)) + "px";
}

function popdown(id) {
    document.getElementById(id).style.display='none';
}


