/**
 * @author jan
 */


$(document).ready(function(){
	if($("#container").height() < $(document).height() - 40){
		$("#container").css("min-height", ($(document).height() - 40 - 2*55) + "px"); 
	}
	if($("#sessions_info_container").size()){
		$(document).find(".gallery").each(function(){
			$(this).children("li").first().addClass("active");
		});
		//if($(".post").size()){initSlideshow()};
		initWelcomeMessage();
		initBollekes();
		initAttendForm();
		initBlog();
		initAbout();
	}
	if($("#page404").size()){
		setInterval(doTheCountDown, 1000);
	}
	$("#jobs").click(function(e){
		e.stopPropagation();
		return false;
	})
});


var intervalid = 0;
var tickcount = 0;
function initWelcomeMessage(){
	if($("#welcome_message").size()){
		if(!$.cookie("alreadyvisited")){
			$.cookie("alreadyvisited", "true", {expires:1000});
			$("#welcome_container").show();
			$(document).bind("click", function(){updateGA("close_welcome_screen"); closeWelcome()});
			intervalid = setInterval("tickWelcome()", 1000);
			tickWelcome();
		} else {
			$("#welcome_container").remove();
		}
	}
}

function closeWelcome(){
	clearInterval(intervalid);
	$("#welcome_container").fadeOut(300);
	$(document).unbind("click", closeWelcome);
	$("#welcome_container").remove();
}

function tickWelcome(){
	$("#welcome_message p").text('Click or wait ' + (10 - tickcount) + ' seconds');
	tickcount++;
	if(tickcount == 11){
		closeWelcome();
	}
}

function initBollekes(){
	$("#sessions_info_container").append($($("#list_container").html()));
	$("#sessions_info_container>a").remove();
	$("#sessions_info_container").hide();
	$("#sessions_info_container form input[type=text]").focus(function(){
		$(this).parent().find("label").hide();
	});
	$("#sessions_info_container form input[type=text]").blur(function(){
		if(this.value == "")
		$(this).parent().find("label").show();
	});
	$("#sessions_info_container form input[type=text]").each(function(){
		if(this.value == ""){
			$(this).parent().find("label").show();
		} else {
			$(this).parent().find("label").hide();
		}
	});
	$(".bol_lijst li.used a").click(function(){return false;});
	$(".you").click(function(){
		openInfoContainer();
		$("#sessions_attend").show();
		$("#sessions_text").hide();
		return false;
	});
	$(".free").hover(function(){
		$(".you a").addClass("hover");
	}, function(){
		$(".you a").removeClass("hover");
	});
	$(".free").click(function(e){
		e.stopPropagation();
		$(".you a").click();
	});
	$(".info_link").click(function(){
		openInfoContainer();
		$("#sessions_attend").hide();
		$("#sessions_text").show();
		return false;
	});
	$("#close").click(function(){closeInfoContainer();return false;});
	$("#pickaslot").click(function(){
		$("#sessions_attend").fadeIn(1000);
		$("#sessions_text").hide();
		return false;
	});
	$("#whatisthisabout").click(function(){
		$("#sessions_attend").hide();
		$("#sessions_text").fadeIn(1000);
		return false;
	});
}

function initAttendForm(){
	$("#attend_form .btn").click(function(){
		$("#attend_form").submit();
		return false;
	});
	$("#attend_form").submit(function(){
		var error = false;
		$("#firstname, #name").each(function(){
			if($(this).val().length < 2){
				$(this).parent().addClass("error");
				error = true;
			} else {
				$(this).parent().removeClass("error");
			}
		});
		var filter = /^[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/;
		if(!filter.test($("#mail").val())){
			$("#mail").parent().addClass("error");
			error = true;
		} else {
			$("#mail").parent().removeClass("error");
		}
		
		if(!error){
			updateGA("submit_subscribe_session");
			$.ajax({
				type:"post",
				url:$("#attend_form").attr("action"),
				data:$("#attend_form").serialize() + "&noredirect=true",
				success:function(html){
					$("#sessions_info_container form input[type=text]").val("");
					$("#sessions_info_container form input[type=text]").each(function(){
						if(this.value == ""){
							$(this).parent().find("label").show();
						} else {
							$(this).parent().find("label").hide();
						}
					});
					$("#sessions_title").text(html);

					$("#sessions_title").css("color", "#76ce87");
				}
			});
		}
		
		return false;
	});
}

function openInfoContainer(){
	if($("#sessions_info_container").is(":hidden")){
		updateGA("open_sessions_box");
		$("#container").bind("click", closeInfoContainer);
		$("#sessions_info").bind("click", function(e){e.stopPropagation()});
		
		$(".bol_lijst").addClass("disable");
		
		$("#bollekes").stop();
		$("#sessions_info_container").stop();
		$("#sessions_info").stop();
		
		$("#bollekes .bol_lijst").css({"height":"33px"});
		$("#bollekes").css("margin-bottom", "440px");
		$("#bollekes").animate({"margin-bottom":"500px"},{duration:1000, easing:"easeOutExpo"});
		
		$("#sessions_info_container").show();
		$("#sessions_info_container").css({"margin-top":"-440px"});
		$("#sessions_info_container").animate({"margin-top":"-500px"},{duration:1000, easing:"easeOutExpo"});
		
		$("#sessions_info").css("height", "310px");
		$("#sessions_info").animate({"height":"370px"},{duration:1000, easing:"easeOutExpo"});
	}
}

function closeInfoContainer(){
	if(!$("#sessions_info_container").is(":hidden")){
		updateGA("close_sessions_box");
		$("#container").unbind("click", closeInfoContainer);
		$("#sessions_info").unbind("click");
		
		$(".bol_lijst").removeClass("disable");
		
		$("#bollekes").stop();
		$("#sessions_info_container").stop();
		$("#sessions_info").stop();
		
		$("#bollekes").css("margin-bottom", "208px");
		$("#bollekes").animate({"margin-bottom":"148px"},{duration:1000, easing:"easeOutExpo", complete:function(){$("#bollekes").css("margin-bottom", "0px");}});
		
		
		$("#sessions_info_container").css({"margin-top":"-208px"});
		$("#sessions_info_container").animate({"margin-top":"-148px"},{duration:1000, easing:"easeOutExpo", complete:function(){$("#sessions_info_container").hide();$("#bollekes .bol_lijst").css({"height":"100px"});}});
		
		$("#sessions_info").css("height", "60px");
		$("#sessions_info").animate({"height":"0px"},{duration:1000, easing:"easeOutExpo"});
	}
}

function initBlog(){
	initFilters();
	initComments();
	initForms();
	$(".post_author").live("click", function(){
		$(window).scrollTo("#about", 300);
		$(".itisi").hide();
		$($(this).attr("href")).find(".itisi").show();
		openAbout(false);
		return false;
	})
}

var offset = 0;
var lastCall = false;
var doingCall = false;

function initFilters(){
	$("#quarter_select").amaiSelect(300);
	$("#filters input").amaiCheck();
	makeLabelsRight();
	$("#filters input").change(function(){
		makeLabelsRight();
	});
	$("#filter_btn").click(function(){
		if($("#filters").is(":hidden")){
			$(this).text("Hide filter");
			$("#filters").slideDown();
		} else {
			$(this).text("Filter");
			$("#filters").slideUp();
		}
		return false;
	});
	//$("#filter_btn").click();
	$("#filters input, #quarter_select").change(function(){
		$("#posts_container").html("");
		offset = 0;
		lastCall = false;
		changePosts();
	});
	$(window).scroll(function(){
		if($(window).scrollTop() > $(document).height() - $(window).height() - 200){
			changePosts();
        }
	});
	$("#posts_loader").hide();
	//if($("#posts_container .post").size() == 0){
		changePosts()
	//} else {
	//	lastCall = true;
	//}
	//$(".post").live("contextmenu", function(){$(this).attr("rel"); return false;});
}

var firstTime = true;

function changePosts(){
	if(!lastCall && !doingCall){
		updateGA("load_posts");
		$("#posts_loader").show();
		doingCall = true;
		var values = $("#filters input").serialize() + "&period=" + $("#quarter_select").val();
		req = $.ajax({
			type:"post",
			url:"/home/ajax_getposts/" + offset,
			data:values,
			success:function(html){
				$("#posts_loader").hide();
				doingCall = false;
				if(html.indexOf("false") == 0){
					lastCall = true;
					if(offset == 0){
						$("#posts_container").append("<p>Uh Oh, I guess you're a bit too specific, try enabling some filters...</p>");
					}
				} else {
					offset += 4;
					html = $(html);
					$("#posts_container").append(html);
					html.children().hide();
					html.children().fadeIn(1000);

					html.find(".gallery").each(function(){
						$(this).children("li").first().addClass("active");
					});
				}
				if(firstTime){
					firstTime = false;
					initSlideshow();
				}

				$(".makelinkshort").each(function(){
					$(this).text($(this).text().replace("http://", ""));
					if($(this).text().length > 50){
						$(this).text($(this).text().substring(0, 50) + "...");
					}
				});
			}
		});
	}
}

function initComments(){
	$(".comment_btn").live("click", function(){
		if(!$(this).parents(".post").hasClass("post_active")){
			$(this).parents(".post").addClass("post_active");
			$(this).parents(".post").find(".post_comment_container").slideDown(300, function(){
				//$.scrollTo(this, 600);
			});
			$(this).text("Close");
		} else {
			$(this).parents(".post").removeClass("post_active");
			$(this).parents(".post").find(".post_comment_container").slideUp(300);
			$(this).text("Comment?");
		}
		return false;
	});
}

function initForms(){
	$(".comment_form textarea").live("focus", function(){
		$(this).parent().find("label").hide();
	});
	$(".comment_form textarea").live("blur", function(){
		if($(this).val() == ""){
			$(this).parent().find("label").show();
		}
	});
	$(".comment_form input[type=text]").live("focus", function(){
		if(this.value == this.defaultValue){
			$(this).val("");
			$(this).css("color", "#000");
		}
	});
	$(".comment_form input[type=text]").live("blur", function(){
		if(this.value == ""){
			this.value = this.defaultValue;
			$(this).css("color", "#ccc");
		}
	});
	$(".comment_form textarea").css("overflow", "hidden");
	$(".comment_form textarea").live("keypress", checkTextarea);
	$(".comment_form textarea").live("keyup", checkTextarea);
	$(".comment_form textarea").live("blur", checkTextarea);
	$(".comment_form .btn").live("click", function(){
		$(this).parents("form").submit();
		return false;
	});
	$(".comment_form").live("submit", function(){
		var error = false;
		$(this).find("input.name").each(function(){
			if($(this).val().length < 2 || this.value == this.defaultValue){
				$(this).addClass("error");
				error = true;
			} else {
				$(this).removeClass("error");
			}
		});
		$(this).find("textarea.text").each(function(){
			if($(this).val().length < 4 || $(this).val().length > 1000){
				$(this).addClass("error");
				$(this).parent().find("label").addClass("error");
				error = true;
			} else {
				$(this).removeClass("error");
				$(this).parent().find("label").removeClass("error");
			}
		});
		var filter = /^[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/;
		$(this).find("input.mailinput").each(function(){
			if(!filter.test($(this).val()) || this.value == this.defaultValue){
				$(this).addClass("error");
				error = true;
			} else {
				$(this).removeClass("error");
			}
		});
		
		var form = $(this);
		if(!error){
			updateGA("submit_comment");
			$.ajax({
				type:"post",
				url:$(this).attr("action"),
				data:$(this).serialize() + "&noredirect=true",
				success:function(html){
					html = $(html);
					form.after(html);
					html.hide();
					html.fadeIn(200);
					form.remove();
				}
			});
		}
		return false;
	});
}

function checkTextarea(){
	//height:150px;
	var calcHeight = this.scrollHeight;
	if(calcHeight > 80){
		$(this).height(calcHeight);
	} else {
		$(this).height(80);
	}
	if(1000 - $(this).val().length >= 0){
		$(this).parent().parent().find(".chars_remaining").css("color", "#000");
	} else {
		$(this).parent().parent().find(".chars_remaining").css("color", "#FF0000");
	}
	$(this).parent().parent().find(".chars_remaining").text((1000 - $(this).val().length) + " chars");
}

function makeLabelsRight() {
	$("#filters input").each(function(){
		if($(this).is(":checked")){
			$(this).parent().find("label").removeClass("disabled");
		} else {
			$(this).parent().find("label").addClass("disabled");
		}
	});
}

function initAbout(){
	$("#about_items li").click(function(){
		$(this).find(".btn").trigger("click");
	});
	$("#about_items li .btn").click(function(e){
		e.stopPropagation();
		if($(this).attr("id") == "aboutLink"){
			openAbout(true);
		} else {
			var url = $(this).attr("href");
			window.location = url;
		}
		return false;
	});
	$(".itisi").hide();
	if(window.location.hash != ""){
		if("#about" == window.location.hash){
			openAbout(true);
		} else if("#subscribe" == window.location.hash){
			$(".you a").click();
		}else {
			$(window.location.hash).find(".itisi").show();
			updateGA("open_about_box" + window.location.hash);
			$(window).scrollTo("#about", 300);
			openAbout(false);
		}
	}
}

function openAbout(hideItisi){
	
	if(hideItisi){
		updateGA("open_about_box");
		$(".itisi").hide();
	} 
	$("#about>h2").find('span').addClass("white");
	$("#about").height($("#about").height());
	$("#container").css("min-height", $("#container").height());
	$("#about_items").hide()
	$("#about").css({"position":"absolute", "margin-left":"130px", "width":"820px"});
	$("#about").animate({'margin-left':'0px', 'width':'950px'}, {easing:"easeOutExpo", duration:1000, complete:function(){
		$("#about_detail").fadeIn(300, function(){
			$("#about").bind("mouseleave",function(){
				closeAbout();
			});
		});
	}});
}

function closeAbout(){
	updateGA("close_about_box");
	$("#about").unbind("mouseleave");
	$("#about_detail").fadeOut(0, function(){
		$("#about").css({"margin-left":"500px", "width":"450px"});
		$("#about").animate({'margin-left':'630px', 'width':'320px'}, {easing:"easeOutExpo", duration:1000, complete:function(){
			$("#about").css({"position":"static", "margin-left":"0px", "float":"left"});
			$("#about").height("auto");
			$("#container").css("min-height", "0");
			$("#about_items").fadeIn(300);
			$("#about>h2").find('span').removeClass("white");
		}});
	});
};

var initHappenedSlideshow = false;
function initSlideshow(){
	if(!initHappenedSlideshow){
		setInterval(doSlideshow, 4000);
		initHappenedSlideshow = true;
	}
}

function doSlideshow(){
	$(".post:hover .gallery").each(function(){
		if($(this).children("li.active").next().size()){
			$(this).children("li.active").removeClass("active").next().addClass("active");
		}else{
			$(this).children("li.active").removeClass("active");
			$(this).children("li").first().addClass("active");
		}
	});
}

var countDownCount = 5;
var redirected = false;

function doTheCountDown(){
	if(countDownCount > 1){
		countDownCount -= 1;
		$("#countdown404").text(countDownCount);
	} else {
		var gotoPage = "";
		if(destination != undefined){			
			gotoPage = destination;
		} 
		if(!redirected)
		window.location = gotoPage;
		redirected = true;
	} 
}

function updateGA(event){
	_gaq.push(['_trackEvent', 'jscalls', event]);
}
