$(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");
	});
});

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 cs(image,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 = image;		
}

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 = self.heroImages[self.position][6];
		}
	}
				

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