$(function() {
	var $windowW = $('html').width();

	//useragent
	var _ua = (function() {
		return {
			ltIE6 : typeof window.addEventListener == "undefined" && typeof document.documentElement.style.maxHeight == "undefined",
			ltIE7 : typeof window.addEventListener == "undefined" && typeof document.querySelectorAll == "undefined",
			ltIE8 : typeof window.addEventListener == "undefined" && typeof document.getElementsByClassName == "undefined",
			ie : document.uniqueID,
			firefox : window.globalStorage,
			opera : window.opera,
			webkit : !document.uniqueID && !window.opera && !window.globalStorage && window.localStorage,
			mobile : /android|iphone|ipad|ipod/i.test(navigator.userAgent.toLowerCase())
		}
	})();
	

	var slideS = 300;
	var $whats = $('#whats');
	var $wH2 = $whats.find('h2');
	var $wContent = $('#whatsContents');
	var whatsDH = $whats.css('top');

	$wContent.find('li:last-child').addClass('lastchild');

	$wH2.click(function() {
		whatsToggle();
	});
	var $pick = $('#pickup');
	var $pH2 = $pick.find('h2');
	var $pContent = $('#pickupContents');
	var $pContentH = $pContent.height();
	$pH2.click(function() {
		pickToggle();
	});
	initID = setTimeout(function() {
		whatsToggle();
		pickToggle();
	}, 1000);
	function whatsToggle() {
		var h;
		if($whats.css('top') == whatsDH) {
			if(_ua.ltIE7) {
				h = 573;
			} else {
				h = 560;
			}
		} else {
			h = whatsDH;
		}
		$whats.animate({
			top : h
		}, slideS);
		$wContent.slideToggle(slideS);
	}

	function pickToggle() {
		$pContent.slideToggle(slideS);
		$('html,body').animate({
			scrollTop : $pContentH
		}, 'normal', 'linear');
	}

	/* pickup */
	var $pickupList = $('#pickupContents ul');
	var $pickupNum = $pickupList.find('li').size();
	var $listW = $pickupList.find('li').width() + 16;
	var $pickupListW = $listW * $pickupNum;

	var FPS = 30;
	var FPSt = 1000 / FPS;

	while($pickupList.width() < $windowW * $pickupNum) {
		$pickupList.find('li').each(function(i) {
			if(i < $pickupNum) {
				$pickupList.append($(this).clone());
			}
		});
		$pickupList.width($listW * $pickupList.find('li').size() * 2);
	}

	var pickupID = setInterval(pickMove, FPSt);

	$pickupList.hover(function() {
		clearInterval(pickupID);
	}, function() {
		pickupID = setInterval(pickMove, FPSt);
	});
	function pickMove() {
		$pickupList.animate({
			left : '-=2px'
		}, FPSt - 10, 'linear', function() {
			if(parseInt($pickupList.css('left')) < -$pickupListW) {
				$pickupList.css('left', '0px');
			}
		})
	}

	var bottleArray = ['#nevada', '#negro', '#rose'];

	for(var bottle = 0; bottle < bottleArray.length; bottle++) {
		bottleRotate($(bottleArray[bottle]), Math.floor(Math.random() * 500 + 1500), Math.floor(Math.random() * 5 + 2));
	}

	function bottleRotate($id, time, r) {
		$id.rotate({
			animateTo : r,
			duration : time,
			easing : $.easing.easeInOutSine,
			callback : function() {
				bottleRotateR($id, time, -r)
			}
		});
	}

	function bottleRotateR($id, time, r) {
		$id.rotate({
			animateTo : r,
			duration : time,
			easing : $.easing.easeInOutSine,
			callback : function() {
				bottleRotate($id, time, -r)
			}
		});
	}

	/* banner */
	var $banner = $('#banner');
	var $bannerNum = $banner.find('li').size();
	var moveTime = 5000;
	var currentNum = 0;
	if(_ua.ltIE7) {
		var $fadeID = setInterval(fadeInterval, moveTime);
		$banner.find('li').each(function(i) {
			var $li = $(this);
			var $img = $li.find('img');
			var $href = $li.find('a');

			$img.css({
				'filter' : 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + $img.attr('src') + '", sizingMethod="scale");'
			});
			if(i != 0) {
				$href.css('cursor', 'default');
				$li.hide();
			} else {
				$href.addClass('current');
			}

			$href.click(function() {
				if(!$(this).hasClass('current')) {
					return false;
				}
			});
			$href.hover(function() {
				if($(this).hasClass('current')) {
					$href.append('<img src="/images/main_on.png" alt="" class="on" />');
					clearInterval($bannerID);
				}
			}, function() {
				if($(this).hasClass('current')) {
					$('.on').remove();
					$bannerID = setInterval(bannerMove, moveTime);
				}
			});
		});
		function fadeInterval() {
			currentNum++;
			if(currentNum == $bannerNum) {
				currentNum = 0;
			}
			$banner.find('li').each(function(i) {
				var $li = $(this);
				var $href = $li.find('a');
				if(i != currentNum) {
					if($href.hasClass('current')) {
						$href.removeClass('current');
					}
					$li.fadeOut(500);
				} else {
					$li.fadeIn(1000);
				}
			});
		}

	} else {
		var posX = [0, 675, 675, 555, 555];
		var posY = [0, 330, 500, -200, -100];
		var posW = [784, 260, 10, 10, 260];
		var rot = [0, 10, 20, -30, -15];
		var posObj = [];
		var $bannerID;

		for(var i = 0; i < posX.length; i++) {
			posObj.push({
				x : posX[i],
				y : posY[i],
				r : rot[i],
				w : posW[i]
			})
		}
		$banner.find('li').each(function(i) {
			var $li = $(this);
			var $img = $li.find('img');
			var $href = $li.find('a');
			$li.css('z-index', 1000 - i);
			if(i != 0) {
				$href.css('cursor', 'default');
				$li.hide();
				if(i <= 2) {
					$img.width(posObj[i].w);
					$li.show().css({
						'top' : posObj[i].y,
						'left' : posObj[i].x
					}).rotate(posObj[i].r);
				} else if(i >= $bannerNum - 2) {
					$img.width(posObj[posObj.length - ($bannerNum - i)].w);
					$li.show().css({
						'top' : posObj[posObj.length - ($bannerNum - i)].y,
						'left' : posObj[posObj.length - ($bannerNum - i)].x
					}).rotate(posObj[posObj.length - ($bannerNum - i)].r);
				} else {
					$li.hide();
				}
			} else {
				$href.addClass('current');
			}

			$href.click(function() {
				if(!$(this).hasClass('current')) {
					return false;
				}
			});
			$href.hover(function() {
				if($(this).hasClass('current')) {
					$href.append('<img src="/images/main_on.png" alt="" class="on" />');
					clearInterval($bannerID);
				}
			}, function() {
				if($(this).hasClass('current')) {
					$('.on').remove();
					$bannerID = setInterval(bannerMove, moveTime);
				}
			});
		});
		function bannerMove() {
			currentNum++;
			if(currentNum == $bannerNum) {
				currentNum = 0;
			}
			$banner.find('li').each(function(i) {
				var $li = $(this);
				var $img = $li.find('img');
				var $href = $li.find('a');
				$li.css('z-index', 1000 - Math.abs(currentNum - i));
				if(i != currentNum) {
					var liNum = i - currentNum;
					switch (liNum) {
						case $bannerNum-1:
							liNum = 4;
							break;
						case $bannerNum-2:
							liNum = 3;
							break;
						case -$bannerNum+1:
							liNum = 1;
							break;
						case -$bannerNum+2:
							liNum = 2;
							break;
						case -2:
							liNum = 3;
							break;
						case -1:
							liNum = 4;
							break;
						case 1:
							liNum = 1;
							break;
						case 2:
							liNum = 2;
							break;
						default:
							liNum = 100;
							break;
					}
					if(liNum != 100) {
						if(liNum != 2) {
							$li.show();
						} else {
							$li.hide();
						}
						$href.removeClass('current').css('cursor', 'default');
						$li.rotate({
							animateTo : posObj[liNum].r,
							easing : $.easing.easeOutBack
						});
						$li.animate({
							'top' : posObj[liNum].y + 30,
							'left' : posObj[liNum].x
						}, 100, "easeInQuart").animate({
							'top' : posObj[liNum].y
						}, 500, "easeOutBack");
						$img.animate({
							'width' : posObj[liNum].w
						}, 1000, "easeOutQuart");
					} else {
						$li.hide();
					}
				} else {
					$href.addClass('current').css('cursor', 'pointer');
					$li.rotate({
						animateTo : posObj[0].r,
						easing : $.easing.easeOutBack
					});
					$li.animate({
						'top' : posObj[0].y + 150,
						'left' : posObj[0].x
					}, 500, "easeInQuart").animate({
						'top' : posObj[0].y
					}, 500, "easeOutBack");
					$img.animate({
						'width' : posObj[0].w
					}, 1000, "easeOutQuart");
				}
			});
		}
		$bannerID = setInterval(bannerMove, moveTime)

	}

});

