﻿/*
画像リストのスクロール処理
*/
$(function(){init_event();});

var scroll_box;
var scroll_obj;

var frame_width;
var frame_offset;
var content_width;

var timer_left;
var timer_right;
var timer_auto;

var scroll_step;
var is_moving_left = false;
var is_moving_right = false;

function init_event(){
 scroll_box = document.getElementById("scrollFrame");
 frame_width = parseInt(scroll_box.style.width);
 frame_offset = get_offset(scroll_box);
 scroll_obj = document.getElementById("scrollImg");
 content_width = parseInt(scroll_obj.style.width);

 scroll_box.onmousemove = function(evt){
 start_scroll(evt);
 }
 scroll_box.onmouseout = function(evt){
 stop_scroll(evt);
 }
 timer_auto = setInterval("auto_scroll()",15); // 自動スクロール開始
}
// スクロール開始
function start_scroll(evt){
 if(timer_auto){
 clearInterval(timer_auto);
 }
 var page_offset = (window.pageXOffset) ? pageXOffset: get_body_element().scrollLeft;
 var mouse_x = window.event ? event.clientX : (evt.clientX ? evt.clientX : 0);
 var mouse_pos = mouse_x - frame_offset - page_offset;
 var hit_area_left = (frame_width - 10) / 2; // 10は非反応領域幅
 var hit_area_right = (frame_width + 10) / 2; // 10は非反応領域幅
 if(mouse_pos > hit_area_right){
 scroll_step = (mouse_pos - hit_area_right) / hit_area_right * 5; // 5は最大増幅値
 if(timer_right){
 clearTimeout(timer_right);
 }
 if(!is_moving_left){
 scroll_left();
 }
 } else if(mouse_pos < hit_area_left){
 scroll_step = (hit_area_left - mouse_pos) / hit_area_left * 5; // 5は最大増幅値
 if(timer_left){
 clearTimeout(timer_left);
 }
 if(!is_moving_right){
 scroll_right();
 }
 }
 else {
 scrollspeed = 0;
 }
}
// スクロール停止
function stop_scroll(evt){
 if(timer_left){
 clearTimeout(timer_left);
 }
 if(timer_right){
 clearTimeout(timer_right);
 }
 is_moving_right = false;
 is_moving_left = false;
}
// 自動スクロール
function auto_scroll(){
 if(parseInt(scroll_obj.style.left) > (frame_width - content_width)){
 scroll_obj.style.left = parseInt(scroll_obj.style.left) - 1 + "px";
 is_moving_right = true;
 } else {
 clearInterval(timer_auto);
 is_moving_right = false;
 }
}
// 右へスクロール
function scroll_right(){
 is_moving_right = true;
 if(parseInt(scroll_obj.style.left) < 0){
 scroll_obj.style.left = parseInt(scroll_obj.style.left) + scroll_step + "px";
 }
 timer_right = setTimeout("scroll_right()",10);
}
// 左へスクロール
function scroll_left(){
 is_moving_left = true;
 if(parseInt(scroll_obj.style.left) > (frame_width - content_width)){
 scroll_obj.style.left = parseInt(scroll_obj.style.left) - scroll_step + "px";
 }
 timer_left = setTimeout("scroll_left()",10);
}

function get_offset(obj){
 var offset = obj.offsetLeft;
 var parent_obj = obj.offsetParent;
 while (parent_obj != null){
 offset += parent_obj.offsetLeft;
 parent_obj = parent_obj.offsetParent;
 }
 return offset;
}
function get_body_element(){
 return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
}

