(function() {

  //document ready
  $().ready(function(){
    shopblogRss(); 
  });

  var shopblogRss = function() {

    var obj = $("#shop-blog").find(".item-frame");
    var url = {
      date: "static/include/getdate.php",
      rss : "static/include/shopblogRss.php"
    };
    var time = {};

    var getDate = function(){
      $.ajax({
        type: "GET",
        url  : url.date,
        cache: true,
        dataType: "xml",
        timeout : 3000,
        error   : function() {
          return true;
        },
        success : function(data) {
          time.y = $("year",data).text();
          time.m = $("month",data).text();
          time.d = $("day",data).text();
          time.res = time.y + "." + time.m + "." + time.d;
          ajaxGetContent();
          return true;
        }
      });
    }

    var ajaxGetContent = function(){
      $.ajax({
        type: "GET",
        url:   url.rss,
        cache: true,
        dataType: "xml",
        timeout : 30000,
        error   : function() {
          return true;
        },
        success : function(data) {
          displayContent(data);
          return true;
        }
      });
    }

    var displayContent = function(data){
      var node = new Array();
      var html = "";
      $("item",data).each(function(i){
        node[i] = {};
        node[i].title = $("item",data).eq(i).children("title").text();
        node[i].link  = $("item",data).eq(i).children("link").text();
        node[i].desc  = $("item",data).eq(i).children("description").text();
        node[i].cat   = $("item",data).eq(i).children("category").text();
        node[i].date  = $("item",data).eq(i).children("date").text();
        node[i].creator = $("item",data).eq(i).children("creator").text();
        node[i].img   = new Image();
        node[i].img.src = node[i].desc;
        var cls = (parseFloat(time.res) == parseFloat(node[i].date))? ' update': 'no';
        html += '    <div class="item">';
        html += '      <ul>';
        html += '        <li class="meta"><span class="date color-txt'+cls+'">'+ node[i].date+'</span>'+ node[i].creator +'</li>';
        html += '        <li><strong><a href="'+ node[i].link +'" target="_blank">'+ node[i].title +'</a></strong></li>';
        html += '      </ul>';
        html += '      <p class="thumb"><a href="'+ node[i].link +'" target="_blank"><img src="'+ node[i].img.src +'" alt="'+ node[i].title +'" style="max-height: 60px;height: auto !important;height: 60px;" /></a></p>';
        html += '      <br class="clear" />';
        html += '    </div>';
      });
      obj.html(html);

      obj.find(".thumb").find("img").parent("a").css({"background":"#FFF"});
      obj.find(".thumb").find("img").hover(
        function(){
          $(this).stop().animate({opacity: 0.6}, 0);
        },
        function(){
          $(this).stop().animate({opacity: 1.0}, 0);
        });
    }

    getDate();

  };
})(jQuery);

