$.ajaxSetup({
  cache: false
});
$(document).ready(function(){
	var globalTimer;
	var speed = 6000;
	var currentSlide = 0;
	var totalSlides = $("#headersList UL LI").length;
	nextSlide();
	if(totalSlides>1){
		globalTimer=setTimeout(nextSlide,speed);
	}
	//SET UP TIMER FOR SLIDESHOW
	function nextSlide(){
		clearTimeout(globalTimer);
		if(currentSlide>(totalSlides-2))
		{
			currentSlide = 0;	
		} else {
			currentSlide++
		}
		goTo(currentSlide);
		globalTimer=setTimeout(nextSlide,speed);	
	}
	
	function goTo(current){
		loadImg($("#headersList UL LI:eq("+current+")").html());
	}
	
	function loadImg(src){
		if($("slider img").length>1){
			$("#slider img:eq(0)").remove();
		}
		var img = new Image();
		$(img).load(function(){
			$(this).hide();
			$("#slider").append(this);
			$(this).fadeIn("slow");
		}).attr('src',src)
		return false;
	}


	$(".toactivate").hide();
	$(".toactivate2").hide();
	$(".activate").each(function(){
		$(this).change(function(){
		 	if($(this).is(":checked")){
		 		$(this).parent().find(".toactivate").each(function(){
		 			$(this).show();
		 		})

		 	}else{
		 		$(this).parent().find(".toactivate").each(function(){
		 			$(this).hide();
		 		})
		 	}
		 })
	 })
	 $(".deactivate").each(function(){
		$(this).change(function(){
		 	if($(this).is(":checked")){
		 		$(this).parent().find(".toactivate").each(function(){
		 			$(this).hide();
		 		})
		 	}
		 })
	 })
	 $(".activate2").each(function(){
		$(this).change(function(){
		 	if($(this).is(":checked")){
		 		$(this).parent().find(".toactivate2").each(function(){
		 			$(this).show();
		 		})

		 	}else{
		 		$(this).parent().find(".toactivate2").each(function(){
		 			$(this).hide();
		 		})
		 	}
		 })
	 })
	 $("#more").click(function(e){
	 	$("#tohide").hide(100,function(){
	 		$("#toslide").animate({width: 680},400)
			$("#toslide .short").fadeToggle(100,function(){
				$("#toslide .long").fadeToggle(100);
			})
	 	});
	 	e.preventDefault();
	 })
	 $("#less").click(function(e){
	 	$("#toslide").animate({width: 360},400,function(){
	 		$("#tohide").show(100);
	 	})
		$("#toslide .short").fadeToggle(100,function(){
			$("#toslide .long").fadeToggle(100);
		})
		e.preventDefault();
	 })
	 $(".grid input").click(function(e){
	 	if($("#otherSelect").is(":checked")){
	 		$("#otherField").slideDown().find("input").focus();
	 	} else {
	 		$("#otherField").slideUp();
	 	}
	 })
	 $(".submit input").each(function(){

	 	$(this).click(function(e){
	 		var complete = true;
	 		$("input.required").each(function(){
	 			$(this).parent().removeClass("invalid");
	 			if(!$(this).val()){
	 				$(this).parent().addClass("invalid");
	 					 $(this).unbind("keyup").keyup(function(){
						 	if(!$(this).val()){
						 		$(this).parent().addClass("invalid");
						 	} else {
						 		$(this).parent().removeClass("invalid");
						 	}
						 })
	 				complete = false;
	 			}
	 			if($(this).hasClass("email")){
	 				if(!validateEmail($(this).val())){
	 					$(this).parent().addClass("invalid");
	 					$(this).unbind("keyup").keyup(function(){
						 	if(!$(this).val()){
						 		$(this).parent().addClass("invalid");
						 	} else {
						 		$(this).parent().removeClass("invalid");
						 	}
						 })
	 					complete = false;
	 				}
	 			}
	 		})
	 		if(complete){
	 			$("form").submit();
	 		}
	 		e.preventDefault();
	 	})
	 })
})
function validateEmail(email) 
{ 
 var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ 
 return email.match(re) 
}

//PROJECTS PAGE
function fetchList(lang,page){
	$("#projectsContainer *").remove();
	$.ajax({
		data: 	{page:page,lang:lang},
		url:"/ajax/get-projects.cfm",
		method:"get",
		success: handleList
	});
}

function handleList(thelist){
	var $container = $("#projectsContainer").append($("<ul></ul>").addClass("projectsList")).find(".projectsList"),
		$rootContainer = $("#projectsContainer"),
		sequencialFadeTime = 120;
	
	$(thelist).each(function(){
		if($(this).is("#nextProjects")){
			$rootContainer.append(this);
		}
		if($(this).is("#prevProjects")){
			$rootContainer.append(this);
		}
	})
	//Hide all projects for sequencial fadein.
	myList = $(thelist).find(".project").each(function(){
		var $project = $(this);
		$project.hide();
		$.wait(sequencialFadeTime*$project.index()).then(function(){
			$project.fadeIn(400);
		})
	})
	$container.append(myList);
	$('.project a[title]').qtip({
		position: { my: "top center", at: "bottom center"},
		style: {classes: 'ui-tooltip-light ui-tooltip-rounded' }
	})
}

function loadProjects(lang){
	fetchList(lang,1);

	$("#nextProjects").live("click",function(e){
		var page = $(this).data("page");
		$(".projectsList").fadeOut(400,function(){
			fetchList(lang,page);
		})
		e.preventDefault();
	})
	$("#prevProjects").live("click",function(e){
		var page = $(this).data("page");
		$(".projectsList").fadeOut(400,function(){
			fetchList(lang,page);
		})
		e.preventDefault();
	})
}


//EVENT PAGE
function loadEvents(){
	fetchEventList(1);
	$("#nextProjects").live("click",function(e){
		var page = $(this).data("page");
		$(".projectsList").fadeOut(400,function(){
			fetchEventList(page);
		})
		e.preventDefault();
	})
	$("#prevProjects").live("click",function(e){
		var page = $(this).data("page");
		$(".projectsList").fadeOut(400,function(){
			fetchEventList(page);
		})
		e.preventDefault();
	})
}

function fetchEventList(page){
	$("#projectsContainer *").remove();
	$.ajax({
		data: 	{page:page},
		url:"/ajax/get-event-pictures.cfm",
		method:"get",
		success: handleEventList
	});
}

function handleEventList(thelist){
	var $container = $("#projectsContainer").append($("<ul></ul>").addClass("projectsList")).find(".projectsList"),
		$rootContainer = $("#projectsContainer"),
		sequencialFadeTime = 120;
	$(thelist).each(function(){
		if($(this).is("#nextProjects")){
			$rootContainer.append(this);
		}
		if($(this).is("#prevProjects")){
			$rootContainer.append(this);
		}
	})
	//Hide all projects for sequencial fadein.
	myList = $(thelist).find(".project").each(function(){
		var $project = $(this);
		$project.hide();
		$project.find("a").fancybox();
		$.wait(sequencialFadeTime*$project.index()).then(function(){
			$project.fadeIn(400);
		})
	})
	$container.append(myList);
	$('.project a[title]').qtip({
		position: { my: "top center", at: "bottom center"},
		style: {classes: 'ui-tooltip-light ui-tooltip-rounded' }
	})
}


//PROJECT DETAIL PAGE

function loadThumbs(){

	$(".project-detail-thumb").each(function(){
		var $thumb = $(this),
			$big = $("project-detail-big-img"),
			src = $thumb.find("a").data("medium"),
			sequencialFadeTime = 350;
		$.wait(sequencialFadeTime*$thumb.index()).then(function(){
			$thumb.fadeIn(400);
		})
		$thumb.click(function(e){
			if($("#project-detail-big img").length>1){
				$("#project-detail-big img:eq(0)").remove();
			}
			var img = new Image();
			$(img).load(function(){
            	$(this).hide();
           	 	$("#project-detail-big").append(this);
            	$(this).fadeIn("slow");
            	$(".project-detail-thumb").each(function(){
            		$(this).removeClass("active");
            	})
            	$thumb.addClass("active");
        	}).attr('src',src)
        	e.preventDefault();
		})
	})
}

//NEWS OVERVIEW PAGE

function loadNews(lang){
	fetchNewsList(lang,1);
	$("#nextProjects").live("click",function(e){
		var page = $(this).data("page");
		$(".newsList").fadeOut(400,function(){
			fetchNewsList(lang,page);
		})
		e.preventDefault();
	})
	$("#prevProjects").live("click",function(e){
		var page = $(this).data("page");
		$(".newsList").fadeOut(400,function(){
			fetchNewsList(lang,page);
		})
		e.preventDefault();
	})
}

function fetchNewsList(lang,page){
	$("#newsContainer *").remove();
	$.ajax({
		data: 	{page:page,lang:lang},
		url:"/ajax/get-news.cfm",
		method:"get",
		success: handleNewsList
	});
}

function handleNewsList(thelist){
	var $container = $("#newsContainer").append($("<ul></ul>").addClass("newsList")).find(".newsList"),
		$rootContainer = $("#newsContainer"),
		sequencialFadeTime = 120;
	
	$(thelist).each(function(){
		if($(this).is("#nextProjects")){
			$rootContainer.append(this);
		}
		if($(this).is("#prevProjects")){
			$rootContainer.append(this);
		}
	})
	//Hide all projects for sequencial fadein.
	myList = $(thelist).find(".new").each(function(){
		var $project = $(this);
		$project.hide();
		$.wait(sequencialFadeTime*$project.index()).then(function(){
			$project.fadeIn(400);
		})
	})
	$container.append(myList);
}


//FETCH LINKS

function loadLinks(){
	fetchLinksList(1);
	$("#nextProjects").live("click",function(e){
		var page = $(this).data("page");
		$(".newsList").fadeOut(400,function(){
			fetchLinksList(page);
		})
		e.preventDefault();
	})
	$("#prevProjects").live("click",function(e){
		var page = $(this).data("page");
		$(".newsList").fadeOut(400,function(){
			fetchLinksList(page);
		})
		e.preventDefault();
	})
}

function fetchLinksList(page){
	$("#newsContainer *").remove();
	$.ajax({
		data: 	{page:page},
		url:"/ajax/get-links.cfm",
		method:"get",
		success: handleLinksList
	});
}

function handleLinksList(thelist){
	var $container = $("#newsContainer").append($("<ul></ul>").addClass("newsList")).find(".newsList"),
		$rootContainer = $("#newsContainer"),
		sequencialFadeTime = 50;
	
	$(thelist).each(function(){
		if($(this).is("#nextProjects")){
			$rootContainer.append(this);
		}
		if($(this).is("#prevProjects")){
			$rootContainer.append(this);
		}
	})
	//Hide all projects for sequencial fadein.
	myList = $(thelist).find(".linkBlock").each(function(){
		var $project = $(this);
		$project.hide();
		$.wait(sequencialFadeTime*$project.index()).then(function(){
			$project.show("slide", { direction: "left", easing: "easeOutBack" }, 1000);
		})
	})
	$container.append(myList);
}



//CONTACT PAGE


function populatePeople(lang,loc){
	$.ajax({
		data: 	{location:loc,lang:lang},
		url:"/ajax/get-people.cfm",
		method:"get",
		success: handlePpl
	});
}

function handlePpl(data){
	if(data != "empty"){
	var $select = $("#location-select");
	$select.append(data);
	fetchDetails();
	} else {
		$("#location-select").remove()
	}
}

function fetchDetails(){
	$("#person-container *").remove();
	$.ajax({
		data: 	{person:$("#location-select option:selected").data("id")},
		url:"/ajax/get-person.cfm",
		method:"get",
		success: handlePerson
	});
	$("#location-select").change(function(){
		$("#person-container *").remove();
		$.ajax({
			data: 	{person:$("#location-select option:selected").data("id")},
			url:"/ajax/get-person.cfm",
			method:"get",
			success: handlePerson
		});
	})
}

function handlePerson(data){
	$("#person-container").append(data);
	$("#location-select").select_unskin().select_skin();
}

//set up the map

function getMap(lat,lng){
	$("#map").gMap({
		mapTypeControl:         false,
	    zoomControl:            false,
	    panControl:             false,
	    scaleControl:           false,
	    streetViewControl:      false,
		zoom:13,
		icon: {
	        image: 'images/marker.png',
	        iconsize: [27, 33],
	        iconanchor: [14, 33],
	        infowindowanchor: [14, 0]
	    },
		markers: [{ latitude: lat, longitude: lng }]
	});
	    
}

//set up contact form validation.

function setUpForm(){
	$("send-contact").click(function(e){
		e.preventDefault();
		var valid = true;
		$(".required").each(function(){
			if(this.val().length > 2){
			} else {
				$(this).parent().addClass("invalid");
				valid = false;
			}
		})
		if(valid){
			$("form").submit();
		}
	})
}
