// Stop page going off the top of the screen when resized
function topBlocker(){
	var h = $(window).height();
	var w = $(window).width();
	var wr = $('#horizon');
	var pg = $('#wrapper');

	(h <= 580 ? wr.addClass('halt') : wr.removeClass('halt'));
	(w <= 960 ? pg.addClass('halt') : pg.removeClass('halt'));
};

// Remove titles to prevent hover tooltips
function removeTitles(){
	$('a[title!=blah]').attr('title','');
	$('img[title!=blah]').attr('title','');
};

// Write out email
function em(where,domain){
	var domain = (domain ? domain : 'gmail');
	var link =  where + "@" + domain + "." + "com";
	document.write("<a hre" + "f=ma" + "ilto:" + link + ">" + link + "</a>"); 	
};

function startGallery(){
	$('#gallery_navigation div').hide();
	$('#caption').hide();
	
	$('#position #total').text($('.gallery img').size());
	$('.gallery li').each(function(index, elem){
		$(elem).addClass('image_'+(index+1));
	});
	$('.gallery ul').cycle({
		fx: 'fade',
		next: '#next',		
		prev: '#previous',
		before: function(currSlideElement, nextSlideElement, options) {
			// Get caption
			$('#caption').text($(nextSlideElement).children('a').children('img').attr('alt'));
			// Get number from filename
			var curr = $(nextSlideElement).attr('class').replace('image_','');
			$('#position #current').text(curr);
		},
		timeout: 0,
		speed: 2000
	});

	if($('#home.home').size() == 0){	
	$('.gallery a').click(function(e){
			e.preventDefault();
			var total = $('.gallery img').size();
			var nxt = parseInt($(this).parent().attr('class').replace('image_',''))+1;
			var adv = (nxt > total ? 0 : nxt-1);
			$('.gallery ul').cycle(adv);			
	});
	}	

	// $('.gallery img:first').one('load', function(e) {
	// 	$('.gallery').fadeIn(3000);
	// }).each(function(){
	// if(this.complete || (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6)) 
	// 	$(this).trigger("load");
	// });
}

// Use first images title as caption
function populateCaption(){
	$('#caption').text($('.gallery img:first').attr('alt'));
};

function removeLoader(){
	$('body').addClass('loaded');
	$('#gallery_navigation div').show();	
	$('#caption').show();	
};

$(document).ready(function() {	
	topBlocker();
	populateCaption();
	startGallery();
	removeTitles();	
});

$(window).resize(function(){
	topBlocker();
});

$(window).load(function(){
	removeLoader();
});
