var b = false;
var timer;
function scaleDown(el){
    var w = el.width;
    var h = el.height;
    var scaleH = 200 / h;
    var scaleW = 150 / w;
    if(scaleH < scaleW){
		el.height = 200;
		el.width = Math.round(w * scaleH);
    }else{
		el.height = Math.round(el.height * scaleW);
	 	el.width = 150;
    }
}	

jQuery.fn.lrcenter = function () {
    this.css("position","absolute");
    //this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
	    return this;
	}
jQuery.fn.tbcenter = function () {
    this.css("position","absolute");
    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
	return this;
	}
			
$(document).ready(function() {		
	$('.controls').fadeTo(0, 0.0);
	$('.shows').fadeTo(0, 0.0).hide();
	$('#stop').click(function(){
		clearInterval(timer);	
	});
	$('#play').click(function(){
		timer = setInterval('photoGallery()',5000);			
	});
	$('#stop').click(function(){
		clearInterval(timer);	
	});
	$('#books').click(function(){
		clearInterval(timer);	
	    $('#grayout').width($(document).width());
	    $('#grayout').height($(document).height());
	    $('.loadbar').fadeTo(0, 0.0);
		$('.controls').fadeTo(0, 0.0);
	    $('#grayout').fadeTo(1000, .40);
	    $('#shows').animate({opacity: 0.0}, 1000, function(){
	    	$('#shows').hide();
	    	$('#slideShowContainer').html("");
	        $('.showLinks').show().animate({opacity: 1.0}, 1000, function(){ 
	            $('#grayout').fadeTo(1000, 0.0).hide();       	
	        });
	    });
	});
	$('#stop,#play,#books').hover(function(){
		$(this).addClass('ui-state-highlight');
		$(this).css("background-color", "#505050");
	}, function(){
		$(this).removeClass('ui-state-highlight');
		$(this).css("background-color", "white");
	});
	
	$('.imgthumbs > img').each( function(i){
			scaleDown(this);
		}
	)
	$('#show').click(function(){
		$('.imgthumbs').animate({opacity: 'show', top: '-=150'}, 'slow');
	});
});

function photoSlideShow(timeout) {
	if(b){
		$('ul.slides').append('<li id="slides-info" class="info"><div class="slides-info-container"><h3></h3><p></p></div></li>');
		$('ul.slides li').css({opacity: 0.0});
		$('ul.slides li:first').css({opacity: 1.0});
		$('#slides-info h3').html($('ul.slides img').attr('title'));
		$('#slides-info p').html($('ul.slides img').attr('alt'));
		$('#slides-info').css({opacity: 0.7, bottom:0});
	}
	timer = setInterval('photoGallery()',timeout);
}
function photoGallery() {
	var current = ($('ul.slides li.active') ?  $('ul.slides li.active') : $('#ul.slides li:first'));
	var next = ((current.next().length) ? ((current.next().attr('id') == 'slides-info') ? $('ul.slides li:first') :current.next()) : $('ul.slides li:first'));
	var title = next.find('img').attr('title');	
	var desc = next.find('img').attr('alt');	
	next.css({opacity: 0.0}).addClass('active').animate({opacity: 1.0}, 1000);
	if(b){
		$('#slides-info').animate({bottom:-70}, 300, function () {
				//Display the content
				$('#slides-info h3').html(title);
				$('#slides-info p').html(desc);				
				$('#slides-info').animate({bottom:0}, 500);	
		});		
	}
	current.animate({opacity: 0.0}, 1000).removeClass('active');
}
