
//======================== 가로 스크롤 인식, 세로 스크롤 픽스 시작 ==================================

var timer_K ;

$(function(){
    $(window).resize(function(){
        var width = parseInt($(this).width());
		var height = parseInt($(this).height());
		//$('#header').css('width',$(document).width());
    }).resize(); // 로딩되면서 창크기에 맞추기 위해 한 번 부름
	$(".gohome").click(function(){start_left()}); 
	$(".goHome").click(function(){start_left()});
});
var k = 0;
var scroll_relative = 0;
function handle(delta) {
	scroll_relative = $(document).width();
	//alert(scroll_relative);
	var scroll_tick = scroll_relative * 0.02;
    var width = parseInt($(this).width());
		if (delta < 0){
			$(document).scrollLeft($(document).scrollLeft()+scroll_tick);
			/* 스크롤의 값의 변화가 있을경우만 액션하도록.. */;
			//if (scroll_relative >= k){k = k + 60;}
			document.body.scrollTop = 0;
		}else{
			$(document).scrollLeft($(document).scrollLeft()-scroll_tick);
			document.body.scrollTop = 0;
		}
}

function wheel(e) {
	clearInterval(timer_K);
    var nDelta = 0;
    if (!e) { // For IE, access the global (window) event object
        e = window.event;
    }
    // cross-bowser handling of eventdata to boil-down delta (+1 or -1)
    if ( e.wheelDelta ) { // IE and Opera
        nDelta= e.wheelDelta;
        if ( window.opera ) {  // Opera has the values reversed
            nDelta= -nDelta;
			handle(nDelta);
        }
    }
    else if (e.detail) { // Mozilla FireFox
        nDelta= -e.detail;
    }
    if (nDelta > 0) {
        handle(1);
    }
    if (nDelta < 0) {
        handle(-1);
    }
    if ( e.preventDefault ) {  // Mozilla FireFox
        e.preventDefault();
    }
    e.returnValue = false;  // cancel default action
} 



/* Initialization code. */
if (window.addEventListener)
	window.addEventListener('DOMMouseScroll', wheel, false);
	window.onmousewheel = document.onmousewheel = wheel;

function focusCheck( name )  
{  
    // 현재 포커스 되어있는 개체 추출  
    var obj = document.activeElement;   
    if( name == obj.name )  
        return true;  
    else  
        return false;  
}

function start_left(){
	move_action(0);
}

function move_scroll(obj){
	var o_w = $("#"+obj).offset();
	var z = o_w.left;
	move_action(z);
}

//가야할 위치
function move_action(val){
	timer_K = setInterval("timer_scroll("+val+")",1);
}

//시작위치와 이동할 위치를 계산하여 좌,우를 선택적으로 이동할수 있도록 조건을 주고
//30%씩 이동하여 0으로 떨어질수 있도록 조건을 주어야함
//가속도 처리 처음은 빠르고 나중엔 천천히
function timer_scroll(endposition){
	//현재위치 k
	var k = $(document).scrollLeft();
	
	//한 틱에 가야할 위치 계산
	var t = parseInt((k - endposition)/5);
	var z = k - t;
	//alert(Z);
	//alert("현재위치:"+k+"이동할 위치:"+z);
	//alert(T);
	v = $(document).width()-$(window).width();
	if(z > v){z = v}
	if(k==z){
		clearInterval(timer_K);
	}else{
		$(document).scrollLeft(z);
	}
}


/* ======================== footer 이미지 랜덤 ======================================================="
/* 
//$(function(){
//	var result = Math.floor(Math.random() * 10) + 1;
//	$("#footer").html("image"+result);
//});
*/


/*function widthSize(){
  document.getElementById("header").style.width = parseInt(document.documentElement.scrollWidth)+"px";
  document.getElementById("footer").style.width = parseInt(document.documentElement.scrollWidth)+"px";
  document.getElementById("content_area").style.width = parseInt(document.documentElement.scrollWidth)+"px";
 }
*/
 /*div width 값과 footer 랜덤 을같이 불러옴 */
//window.onload=function (){
//widthSize();
/*footerImg();
}

/*footer 배경 랜덤 */
/*function footerImg(){
 var bgImg = ['../images/common/footer.gif','../images/common/header.gif','../images/common/icon_love.gif'];
 var footran = Math.floor(bgImg.length*Math.random());
 var footer1 = document.getElementById("footer");
 footer1.style.backgroundImage = 'url('+bgImg[footran]+')';
 footer1.style.backgroundRepeat = 'no-repeat';
}
*/

//======================================== 이동 레이어 시작=====================================================
$(document).ready(function(){   
  var $doc           = $(document);   
  var position       = 0;   
  var left = $doc.scrollLeft(); //현재 스크롤바 위치   
  
  /*사용자 설정 값 시작*/  
  var pageHeight    = 1000; // 페이지 폭, 단위:px   
  var speed          = "slow";     // 따라다닐 속도 : "slow", "normal", or "fast" or numeric(단위:msec)   
  var easing         = 'linear'; // 따라다니는 방법 기본 두가지 linear, swing   
  var $layer         = $('#toplocationLayer'); // 레이어 셀렉팅   
  var layerLeftOffset = 950;   // 레이어 높이 상한선, 단위:px   
  $layer.css('z-index', 10);   // 레이어 z-인덱스   
  /*사용자 설정 값 끝*/  
  
  $layer.css('top', "50px");   

  
  // 스크롤 바를 내린 상태에서 리프레시 했을 경우를 위해   
  if (top > 0 )   
    $doc.scrollLeft(layerLeftOffset+left);   
  else  
    $doc.scrollLeft(0);   
  
  // 최초 레이어가 있을 자리 세팅   
  $layer.css('left',layerLeftOffset);   
  
 
  //스크롤이벤트가 발생하면   
  $(window).scroll(function(){   
	    xPosition = $doc.scrollLeft()+layerLeftOffset;
	    xPosition = xPosition - 640;
	    if(xPosition < 950){xPosition = 950}
	    	//alert(xPosition)
	    	$layer.animate({"left":xPosition }, {duration:speed, easing:easing, queue:false});
	    	$('#shoppFooter').css("width",$(document).width());
	    	//$('#header').css("width",$(document).width());
  });
});

function chkpoint(obj){
	   //원료이야기
		  var imgPosition = $("#"+obj).offset();
		  alert(imgPosition.left);
		  position_left(imgPosition.left)
}

function position_left(pval){
	aaa = setInterval("position_scroll("+pval+")", 1);
}

function position_scroll(pval){
	var k = $(document).scrollLeft();
	$(document).scrollLeft(k+100);
	var m = $(document).width();
	var l = k + $(window).width();
	if (k >= pval||l == m){
		clearInterval(aaa);
	}
}

