function include_blog(url) {

  $.ajax({

    url      : url, 
    type     : 'GET',
    dataType : 'json',
    timeout  : 5000,
    success  : function( json ) {

      $( "#header-tw-inner" ).html( json[0].text + "<a href='http://twitter.com/obunsha_digital/status/" + json[0].id + "'>" + relative_time( json[0].created_at ) + "</a>" );

      for( i=0; i<3; i++ ) {

        r = new RegExp("(https?://[-_.!~*'()a-zA-Z0-9;/?:@&=+$,%#]+)", "g");
        var text   = json[i].text.replace( r, '<a href="$1">$1 </a>');

        $( "#twitter-inner ul" ).append(
          $( "<li></li>" ).html( text ).append(
            $( "<a></a>" )
              .attr( "href", "http://twitter.com/obunsha_digital/status/" + json[i].id )
              .append( $( "<span></span>" ).addClass("time").html( relative_time( json[i].created_at ) )
            )
          )
        )
      }
    },
    error: function() {

      $( "#header-tw-inner" ).html( "ここでつぶやいています。" ).append(
          $( "<a></a>" ).attr( "href", "http://twitter.com/obunsha_digital/" ).html( "twitter" )
      )
    }
  });
};

function include_twitter(url) {

  $.ajax({

    url  : url,
    type : 'GET',
    dataType: 'json',
    timeout: 5000,
    success: function( json ) {

      for( i=0; i<3; i++ ) {

        var blog_data = {
          title      : json.entries[i].title,
          entry_url  : json.entries[i].link,
          entry_body : json.entries[i].summary,
          entry_time : json.entries[i].issued.replace(/T\d+:\d+:\d+/,"").replace(/-/g,"/")
        }

        var joint = "?anlytop=center";

        // body blog
        $( "#obunsha-blog" ).append(
          $( "<div></div>" ).addClass( "top-article" ).addClass( "clearfix" ).addClass( "blog" + (i+1) ).append(
            $( "<h5></h5>" ).append(
              $( "<span></span>" ).html( "編集部ブログ" )
            )
          )
        )
        $( ".blog" + (i+1) ).append(
          $( "<h4></h4>" ).append(
            $( "<a></a>" ).attr( "href", blog_data.entry_url + joint ).html( blog_data.title ).append( "<br>" ).parent().append(
              "<span class='meta'><span class='update'>" + blog_data.entry_time + "</span> | <a href='http://obunsha-toefl.livedoor.biz/" + joint + "'>編集部ブログ</a></span>"
            )
          )
        )
        $( ".blog" + (i+1) ).append(
          $( "<p></p>" ).html( blog_data.entry_body ).append(
            $( "<a></a>" ).attr( "href", blog_data.entry_url + joint ).html( "つづきを読む" )
          )
        )
      }
    },
    error: function() {
      //何もなし
    }
  });
};

function include_youtube() {

  var movie_params = new Array(4);
  movie_params = [
                   ['「リーディング」', 'reading'  , "http://www.youtube.com/v/AYZOCsE3KK4&feature=player_embedded" ],
                   ['「リスニング」'  , 'listening', "http://www.youtube.com/v/FXK-5WD2vV0&feature=player_embedded" ],
                   ['「ライティング」', 'writing'  , "http://www.youtube.com/v/MwMNUxH8_kY&feature=player_embedded" ],
                   ['「スピーキング」', 'speaking' , "http://www.youtube.com/v/mpc5lAoZWCk&feature=player_embedded" ]
                 ];
  var n = Math.floor( Math.random() * 4 );

  $("#youtube-thumbnail-header .movie-title").text(movie_params[n][0]);
  $("#youtube-thumbnail-header .thumbnail img").attr("src", "/static/img/thumbnail_" + movie_params[n][1] + ".png");
  $("#youtube-thumbnail-header .thumbnail").attr("href", movie_params[n][2]);
}

function include_pickup( params ) {

  var text  = params[0];
  var image = params[1];

  $("#information-twitter-text").html( text );
  $("#information-twitter-thumbnail").attr( "src", image );
}

$(function(){

  include_blog( blog_params );
  include_twitter( twitter_params );
  include_youtube();
  if( pickup_params ) include_pickup( pickup_params );

  $('#twitter .dialog_link').hover(
    function() { $(this).addClass('ui-state-hover'); }, 
    function() { $(this).removeClass('ui-state-hover'); }
  );

  var generators = [
    { name : "global-navi",           code : "navi",   joint : '?ga=' },
    { name : "knowledge-index",       code : "left",   joint : '?ga=' },
    //{ name : "experience-link",       code : "banner", joint : '?ga=' },
    { name : "information-top",       code : "index",  joint : '?ga=' },
    { name : "content-center-top",    code : "center", joint : '?ga=' },
    { name : "content-center-bottom", code : "center", joint : '?ga=' },
    { name : "intro",                 code : "right",  joint : '?ga=' },
    { name : "lineup",                code : "right",  joint : '?ga=' }
  ]

  for( i=0; i<generators.length; i++ ) {
    $("#" + generators[i].name + " a").each(
      function(){
        var href = $(this).attr("href");
        $(this).attr("href", href + generators[i].joint + generators[i].code)
      }
    );
  }

  Shadowbox.init({ language: 'en', players:  ['img', 'html', 'iframe'] });

  $('.dialog').dialog({
    autoOpen: false,
    width: 650,
    modal: true,
    buttons: {
      "OK": function() { 
        $(this).dialog("close"); 
      }
    }
  });

  $('.dialog_link').click(function(){
    var index = $(this).attr("id").slice(11);
    $('#dialog' + index).dialog('open');
    return false;
  });

});




