$(function() {
	if ($('.cake-sql-log').length > 0) {
		$('.cake-sql-log').hide();
		$('<a href="#">Toggle Sql Log<\/a>')
			.insertBefore($('.cake-sql-log'))
			.click(function() {
				$('.cake-sql-log').toggle();
				return false;
			});
	}

	window.setTimeout(function() {
		$('ul.messages').fadeOut(function() {
			$('ul.messages').remove();
		});
	}, 5000);
});

function booksNav_initCallback(carousel) {
	$('#booksNav-next').bind('click', function() {
		carousel.next();
		return false;
	});

	$('#booksNav-prev').bind('click', function() {
		carousel.prev();
		return false;
	});

	$(document).keydown(function(e) {
		switch (e.keyCode) {
			case 38: // up arrow
				carousel.prev();
				break;
			case 40: // down arrow
				carousel.next();
				break;
		}
	});
};

//code for image gallery.
var jessesGridMagic = jessesGridMagic || {
	init: function(name) {
		$('#gridThumbs li').mouseenter(function() {
			$('#gridThumbs li').removeClass('active');
			$(this).addClass('active');
			jessesGridMagic.swapImage($(this));
		});
		$('#gridThumbs:first-child').addClass('active');
	},

	swapImage: function(caller) {
		var newImage = $(caller).children('a').first().attr('href');
		var newTitle = $(caller).children('a').first().children('img').attr('alt');
		$('#jessesMagicGridContent img').attr('src', newImage);
		$('#jessesMagicGridContent p').html(newTitle);
	}
}

function init_dw_Scroll() {
	var wndo = new dw_scrollObj('wn', 'lyr1', 't1');
	wndo.setUpScrollbar("dragBar", "track", "h", 1, 1);
	wndo.setUpScrollControls('scrollbar');
}

// if code supported, link in the style sheet and call the init function onload


$(document).ready(function() {
	if (typeof dw_scrollObj != 'undefined')	{
		if (dw_scrollObj.isSupported()) {
			//dw_Util.writeStyleSheet('css/scroll.css');
			dw_Event.add( window, 'load', init_dw_Scroll);
		}

		Shadowbox.init({ 
			modal: true,
			onOpen: function() {
				$('#sb-overlay').click(function() { 
					if (Shadowbox.isOpen()) {
						Shadowbox.close();
					}
				});
			}
		});
		var selectedPortfolio = $('#booksNav li.active').index() + 1;//scroller is not 0 based

		$('li', '#booksNav').each(function() {
			$(this).css('height', $(this).height());
		});

		$('#booksNav').jcarousel({
			start: selectedPortfolio,
			vertical: true,
			scroll: 1,
			wrap: 'both',
			initCallback: booksNav_initCallback,
			buttonNextHTML: null,
			buttonPrevHTML: null
		});
		jessesGridMagic.init();
	}
});

