jQuery(function ($){

/*
スクロールトップ
------------------------------------------------------------------------------------------------
*/

var sc = new BoundScroller();
$("#scrollTop a").click(function (){
sc.startScroll();
})

function BoundScroller () {
var timer = null, stage = $(window), mTop = 0, reverseBody = $("body"), mOriginalTop = 0;
this.currentTop = 0;
mTop = parseInt(reverseBody.css("margin-top").replace("px",""))+30;
mOriginalTop = parseInt(reverseBody.css("margin-top").replace("px",""));
this.startScroll = function (){
currentTop = $(window).scrollTop();
var reverseScroll = false;
timer=setInterval(function (){
if(!reverseScroll) {
if(currentTop<parseInt(mTop+1)) { reverseScroll = true; currentTop = 0; stage.scrollTop(0); }
else {currentTop += ((-mTop)-currentTop)*.3;stage.scrollTop(currentTop);}
}else{
currentTop += ((mTop)-currentTop)*.5;
reverseBody.css("margin-top",currentTop);
if(Math.abs(currentTop-mTop)<=1) {
clearInterval(timer);
var reverseTimer = setInterval(function (){
currentTop += (mOriginalTop-currentTop)*.35;
reverseBody.css("margin-top",currentTop);
if(Math.abs(mOriginalTop-currentTop) <= 1) {
clearInterval(reverseTimer);
reverseBody.css("margin-top",mOriginalTop);
}
}, 1000/30);
}
}

},1000/30)
}
}


/*
topnewsの画像を出す
------------------------------------------------------------------------------------------------
*/

var topnews = $('section#topnews');
if(topnews.get(0)) {
topnews.find("li").each(function (){
 var urls = $(this).find("a").attr("href");
 urls = urls.replace("http://akibahideki.com","");
 urls = urls.replace("http://www.akibahideki.com","");
  var targetElm = $(this); 
  var targetURL = urls;
  $.ajax({
    url: urls,
    cache: true,
    success: function (data){
    var imgs = $(data).find("article#entry section img").get(0);
    var tsrc =$(imgs).attr("src");
    targetElm.prepend("<img class='thumb' style='opacity: 0;' src='"+tsrc+"' alt=''>");
    var texts = $(data).find("article#entry section").text();
    texts = texts.substring(0,100) + "  ...";
    targetElm.find("a:not(.thumb)").append("<span>"+texts+"</span>");

    /* canvas */
    var getimg = $(targetElm).find("img.thumb");
    var canvas = targetElm.find("a").prepend("<canvas width='86' height='53'></canvas>");
 
    getimg.get(0).onload = function (){
    var imgsize = {w:getimg.width(), h:getimg.height()};
    var ctx = targetElm.find("canvas").get(0).getContext("2d");
    var ws = 86/imgsize.w;
    var hs = 53/imgsize.h;
    ctx.setTransform(hs, 0, 0, hs, Math.floor((86-imgsize.w*hs)/2), 0);
    ctx.drawImage(getimg.get(0), 0, 0);

    getimg.remove();

var aTag = targetElm.find("canvas");
    targetElm.hover(function (){
      aTag.addClass("hover");
    },
    function (){
      aTag.removeClass("hover");
    }); //targetElm.hover

    }//getimg.get(0).onload
    
   } //success: function (data)
 }); //$.ajax
}) //topnews.find("li").each(function ()
} //if(topnews.get(0)) 

});


/*
スマホ用、画像などの大きさハミ出し
------------------------------------------------------------------------------------------------
*/

if (navigator.userAgent.indexOf('iPhone') > 0 || navigator.userAgent.indexOf('Android') > 0 || navigator.userAgent.indexOf('iPod') > 0) {

$(window).bind("resize load", function (){
var stageWidth = 0;
if($(window).height()>$(window).width()){
//縦向きの場合の命令
stageWidth = $(window).width();
}else{
//横向きの場合の命令
stageWidth = $(window).width();
}
var ww = [];
$("footer.asset-footer iframe").attr("width","100%");
$("#entryContext img,#entryContext object,#entryContext iframe,#entryContext a").each(function (){
ww.push($(this).width());
  if($(this).width() > stageWidth) {
    var scale = (stageWidth-20)/$(this).width();
    /*$(this).css("-webkit-transform","scale("+scale+")");
    $(this).css("-moz-transform","scale("+scale+")");
    $(this).css("-ms-transform","scale("+scale+")");
    $(this).css("-o-transform","scale("+scale+")");*/
  $(this).css({"width":$(this).width()*scale, "height":$(this).height()*scale})
  }
})

//$("#entry").append(ww + ", ");

})
 
}



