//ÄíÅ° ¼³Á¤ (´ÜÀ§ ÀÏ)
// 1  : 1ÀÏ 
// 0 : ºê¶ó¿ìÀú ´ÝÀ»¶§±îÁö Àû¿ë
// -1 : ÄíÅ° Àû¿ëÇÏÁö ¾ÊÀ½
var setday = 1; 

var columnReadyCounter = 0;
// ÀÚµ¿ ÅÇÀÌµ¿ ½ºÇÇµå°ª
var auto_speed = 4000;
// This is the callback function for the "hiding" animations
// it gets called for each animation (since we don't know which is slowest)
// the third time it's called, then it resets the column positions
function ifReadyThenReset() {
	
	columnReadyCounter++;
	
	if (columnReadyCounter == 3) {
		$(".col").not(".current .col").css("top", 350);
		columnReadyCounter = 0;
	}

};

var setpath = ''; //ex) /main/
var setdomain = ''; //ex) www.blueb.co.kr
var setsecure = false; //

// Ãß°¡ ÀÚµ¿ÇÔ¼ö
var tabindex;
var tabcnt;
var timer;
var animated1=false;
var animated2=false;
var animated3=false;
if(!$.cookie('tabcookie1'))
	tabindex = 0;
else
	tabindex = parseInt($.cookie('tabcookie1'));	

function auto_event(){
	if(animated1 == false && animated2 == false && animated3 == false){
		if(tabcnt == tabindex + 1)
			tabindex = 0;
		else
			tabindex = tabindex+1;
		$('.tabs a:eq('+tabindex+')').trigger('click');
		timer = setTimeout('auto_event()', auto_speed);
	}else{
		timer = setTimeout('auto_event()', 100);
	}
};

// When the DOM is ready
$(function() {
	tabcnt = $('.tabs a').length;
	timer = setTimeout('auto_event()', 1000);

	var $allContentBoxes = $(".content-box"),
	    $allTabs = $(".tabs li a"),
	    $el, $colOne, $colTwo, $colThree,
	    hrefSelector = "",
	    speedOne = 1000,
		speedTwo = 2000,
		speedThree = 1500;

	var tabvar1 = 0;
	var tabvar2 = '#one';

	// first tab and first content box are default current
	if(setday>-1){
		if(!$.cookie('tabcookie1')) $.cookie('tabcookie1',tabvar1,{ expires: setday});
		if(!$.cookie('tabcookie2')) $.cookie('tabcookie2',tabvar2,{ expires: setday});
		tabvar1 = $.cookie('tabcookie1');
		tabvar2 = $.cookie('tabcookie2');
	}
	$('.tabs a:eq('+tabvar1+')').addClass("current");
	$(tabvar2).addClass("current");

	$(".box-wrapper .current .col").css("top", 0);	

	$("#slot-machine-tabs").hover(
		function(){
			clearTimeout(timer);
		},function(){
			timer = setTimeout('auto_event()', 1000);
		}
	);

	$("#slot-machine-tabs").delegate(".tabs a", "click", function() {
		animated1 = true;
		animated2 = true;
		animated3 = true;
		$el = $(this);

		if ( (!$el.hasClass("current")) && ($(":animated").length == 0 ) ) {		
			tabindex = $el.parent().index();
			// current tab correctly set
			$allTabs.removeClass("current");
			$el.addClass("current");
			$.cookie('tabcookie1', $el.parent().index(),{ expires: setday});
			
			// optional... random speeds each time.
			speedOne = Math.floor(Math.random()*1000) + 500;
			speedTwo = Math.floor(Math.random()*1000) + 500;
			speedThree = Math.floor(Math.random()*1000) + 500;
		
			// each column is animated upwards to hide
			// kind of annoyingly redudundant code
			
			$colOne = $(".box-wrapper .current .col-one");
			$colOne.animate({
				"top": -$colOne.height()
			}, {duration:speedOne,complete:function(){ animated1 = false; }});
		
			$colTwo = $(".box-wrapper .current .col-two");
			$colTwo.animate({
				"top": -$colTwo.height()
			}, {duration:speedTwo,complete:function(){ animated2 = false; }});
		
			$colThree = $(".box-wrapper .current .col-three");
			$colThree.animate({
				"top": -$colThree.height()
			}, {duration:speedThree,complete:function(){ animated3 = false; }});
			
			// new content box is marked as current
			$allContentBoxes.removeClass("current");		
			hrefSelector = $el.attr("href");
			$.cookie('tabcookie2', hrefSelector,{ expires: setday});
			$(hrefSelector).addClass("current");
		
			// columns from new content area are moved up from the bottom
			// also annoying redundant and triple callback seems weird
			$(".box-wrapper .current .col-one").animate({
				"top": 0
			}, speedOne, function() {
				ifReadyThenReset();
			});
	
			$(".box-wrapper .current .col-two").animate({
				"top": 0
			}, speedTwo, function() {
				ifReadyThenReset();
			});
		
			$(".box-wrapper .current .col-three").animate({
				"top": 0
			}, speedThree, function() {
				ifReadyThenReset();
			});
		
		}
	});

});
