if (typeof Popup == 'undefined') {
    var Popup = {};
}

$(document).ready(function() {
  // Item shadow
  $('li.shelf-box div.item-shadow, div.user-rank').pngFix();
  $('li.shelf-box div.item-shadow img').each(function () {
    var dh = 210;
    var iw = $(this).parent().next().children('a').children('img').width();
    var ih = $(this).parent().next().children('a').children('img').height();
    var oh = 50 * (dh-ih)/dh + 25;
    $(this).width(iw * 1.375);
    $(this).height(ih * 1.1875);
    // $(this).parent().next().css('bottom',oh);
    // $(this).parent().css('bottom',oh-ih/8);
  });
  if(!($.browser.msie && $.browser.version == '6.0')) {
    $('li.shelf-box div.item-image a, li.shelf-box div.item-pop a').each(function() {
      var elm = $(this);
      var id = (elm.attr('href').match(/pop\/(\d+)/) || [])[1];
      elm.click(function() {
        var pop = elm.parent().parent().find('div.item-pop img');
        if(pop.data('on_zooming')) {
          $(pop).attr('src', $(pop).attr('src').replace(/(_s)?.png/, '_s.png'));
          $(pop).animate({
            width: "128px",
            marginLeft: "0px"
          },200);
        }
        Popup.show(id);
        return false;
      });
      elm.attr('href', 'javascript:void(0);');
    });
  }
  $('#popup-close a').click(function() {
    Popup.hide();
    return false;
  });
  $('#shadow').click(function() {
    if($('#popup').hasClass('open')) Popup.hide();
    return false;
  });
  /*
  // POP zoom(except IE6)
  if(!($.browser.msie && $.browser.version == '6.0')) {
    $('li.shelf-box').hover(function() {
      var pop = $(this).find('div.item-pop img');
      var po = $(this).offset();
      if(!$(pop).data('on_zooming')) {
        $(pop).data('on_zooming', true);
        $(pop).parent().parent().css("z-index","10000");
        $(pop).attr('src', $(pop).attr('src').replace(/_s.png/, '.png'));
        $(pop).animate({
          width: "240px",
          marginLeft: "-56px"
        },250);
      }
    }, function() {
      var pop = $(this).find('div.item-pop img');
      if($(pop).data('on_zooming')) {
        $(pop).attr('src', $(pop).attr('src').replace(/(_s)?.png/, '_s.png'));
        $(pop).animate({
          width: "128px",
          marginLeft: "0px"
        },200);
        $(pop).parent().parent().css("z-index","inherit");
        $(pop).data('on_zooming', false);
      }
    });
  }
  */
  if(location.hash.match(/POP:(\d+)/)) {
    Popup.show(RegExp.$1);
  }
});

Popup.show = function (id) {
  if(!id) return;
  var user_id = (location.href.match(/user\/(\d+)/) || [])[1];
  if(parseInt( (location.href.match(/pop\/(\d+)/) || [])[1] ) != id) location.hash = "POP:"+id;
  $('select').hide(); // for IE
  $('#popup').html('');
  $('#popup').load("/pop/show_partial/"+id + ( (user_id)? "?associate=true" : "") );
  this.shadow(true, function(){ $('#popup').show().addClass('open'); });
}

Popup.hide = function () {
  if(location.href.match(/(.+)\/pop\/\d+/)) {
    location.href = RegExp.$1;
  } else {
    location.hash = "";
    $('#popup').removeClass('open').hide();
    $('select').show(); // for IE
    this.shadow(false);
  }
}

Popup.shadow = function(display, callback) {
  if(display) {
    var shadow_height = Math.max( $('body:first').height(), $('html').height());
    $('#shadow').height(shadow_height).show().fadeTo(500,0.8,function () {
      if(callback) callback();
    });
  } else {
    $('#shadow').fadeTo(500,0).hide();
  }
}

function add_favorite(self, model, id) {
  $.post("/"+model+"/add_favorite/"+id);
  $(self).parent().prev().html('GJしました!<br/><a href="/user/gjs">あなたがGJしたPOP一覧</a>'); 
  $(self).parent().html("");
}
