(function(){

 	var init = function(){
		var path = "data/pub/uBFF8WbYvK.swf";
	 
		var flashvars = {
		  path: path
		};
		var params = {
		  menu: "false",
		  wmode: "transparent",
		  bgcolor: "#000000",
      quality: "high"
		};
		var attributes = {};
		swfobject.embedSWF("static/flash/player.swf", "minkPlayer", 425, 267, "10", null, flashvars, params, attributes);
    rss();


    var ranking_url = "ranking.json";
    $.getJSON( ranking_url, ranking );
	};
  var rss = function(){
    var count = 0;
    $.ajax({
      url: "rss.xml",
      dataType:"xml",
      success: function(xml){
        $(xml).find('item').each(function(){
          if(count > 4) return;
          count++;
          var title = $(this).find('title').text();
          var url = $(this).find('link').text();
          var desc = $(this).find('description').text();
          var elem = document.createElement("div");
          var wp;
          var ch;
          elem.innerHTML = desc;
          $(elem).find("img").each(function(){
            var src = $(this).attr("src");
            if(src.match(/jpg/)){
              wp = src;
            } else if(src.match(/png/)){
              ch = src;
            }
          });

          if(title == "no address") title = "";

          var wp_elem = '';
          var ch_elem = '';
          if(wp){
            wp_elem = '<img src="' + wp + '" alt="" width="242" height="140" style="position:absolute; top:0; left:-51px;" />';
          }
          if(ch){
            ch_elem = '<img src="' + ch + '" alt="" class="iepngfix" width="242" height="140" style="position:absolute; top:0; left:-51px;" />';
          }
          var left = 5;
          if($.browser.msie && $.browser.version < 8 && navigator.userAgent.indexOf("MSIE 8.0") < 0){
              left = 0;
          }
          var html = '<div class="sItemBox">' + '\n' +
          '<p class="sItemImage"><a href="' + url + '" style="display:block; position:relative; top:0; left:' + left + 'px; width:140px; height:140px; overflow:hidden;">' + wp_elem + ch_elem + '</a></p>' + '\n' +
          '<p class="sItemTxt"><a href="' + title + '">' + title + '</a></p>' + '\n' +
          '</div>';
          $("#newMovieInBox").append(html);
        });
        $(document).pngFix(); 
      }
    });
  }

  var ranking = function(json){
    var base_link = "http://supersea-okinawa2-movie.jp/movie_preview.html?name=";
    $("#newRankingInBox").html("");
    var rank = 1;
    $.each(json.ranking, function(i, item){
      var url = base_link + item.id;
      var title = "";
      if(item.url) title = item.url;
      if(title == "no address") title = "";
      var wp_elem = '';
      var ch_elem = '';
      if(item.wp){
        wp_elem = '<img src="' + item.wp + '" alt="" width="242" height="140" style="position:absolute; top:0; left:-51px;" />';
      }
      if(item.ch){
        ch_elem = '<img src="' + item.ch + '" alt="" width="242" height="140" style="position:absolute; top:0; left:-51px;" />';
      }
      var left = 5;
      if($.browser.msie && $.browser.version < 8 && navigator.userAgent.indexOf("MSIE 8.0") < 0){
          left = 0;
      }
      var html = '<div class="rankingItemBox">' + '\n' +
      '<p class="rankNo"><img src="static/images/top/rankNo' + rank + '.gif" alt="' + rank + '位" /></p>' + '\n' +
      '<p class="sItemImage"><a href="' + url + '" style="display:block; position:relative; top:0; left:' + left + 'px; width:140px; height:140px; overflow:hidden;">' + wp_elem + ch_elem + '</a></p>' + '\n' +
      '<p class="sItemTxt"><a href="' + title + '">' + title + '</a></p>' + '\n' +
      '</div>';
      $("#newRankingInBox").append(html);
      rank++;
    });
    $(document).pngFix(); 
  };
 $(document).ready(init);
})(jQuery);
