var switch_paged_img = function(img_src, max){
  var r = new RegExp("(\\d{2})\\-" + max);
  var pos_match = img_src.match(r);
  var pos = parseInt(pos_match[1].replace(/^0/, '')) || 1;
  var title = '';
  var pre_img = '';
  var next_img = '';
  if (pos > 1)
    var pre_img = img_src.replace(r, ((pos - 1) < 10 ? '0' + (pos - 1).toString() : (pos - 1).toString()) + "-" + max);
    if (pos < 24)
      var next_img = img_src.replace(r, ((pos + 1) < 10 ? '0' + (pos + 1).toString() : (pos + 1).toString()) + "-" + max);

    $.fancybox([{
      "href" : img_src,
      "title" : (pre_img ? '<a class="pre_img" href="#" onclick="return switch_paged_img(\'' + pre_img+ '\', '+ max + '); ">前のページへ</a>' : '')+
          (next_img ? '<a class="next_img" href="#" onclick="return switch_paged_img(\'' + next_img+ '\', '+ max+ ');">次のページへ</a>' : '')
    }],{
      "padding": 0,
      "transitionIn": 'none',
      "transitionOut": 'none',
      "type" : 'image',
      "changeFade": 0
    })
    return false;
  };


$(function(){
  if ($('#topics').size()){
    var fl = (function(isIe){
      try{
        return isIe ? (new ActiveXObject('ShockwaveFlash.ShockwaveFlash')) : navigator.plugins['Shockwave Flash'];
      }catch(e){
        return false;
      }
    }(navigator.userAgent.match(/MSIE/)));
    var elm = $('#topics');

    elm.find('.clearfix object').remove();
    elm.find('header').css({"display":'block'});
    elm.find('article.clearfix').addClass('box').find('#alternate-ss').css({"display":'block'}).find('li:first-child').css({"display":'block'});

    var pos = 0;
    var lis = elm.find('#alternate-ss li');
    var flash = elm.find('#flash');
    var tm = 8000;
    flash.css({"opacity":0,"display":'block'}).click(function(){
      window.location = flash.parent().find('.active a').attr('href');
      return false;
    }).hover(function(){ clearInterval(iv); },
             function(){ setTimeout(interval, 0); });
    var sf = function(){
      var current = pos;
      var next = pos < lis.length - 1 ? pos + 1 : 0;
      flash.animate({"opacity":1}, 200, function(){
        $(lis[current]).css({"display":'none'}).removeClass('active');
        $(lis[next]).css({"display":'block'}).addClass('active');
        $(lis[next]).css({"opacity":1, "display":'block'}, 50);
        flash.animate({"opacity":0}, 200);
      });
      pos = next;
    };
    var iv = null;
    var interval = function(){ iv = setInterval(sf, tm); };
    setTimeout(interval, tm);
  }
});
