$(function(){ $('img[src$=.png]').ifixpng(); });
$(function(){ $('ul.nav').superfish({
		autoArrows: false,
		dropShadows: false,
		animation: {visible: 'show'}
	});
});

var PROD_HOST = 'kmcmenamin.com';

//this comes straight from prototype: http://www.prototypejs.org
String.prototype.endsWith = function(pattern){
	var d = this.length - pattern.length;
	return d >= 0 && this.lastIndexOf(pattern) === d;
};

$.fn.extend({
	valign: function() {
		return this.each(function() {
			var node = $(this);
			var parent = $(node.parent()[0]);
			var adj = Math.floor((parent.height() - node.height()) / 2);
			node.css({'position': 'relative', 'top': adj + 'px'});		
		});
	},
	resizeToParent: function(expr) {
		return this.each(function() {
			var node = $(this);
			var parent = $(node.parent()[0]);
			
			var nh = node.height();
			var nw = node.width();
			var ph = parent.height();
			var pw = parent.width();

			if(nh > ph || nw > pw)
			{
				var rh = ph / nh;
				var rw = pw / nw;
				var r = rh < rw ? rh : rw;
				node.height(Math.floor(nh * r)).width(Math.floor(nw * r));
			}
		});
	},
	invisible: function() {
		return this.each(function() {
			$(this).css('visibility', 'hidden');
		});
	},
	visible: function() {
		return this.each(function() {
			$(this).css('visibility', 'visible');
		});
	}	
});

//sets up the slideshow after the json call
function createslideshow(node, obj)
{
	var node = $(node);
	//have to hide these, otherwise IE increases the size of the container to accomodate
	//(and messes up the containerheight setting)
	$(obj).each(function(idx){ $('<img/>').attr('src',this).hide().appendTo(node); });
	node.cycle({ 
	    fx:    'fade', 
	    speed:  5000 
	 });
}

//it seems to help to keep global references to these image objects
var imgs = [];
//sets up the gallery after the json call
function creategallery(node, obj)
{
	var thumbnails = $(node + ' .thumbnails');
	var display = $(node + ' .display');
	var prev = $(node + ' .prev');
	var next = $(node + ' .next');
	
	var test_li = thumbnails.find('li:first');
	var c = Math.floor(thumbnails.width() / test_li.width());
	var r = Math.floor(thumbnails.height() / test_li.height());
	var pagesize = r * c;
	thumbnails.empty();
	
	prev.click(function(){
		var list = $(node + ' .thumbnails li');
		var idx = list.index(list.filter(':visible:first')[0]);

		var low = ':gt(' + (idx - pagesize - 1) + ')';
		var high = ':lt(' + idx + ')';
		
		if(idx - pagesize <= 1)
		{
			low = '';
			prev.invisible();
		}
		next.visible();
		
		list.filter(':visible').hide();
		//:lt has to come before :gt
		list.filter(high+low).show();
	});
	
	next.click(function(){
		var list = $(node + ' .thumbnails li');
		var idx = list.index(list.filter(':visible:last')[0]);
		
		var low = ':gt(' + idx + ')';
		var high = ':lt(' + (idx + pagesize + 1) + ')';
		
		if(idx + pagesize >= obj.images.length - 1)
		{
			high = '';
			next.invisible();
		}
		prev.visible();
		
		list.filter(':visible').hide();
		list.filter(high+low).show();
	});
	
	if(pagesize < obj.images.length)
		next.visible();
		
	var loading = display.find('img').valign();	

	$(obj.images).each(function(idx){
		var filename = this;
		var li = $('<li/>').css('display', idx < pagesize ? '' : 'none')
			.appendTo(thumbnails).click(function(){
				$(this).addClass('selected').siblings().removeClass('selected');
				display.find('.photo').remove();
				
				var img = imgs[idx];
				if(typeof(img != 'object'))
					img = getImage(idx);
					
				var photo = $('<img src="' + img.src + '"/>').addClass('photo').hide().appendTo(display);
				insert = function(){
					loading.hide();
					photo.valign().resizeToParent().show();
				};
				if(img.complete)
				{
					insert.call(img);
				}
				else
				{
					loading.show();
					addLoadEvent(img, insert);
				}
			});
			
		$('<img>').attr('src', obj.dir + '/thumbnails/' + filename).appendTo(li);
		imgs[idx] = obj.dir + '/' + filename;

		if(idx == 0)
		{
			li.trigger('click');
		}

	});
	
	//browsers scroll the window on the keydown event,
	//hook into that to block the default action
	$(document).keydown(function(e){
		var selected = $('li.selected');
		
		if(e.which == 37 && selected.prev().length > 0) //left arrow
		{
			if(selected.prev(':visible').length == 0)
				prev.trigger('click');
				
			selected.prev(':last').trigger('click');
		}
		else if(e.which == 39 && selected.next().length > 0) //right arrow
		{
			if(selected.next(':visible').length == 0)
				next.trigger('click');
			
			selected.next(':first').trigger('click');
		}
		
		return false;		
	});	
	
	function getImage(idx)
	{
		if(typeof(imgs[idx]) != 'object')
		{
			//using jQuery object seems to result in inconsistent behavior
			var img = new Image();
			img.src = imgs[idx];
			imgs[idx] = img;
		}
		return imgs[idx];
	}
	
	function preloadImage(idx)
	{
		//find the next that hasn't been loaded
		while(typeof(imgs[idx]) == 'object' && idx < imgs.length)
			++idx;
			
		var img = getImage(idx);
		if(++idx < imgs.length)
			//try to cut down on the callstack by using setTimeout
			addLoadEvent(img, function(){ setTimeout(function(){ preloadImage(idx); }, 1); });
	}

	preloadImage(0);
}

//http://simonwillison.net/2004/May/26/addLoadEvent/
function addLoadEvent(obj, func)
{
	if(typeof(obj.onload) != 'function')
	{
		obj.onload = func;
	}
	else
	{
		var oldonload = obj.onload;
		obj.onload = function(){
			oldonload();
			func();
		}
	}
}