var $window;
var $html;
var $body;
var device;
var os = ["", 0];

$(function($){
	$window = $(window);
	$html = $("html");
	$body = $("body");
	
	if(!device){
		var ua = navigator.userAgent;
		if(ua.search(/iPhone/) != -1 || ua.search(/iPad/) != -1 || ua.search(/iPod/) != -1){
			device = "iphone";
			os[0] = "ios";
			if(ua.search(/iPhone/) != -1){
				ua.match(/iPhone OS (\w+){1,3}/g);
				os[1] = (RegExp.$1.replace(/_/g, '')+'00').slice(0,3);
			}
			else if(ua.search(/iPad/) != -1){
				ua.match(/CPU OS (\w+){1,3}/g);
				os[1] = (RegExp.$1.replace(/_/g, '')+'00').slice(0,3);
			}
		}else if(ua.search(/Android/) != -1){
			device = "android";
			os[0] = "android";
		}else{
			device = "pc";
			os[0] = "other";
		}
		$body.addClass(device);
	}

	$.hashScroll();
	
	var $menu_holder = $("#menu_holder");
	if($menu_holder.length > 0){
		var $menu = $("#menu");
		var $contents = $("#contents");
		var default_menu_top = $menu.offset().top;
		var last_scroll_bottom = 0;
		var is_fixed = false;
		var menu_scroll_func = function(){
			var menu_height = $menu.height();
			if($contents.height() <= menu_height) return;
			
			var window_height = $window.height();
			var menu_top = $menu.offset().top;
			var menu_bottom = menu_top + menu_height;
			var default_menu_bottom = default_menu_top + menu_height;
			var scroll_top = $window.scrollTop();
			var scroll_bottom = scroll_top + window_height;
			var is_down = scroll_bottom > last_scroll_bottom;
			if(is_fixed){
				if((is_down && scroll_bottom < menu_bottom) || (!is_down && scroll_top > menu_top) || (menu_top < default_menu_top)){
					menu_top += 20;
					if(menu_top < default_menu_top) menu_top = default_menu_top;
					$menu.css("marginTop", menu_top - default_menu_top);
					$menu.removeClass("fixed_top");
					$menu.removeClass("fixed_bottom");
					is_fixed = false;
				}
			}
			else if(menu_height <= window_height){
				if(scroll_top > default_menu_top){
					$menu.css("marginTop", 0);
					$menu.removeClass("fixed_bottom");
					$menu.addClass("fixed_top");
					is_fixed = true;
				}
			}
			else if(is_down && scroll_bottom > menu_bottom){
				if(scroll_bottom < $body.height()){
					$menu.css("marginTop", 0);
					$menu.removeClass("fixed_top");
					$menu.addClass("fixed_bottom");
					is_fixed = true;
				}
			}
			else if(!is_down && scroll_top < menu_top && menu_top > default_menu_top){
				$menu.css("marginTop", 0);
				$menu.removeClass("fixed_bottom");
				$menu.addClass("fixed_top");
				if($menu.offset().top < default_menu_top){
					$menu.removeClass("fixed_top");
				}
				else is_fixed = true;
			}
			last_scroll_bottom = scroll_bottom;
		};
		$window.scroll(menu_scroll_func);
		$window.resize(menu_scroll_func);
		menu_scroll_func();
	}
});

$.easing.quart = function (x, t, b, c, d) {
	return -c * ((t=t/d-1)*t*t*t - 1) + b;
};
$.fn.extend({
	smoothScroll: function() {
		var targetOffset = this.offset().top;
		$body = $('html,body');
		$body.stop(true, false);
		var stop_func = function(){$body.stop(true, false);};
		$window.one("mousewheel", stop_func);
		if(os[0] == "ios") $window.one("touchstart", stop_func);
		$body.animate({ scrollTop: targetOffset }, 1200, 'quart', function(){$window.unbind("mousewheel", stop_func);$window.unbind("touchstart", stop_func);});
		return this;
	}
});
$.hashScroll = function(){
	$('a[href^=#]').live("click", function() {
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
			var $target = $(this.hash);
			$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
			if ($target.length) {
				$target.smoothScroll();
			}
		}
		return false;
	});
};

function getcookie(key){
	var tmp = document.cookie + ";";
	var index1 = tmp.indexOf(key, 0);
	if(index1 != -1){
	tmp = tmp.substring(index1, tmp.length);
	var index2 = tmp.indexOf("=", 0) + 1;
	var index3 = tmp.indexOf(";", index2);
	return(unescape(tmp.substring(index2,index3)));
	}
	return("");
}

function setcookie(key,val,mode,path){
	if(mode){exp=new Date();exp.setTime(exp.getTime()+1000*60*60*24*30);document.cookie = key + "=" + escape(val) + ";expires="+exp.toGMTString()+";domain=yakkun.com;path="+path+";";}
	else document.cookie = key + "=" + escape(val) + ";expires=Fri, 31-Dec-1999 23:59:59;path="+path+";";
}

/* Copyright (c) 2009 Brandon Aaron (http://brandonaaron.net)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 * Thanks to: http://adomas.org/javascript-mouse-wheel/ for some pointers.
 * Thanks to: Mathias Bank(http://www.mathias-bank.de) for a scope bug fix.
 *
 * Version: 3.0.2
 * 
 * Requires: 1.2.2+
 */
(function(c){var a=["DOMMouseScroll","mousewheel"];c.event.special.mousewheel={setup:function(){if(this.addEventListener){for(var d=a.length;d;){this.addEventListener(a[--d],b,false)}}else{this.onmousewheel=b}},teardown:function(){if(this.removeEventListener){for(var d=a.length;d;){this.removeEventListener(a[--d],b,false)}}else{this.onmousewheel=null}}};c.fn.extend({mousewheel:function(d){return d?this.bind("mousewheel",d):this.trigger("mousewheel")},unmousewheel:function(d){return this.unbind("mousewheel",d)}});function b(f){var d=[].slice.call(arguments,1),g=0,e=true;f=c.event.fix(f||window.event);f.type="mousewheel";if(f.wheelDelta){g=f.wheelDelta/120}if(f.detail){g=-f.detail/3}d.unshift(f,g);return c.event.handle.apply(this,d)}})(jQuery);