(function($) {	
	var t = 800;
	var offsetX = -10;
	var view = $('#view');
	var noFade = $('html.ie6,html.ie7,html.ie8').size()==1;
	
	function keepPadding(container) {
		var image = $('.image', container);
		var t2 = 3000;
		image.animate({'left':container.width()-image.width()}, t2, 
				function() {
					image.animate({'left':0}, t2, function(){keepPadding(container);});
				}
			);
	}
	//$('#view_09').addClass('special');
	//$('#view_06').addClass('special');
	//$('#view_06 .overlay').addClass('special');
	//keepPadding($('#view_09'));
	
	$('#view_09').attr('class', 'type3');
	
	$('#view li#view_06 a')
		.mouseenter(function(){
			var container = $(this);
			var image = $('.image', this);
			var overlay = $('.overlay', this);
			if (noFade) {
				$('.overlay', this)
					.css('top', 10)
					.show()
					.animate({'top':0}, {queue: false, duration: t});
			}else {
				$('.overlay', this)
					.css('top', 10)
					.animate({'top':0}, {queue: false, duration: t})
					.fadeIn(t);
			}
		})
		.mouseleave(function(){
			if (noFade) {
				$('.overlay', this)
					.animate({'top':10}, {queue: false, duration: t}).hide();
			}else {
				$('.overlay', this)
					.animate({'top':10}, {queue: false, duration: t})
					.fadeOut(t);
			}
		})
		
	$('#view li:not(#view_06) a')
		.each(function(index){
		    var container = $(this);
			$('.image', this).css('top', 0);
			var type = container.parent().attr("class");
			switch(type) {
			    case "type3":
                    keepPadding(container);
			        break;
			}
		})
		.mouseenter(function(){
			var container = $(this);
			var image = $('.image', this);
			var overlay = $('.overlay:not(.special)', this);
			
			overlay.css('left', offsetX);
			if (noFade) {
				overlay.show();
			}else {
			    overlay.fadeIn(t);
			}
			
			overlay.animate({'left':0}, {queue: false, duration: t})
			
			//image move type
			var type = container.parent().attr("class");
			switch(type) {
			    case "type1":
			        $('.image', this).animate({'top':container.height()-image.height()}, t);
			        break;
			    case "type2":
			        $('.image', this).animate({'left':container.width()-image.width()}, t);
			        break;
			}
		})
		.mouseleave(function(){
		    var overlay = $('.overlay:not(.special)', this);
		    overlay.animate({'left':offsetX}, {queue: false, duration: t});
			if (noFade) {
				overlay.hide();
			}else {
				overlay.fadeOut(t);
			}
			if (!$(this).parent().hasClass('special')) {
				$('.image', this).animate({'top':0, 'left':0}, t);
			}
		})
	
	var view_width = view.width();
	var cx;
	var minX;
	var maxX;
	
	function stageResize() {
		cx = $(window).width()/2;
		minX = cx-view_width;
		maxX = -cx;
	}
	stageResize();
	$(window).resize(function() {
		stageResize();
	});
	
	
	$('#main')
	    .mousemove(function(event){
	        var mx = event.pageX;
	        var dx = Math.abs(mx-cx);        
	        if (dx>400) {
	            var curX = view.position().left;
	            if (mx<cx) {
	                view.stop().animate({'left':maxX}, (maxX-curX)*800/(dx-350), 'linear');
	            }else {
	                view.stop().animate({'left':minX}, (curX-minX)*800/(dx-350), 'linear');
	            }
	        }else {
	            view.stop();
	        }
	    });
	//*/
	/*$('#btn-scroll-left')
	    .mouseenter(    
		    function() {
		        var curX = view.position().left;
		        view.stop().animate({'left':maxX}, (maxX-curX)*5, 'linear');
		    })
		.mouseleave(
		    function() {
		        var curX = view.position().left;
		        view.stop();
		    })
	$('#btn-scroll-right')
	    .mouseenter(    
		    function() {
		        var curX = view.position().left;
		        view.stop().animate({'left':minX}, (curX-minX)*5, 'linear');
		    })
		.mouseleave(
		    function() {
		        view.stop();
		    })
	*/
	
	var swipeBaseX = 0;
	 
	function swipeStatus(event, phase, direction, distance) {
		if( phase=="move" && (direction=="left" || direction=="right" || direction=="up" || direction=="down")) {
			var duration=0;
			if (direction == "left") {
			    view.css("-webkit-transition-duration", (duration/1000).toFixed(1) + "s");
		        view.css("-moz-transition-duration", (duration/1000).toFixed(1) + "s");
		        //var value = (distance<0 ? "" : "-") + Math.abs(distance).toString();
		        var value = swipeBaseX - distance;

		        view.css("-webkit-transform", "translate3d("+value +"px,0px,0px)");
		        view.css("-moz-transform", "translate("+value+"px, 0px)");	
			} else if (direction == "right") {
				view.css("-webkit-transition-duration", (duration/1000).toFixed(1) + "s");
		        view.css("-moz-transition-duration", (duration/1000).toFixed(1) + "s");
		        //var value = (distance<0 ? "" : "-") + Math.abs(distance).toString();
		        var value = swipeBaseX + distance;
		        
		        view.css("-webkit-transform", "translate3d("+value +"px,0px,0px)");
		        view.css("-moz-transform", "translate("+value+"px, 0px)");
			}
			
		} else if ( phase == "cancel") {
			
		} else if ( phase =="end" )	{
		    var duration=500;
		    if (direction == "left") {
			    view.css("-webkit-transition-duration", (duration/1000).toFixed(1) + "s");
		        view.css("-moz-transition-duration", (duration/1000).toFixed(1) + "s");
		        //var value = (distance<0 ? "" : "-") + Math.abs(distance).toString();
		        var value = swipeBaseX - distance - 80;
		        if (value<-755) {
		            value = -755;
		        }
		        view.css("-webkit-transform", "translate3d("+value +"px,0px,0px)");
		        view.css("-moz-transform", "translate("+value+"px, 0px)");	
		        swipeBaseX = value;
			} else if (direction == "right") {
				view.css("-webkit-transition-duration", (duration/1000).toFixed(1) + "s");
		        view.css("-moz-transition-duration", (duration/1000).toFixed(1) + "s");
		        //var value = (distance<0 ? "" : "-") + Math.abs(distance).toString();
		        var value = swipeBaseX + distance + 80;
		        if (value>710) {
		           value = 710;
		        }
		        view.css("-webkit-transform", "translate3d("+value +"px,0px,0px)");
		        view.css("-moz-transform", "translate("+value+"px, 0px)");
		        swipeBaseX = value;
			}
		}
	}
	
	var swipeOptions=	{
							triggerOnTouchEnd : true,
							swipeStatus : swipeStatus,
							threshold:200
						}
	if ($('#container').swipe) {
        $('#container').swipe( swipeOptions );
    }
	
	
	$('#main-menu > ul > li')
		    .mouseenter(
		        function() {
		            $("ul", this).fadeIn();
		        })
		    .mouseleave(
		        function() {
		            $("ul", this).fadeOut();
		        })
	
	//html 5 video
	if (noFade||navigator.userAgent.indexOf("Firefox")!=-1) {
		$('.swf-player').css('display', 'block');
	}else {
		$('.html5-player')
			.css('display', 'block')
			.attr('muted', 'true')
			.mouseenter(
		        function(e) {
		            $(this).removeAttr('muted');
		        })
		    .mouseleave(
		        function(e) {
		            $(this).attr('muted', 'true');
		        })
		    .bind("touchstart", 
                function(e){
                    $(this).get(0).play();
                });
		$('.html5-player')
			.each(
				function(index) {
					var block = $(this).parent().parent();
					$(this).width(block.width());
					$(this).height(block.height());
				});
		$('.html5-player').trigger("touchstart");
	}
})(jQuery);
