function NewsGallery_OnLoad()
{
	$(this).NewsGallery.Init();
}

(function ($)
{
	this.SelectedIndex	= -1;
	this.ItemWidth		= 0;
	this.ItemHeight		= 0;
	this.ItemsCount		= 0;
	this.IntervalID		= 0;
	this.RotationTime	= 3000;
	this.FadeTime		= 1000;
	
	$.fn.NewsGallery = function()
	{
		this.Init			= function()
		{
			// Hide all the news
			$("#NewsGallery").hover(NewsGallery_OnMouseOver, NewsGallery_OnMouseOut);
			//$("#NewsGallery #NewsItemContainer").hide();
			//$("#NewsGallery #NewsItemContainer > div").hide();
			$("#NewsGallery #NewsItemContainer > div:gt(0)").hide();
			NewsGallery_ShowItem(0);
			//$("#NewsGallery #NewsItemContainer > div:eq(0)").addClass("selected");
			$("#NewsGallery #NewsListContainer div:eq(0)").addClass("selected");
			// Modify the item containers of the news
			/*
			$("#NewsGallery #NewsItemContainer .TextContainer").height($("#NewsGallery #NewsItemContainer").height()
																	- $("#NewsGallery #NewsItemContainer .ImageContainer").height()
															);
			*/
			// Modify the image containers of the news
			$("#NewsGallery #NewsItemContainer").width($("#NewsGallery").width() - 
													$("#NewsGallery #NewsListContainer").width()
													- GetHorizontalSpace($("#NewsGallery #NewsListContainer"))
													);
			//$("#NewsGallery #NewsItemContainer .ImageContainer img").load(Image_OnLoad);
			// Modify the newslist items to fit vertically
			/*
			$("#NewsGallery #NewsListContainer div").height($("#NewsGallery").height() / this.ItemsCount - GetVerticalSpace($("#NewsGallery #NewsListContainer div")));
			// the last news must have the height adjustable
			$("#NewsGallery #NewsListContainer div:last").height($("#NewsGallery").height() -
																(
																	$("#NewsGallery #NewsListContainer div:first").height()
																	+ GetVerticalSpace($("#NewsGallery #NewsListContainer div:first"))
																) * (this.ItemsCount - 1)
																- GetVerticalSpace($("#NewsGallery #NewsListContainer div:last"))
																+ ParseInt($("#NewsGallery #NewsListContainer div:first").css("margin-top"))
																);
			$("#NewsGallery #NewsListContainer div:first").css("margin-top", "0px");
			*/
			$("#NewsGallery #NewsListContainer div").click(NewsItem_OnClick).hoverIntent(NewsItem_OnMouseOver, NewsItem_OnMouseOut);
			this.IntervalID		= setInterval(NewsGallery_ShowNextItem, this.RotationTime);
			$("#NewsGallery").css("visibility", "visible");
		};

		function NewsGallery_OnMouseOver()
		{
			// Stop doing rotation
			clearInterval($(this).NewsGallery.IntervalID);
			$(this).NewsGallery.IntervalID		= 0;
			return true;
		};

		function NewsGallery_OnMouseOut()
		{
			$(this).NewsGallery.IntervalID		= setInterval(NewsGallery_ShowNextItem, $(this).NewsGallery.RotationTime); 
			return true;
		};

		function NewsGallery_ShowNextItem()
		{
			// Show next item number
			NewsGallery_ShowItem(($(this).NewsGallery.SelectedIndex + 1) % $(this).NewsGallery.ItemsCount);
		};
		
		function NewsGallery_ShowItem(id)
		{
			if ($(this).NewsGallery.SelectedIndex == id) return;
			$(this).NewsGallery.SelectedIndex	= id;
			$("#NewsGallery #NewsListContainer div.selected").removeClass("selected");
			$("#NewsGallery #NewsListContainer div:eq(" + $(this).NewsGallery.SelectedIndex + ")").addClass("selected");
			$("#NewsGallery #NewsItemContainer > div.selected").removeClass("selected").fadeOut($(this).NewsGallery.FadeTime, NewsItem_Hide);
			$("#NewsGallery #NewsItemContainer > div:eq(" + $(this).NewsGallery.SelectedIndex + ")").fadeOut(0, NewsItem_Show).addClass("selected");
		};
		
		function NewsItem_Hide()
		{
//			$("#NewsGallery #NewsItemContainer > div:eq(" + $(this).NewsGallery.SelectedIndex + ")").fadeOut(0, NewsItem_Show).addClass("selected");
		};
		
		function NewsItem_Show()
		{
			$(this).fadeIn($(this).NewsGallery.FadeTime);
		}
		
		function NewsItem_OnClick()
		{
		//	alert(this.id);
		};
		
		function NewsItem_OnMouseOver()
		{
			NewsGallery_ShowItem(parseInt(this.id));
		};
		
		function NewsItem_OnMouseOut()
		{
		};
		
		function Image_OnLoad()
		{
			var	containerWidth	= $(this).parent("div").width();
			var	containerHeight	= $(this).parent("div").height();
			var	imageWidth		= $(this).width();
			var	imageHeight		= $(this).height();
			var	width			= 0;
			var	height			= 0;
			var	resize			= false;
			if (imageWidth > containerWidth)
			{
				width	= containerWidth;
				height	= imageHeight * (width / imageWidth);
				resize	= true;
			}
			if (height > containerHeight)
			{
				height	= containerHeight;
				width	= imageWidth * (height / imageHeight);
				resize	= true;
			}
			
			if (resize)
			{
				$(this).width(width);
				$(this).height(height);
			}
			$(this).css("left", ($(this).parent("div").width() - $(this).width()) / 2 + ParseInt($(this).parent("div").css("padding-left")));
			$(this).css("top", ($(this).parent("div").height() - $(this).height()) / 2 + ParseInt($(this).parent("div").css("padding-top")));
			$(this).css("visibility", "visible");
		};
		
		function GetVerticalSpace(obj)
		{
			return	ParseInt(obj.css("padding-top"))
					+ ParseInt(obj.css("padding-bottom"))
					+ ParseInt(obj.css("border-top-width"))
					+ ParseInt(obj.css("border-bottom-width"))
					+ ParseInt(obj.css("margin-top"))
					+ ParseInt(obj.css("margin-bottom"));
		};
		
		function GetHorizontalSpace(obj)
		{
			return	ParseInt(obj.css("padding-left"))
					+ ParseInt(obj.css("padding-right"))
					+ ParseInt(obj.css("border-left-width"))
					+ ParseInt(obj.css("border-right-width"))
					+ ParseInt(obj.css("margin-left"))
					+ ParseInt(obj.css("margin-right"));
		};

		return this;
	} ();

}) (jQuery);
