jQuery.noConflict();
(function($) {
$(function() {
  var context;

  $('head').append(
      '<style>'
    + '#bnoverlay {' 
    + '  display: none;' 
    + '  position: absolute;'
    + '  background: #000000;'
    + '  top: 0;' 
    + '  left: 0;'
    + '  z-index: 99;'
    + '}'
    + '#bnpopup { '
    + '  display: none;' 
    + '  position: absolute;'
    + '  width: 100%;' 
    + '  text-align: center;'
    + '  z-index: 100;'
    + '}'
    + '#bncontainer { '
    + '  margin: 0 auto;' 
    + '  background: #FFFFFF;'
    + '  position: relative;'
    + '}'
    + '#bntitle { '
    + '  height: 30px;'
    + '  line-height: 30px;'
    + '  padding: 10px 30px;'
    + '  text-align: left;'
    + '  color: #FFFFFF;'
    + '  background: #00AEEB;'
    + '  width: 100%;'
    + '}'
    + '#bntitle strong { '
    + '  font-weight: bold;'
    + '  font-size: 16px;'
    + '  margin-right: 20px;'
    + '}'
    + '#bntitle span { '
    + '  font-size: 14px;'
    + '  font-weight: bold;'
    + '}'
    + '#bnbody { '
    + '  width: 100%;'
    + '  padding: 20px 30px 0 30px;'
    + '  background: #FFFFFF;'
    + '}'
    + '#bnctl { '
    + '  width: 100%;'
    + '  height: 20px;'
    + '  padding: 20px 30px;'
    + '  background: #FFFFFF;'
    + '}'
    + '#bnctl1 { '
    + '  float: left;'
    + '  text-align: left;'
    + '  width: 48%;'
    + '}'
    + '#bnctl2 { '
    + '  float: right;'
    + '  text-align: right;'
    + '  width: 48%;'
    + '}'
    + '.continue { '
    + '  color: #00AEEB;'
    + '  text-decoration: underline;'
    + '  cursor: pointer;'
    + '}'
    + '#bnclose { '
    + '  cursor: pointer;'
    + '}'
    + '</style>'
    + '');

  $('body').append(
      '<div id="bnoverlay"></div>'
    + '<div id="bnpopup">'
    + '<div id="bncontainer">'
    + '<div id="bntitle"><strong>　</strong><span></span></div>'
    + '<div id="bnbody"><img id="bnpic" src="" alt=""></div>'
    + '<div id="bnctl">'
    + '<div id="bnctl1"><span id="bnclose">× CLOSE</span></div>'
    + '<div id="bnctl2"><span id="bnprev">&lt; PREV</span> ｜ <span id="bnnext" class="continue">NEXT &gt;</span></div>'
    + '</div>'
    + '</div>'
    + '</div>'
    + '');

  $('.bn').click(function() {
    $this = $(this);
    context = {
      id : $this.attr('id'),
      index : 1,
      title : $this.attr('title'),
      max : (/(\d+)/.exec($this.attr('class'))||[])[1],
      images : [null]
    };

    if (!context.max) {
      return;
    }

    if (context.max == 1) {
      $('#bnctl2').hide();
    } else {
      $('#bnctl2').show();
    }

    _preloadImages();
    _changeImage();

    $('#bnprev').removeClass('continue');
    $('#bnnext').addClass('continue');

    $('#bnoverlay').fadeTo('fast', 0.8);
    var mainTitle = (/([^\[]+)/.exec(context.title)||[])[1];
    var subTitle = (/\[(.+?)\]/.exec(context.title)||[])[1];
    $('#bntitle strong').text(mainTitle).next().text(subTitle);
    $('#bnpopup').fadeIn();
  });

  $('#bnnext').click(function() {
    if (context.index >= context.max) {
      return;
    }
    $('#bnprev').addClass('continue');
    context.index++;
    if (context.index + 1 > context.max) {
      $(this).removeClass('continue');
    }
    _changeImage();
  });


  $('#bnprev').click(function() {
    if (context.index <= 1) {
      return;
    }
    $('#bnnext').addClass('continue');
    context.index--;
    if (context.index - 1 < 1) {
      $(this).removeClass('continue');
    }
    _changeImage();
  });

  $('#bnclose, #bnoverlay').click(function() {
    $('#bnoverlay').fadeOut();
    $('#bnpopup').fadeOut(function() {
      $('#bntitle strong').text('　').next().text('　');
      $('#bnpic').attr('src', '');
    });
  });

  $(window).resize(_adjust);

  function _changeImage() {
    var currentImage = context.images[context.index];
    var image = new Image();
    $(image)
      .load(function() {
        $('#bnpic').attr('src', currentImage.src);
        $(this).load(function() {});
        $('#bncontainer').animate({ width: this.width + 60, height: this.height + 110}, _adjust);
      })
      .attr('src', currentImage.src);
  }

  function _preloadImages() {
    for (var i = 1; i <= context.max; i++) {
      var image = new Image();
      image.src = 'http://www.dartslive.co.jp/wp-content/media/img/' + context.id + '/' + 'bn' + i + '.jpg';
      context.images.push(image);
    }
  }

  function _adjust() {
    var arrPageSizes = ___getPageSize();
    $('#bnoverlay').css({
        width : arrPageSizes[0],
        height : arrPageSizes[1]
    });

    var arrPageScroll = ___getPageScroll();
    $('#bnpopup').css({
      top : arrPageScroll[1] + (arrPageSizes[3] / 10),
      left : arrPageScroll[0]
    });
  }

  /**
   / THIRD FUNCTION
   * getPageSize() by quirksmode.com
   *
   * @return Array Return an array with page width, height and window width, height
   */
  function ___getPageSize() {
    var xScroll, yScroll;
    if (window.innerHeight && window.scrollMaxY) {  
      xScroll = window.innerWidth + window.scrollMaxX;
      yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
      xScroll = document.body.scrollWidth;
      yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
      xScroll = document.body.offsetWidth;
      yScroll = document.body.offsetHeight;
    }
    var windowWidth, windowHeight;
    if (self.innerHeight) {  // all except Explorer
      if(document.documentElement.clientWidth){
        windowWidth = document.documentElement.clientWidth; 
      } else {
        windowWidth = self.innerWidth;
      }
      windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
      windowWidth = document.documentElement.clientWidth;
      windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
      windowWidth = document.body.clientWidth;
      windowHeight = document.body.clientHeight;
    }  
    // for small pages with total height less then height of the viewport
    if(yScroll < windowHeight){
      pageHeight = windowHeight;
    } else { 
      pageHeight = yScroll;
    }
    // for small pages with total width less then width of the viewport
    if(xScroll < windowWidth){  
      pageWidth = xScroll;    
    } else {
      pageWidth = windowWidth;
    }
    arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
    return arrayPageSize;
  };
  /**
   / THIRD FUNCTION
   * getPageScroll() by quirksmode.com
   *
   * @return Array Return an array with x,y page scroll values.
   */
  function ___getPageScroll() {
    var xScroll, yScroll;
    if (self.pageYOffset) {
      yScroll = self.pageYOffset;
      xScroll = self.pageXOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) {   // Explorer 6 Strict
      yScroll = document.documentElement.scrollTop;
      xScroll = document.documentElement.scrollLeft;
    } else if (document.body) {// all other Explorers
      yScroll = document.body.scrollTop;
      xScroll = document.body.scrollLeft;  
    }
    arrayPageScroll = new Array(xScroll,yScroll);
    return arrayPageScroll;
  };
  /**
  * Stop the code execution from a escified time in milisecond
  *
  */
  function ___pause(ms) {
    var date = new Date(); 
    curDate = null;
    do { var curDate = new Date(); }
    while ( curDate - date < ms);
  }
});
})(jQuery);


