$(document).ready(function() {
	
	$("#navigation li").hover(
		function(){
			$(this).addClass("selected");
			$("ul",this).show();
		},
		function(){
			$(this).removeClass("selected");
			$("ul",this).fadeOut(200);
		}
	);	
	
	$(".new").click(function() {
		window.open($(this).attr("href"));
		return false;
	});
	
	$("#search").focus(function() {
		var val = $(this).val();
		if(val == "Search the site" || val ==  "Chercher sur ce site" || val == "Suche") {
			 $(this).val("");
		}
	});
	
	$("#smallsearch").unbind("focus,blur").focus(function () {		
		if ($(this).val() == $(this)[0].defaultValue) {
        	$(this).val("");
        }
	 }).blur(function() {
        if ($(this).val() == "") {
        	$(this).val($(this)[0].defaultValue);
     	}
	 });
	
	$(".delete").click(function() {	
		result = confirm("Are you sure you want to delete this? Deletion cannot be undone.");
		if(result) {
			return true;
		} else {
			return false;
		}				
	});
			
	$("#notice_title,#notice_body").focus(function() {
		$(this).addClass("sel");
	});		
	$("#notice_title,#notice_body").blur(function() {
		$(this).removeClass("sel");
	});
	
	/* Members Questionnaire */	
	$("#organisationtype").change(function() {
		var type = $(this).find("option:selected").parent().attr("label");
		$(".typeSwitch").hide();
		if(type=="Protected Area") {
			$("#protectedFieldsHolder").show();		
			$("#protectedFieldsHolder input:first").focus();	
		} else {
			$("#publicFieldsHolder").show();
			$("#protectedFieldsHolder textarea:first").focus();
		}			
	});
	
	$(".sameAsMain").click(function() {
		var cID 		= $(this).parent().parent().attr("id");
		if($(this).is(":checked")) {	
			$("#"+cID+" .contactWrapper").hide();		
			var title 		= $("#c1title").val();
			var fname 		= $("#c1firstname").val();
			var sname 		= $("#c1lastname").val();
			var position 	= $("#c1position").val();
			var email 		= $("#c1email").val();
			var tel 		= $("#c1tel").val();
			$("#"+cID+"title option[value='"+title+"']").attr("selected",true);
			$("#"+cID+"title").attr("disabled","disabled")
			$("#"+cID+"firstname").val(fname).attr("disabled","disabled");
			$("#"+cID+"lastname").val(sname).attr("disabled","disabled");
			$("#"+cID+"position").val(position).attr("disabled","disabled");
			$("#"+cID+"email").val(email).attr("disabled","disabled");
			$("#"+cID+"tel").val(tel).attr("disabled","disabled");
		} else {
			$("#"+cID+" .contactWrapper").show();
			$("#"+cID+"title option").attr("selected",false);
			$("#"+cID+"title").attr("disabled",false).focus();
			$("#"+cID+"firstname").val("").attr("disabled",false);
			$("#"+cID+"lastname").val("").attr("disabled",false);
			$("#"+cID+"position").val("").attr("disabled",false);
			$("#"+cID+"email").val("").attr("disabled",false);
			$("#"+cID+"tel").val("").attr("disabled",false);
		}
	})
	
	
	$(".hideshowList").addClass("hidden").click(function(){
		var $h2 = $(this);
		if($h2.hasClass('hidden')) {
			$h2.removeClass("hidden").parent().find('.hideshow').slideDown()
		} else {
			
			$h2.parent().find('.hideshow').slideUp(500,function(){
				$h2.addClass("hidden")
			});
			
		}
		
	})
	
	
	$(".hideshow").hide()
	
});

function g(id,width,height,text) {	
	
	var s = scrolled();
	var v = viewport();	

	$("#galleryPopup").hide();
	var newimg = new Image();
	newimg.onload = function() {
		$("#galleryPopup").css({top:(((v[1]-height)/2)-20+s[1])+"px",marginLeft:"-"+(width/2)+"px",width:(width)+"px"}).html('<img src="'+newimg.src+'" alt="" /><p>'+text+'</p><a href="#" onclick="c();return false" class="close">Close</a>').fadeIn();
	}
	newimg.src = "/uploaded/images/" + id + ".jpg";		
}

function c() {
	$("#galleryPopup").hide();
}


function viewport() {
	var screenX = 0;
	var screenY = 0;
	if(self.innerHeight > 0) {
		screenY = self.innerHeight;
		screenX = self.innerWidth;
	} else if (document.documentElement.clientHeight && document.documentElement.clientHeight > 0) {		
		screenY = document.documentElement.clientHeight;
		screenX = document.documentElement.clientWidth;
	} else {
		screenY = document.body.clientHeight;
		screenX = document.body.clientWidth;
	}
	return new Array(screenX,screenY);
}

function scrolled() {
	if(document.documentElement.scrollTop) {		
		scrollY = document.documentElement.scrollTop;
		scrollX = document.documentElement.scrollLeft;
	} else {
		scrollY = window.pageYOffset;
		scrollX = window.pageXOffset;
	}
	
	scrollY = scrollY == undefined ? 0 : scrollY;
	
	return new Array(scrollX,scrollY);
}	

function SlideShow(imageArray) {
	
	
	var self 		= this;
	this.interval 	= 5000;
	this.position	= 0;
	this.endImage	= imageArray.length-1;	
	this.heroImages	= imageArray;
	this.pause		= false;

	
	this.swapImage = swapImage;
	function swapImage() {
		if(self.pause) {
			setTimeout(function() { self.swapImage(); },self.interval);
		} else {
			self.position++;

			if(self.position > self.endImage) {
				self.position = 0;
			}

			var newimg = new Image();
			newimg.onload = function() {
				$("#hero img").fadeOut(1000,function() {
					$("#overlay").fadeOut(500, function() {
						$("#hero img").attr("src",newimg.src).fadeIn(1000, function() {
							if(self.heroImages[self.position][3] !="") {
								$("#overlay").html('<p>'+self.heroImages[self.position][3]+'</p>').fadeIn();
							}						
							setTimeout(function() { self.swapImage(); },self.interval);
						});							
					});						
				});
			}
			newimg.src = "/uploaded/images/" + self.heroImages[self.position][1] + ".jpg";
		}
	}
				

	// set first callback
	setTimeout(function() { self.swapImage(); },self.interval);
}

