// 検索条件
  var searchCond = {};
  var lo_keyword = new Array();

  // --- 初期化 ---
  $(function(){

    $("#subcategoryList div").each(function(){
      lo_keyword.push( $(this).attr("id") );
    });

    jQuery.each( lo_keyword , function() {
    // --- イベントハンドラ ---
      search(this);
    });

    // -- search_recommend --
    var yID = $("dl.recommendMovie").attr("id");
    search_recommend(yID);

  });


  // --- 検索 ---
  function search(cond) {

  var str_id =new Array();
      str_id = cond.split("____");

    if ( str_id[1] ){

    // ajax通信定義
    $.ajax({
      dataType: "jsonp",
          data: {
                  "alt":"json-in-script"
                },
         cache: true,
           url: "http://gdata.youtube.com/feeds/api/videos?vq={\""+ str_id[1] +"\"}",
       success: function (data) {

          // 検索結果件数を取得・表示
          searchCond.total = parseInt(data.feed.openSearch$totalResults.$t,10);

          // 検索結果が0件
          if (searchCond.total == 0) {
            $("#" + cond + "> dl > dd > p.title").html(
                "動画の掲載は終了いたしました。"
              )
              return;
          }
          // エントリを参照してサムネイルを生成
          $.each(data.feed.entry, function(i,item){
            var group = item.media$group;

            $("#" + cond + "> dl > dt").html(
             $("<a>").attr(({ href:group.media$player[0].url+"&amp;hl=ja&amp;autoplay=1&amp;width=640&amp;height=385", title:group.media$description.$t, alt:"" })).html(
                $("<img>").attr("src", group.media$thumbnail[2].url)
              )
            )

            $("#" + cond + "> dl > dd > p.date").html(
              formatDate(item.published.$t)
            )

            $("#" + cond + "> dl > dd > p.title").html(
                item.title.$t
              )
            $('a[href^=http://www.youtube.com/watch]').lightpop({overlayBgColor:'#000',contentBorder:'1px solid silver'});

         });
       }
    });
    }
  }

  function search_recommend(sid) {

  if (sid){
    var str_id2 = new Array();
        str_id2 = sid.split("____");

    if ( str_id2[1] ) {

    // ajax通信定義
    $.ajax({
      dataType: "jsonp",
          data: {
                  "alt":"json-in-script"
                },
         cache: true,
           url: "http://gdata.youtube.com/feeds/api/videos?vq={\""+ str_id2[1] +"\"}",
       success: function (data) {

          // 検索結果件数を取得・表示
          searchCond.total = parseInt(data.feed.openSearch$totalResults.$t,10);

          // 検索結果が0件
          if (searchCond.total == 0) {
            $("dl#" + sid + "> dd > p.title").html(
                "動画の掲載は終了いたしました。"
              )
              return;
          }
          // エントリを参照してサムネイルを生成
          $.each(data.feed.entry, function(i,item){

            var group = item.media$group;

            $("dl#" + sid+ "> dt").html(
             $("<a>").attr(({ href:group.media$player[0].url+"&amp;hl=ja&amp;autoplay=1&amp;width=640&amp;height=385", title:group.media$description.$t, alt:"" })).html(
                $("<img>").attr("src", group.media$thumbnail[0].url)
              )
            )

            $("dl#" + sid + "> dd > p.date").html(
              formatDate(item.published.$t)
            )

            $("dl#" + sid + "> dd > p.title").html(
                item.title.$t
              )

            $("dl#" + sid + "> dd > p.text").html(
              group.media$description.$t
            );

            $('a[href^=http://www.youtube.com/watch]').lightpop({overlayBgColor:'#000',contentBorder:'1px solid silver'});

         });
       }
    });
   }
  }
  }

var formatDate = function(dateString) {
                var year  = dateString.substr(0,4);
                var month = dateString.substr(5,2);
                var day   = dateString.substr(8,2);
                return year+"年"+month+"月"+day+"日";
            }

