// CONFIGURATION VARIABLES
var fade_in_content = true; // This will fade in whatever content is in the shelf
var gfx_on = "images/shelf/open.";
var gfx_off = "images/shelf/close.";
var open_wait = .5; // How many seconds till we auto-open the shelf?
var close_wait = 20; // How many seconds till we auto-close the shelf?
var timer_override = false; // This is set to true if the user opens/closes the shelf.

// NO NEED TO EDIT BELOW HERE
var shelf_wait_id = 0;
var shelf_content_height = 0;
var shelf_state = 0;
var extension='png';

$(document).ready(function(){
	open_wait = open_wait*1000;
	close_wait = close_wait*1000;
	//shelf_wait_id = setTimeout(AutoOpen, open_wait);
	if(fade_in_content) { $('.shelf-content').hide(); }
	$('.shelf-handle').click(function(){
		timer_override=true;
		ToggleShelf();
	});

	if($.browser.name=='msie' && $.browser.versionX==6) { extension='png'; }
	gfx_on = gfx_on + extension;
	gfx_off = gfx_off + extension;
	
	$('.shelf-handle').hover(function(){
		if(shelf_state == 0) {
			$(this).css('background-image','url(images/shelf/open_on.' + extension + ')');
		} else {
			$(this).css('background-image','url(images/shelf/close_on.' + extension + ')');
		}
	},function(){
		if(shelf_state == 0) {
			$(this).css('background-image','url(images/shelf/open.' + extension + ')');
		} else {
			$(this).css('background-image','url(images/shelf/close.' + extension + ')');
		}
	});
});

function ToggleShelf() {
	shelf_content_height = $('.shelf-content').height() + 20;
	footer_height = $('.footer').position().top;
	if(shelf_state == 0) {
		$('.shelf-wrapper').css('border-top','1px solid #a6a6a6');
		$('.shelf-handle').css('background-image','url(' + gfx_off + ')');
		$('.shelf-wrapper').animate({
			height: '+=' + shelf_content_height
		}, 500, function() {
			if(fade_in_content) { $('.shelf-content').fadeIn(); }
		});
		shelf_state = 1;
	} else {
		$('.shelf-handle').css('background-image','url(' + gfx_on + ')');
		$('.shelf-wrapper').animate({
			height: '-=' + shelf_content_height
		}, 500, function() {
			$('.shelf-wrapper').css('border-top','none');
			if(fade_in_content) { $('.shelf-content').hide(); $('.shelf-wrapper').css('height','0px');  }
		});
		shelf_state = 0;
	}
}

function AutoOpen() {
		if(!timer_override) {
			ToggleShelf();
			shelf_wait_id = setTimeout(AutoClose, close_wait);
		} else { clearTimeout(shelf_wait_id); }
}

function AutoClose() {
	if(!timer_override) {
		ToggleShelf();
		clearTimeout(shelf_wait_id);
	} else { clearTimeout(shelf_wait_id); }
}
