// Load jQuery
google.load("jquery", "1.4");
google.setOnLoadCallback(function() {
    $(document).ready(initPage);
})

function initPage(){
	var showMastheadInfoButton = true;
	
	switch (currentPage) {
        case "home":
			showMastheadInfoButton = false;
            initHome();
            break;
		case "question":
			initQuestion();
			break;
		case "nieuws":
			initNieuws();
			break;
		case "archief":
			initArchief();
			break;
        default:
            break;
    }
	
	if(showMastheadInfoButton){
		$("#show-masthead-info").click(showMastheadinfo)
	}
}

function showMastheadinfo(ev){
	$("#masthead-info").show(500);
	$("#show-masthead-info").hide(500);
	ev.preventDefault();
}



/* HOME */
function initHome(){
	initQuestionForm();
}

function initQuestionForm(){
	$(".question-form-category small").click(function(ev){
		if($(this).parent().find("p").css("display") == "none"){
			$(this).parent().find("p").show(250);
		}else{
			$(this).parent().find("p").hide(250);
		}

		ev.preventDefault();
		ev.stopPropagation();
	});
	
	$(".question-form-category").click(function(){
		$(this).parent().find(".selected").removeClass("selected");
		$(this).addClass("selected");
		$(this).find("input")[0].checked = true;
	});
	$("input[name=action[post-question]]").click(sendQuestionForm);
}

function sendQuestionForm(ev){
	var form = $(this).parents("form");
	checkForm(ev, form);
}


/* QUESTION */
function initQuestion(){
	$("input[name=action[post-comment]]").click(sendCommentForm);
}

function sendCommentForm(ev){
	var form = $(this).parents("form");
	checkForm(ev, form);
}


/* Nieuws */
function initNieuws(){
	$("input[name=action[post-news-comment]]").click(sendNewsCommentForm);
}

function sendNewsCommentForm(ev){
	var form = $(this).parents("form");
	checkForm(ev, form);
}



/* ARCHIEF */
function initArchief(){
	initCalendar();
}


/* CALENDAR */
var calendarMonths;
function initCalendar(){
	calendarMonths = $('.calendar-month');
	calendarMonths.parent().find(".calendar-month:not('.active')").css({display:"none"});
	
	var monthIndex = 0, previousMonth, nextMonth;
	calendarMonths.each(function(){
		nextMonth = monthIndex - 1;
		if(nextMonth < 0){
			nextMonth = 0;
		}
		
		previousMonth = monthIndex + 1;
		if(previousMonth >= calendarMonths.length -1){
			previousMonth = calendarMonths.length -1;
		}
		
		$(this).find('#calend-month-back').click(onMonthNavClick).data("newMonthIndex", previousMonth);
		$(this).find('#calend-month-next').click(onMonthNavClick).data("newMonthIndex", nextMonth);
		++monthIndex;
	})
	
	//$('.calendar-month tr a').click(function(ev){
		//var contentURL = $(this).attr("href");
		//console.log();
	//})
	
	$("#calendar-content>div:not('.active')").css({display:"none"});
	
	showMonth(0);
}

function showMonth(monthIndex){
	$('#calendar div.active').css({display:"none"}).removeClass("active");
	var activeMonth = $(calendarMonths[monthIndex])
	activeMonth.css({display:"block"}).addClass("active");
	
	$('#calendar-content div.active').css({display:"none"}).removeClass("active");
	var activeMonthContent = $("#"+activeMonth.attr("title"));
	activeMonthContent.css({display:"block"}).addClass("active");

	if(activeMonthContent.length == 0){
		$("#questions-none").css({display:"block"}).addClass("active");
	}
}

function onMonthNavClick(ev){
	showMonth($(this).data("newMonthIndex"));
	ev.preventDefault();
}




/* FORM */
function checkForm(ev, form){
	var allInputs = form.find(":input"), input, inputName, inputVal, fieldRequired, fieldOK, fieldType, inputLabel;
	var goSend = true, postData = new Object();
	
	allInputs.each(
		function(){
			input = $(this);
			inputName = input.attr("name");
			fieldType = input.attr("type");
			fieldRequired = input.hasClass("form-required");
			
			inputVal = "";
			switch(input.attr("type")){
				case "radio":
					inputVal = input.parents("fieldset").find(":checked").val();
					break;
				case "checkbox":
					inputVal = input.attr('checked');
					break;
				default:
					inputVal = input.val();
					break;
			} 
			
			postData[inputName] = inputVal;
			
			if(fieldType == "hidden" || fieldType == "submit" || !fieldRequired){
				return;
			}
			
			fieldOK = true;
			switch(input.attr("name")){
				case "fields[email]":
					var isEmail_test = isMail(inputVal);
					if(!isEmail_test){
						goSend = false;
						fieldOK = false;
					}
					break;
				default:
					if(inputVal == "" || inputVal == undefined){
						goSend = false;
						fieldOK = false;
					}
			}
			
			
			if(fieldOK){
				form.find("label[for='"+inputName+"']").removeClass("form-required-error");	
			}else{
				form.find("label[for='"+inputName+"']").addClass("form-required-error");	
			}
			
		}
	)
	
	if (goSend) {
		//form.find(".form-result").load(root+"/ajax-post-question/", postData, function(data){});
	}else{
		ev.preventDefault();
	}
}

function isMail(emailAdress){
	var isEmail_re = /^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/;
	return emailAdress.search (isEmail_re) != -1;
} 
