﻿
$(document).ready(function () {
	SetHeader();
	SetButtons();
	SetContentHeight();
	bgimg = $("#bg img");
	var hash = window.location.hash;
	var idtoclick = hash + "-link";
	$(idtoclick).click();
	$("body").scrollTop(0);
});

$(window).load(function () {
	InitBackgroundImage();
	$(window).resize(function () {
		SetBackgroundImage();
		SetContentHeight();
	});
});

var allNav = $("#nav ul li");
var contentitems = $("#content-main > div");
var invitationinfo = $("p#invitation-intro");
var moreinfonav = $("div#more-info-nav");
var moreinfonavlinks = moreinfonav.find("a");
var moreinfoitems = $("div#more-info > div");

var SetHeader = function () {

	$("#nav ul li").click(function () {
		HeaderSelect($(this));
	});

	var loginlink = $("#nav ul li#login-link");
	loginlink.click(function () {
		$("div#login").show();
	});

	var invitationlink = $("#nav ul li#invitation-link");
	invitationlink.click(function () {
		$("div#invitation").show();
		invitationinfo.show();
	});

	$("a.invitation").click(function () {
		invitationlink.click();
	});

	var moreinfolink = $("#nav ul li#more-info-link");
	moreinfolink.click(function () {
		$("div#more-info").show();
		moreinfonav.show();
	});

	moreinfonavlinks.click(function () {
		var clicked = $(this);
		MoreInfoNavSelect(clicked);
	});

	var aboutlink = $("a#about-link", moreinfonav);
	aboutlink.click(function () {
		$("div#more-info div#about").show();
	});

	var teamlink = $("a#team-link", moreinfonav);
	teamlink.click(function () {
		$("div#more-info div#team").show();
	});

	var contactlink = $("a#contact-link", moreinfonav);
	contactlink.click(function () {
		$("div#more-info div#contact").show();
	});

	var faqlink = $("a#faq-link", moreinfonav);
	faqlink.click(function () {
		$("div#more-info div#faq").show();
	});

	var jobslink = $("a#jobs-link", moreinfonav);
	jobslink.click(function () {
		$("div#more-info div#jobs").show();
	});
};

var HeaderSelect = function (clicked) {
	allNav.removeClass("selected");
	clicked.addClass("selected");
	contentitems.hide();
	invitationinfo.hide();
	moreinfonav.hide();
};

var MoreInfoNavSelect = function (clicked) {
	var moreinfolink = $("#nav ul li#more-info-link");
	moreinfolink.click();
	moreinfonavlinks.removeClass("selected");
	clicked.addClass("selected");
	moreinfonav.show();
	moreinfoitems.hide();
};

var SetButtons = function () {
	$("#btn-login").mouseover(function () {
		$(this).attr("src", "/splash/images/btn_login_over.png");
	});
	$("#btn-login").mouseout(function () {
		$(this).attr("src", "/splash/images/btn_login.png");
	});
	$("#btn-login").mousedown(function () {
		$(this).attr("src", "/splash/images/btn_login_down.png");
	});
	$("#btn-login").mouseup(function () {
		$(this).attr("src", "/splash/images/btn_login_over.png");
	});

	$("#btn-invitation").mouseover(function () {
		$(this).attr("src", "/splash/images/btn_invitation_over.png");
	});
	$("#btn-invitation").mouseout(function () {
		$(this).attr("src", "/splash/images/btn_invitation.png");
	});
	$("#btn-invitation").mousedown(function () {
		$(this).attr("src", "/splash/images/btn_invitation_down.png");
	});
	$("#btn-invitation").mouseup(function () {
		$(this).attr("src", "/splash/images/btn_invitation_over.png");
	});

	var invaddress = $("div#invitation-address");
	$("input#isartist").click(function () {
		if ($(this).is(':checked')) {
			invaddress.show();
		} else {
			invaddress.hide();
		}
	});

	$("form#form-invitation").submit(function () {
		var form = $(this);
		$.post('/signup/index',
			$(form).serialize(),
			function(result) {
				$(form).hide();
				$("div#invitation-confirm").show();
			});
		return false;
	});
	
	$('form#member-login').submit(function() {
		$('.error.passWrong').show();
		return false;
	});
};


/**
 * Make sure that background image always cover the entire window regardless of window height and width
 * Similar essentially to background-size: cover in CSS3
 * @return
 */

var bgimg;
var img_w;
var img_h;
var img_aspect;

var InitBackgroundImage = function () {
	img_w = bgimg.width();
	img_h = bgimg.height();
	img_aspect = img_w / img_h;
	SetBackgroundImage();
	bgimg.fadeIn("normal");
};

var SetBackgroundImage = function () {
	var window_w = $(window).width();
	var window_h = $(window).height();
	var window_aspect = window_w / window_h;

	if (window_aspect > img_aspect) {
		bgimg.width(window_w);
		bgimg.height(Math.round(window_w / img_aspect));
	} else {
		bgimg.width(Math.round(window_h * img_aspect));
		bgimg.height(window_h);
	}
};

/**
 * Scale content height to be somewhat smaller than the window height
 * @return
 */
var SetContentHeight = function() {
	var targetHeight = $(window).height() - 200;
	$("#more-info .form-content").css('max-height', targetHeight);
};
