if (typeof Archeage === "undefined") {
	var Archeage = {};
}


/**
 * 메인화면 좌측 패치노트 목록
 */
Archeage.PatchnoteEvents = function($contentArea) {
	this.$contentArea = $contentArea;
	this.$patchnotes = $(".patchnoteEvent", this.$contentArea);
	this.$prevButton = $("a.prevButton", this.$contentArea);
	this.$nextButton = $("a.nextButton", this.$contentArea);

	this.totalCount = this.$patchnotes.length;
	this.currentPosition = 0;

};

Archeage.PatchnoteEvents.prototype.init = function() {

	if (this.totalCount == 1) {
		return;
	}

	var that = this;

	this.$prevButton.bind("click", function() {
		that.prev();
		return false;
	});

	this.$nextButton.bind("click", function() {
		that.next();
		return false;
	});
};

Archeage.PatchnoteEvents.prototype.prev = function() {
	if (this.currentPosition == 0) {
		return;
	}

	$(this.$patchnotes.get(this.currentPosition)).hide();

	this.currentPosition--;
	$(this.$patchnotes.get(this.currentPosition)).show();

	this.fixButtons();
};

Archeage.PatchnoteEvents.prototype.next = function() {
	if (this.currentPosition == (this.totalCount - 1)) {
		return;
	}

	$(this.$patchnotes.get(this.currentPosition)).hide();

	this.currentPosition++;
	$(this.$patchnotes.get(this.currentPosition)).show();

	this.fixButtons();
};

Archeage.PatchnoteEvents.prototype.fixButtons = function() {
	if (this.currentPosition > 0) {
		this.on(this.$prevButton);
	} else {
		this.off(this.$prevButton);
	}

	if (this.currentPosition < (this.totalCount - 1)) {
		this.on(this.$nextButton);
	} else {
		this.off(this.$nextButton);
	}
};

Archeage.PatchnoteEvents.prototype.on = function($button) {
	$button.addClass("on");
	$button.removeClass("off");
};

Archeage.PatchnoteEvents.prototype.off = function($button) {
	$button.addClass("off");
	$button.removeClass("on");
};

/**
 * 메인화면 우측 이벤트 목록
 */
Archeage.CurrentEvents = function($contentArea) {
	this.$contentArea = $contentArea;
	this.$slides = $(".slide > span > a", this.$contentArea);
	this.$contents = $(".banner_event", this.$contentArea);

	this.totalCount = this.$slides.get().length;
	this.currentPosition = 0;
	this.rolling = true;
};

Archeage.CurrentEvents.ROLLING_INTERVAL = 3000;

Archeage.CurrentEvents.prototype.init = function() {
	var that = this;
	this.$slides.bind("click", function() {
		that.rolling = false;

		that.show($(this));
		return false;
	});

	var randomShowPosition = Archeage.random(this.$slides.length);
	this.show($(this.$slides.get(randomShowPosition)));
	this.roll(true);
};

Archeage.CurrentEvents.prototype.show = function($slide) {
	var targetPosition = parseInt($slide.text()) - 1;
	if (targetPosition === this.currentPosition) {
		return;
	}

	$(this.$slides.get(this.currentPosition)).removeClass("active");
	$(this.$slides.get(targetPosition)).addClass("active");

	$(this.$contents.get(this.currentPosition)).hide();
	$(this.$contents.get(targetPosition)).show();

	this.currentPosition = targetPosition;
};

Archeage.CurrentEvents.prototype.roll = function (start) {
	if (!this.rolling) {
		return;
	}

	if (!start) {
		var next = this.currentPosition + 1;
		if (next >= this.totalCount) {
			next = 0;
		}
		this.show($(this.$slides.get(next)));
	}

	var that = this;
	setTimeout(function () { that.roll(false); }, Archeage.CurrentEvents.ROLLING_INTERVAL);
};

/**
 * 메인화면 좌측 아키에이지의 화제!
 */
Archeage.MainHots = function($contentArea) {
	this.$contentArea = $contentArea;
	this.$hots = $("div.hot", $contentArea);

	this.rolling = true;
	this.totalCount = this.$hots.get().length;

	this.titles = [];
	this.contents = [];

	for (var i = 0; i < this.$hots.length; i++) {
		this.titles.push($(".tab_hot li a", $(this.$hots.get(i))));
		this.contents.push($(".hot_contents", $(this.$hots.get(i))));
	};

	this.currentActive = 0;
};

Archeage.MainHots.ROLLING_INTERVAL = 3000;

Archeage.MainHots.prototype.init = function() {
	this.loadCurrentActive();

	var that = this;
	for (var i = 0; i < this.titles.length; i++) {
		this.titles[i].bind("click", function() {
			that.rolling = false;
			return that.show($(this));
		});
	}

	this.roll(true);
};

Archeage.MainHots.prototype.loadCurrentActive = function() {
	for (var i = 0; i < this.titles.length; i++) {
		if (this.titles[i].hasClass("active")) {
			this.currentActive = i;
			return;
		}
	}
};

Archeage.MainHots.prototype.show = function($clickedTitle) {
	if ($clickedTitle.hasClass("active")) {
		return false;
	}

	this.titles[this.currentActive].removeClass("active");
	this.contents[this.currentActive].hide();

	this.currentActive = $clickedTitle.data("index");

	this.titles[this.currentActive].addClass("active");
	this.contents[this.currentActive].show();

	return false;
};

Archeage.MainHots.prototype.roll = function(start) {
	if (this.rolling === false) {
		return;
	}

	if (!start) {
		var next = this.currentActive + 1;
		if (next >= this.totalCount) {
			next = 0;
		}

		this.show(this.titles[next]);
	}

	var that = this;
	setTimeout(function() {that.roll(false);}, Archeage.MainHots.ROLLING_INTERVAL);
};

$(document).ready(function(){
	// 로그인 창에서 엔터 누르면 로그인함.
	$("#authenticate").keypress(function(e){
		if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
			$("#loginButton").click();
		}
	});

	$username = $('#j_username');
	$password = $('#j_password');

	$username.focusout(function(event){
		if($username.val().length >= 1){
			$username.removeClass('no_id');
		} else {
			$username.addClass('no_id');
		}
	});

	$password.focusout(function(event){
		if($password.val().length >= 1){
			$password.removeClass('no_pw');
		} else {
			$password.addClass('no_pw');
		}
	});

	$username.focus(function(event){
		$username.removeClass('no_id');
	});

	$password.focus(function(event){
		$password.removeClass('no_pw');
	});

	var isAlreadyCapsLockWanring = false;
	$password.keypress(function(event){
		isAlreadyCapsLockWanring = $.user.fireCapsLockWarning(event, isAlreadyCapsLockWanring, $password);
	});

	// Back버튼으로 메인 페이지로 돌아왔을 때 "아이디" 이미지가 로그인 상자에 남아있는 현상 제거.
	if ($username.val() && $username.val().length > 0) {
		$username.removeClass('no_id');
	}

	$('#loginButton').click(function(event){
		if( $.aa.isEmptyText( $username, '아이디를 입력하세요.') ) {
			return false;
		};

		if( $.aa.isEmptyText( $password, '비밀번호를 입력하세요.') ) {
			return false;
		};

		if(aos_is_new() && aos_isrunning("40")) {
			aos_copy_to_form(document.authenticate);
		}

		$("#authenticate").submit();
		return false;
	});
	$('.ie6br .close').bind('click', function() {
		$('.ie6br').hide();
		return false;
	});

	$(".characterHomeGuide").bind("click", function() {
		var url = $(this).attr("href");
		window.open(url, "characterHomeGuide", "width=677px,height=800px,menubar=no,toolbar=no,scrollbars=yes");
		return false;
	});

	$("#mainCloseImg").bind("click", function() {
		$('.column_layer').fadeOut(1000);
	});

	new Archeage.PatchnoteEvents($("#maincontentArea .column .column_left .patch")).init();
	new Archeage.CurrentEvents($("#maincontentArea .column .column_right .main_event")).init();
	new Archeage.MainHots($("#maincontentArea .column .column_left .hot_archeage")).init();
});
