// pushy garnet javascript
// pushygarnet@gmail.com

consoleExists = false;

function debug(text, depth) {
	if (consoleExists != true) {
		$('body').prepend('<div id="debug">');
		$('#debug').css({'z-index':'100000', 'height':'100%', 'width':'100%', 'font-size':'10px', 'display':'none'});
		consoleExists = true;
		debug("debug console ready!");
		debug (text);
	} else {
		var frontpad = '';
		if (depth != null) {
			for (i=0; i<depth; i++) {
				frontpad += ' &nbsp; ';
			}
		}
		if (typeof(text) == 'object') {
			$('#debug').append('object:<br>');
			for (key in text) {
				if (typeof(text[key]) != 'function') {
					$('#debug').append(frontpad+' &nbsp; '+key+':'+text[key]+'<br>');
					if (typeof(text[key]) == 'object') {
						// debug (text[key], depth+1);
					}
				}
			}
		} else {
			$('#debug').append(frontpad+text+'<br>');
		}
	}
}

$(document).ready(function() {
	// start the flipper
	$('#panels').cycle({fx:'shuffle', timeout:0, pager:'#nav', pagerAnchorBuilder:function (idx,slide) {
		return '#nav li:eq(' + idx + ') a';
	}, before:function(from, to) {
		if (from.id == "art") {
			// pause art slideshow
			$('#slideshow').cycle('pause');
		}
	}, after:function(from,to) {
		if (to.id == "art") {
			// unpause art slideshow
			$('#slideshow').cycle('resume');
		}
	}});
	
	slidesToAdd = false;
	
	function addSlides(opts) {
		if (opts.addSlide) {
			if (slidesToAdd) {
				// debug ("let's add some slides!");
				$('#slideadd').children('.imglink').each(function(i) {
					// debug ('adding slide '+i+':'+this);
					wireSlideImage($(this));
					opts.addSlide($(this));
					// opts.addSlide($(this).colorbox({transition:'elastic'}));
				});
				$('#slideadd').children('#next').appendTo($('#slideshow'));
				slidesToAdd = false;
				$('#slideadd').remove();
			}
		}
	}
	
	function wireSlideImage(image) {
		image.click(function() {
			$('#slideshow').cycle('pause');
		});
		image.colorbox({transition:'elastic'});
	}
	
	// get the image list
	$('#slideshow').load("zen/art/", function(){
		// make it start shuffling too
		$(this).
			before('<a href="#" id="prevslide" class="slidenav">prev</a>').
			after('<a href="#" id="nextslide" class="slidenav">next</a>').
			cycle({fx:'shuffle', shuffle:{top:-30, left:-470}, prev:'#prevslide', next:'#nextslide', slideExpr:'.imglink', before:function(curr, next, opts, forwards) {
				// load up more slides
				if (($(next).next().hasClass('pagenav'))) {
					slidestack = [];
					// debug ("load more!");
					nextPage = ($(curr).nextAll().filter('#next').attr('href'));
					// debug ("loading "+nextPage);
					$(curr).siblings('.pagenav').remove();
					$(curr).parent().after('<div id="slideadd">');
					$('#slideadd').css({'display':'none'}).load(nextPage, function(data, status) {
						// debug ("recieved data, "+status);
						slidesToAdd = true;
					});
				};
				addSlides(opts);
			}, after:function(curr, next, opts, forwards) {
				addSlides(opts);
			}});
		$('#slideshow').cycle('pause');	// start paused
		// wire up popups
		wireSlideImage($("#slideshow a"));
	});
	
})
