var gallery = {
	slideshow_timeout : 7000,
	slideshow : null,
	current : 1,
	all : 0,

	show : function( no ){
		if( no == gallery.current ) return 0;
		if( gallery.all < 2 ){
			clearInterval( gallery.slideshow );
			$('#hb_controll').hide();
			return;
		}
		if( isNaN( no ) ){
			if( gallery.current >= gallery.all )
				no = 1;
			else
				no = gallery.current+1;
		}else{ // restart counting after hand-selecting image
			clearInterval( gallery.slideshow );
			gallery.slideshow = null;
			gallery.slideshow = setInterval( 'gallery.show()', gallery.slideshow_timeout );
		}

		$( '#hb_preview .hb_gallery_image:nth-child(' + no +')' ).stop(true,false).css('opacity', 1).fadeIn('slow');
		$( '#hb_preview .hb_gallery_image:nth-child(' + gallery.current +')' ).stop(true,false).css('opacity', 0).fadeOut('slow');
		var height = $( '#hb_preview .hb_gallery_image:nth-child(' + no +') .hb_img_prev' ).height();
		if($( '#hb_preview .hb_gallery_image:nth-child(' + no +') .hb_img_prev' ).is('.even')) {
			$( '#hb_preview .hb_gallery_image:nth-child(' + no +') .hb_img_prev' ).css('top', 0);
			$( '#hb_preview .hb_gallery_image:nth-child(' + no +') .hb_img_prev' ).animate({top : -(height-400)}, gallery.slideshow_timeout, 'linear');
		} else if($( '#hb_preview .hb_gallery_image:nth-child(' + no +') .hb_img_prev' ).is('.odd')) {
			$( '#hb_preview .hb_gallery_image:nth-child(' + no +') .hb_img_prev' ).css('top', -(height-400));
			$( '#hb_preview .hb_gallery_image:nth-child(' + no +') .hb_img_prev' ).animate({top : 0}, gallery.slideshow_timeout, 'linear');
		}
		$( '#hb_miniature img:nth-child(' + no +')' ).css( 'border-color', '#3359c6' );
		$( '#hb_miniature img:nth-child(' + gallery.current +')' ).css( 'border-color', 'transparent' );
		gallery.current = no;
	},
	slideshowControll : function(action ){
		if( (gallery.slideshow == null && !action) || action=='start'){
			gallery.slideshow = setInterval( 'gallery.show()', gallery.slideshow_timeout );
			$( '#hb_controll' ).css( 'background-image', 'url(/hb/img/offers/playpause_0.gif)' );
		}else if(( gallery.slideshow != null && !action) || action=='stop') {
			clearInterval( gallery.slideshow );
			gallery.slideshow = null;
			$( '#hb_preview .hb_gallery_image:nth-child(' + gallery.current +') .hb_img_prev' ).stop(true,false);
			$( '#hb_controll' ).css( 'background-image', 'url(/hb/img/offers/playpause_1.gif)' );
		}
	}
}
$( document ).ready( function(){
	$( '#hb_controll' ).click( function(){ gallery.slideshowControll( '' ); } );
	gallery.slideshow = setInterval( 'gallery.show()', gallery.slideshow_timeout );
	$( '#hb_preview .hb_gallery_image:even .hb_img_prev').addClass('even');
	$( '#hb_preview .hb_gallery_image:odd .hb_img_prev').addClass('odd');
	var imgArr = new Array();
        var imgLoaded = 0;
        $( '#hb_preview .hb_gallery_image .hb_img_prev' ).each(function(index, elem){
            imgArr[index] = new Image();
            imgArr[index].src = $(elem).attr('src');
            imgArr[index].onload = function(){
                imgLoaded++;
                if(imgLoaded == $( '#hb_preview .hb_gallery_image .hb_img_prev' ).length) {
                    $( '#hb_preview .hb_gallery_image:first .hb_img_prev' ).animate({top : -($( '#hb_preview .hb_gallery_image:first .hb_img_prev' ).height()-400)}, gallery.slideshow_timeout, 'linear');
                }
            };
        });
	
	
	$( '#hb_miniature img').each( function( no, img ){
		if( no == 0 )
			$( img ).css( 'border-color', '#3359c6' );
		$( img ).click( function(){ gallery.show( no+1 , img ); } );
		gallery.all = $( '#hb_miniature img' ).length;
	} );
} );
