var currentImage;
var currenthref;
var thumbIndex;

function innitGallery()
{

	if (document.getElementById('maingalleryimage') != null) 
	{
		var heightCurrent = document.getElementById('maingalleryimage').offsetHeight;
		currentImage = $('div.thumbs a:eq(0)').get(0);

		$('#maingalleryimage').load(function() { 
			$('#mainpiccontainer').height(this.offsetHeight);  
			$(this).unbind();
			});
	
		$("a.gallerylink").click(function(){
	
			var t = this.title;
		
			currentImage = this;
			
			showImage(t,this.href);
	
			this.blur();
		
			return false;
	
		});
		
		$("a.prevthumbscontrolbutton").click(function(){
	
			currenthref = currentImage.href;
	
			$('div.thumbs a').each(function (i) {
				
				if (this.href == currenthref)
				{
					selectImage(i-1);
				}
			});
	
			return false;
			
		});
		
	
		$("a.nextthumbscontrolbutton").click(function(){
			
			currenthref = currentImage.href;
			
			$('div.thumbs a').each(function (i) {
				
				if (this.href == currenthref)
				{
					selectImage(i+1);
				}
			});
			
			return false;
	
		});
	}
}


function selectImage(i)
{
	if (i >= 0 && i < $('div.thumbs a').length)
	{

		var thisimage = $('div.thumbs a:eq('+i+')').get(0);
		currentImage = thisimage;
		showImage(thisimage.title, thisimage.href);
	}
}

function showImage(title, href)
{
			
			
			imgPreloader = new Image();
			
			$("#maingalleryimage").hide(); // hide the image
			
			// once image is preloaded, resize image container
			imgPreloader.onload=function(){
				
				document.getElementById('maingalleryimage').src = imgPreloader.src;
				
				centerThumbs();
				
				resizeImageContainer(imgPreloader.height);
				
				//$(".mainpiccontainer").empty().append('<img id="maingalleryimage" src="'+href+'" alt="'+title+'"/>');
				
				
				
			}
			imgPreloader.src = href;
	
}

function resizeImageContainer(imgHeight) {

		// get curren width and height
		var heightCurrent = document.getElementById('mainpiccontainer').offsetHeight;

		// get new width and height
		var heightNew = imgHeight;

		// scalars based on change from old to new
		yScale = ( heightNew / heightCurrent) * 100;

		// calculate size difference between new and old image, and resize if necessary
		hDiff = heightCurrent - heightNew;

		$("#mainpiccontainer").animate({height: heightNew},400,'linear',function(){
			$("#maingalleryimage").fadeIn();
		});


		// if new and old image are same size and no scaling transition is necessary, 
		// do a quick pause to prevent image flicker.
		//if((hDiff == 0) && (wDiff == 0)){
		//	if (navigator.appVersion.indexOf("MSIE")!=-1){ Lightbox.pause(250); } else { Lightbox.pause(100);} 

}

function centerThumbs()
{
	// we have the current thumb, so if it's not first, or last, we need to make sure it's
	// in the middle.  This is about counting how far in it is basically.
	
	// count how far in it is!
	if (currentImage != null)
	{
		thumbIndex = 0;
		var numThumbs = $('div.thumbs a').each(function (i) {
			if (this.href == currentImage.href)
			{
				thumbIndex = i;
			}
		}).length;
	
		if (numThumbs>3)
		{
			if (thumbIndex <= 1) 
			{
				var intMove = 0;
			}
			else if (thumbIndex > (numThumbs - 3) && numThumbs > 3)  
			{
				var intMove = ((numThumbs-3)*133);
			}
			else 
			{
				var intMove = ((thumbIndex-1)*133);
			}
			$('div.thumbslider').animate({left: -intMove}, "fast");
			
		}
		$('div.whichpage span.thispage').text((thumbIndex+1));
	}
	
}


function innitNews()
{
	if (document.getElementById('homenewsblock') != null) 
	{
			
		$("#morenews-next").click(function(){

			doMoveNews(-1);
			return false;
			
		});

		$("#morenews-prev").click(function(){

			doMoveNews(1);
			return false;
			
		});

		
	}
}

function doMoveNews(intIncrement)
{
	
			//alert($("#newsitemsslider").position().left);
			var strLeft = $("#newsitemsslider").css('left');
			if (strLeft=='auto') strLeft = '0px';
			var intLength = strLeft.substring(0,(strLeft.length  - 2))-0;
			
			var oldOffset = Math.floor(intLength/330);
			var newOffset = oldOffset + intIncrement;

			var maxOffset = ($("#newsitemsslider div.newscouple").length * -1);
			if (newOffset <= 0 && 
					newOffset > maxOffset)
			{
				
				$("#newsitemsslider").animate({left: (330 * newOffset) }, "fast");
			
			}
				
}


function innitBlog()
{
	if (document.getElementById('homeblogblock') != null) 
	{
			
		$("#moreblogs-next").click(function(){

			doMoveBlog(-1);
			return false;
			
		});

		$("#moreblogs-prev").click(function(){

			doMoveBlog(1);
			return false;
			
		});

		
	}
}


function doMoveBlog(intIncrement)
{
	
			//alert($("#newsitemsslider").position().left);
			var strLeft = $("#blogitemsslider").css('left');
			if (strLeft=='auto') strLeft = '0px';
			var intLength = strLeft.substring(0,(strLeft.length  - 2))-0;
			
			var oldOffset = Math.floor(intLength/330);
			var newOffset = oldOffset + intIncrement;

			var maxOffset = ($("#blogitemsslider div.blogitem").length * -1);
			if (newOffset <= 0 && 
					newOffset > maxOffset)
			{
				
				$("#blogitemsslider").animate({left: (330 * newOffset) }, "fast");
			
			}
				
}
