$(document).ready(function() {
	Cufon.replace('h1')('#rightCol h5')
	$('#navigation li:has(ul)').hover(
		function() { $('ul', this).css('display', 'block'); },
		function() { $('ul', this).css('display', 'none'); }
	);
	
// GALLERY SCRIPTS
	if ($('#mainImg').length >  0) {
		//preload images
		$('.ngg-gallery-thumbnail a').each(function(){
			$('<img/>')[0].src = $(this).attr('href');
		});
		//hide title bar if there is no title
		if ($('.ngg-gallery-thumbnail img').attr('title') == ' ') {
			$('#imgTitle').hide();
		}
		//start the gallery
		firstImage = $('.ngg-gallery-thumbnail a:first');
		lastImage = $('.ngg-gallery-thumbnail a:last');
		transition(firstImage);
		autoTransition();
	}
});

jQuery.fn.center = function () {
	this.css("position","absolute");
	this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
	this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
	return this;
}

function transition(newLink) {
	$('.ngg-gallery-thumbnail a').removeClass('selected');
	newLink.addClass('selected');
	$('#imgContainer img').hide().attr('src',newLink.attr('href')).fadeIn(800);
	
	var imgTitle = newLink.children('img').attr('title');
	$('#imgTitle').html(imgTitle);
	$('#imgContainer img').attr('title',imgTitle);
	
	var imgCaption = newLink.attr('title');
	$('#imgContainer').attr('title',imgCaption);
	$('#imgCaption').html(imgCaption);
	
	$('#imgContainer').attr('href', newLink.attr('href'));
}

function autoTransition() {
	intervalHandle = setInterval(function() {
		if (lastImage.hasClass('selected')) {  
			transition(firstImage);
		} else {
			nextImage = $('.ngg-gallery-thumbnail a.selected').parents('.ngg-gallery-thumbnail-box').next().find('a');
			transition(nextImage);
		}
	}, 6000);
}

$('#mainImg').hover(function() {
	clearInterval(intervalHandle);
}, function() {
	autoTransition();
});



////   Popup scripts  ////
function popupLoad() {
	//Get the screen height and width
	var maskHeight = $(document).height();
	var maskWidth = $(window).width();
	$('#mask').css({'width':maskWidth,'height':maskHeight}).animate({opacity:0.8},600);
	$('#imgPreload').center().show();
}
function popupStart() {
	$('#popupImg img').load(function() {
		$('#popupImg').width($(this).width());
				
		$('#imgPreload').hide();
		$('#popupImg').center().animate({opacity:1},600);
	});
}

//sidebar galleries
$('#imgContainer, .ngg-gallery-thumbnail a').click(function(e) {
	$('#mask, #popupImg').css({'opacity':0,'visibility':'visible'});
	e.preventDefault();
	popupLoad();	
	var linkArray = $(this).attr('href').split('/');	
	$('#popupImg').html('<img src="http://www.canning-studios.com/wp-content/gallery/'+linkArray[5]+'/large/'+linkArray[6]+'" height="'+$(window).height()*0.7+'" /><p><strong>'+$(this).children('img').attr('title')+'</strong></p><p>'+$(this).attr('title')+'</p><div id="closeBtn"></div>');	
	popupStart();
});

//close popup
$('#mask, #popupImg').click(function () {
	$('#mask, #popupImg').animate({'opacity':0},600);
	setTimeout('$("#mask, #popupImg").css({"visibility":"hidden"})', 600);
});	

//blog post galleries
$('.gallery-item a').click(function(e) {
	$('#mask, #popupImg').css({'opacity':0,'visibility':'visible'});
	e.preventDefault();
	popupLoad();	
	$('#popupImg').html('<img src="'+$(this).attr('href')+'" height="'+$(window).height()*0.7+'" /><p>'+$(this).children('img').attr('alt')+'</p><div id="closeBtn"></div>');	
	popupStart();
});
