function mycarousel_initCallback(carousel) {
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() { carousel.startAuto(0); });
    carousel.buttonPrev.bind('click', function() { carousel.startAuto(0); });
    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() { carousel.stopAuto(); }, function() { carousel.startAuto(); });
};
$(document).ready(function (){ 

	$("img").pngfix();
	
	$('#mycarousel01').jcarousel({
		scroll: 1,
		auto: 0,
		visible: 5,
		wrap: 'circular',
		initCallback: mycarousel_initCallback,
		buttonNextHTML: null,
		buttonPrevHTML: null
	});
	
	$('#mycarousel02').jcarousel({
		scroll: 1,
		auto: 7,
		visible: 2,
		wrap: 'circular',
		initCallback: mycarousel_initCallback,
		buttonNextHTML: null,
		buttonPrevHTML: null
	});

	var $page 	= $('#contents'),
	$news				= $('#news'),
	$menu 			= $('#nav'),
	$menuNav 		= $('#nav ul'),
	$menuItems	= $menuNav.children('li'),
	totalItems	= $menuItems.length;

	var SetItem = (function(){
		var init = function(){
			loadPage();
			initWindowEvent();
		},
		loadPage = function() {
			$.when(slideOutMenu(1500)).done(function(){
			$.when(toggleMenuItems('up')).done(function(){
			$.when(topMenu('down',2000)).done(function(){
			$.when(actionDelay(2000)).done(function(){
				$("#marquee01").marquee(); 
				$("#marquee02").marquee(); 
			});
			});
			});
			});
		},
		slideOutMenu = function(time){
			return $.Deferred(
				function(dfd) {
					winWidth = $(window).width();
					$page.animate({
						width	: winWidth + 'px'
					},time, "easeInQuart", dfd.resolve);
				}
			).promise();
		},
		toggleMenuItems = function(dir) {
			return $.Deferred(
				function(dfd) {
					$menuItems.each(function(i) {
						var $el_title	= $(this).children('a:first'),
							marginTop, opacity, easing;
						if(dir === 'up'){
							marginTop	= '0px';
							opacity		= 1;
							easing		= 'easeOutBack';
						}
						else if(dir === 'down'){
							marginTop	= '80px';
							opacity		= 0;
							easing		= 'easeInBack';
						}
						$el_title.stop()
						.animate({
							marginTop	: marginTop,
							opacity		: opacity
						}, 200 + i * 200 , easing, function(){
							if(i === totalItems - 1)
								dfd.resolve();
						});
					});
				}
			).promise();
		},
		topMenu = function(time) {
			$news.stop()
			.animate({
				marginTop	: '0px',
				opacity		: 1
			}, time , 'linear' );
		},
		actionDelay = function(time) {
			return $.Deferred(
				function(dfd) {
				    setTimeout(function(){ dfd.resolve(); }, time);
				}
			).promise();
		},
		initWindowEvent = function() {
			$(window).bind('resize.Menu' , function(e) {
				slideOutMenu(500);
			});
		};
		
		return {
			init : init
		};
	})();
	SetItem.init();

	// jQuery Image Alpha
	var sel = 0;
	$('.hvr').mouseover(function() {
		if ( sel == 0 ){ $(this).fadeTo(100, 0.5); sel = 1; } 
		else { $(this).fadeTo(100, 1); sel = 0; }
	});
	$('.hvr').mouseout(function() {
		if ( sel == 0 ){ $(this).fadeTo(100, 0.5); sel = 1; } 
		else { $(this).fadeTo(100, 1); sel = 0; }
	});
});

