$(document).ready(function() {

	//Download PDF


	//Modal gallery
	$(".photoList li").click(function(e){
		$(".photoList li").removeClass("current");
		$(this).addClass("current");
		$(this).parent().parent().next('.photoPopOut').toggle();
		$(".photoListBig li").addClass("hide");
		for(i=0; i<=$(".photoList li").length; i++){
			if($(".photoList li").eq(i).attr('class') == ("current")){
				var showImg = i;
				$(".photoListBig li").eq(showImg).removeClass("hide");
			} else {
				
			}
		}
		return false;
	});

	//Build gallery picker
	/*function buildPos(){
		for(i=1; i<=$(".photoList li").length; i++){
			$("#photoPosition").append('<li>' + i + '</li>');
		}
	}

	buildPos();
*/
	/*$(".photoList").delegate("li", "click", function(){
			if($(".photoList li").eq(i).attr('class') == ("current")){
				var showImg = i;
				$(".photoListBig li").eq(showImg).removeClass("hide");
			} else {
				
			}
		$(this).toggleClass("hover");
	});*/

	$(".photoPopOut").click(function(e){
				$(this).toggle();
				return false;
			});


	//Modal video player
	$("#videoOpen").click(function(e){
				$("#videoHolder").toggleClass("hide");
				return false;
			});

$(".newArtistBtn").click(function(){
	/*return false;*/
});

$("#videoOpen").mouseenter(function() {
  $('#videoPhoto').fadeOut('slow');
});

$("#videoOpen").mouseleave(function() {
 $('#videoPhoto').fadeIn('slow');
});

	$("#videoHolder").click(function(e){
				$(this).toggleClass("hide");
				return false;
			});

	//Homepage random block
	var randomNum = Math.floor(Math.random()*5);
	var myCars=new Array(); // regular array (add an optional integer
	myCars[0]="That&#8217;s enough to buy <b>10,619 shelters</b> for use in emergencies such as in Haiti";       // argument to control array's size)
	myCars[1]="That&#8217;s enough to buy <b>48,000 goats</b>, providing poor farmers with milk to sell or feed their families&#8230;";
	myCars[2]="That&#8217;s enough to build over <b>700 classrooms</b>, helping to educate thousands of kids for years to come&#8230;";
	myCars[3]="That&#8217;s enough to build <b>300 new wells</b> in Mali.";
	myCars[4]="That&#8217;s enough to buy over <b>11,000 beehives</b> to give farmers in Honduras a source of income.";
	myCars[5]="That&#8217;s enough to train almost <b>55,000 health workers</b> who can provide care for people in remote areas.";
	$("#backgroundThumb").addClass("homeThumb"+randomNum);
	$(".detail").html(myCars[randomNum]);

	// Nested left hand navigation
//	function setOpen(){
//		$('#tabs ul li a.active').parents("li").siblings("ul").addClass("collapse");
//		$('#tabs ul li a.active').parents("li").next("ul").addClass("open");
//		$('#tabs ul ul li a.active').parents("ul").siblings("ul").addClass("collapse");
//		$('#tabs ul ul li a.active').parents("ul").next("ul").addClass("open");
//		$('.smallEventList .eventSmall').eq(3).addClass("noMargin");
//		$('.smallEventListTwo .eventSmall').eq(3).addClass("noMargin");
//	};
//	
//	setOpen();

	/*$(){
	
	}*/

	//Homepage news block
	for(i=2;i<5;i++){
		$('.colThree').children("div").eq(i).addClass("hide");
	}
	
	//Hide empty tags
	$('label, p, span').each(function() {
         if ($(this).text() == "") {
               $(this).addClass("hide");
         }
	});
	
	//Disable links in preview mode
	if ($(".previewContents").length>0){
		$("a").click(function() { 
			//alert("You are currently in Preview mode - all links are disabled.");
			$("#videoHolder").show();
		return false; 
		});
		$("a").addClass("disabledLink");
		$("#closeWindow").click(function() { 
			window.close(); 
			return false; 
		});
	}
	
	$("#returnPreview").click(function() { 
			$("#videoHolder").hide();
		});
		
	$("#closePreviewWindow").click(function() { 
			window.close(); 
			return false; 
		});
	
	//Set active state on progress bar
	var listLength = $("#eventProgressBar li").size();
			for(i=0;i<listLength;i++){
					if($("#eventProgressBar li").eq(i).children("a").attr("class") === "active"){
						$("#eventProgressBar li").eq(i).children("a").parents("li").prev().children("a").addClass("prevActive");
					} else{
					}
				}
	
	//Character count for message
			$("#message_text").keydown(function(e){
				var charTotal = $("#message_text").val();
				$("#charLeft").html((600 - charTotal.length) + " left.");
			});

		
	// Homepage slideshow fade
		var imgFade = 4;			
			function fadeImage(imgFade){
				// Fade for forward and back buttons
				$('#slideshow').hover(
					function () {
						$('.rightArrow, .leftArrow').fadeTo('fast', 1, function() {
							// Faded
						});
					}, 
					function () {
						$('.rightArrow, .leftArrow').fadeTo('fast', 0, function() {
							//Faded
						});
					}
				);
				// Continuous fade for images
				$('#slideshow div img').eq(imgFade).delay(2000).animate({
		   			opacity: 0
  				}, 2000, function() {
					if(imgFade==1){
						$('#slideshow div img').eq(2).delay(2000).fadeTo(2000, 1, function() {
							$('#slideshow div img').css("opacity", "1");
							$('#slideshow div span.copyright').css("opacity", "1");
							fadeImage(2);
						});	
					} else {
						fadeImage(imgFade-1);
					}
  			});
  			// Click to go forward and back through images
				$('.rightArrow').click(function(){
					fadeImage(2)
				});
				$('.leftArrow').click(function(){
					fadeImage(1)
				});
			}
			fadeImage(imgFade);
	
	// Datepicker
		$('#datepicker').datepicker({inline: true, dateFormat: 'dd/mm/yy'});
		$('#event_date').datepicker({inline: true, dateFormat: 'dd/mm/yy'});
		$('#event_date_text').datepicker({inline: true, dateFormat: 'dd/mm/yy'});
		
	
	//Drop down nav for event editing
	$('.dropDown').hover(function(e){
		//alert("sfdgsdfg");
		$(this).children('#subNav').toggleClass("hide");
	});
	
	// Accordion
	$(function() {
		$("#accordion").accordion();
	});

	$(".faq h2").click(function(e){
		$(this).parent("div").toggleClass("show");
	});

	/* tooltip*/
	/* Form validation to remove error message */
	$("input[type=text]").focus(function(e){
		$(this).addClass("active");
		$(this).siblings(".dd").show();
		$(this).siblings(".dd").addClass("tooltip");
		$(this).siblings("label").addClass("activeLabel");
		if($(this).val() > ""){
			$(this).addClass("errorFixed");
			/*$(this).next("label").addClass("hide");*/
			$(this).removeClass("input-validation-error");
		} else {
			$(this).removeClass("errorFixed");
			//$(this).addClass("input-validation-error");
			/*$(this).next("label").removeClass("hide");*/
		}
		var inputWidth = $(this).width();
	});	
	$("input[type=text]").keyup(function(e){
		if($(this).val() > ""){
			$(this).addClass("errorFixed");
			/*$(this).next("label").addClass("hide");*/
			$(this).removeClass("input-validation-error");
		} else {
			$(this).removeClass("errorFixed");
			//$(this).addClass("input-validation-error");
			/*$(this).next("label").removeClass("hide");*/
		}
		var inputWidth = $(this).width();
	});	
	$("input[type=text]").blur(function(e){
		$(this).removeClass("active");	 
		$(this).siblings(".dd").hide();
		$(this).siblings(".dd").removeClass("tooltip");
		$(this).siblings("label").removeClass("activeLabel");
		if($(this).val() > ""){
			$(this).addClass("errorFixed");
			/*$(this).next("label").addClass("hide");*/
			$(this).removeClass("input-validation-error");
		} else {
			$(this).removeClass("errorFixed");
			//$(this).addClass("input-validation-error");
			/*$(this).next("label").removeClass("hide");*/
		}
	});	
	
	/* Collapse */
	$("h2.collapse").click(function(){
				$(this).next("div.contents").slideToggle('slow');
				$(this).parent().toggleClass('open closed');
		});
	
	/* set initial active state for entry */
			var listLength = $("#tab li").size();
			$("#tab li").children("a").eq(0).addClass("active");
				for(i=0;i<listLength;i++){
					if($("#tab li").eq(i).children("a").attr("class") === "active"){
						$(".tabContent").eq(i).show();
					} else{
						$(".tabContent").eq(i).hide();
					}
				}
			
			$(".disabledLink").live("click", function(e){
			
				return false;
			});
			
			/* Click event for tab */
			$("#tab li a").live("click", function(e){
				$("#tab li a").removeClass("active");
				$(this).addClass("active");
				var listLength = $("#tab li").size();
				for(i=0;i<listLength;i++){
					if($("#tab li").eq(i).children("a").attr("class") === "active"){
						//alert($("#formContent").children("div").eq(i).html());
						$(".tabContent").eq(i).show();
					} else{
						$(".tabContent").eq(i).hide();
					}
				}
				return false;
			});
			
			/* Map grow */
			$("#largeMap").click(function(e){
				$("#largeMap").hide();
				$(".googleMap").hide();
				$("#smallerMap").hide();
				$("#largeGoogleMap").show();
				initialize();
			});	
			$("#smallMap").click(function(e){
				$("#largeMap").show();
				$(".googleMap").show();
				$("#smallerMap").show();
				$("#largeGoogleMap").hide();
			});	
			
			/* Carousel - reusable for marker and non-marker version */
			var imgWidth = $(".news div").width();
			var imgCount = $(".news div").size();
			var imgTotal = imgWidth * imgCount;
			var position = 0;
			$(".content .img").width(imgTotal);
			$(".leftMove").click(function(e){
				position++;
				var newPos = (position * imgWidth);
				$(".news .content .img").animate({marginLeft: "-"+newPos}, 500, function(){});
				if(position+1 === (imgCount)){
					$(".leftMove, .nolinkRight").hide();
					$(".nolinkLeft").show();
				} else {
					$(".nolinkLeft, .nolinkRight").hide();
					$(".leftMove, .rightMove").show();
				}
				$("ul#carouselMarkers li").removeClass("active");
				$("ul#carouselMarkers li").eq(position).addClass("active");
				return false;
			});
			$(".rightMove").click(function(e){
				position--;
				var newPos = (position * imgWidth);
				$(".news .content .img").animate({marginLeft: "-"+newPos}, 500, function(){});
				if(position === 0){
					$(".rightMove, .nolinkLeft").hide();
					$(".nolinkRight").show();
				} else{
					$(".nolinkLeft, .nolinkRight").hide();
					$(".leftMove, .rightMove").show();	
				}
				$("ul#carouselMarkers li").removeClass("active");
				$("ul#carouselMarkers li").eq(position).addClass("active");
				return false;
			});	
			$("ul#carouselMarkers li").click(function(e){
				$("ul#carouselMarkers li").removeClass("active");
				$(this).addClass("active");
				var myItem = ($(this).html())-1;
				var newPosition = (myItem * imgWidth)-6;
				$(".news .content .img").animate({marginLeft: "-"+newPosition}, 500, function(){});
				if(myItem === 0){
					$(".rightMove, .nolinkLeft").hide();
					$(".nolinkRight").show();
				} else{
					$(".nolinkLeft, .nolinkRight").hide();
					$(".leftMove, .rightMove").show();		
				}
				if(myItem === 2){
					$(".leftMove, .nolinkRight").hide();
					$(".nolinkLeft").show();
				}
				return false;
			});

});
