var nowScrollY = 0;
var nowPosition = 1;
var autoscroll = false;

$(document).ready(function() { //when the document is ready...

	var $window = $(window);
	var $yoon_1	= $('#yoon_1');
	var $yoon_2	= $('#yoon_2');
	var $yoon_3	= $('#yoon_3');
	var $yoon_4	= $('#yoon_4');
	var $yoon_5	= $('#yoon_5');
	var $yoon_6	= $('#yoon_6');

	var windowHeight = $window.height(); //get the height of the window
	 
	var p1 = 2;
	var p2 = 0.8;
	var p3 = 0.5;
	var p4 = 0.4;
	var p5 = 0.27;

	//chgMenu(nowPosition);

	function chgMenu(nowM) {
		for(var i = 1 ; i <= 6 ; i++) {
			var t	= document.getElementById("menu_"+i);
			if(nowM == i) {
				eval("menu_"+i+"_src = menu_"+i+"_src_on_sel");
				eval("menu_"+i+"_src_out = menu_"+i+"_src_sel");
				t.src = 'img/bt_m'+i+'_sel.png';
			}
			else {
				eval("menu_"+i+"_src = menu_"+i+"_src_on");
				eval("menu_"+i+"_src_out = menu_"+i+"_src_off");
				t.src = 'img/bt_m'+i+'_off.png';
			}
		}
	}

	/*arguments: 
		x = horizontal position of background
		windowHeight = height of the viewport
		pos = position of the scrollbar
		adjuster = adjust the position of the background
		inertia = how fast the background moves in relation to scrolling
	*/
	function newPos(x, windowHeight, pos, adjuster, inertia){
		return x + "% " + (-((windowHeight + pos) - adjuster) * inertia)  + "px";
	}

	function pageScroll() {
		nowScrollY	= $window.scrollTop();
		var start = nowPosition-1;
		var end = nowPosition+1;
		if (start < 1) start = 1;
		if (end > 6) end = 6;

		if(yooneunhye == true)
		{
			i = start;
			calc1	= -(nowScrollY * p1);
			calc2_1	= -(nowScrollY * p2);
			calc2	= -(nowScrollY * p3);
			calc3_1	= nowScrollY-(nowScrollY * p4);

			do
			{
				switch(i) {
					case 1 :
						$("#m1_2").css("top",calc2);
						$("#yoon_1").css({backgroundPosition : newPos(85, windowHeight, nowScrollY, 1200, p5)});
						break;
					case 2 :
						$("#m2_1").css("top",calc1 + 2000);
						$("#m2_2_1").css("top",calc2_1 + 550);
						$("#yoon_2 .contents").css({backgroundPosition : newPos(7, windowHeight, nowScrollY, 2600, p4)});
						$("#yoon_2").css({backgroundPosition : newPos(50, windowHeight, nowScrollY, 1100, p5)});
						break;
					case 3 :
						$("#yoon_3").css({backgroundPosition : newPos(-5, windowHeight, nowScrollY, 2400, p5)});
						break;
					case 4 :
						$("#m4_2").css("top",calc2 + 1600);
						$("#m4_3_1").css("top",calc3_1 - 1500);
						$("#yoon_4").css({backgroundPosition : newPos(100, windowHeight, nowScrollY, 3300, p5)});
						break;
					case 5 :
						$("#yoon_5").css({backgroundPosition : newPos(13, windowHeight, nowScrollY, 4290, p5)});
						break;
					case 6 :
						$("#yoon_6 .contents").css("top",calc3_1);
						$("#yoon_6").css({backgroundPosition : newPos(50, windowHeight, nowScrollY, 4000, p5)});
						break;
				} // end switch
				i++;
			}
			while (i<=end);
		}
	}

	$window.resize(function(){ //if the user resizes the window...
		pageScroll(); //move the background images in relation to the movement of the scrollbar
		//RepositionNav(); //reposition the navigation list so it remains vertically central
	});		

	$window.bind('scroll', function(){ //when the user is scrolling...
		nowScrollY	= $window.scrollTop();
		nowPosition	= Math.floor((nowScrollY/840) + 1.5);
		// 메뉴색 바꾸기
		if(yooneunhye == true) {
			if(autoscroll == false) pageScroll();
			chgMenu(nowPosition);
		}
	});
	
});
