/*  @twitter取得  */
$(function() {
  var tmpl = {};  //テンプレート保存のための変数
  $.tmplLoad = function(callback) {
    $.get('http://www.sapporo-collection.jp/tmpl/twitter.html')
    .done(function(res) {
      var tmplTxt = $('<div>' + res + '</div>');  //テンプレートを取得するためにjQueryオブジェクトに変換
      tmpl.twtr = tmplTxt.find('#tmpl_twitter');  //テンプレートを取得し、保存
      callback();
    });
  };

  $.tmplLoad(function() {
    var tweets = [];  //タイムラインを入れる箱（配列）

    $.when(
      getTweet('satsucolle')
    )
    .done(function(res) {
      var current = undefined;
      $.merge(tweets, res);  //タイムラインデータをマージして1つにしておく

      for (var c in tweets) {
        tweets[c].created_at = getTime(tweets[c].created_at);  //投稿時間の表示変更
        tweets[c].text = twttrUserAnchorStyle(tweets[c].text);  //リンク等を置き換え
      }

      $('#tweets ul').html(tmpl.twtr.tmpl(tweets));  //tweetsをテンプレートに渡して表示
    });
  });

  function getTweet(name) {
    return $.ajax({
      url: 'http://api.twitter.com/1/statuses/user_timeline.json',
      data: {
        screen_name: name,
        count: 10
      },
      dataType: 'jsonp',
      type: 'GET'
    });
  }

  //リンクやハッシュの置き換え関数
  function twttrUserAnchorStyle(tweet) {
    return tweet
      .replace(/(https?\:\/\/[^ 　]+)/g, '<a href="$1" target="_blank">$1</a>')
      .replace(/(\#[^ 　]+)/g, '<a href="https://twitter.com/search?q=$1" target="_blank">$1</a>')
      .replace(/@(\w{1,15})/g, '<a href="http://twitter.com/$1" target="_blank">@$1</a>');
  }

  //投稿時間の表示変更関数
  function getTime(created_at) {
    created_at = replaceTwTimeStr(created_at);
    var current = new Date(created_at);
    return current.getFullYear() +
      '-' + (current.getMonth()+1) +
      '-' + current.getDate() +
      ' ' + current.getHours() +
      ':' + current.getMinutes() +
      ':' + current.getSeconds();
  }

  //IEではDateオブジェクトが作成されないため文字列を置換
  function replaceTwTimeStr(time) {
    var tArray = time.split(' '),
    timeStr = tArray[0] + ' ' + tArray[1] + ' ' + tArray[2] + ' ' + tArray[5] + ' ' + tArray[3] + ' ' + tArray[4];
    return timeStr;
  }
});

/* @facebookページ「いいね」数取得 */
$(function() {
  $.when(
    $.ajax({
      url: 'https://graph.facebook.com/SAPPORO.COLLECTION',
      dataType: 'jsonp',
      type: 'GET'
    })
  ).done(function(res) {
    if (res.likes === undefined) $('#sec_facebook .like').prepend('<strong>' + 0 + '</strong>');
    else $('#sec_facebook .like').prepend('<strong>' + res.likes + '</strong>');
  });
});

$(function() {

  var bodyId = $('body').attr('id');

  //グローバルナビのドロップダウン
  $('#primarynav ul ul').hide().each(function() {
    var _this = $(this);
    var h = _this.height();
    _this.parent().hover(
      function() {_this.css({height: h}); _this.stop(true, true).slideDown(300, 'easeInOutCirc');},
      function() {_this.stop(true, true).slideUp(300, 'easeInOutCirc');}
    );
  });

  /* @content部分の高さ取得 */
  $(window).load(function() {
    if (bodyId != 'home') $('#doc').css({height: $('#doc').height(), marginBottom: 30});
  });

  /* @mailmagazineのcolorbox表示 */
  $('#action_mail a').colorbox({iframe:true, width: '760px', height: '520px'});

  /* @twitterのjScrollPaneの適用 */
  $(window).load(function() {
    setTimeout(function() {
      $('#tweets').jScrollPane();
    }, 500);
  });

  if (bodyId != 'home') {
    //各種snsボタンの設置
    $.when([
      $.getScript("http://platform.twitter.com/widgets.js")
    ])
    .done(function() {
      $('#sns').socialButton();
    });

    $(window).load(function() {
      //snsボタンの位置調整
      var h = $('#content').innerHeight();
      $('#sns').css({position: 'absolute', right: 0, top: h - 15});
    });
  }

  /* @home */
  if (bodyId == 'home') {
    $(window).load(function() {
      //footerの位置調整
/*      var header = $('#globalheader').outerHeight(),
          slideshow = $('#slideshow').outerHeight(),
          content = $('#content').outerHeight();
      $('#globalfooter').css({position: 'absolute', left: 0, top: (header + slideshow + content + 30)});
*/
    });
    //jCarouselの実行
    $('#sec_guest .gl-content').each(function() {
      $(this).jcarousel({
        scroll: 0,
        auto: 4,
        wrap: 'circular'
      });
    });

    $('#sec_guestmodel .gl-content').each(function() {
      $(this).jcarousel({
        scroll: 0,
        auto: 4,
        wrap: 'circular'
      });
    });

    $('#sec_brand .gl-content').each(function() {
      $(this).jcarousel({
        scroll: 1,
        auto: 4,
        wrap: 'circular'
      });
    });

    //一時的なカーソルの透明度
    $('body#home #content #sec_guest .jcarousel-prev').css({opacity: '0.3', cursor: 'default'});
    $('body#home #content #sec_guest .jcarousel-next').css({opacity: '0.3', cursor: 'default'});
    $('body#home #content #sec_guestmodel .jcarousel-prev').css({opacity: '0.3', cursor: 'default'});
    $('body#home #content #sec_guestmodel .jcarousel-next').css({opacity: '0.3', cursor: 'default'});

    //newsのjScrollPaneの適用
    $('#news').jScrollPane();
  }

  /* @guest */
  if (bodyId == 'guest') {
    $('.gs-list a').colorbox({
      iframe: 'true',
      width: '750px',
      height: '480px'
    });
  }

  /* @blog */
  if (bodyId == 'blog') {
    //$('#main_content article div a').colorbox();
  }

  /* @for teaser */
  if(!(_ua.ltIE6)) {
    if ($('body').hasClass('teaser')) {
      $('#img_container img:not(:first-child)').maxImage({
        isBackground: true,
        slideShow: true,
        slideShowTitle: false,
        slideDelay: 5,
        overflow: 'auto',
        maxFollows: 'both',
        verticalAlign: 'middle',
        horizontalAlign: 'center',
        loaderClass: 'loader',
        resizeMsg: {show: false}
      });
    }
  }
});

