(function($) {
	$.fn.hzgallery = function(settings) {
		var config = {
			'next': 'next',
			'prev': 'prev',
			'thumbsPerPage': 3,
			'currentPage': 1,
                        'currentImage' : 1
		};

		$.extend(this, {
			getCurrentPage: function() {
				return this.currentPage;
			},

                        getCurrentImage: function() {
                                return this.currentImage;
                        },
			getNumImages: function() {
				c = 0;
				this.find('ul.thumbs > li').each(function() {
					c += 1;
				});
				
				return c;
			},
			
			getNumPages: function() {
				return Math.ceil(this.getNumImages() / this.thumbsPerPage);
			},
			
			nextPage: function() {
				if(this.getCurrentPage() < this.getNumPages()) {
					this.currentPage += 1;
				}

				this.updateThumbs(true);
			},
			
			prevPage: function() {
				if(this.getCurrentPage() > 1) {
					this.currentPage -= 1;
				}

				this.updateThumbs(false);
			},

                        nextImage: function() {
                            if(this.getCurrentImage() < this.getNumImages()) {
                                this.currentImage += 1;
                            }

                            this.updateImage();
                        },

                        prevImage: function() {
                            if(this.getCurrentImage() > 1) {
                                this.currentImage -= 1;
                            }

                            this.updateImage();
                        },

                        updateImage: function() {
                            a = this.find('ul.thumbs #' + this.getCurrentImage());
                            imghref= a.attr('href');
                            isTall = a.find(".tall").length > 0;

                            biw = $('.bigimage').width();
                            bih =$('.bigimage').height();
                            if(isTall) {
                                biw = Math.floor(bih * 0.8);
                            }
                                    
                            img = '<img src="' + imghref + '" style="width:' + (biw) + ';height:' + (bih) + ';">';
                            $('.bigimage').html(img);
                            hzgal.load_big_image();
                        },

			updateThumbs: function(isNext) {
				var page = this.getCurrentPage() - 1;
				var start = page * this.thumbsPerPage;
				var stop = start + this.thumbsPerPage - 1;
				var el = [];
				
				$thumbsUl = this.find('ul.thumbs');
				$thumbsUl.find('li').each(function(i) {
					var $li = $(this);
					
					if (i >= start && i <= stop) {
						el.push($li.find('img'));
                                                $li.show();
					} else {
						$li.hide();
                                                $li.find('img').hide();
					}
				});

                                if(!isNext) { el.reverse(); }
				this.showThumbs(el);
				
				this.updatePager();
			},

			showThumbs: function(jq) {
				st = this;
				jq[0].fadeIn(100, function() {
					(jq=jq.slice(1)).length && st.showThumbs(jq);
				});
			},
			
			hideThumbs: function(jq) {
				st = this;
				jq[0].fadeOut(100, function() {
					(jq=jq.slice(1)).length && st.hideThumbs(jq);
				});
			},

			updatePager: function() {
				this.find('.pagination').remove();

				gallery = this;
                                pager = gallery.find('#pagination');

				var pagination = '<div class="pagination">';

				if(this.getCurrentPage() > 1) {
					pagination += '<a href="#" class="prevPage"><img src="/themes/hanszeuthen/images/arrow_prev.png"></a>';
				} else {
                                    pagination += '<img style="verical-align:middle;" src="/themes/hanszeuthen/images/arrow_prev.png">';
                                }

//				pagination += ' ' + this.getCurrentPage() + ' of ' + this.getNumPages();
				
				if(this.getCurrentPage() < this.getNumPages()) {
					pagination += ' <a href="#" class="nextPage"><img src="/themes/hanszeuthen/images/arrow_next.png"></a>';
				} else {
                                    pagination += ' <img src="/themes/hanszeuthen/images/arrow_next.png">';
                                }
				
				
				pagination += '</div>';
				
				pager.html(pagination);
                                
                                pager.find('.pagination').css({'padding-top' : ((pager.height() - 8) / 2)});

                                pager.find('.nextPage').click(function(e) {
					var elcur = [];
                                        var elcur2 = [];
					
					$thumbsUl = gallery.find('ul.thumbs');
					$thumbsUl.find('li').each(function(i) {
						var $li = $(this);
					
						if($li.is(":visible")) {
							elcur.push($li.find('img'));
                                                        elcur2.push($li);
						}
					});

					gallery.hideThumbs(elcur);
					
					var wait = setInterval(function() {
						anim = false;
						for(var i in elcur) {
							if(elcur[i].is(":animated") ) {
								anim = true;
							}
						}

						if(!anim) {
							clearInterval(wait);
                                                        for(var j in elcur2) {
                                                            elcur2[j].hide();
                                                        }
							gallery.nextPage();
						}
					}, 200);				

					e.preventDefault();
				});
				
				pager.find('.prevPage').click(function(e) {
					var elcur = [];
					
					$thumbsUl = gallery.find('ul.thumbs');
					$thumbsUl.find('li').each(function(i) {
						var $li = $(this);
					
						if($li.is(":visible")) {
							elcur.push($li);
						}
					});

					gallery.hideThumbs(elcur.reverse());
					
					var wait = setInterval(function() {
						anim = false;
						for(var i in elcur) {
							if(elcur[i].is(":animated") ) {
								anim = true;
							}
						}

						if(!anim) {
							clearInterval(wait);
							gallery.prevPage();
						}
					}, 200);				

					e.preventDefault();
				});
			},

                        load_thumbs : function() {
                            $('#content ul.thumbs li img').hide();
                            $('#content ul.thumbs li .thumb').append('<div class="loader"><div id="flashContent"><TABLE WIDTH="100%" HEIGHT="100%" BORDER="0" CELLSPACING="0" CELLPADDING="0"><TR><TD ALIGN="CENTER" VALIGN="MIDDLE"><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="128" height="128" id="loader-hans-6" align="middle"><param name="movie" value="/themes/hanszeuthen/flash/loader-hans-6.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#101010" /><param name="play" value="true" /><param name="loop" value="true" /><param name="wmode" value="window" /><param name="scale" value="showall" /><param name="menu" value="true" /><param name="devicefont" value="false" /><param name="salign" value="" /><param name="allowScriptAccess" value="sameDomain" /><!--[if !IE]>--><object type="application/x-shockwave-flash" data="/themes/hanszeuthen/flash/loader-hans-6.swf" width="128" height="128"><param name="movie" value="loader-hans-6.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#101010" /><param name="play" value="true" /><param name="loop" value="true" /><param name="wmode" value="window" /><param name="scale" value="showall" /><param name="menu" value="true" /><param name="devicefont" value="false" /><param name="salign" value="" /><param name="allowScriptAccess" value="sameDomain" /><!--<![endif]--><a href="http://www.adobe.com/go/getflash"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a><!--[if !IE]>--></object><!--<![endif]--></object></TD></TR></TABLE></div></div>');

                            $('#content ul.thumbs li img').imgpreload(function() {
                                $('#content ul.thumbs li .loader').remove();
                                $('#content ul.thumbs li img').show();
                            });
                        },

                        load_big_image : function() {
                            $('.bigimage img').hide();
                            $('.bigimage').append('<div class="loader"><div id="flashContent"><TABLE WIDTH="100%" HEIGHT="100%" BORDER="0" CELLSPACING="0" CELLPADDING="0"><TR><TD ALIGN="CENTER" VALIGN="MIDDLE"><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="128" height="128" id="loader-hans-6" align="middle"><param name="movie" value="/themes/hanszeuthen/flash/loader-hans-6.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#000000" /><param name="play" value="true" /><param name="loop" value="true" /><param name="wmode" value="window" /><param name="scale" value="showall" /><param name="menu" value="true" /><param name="devicefont" value="false" /><param name="salign" value="" /><param name="allowScriptAccess" value="sameDomain" /><!--[if !IE]>--><object type="application/x-shockwave-flash" data="/themes/hanszeuthen/flash/loader-hans-6.swf" width="128" height="128"><param name="movie" value="loader-hans-6.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#000000" /><param name="play" value="true" /><param name="loop" value="true" /><param name="wmode" value="window" /><param name="scale" value="showall" /><param name="menu" value="true" /><param name="devicefont" value="false" /><param name="salign" value="" /><param name="allowScriptAccess" value="sameDomain" /><!--<![endif]--><a href="http://www.adobe.com/go/getflash"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a><!--[if !IE]>--></object><!--<![endif]--></object></TD></TR></TABLE></div></div>');

                            $('.bigimage img').imgpreload(function() {
                                $('.bigimage .loader').remove();
                                $('.bigimage img').fadeIn(240);
                            });
                        },

                        init_gallery : function() {
                            $('#content ul.thumbs li .thumb').append('<div class="loader"><div id="flashContent"><TABLE WIDTH="100%" HEIGHT="100%" BORDER="0" CELLSPACING="0" CELLPADDING="0"><TR><TD ALIGN="CENTER" VALIGN="MIDDLE"><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="128" height="128" id="loader-hans-6" align="middle"><param name="movie" value="/themes/hanszeuthen/flash/loader-hans-6.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#101010" /><param name="play" value="true" /><param name="loop" value="true" /><param name="wmode" value="window" /><param name="scale" value="showall" /><param name="menu" value="true" /><param name="devicefont" value="false" /><param name="salign" value="" /><param name="allowScriptAccess" value="sameDomain" /><!--[if !IE]>--><object type="application/x-shockwave-flash" data="/themes/hanszeuthen/flash/loader-hans-6.swf" width="128" height="128"><param name="movie" value="loader-hans-6.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#101010" /><param name="play" value="true" /><param name="loop" value="true" /><param name="wmode" value="window" /><param name="scale" value="showall" /><param name="menu" value="true" /><param name="devicefont" value="false" /><param name="salign" value="" /><param name="allowScriptAccess" value="sameDomain" /><!--<![endif]--><a href="http://www.adobe.com/go/getflash"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a><!--[if !IE]>--></object><!--<![endif]--></object></TD></TR></TABLE></div></div>');

                            start = 1;
                            stop = 3;
                            $thumbsUl = this.find('ul.thumbs');
                            $thumbsUl.find('li').each(function(i) {
                                var $li = $(this);

                                if (i >= start && i <= stop) {
                                    $li.show();
                                } else {
                                    $li.hide();
                                }
                            });

                            $('#content ul.thumbs li img').hide();

                            $('#content ul.thumbs li img').imgpreload(function() {
                                $('#content ul.thumbs li .loader').remove();
                                hzgal.updateThumbs(true);
                            });
                        },

                        reg_big_image : function() {
                            this.find('.thumb a').click(function(e) {
                                $('#container').find('.bigimage').remove();
                                $('body').find('.bigimage-nav').remove();
                                $('#header, #footer').css({'visibility' : 'hidden'});
                                e.preventDefault();

                                hzgal.currentImage = parseInt($(this).attr('id'));

                                imghref = $(this).attr('href');
                                isTall = $(this).find(".tall").length > 0;
                                imagediv = '<div class="bigimage" style="position:relative;top:-' + ($('#main').height() + $('#footer').height() + 25) + ';margin:0 32px 0 32px;height:' + ($('#main').height()+25) + ';text-align:center;background:#000000;z-index:100;width:' + ($('#main').width()+50) + '"></div>';
                                navdiv = '<div class="bigimage-nav" style="text-align:center;"><a href="#"><img src="/themes/hanszeuthen/images/arrow_prev2.png" class="prev-image"></a><a href="#"><img src="/themes/hanszeuthen/images/arrow_next2.png" class="next-image"></a></div>';

                                container_height = $('#container').height();
                                totop =  '-' + (container_height - 20) + 'px';
                                toheight = (container_height - 40) + 'px';

                                $('#container').css({'height' : container_height});
                                $('#container').append(imagediv);
                                $('body').append(navdiv);

                                $('.bigimage').animate({'height' : toheight , 'top' : totop}, 250, function() {
                                    biw = $('.bigimage').width();
                                    bih = $('.bigimage').height();
                                    if(isTall) {
                                        biw = Math.floor(bih * 0.8);
                                    }

                                    img = '<img src="' + imghref + '" style="width:' + (biw) + ';height:' + (bih) + ';">';
                                    $('.bigimage').html(img);
                                    hzgal.load_big_image();


                                    $('.bigimage').click(function() {
                                        $('.bigimage img').fadeOut(80);
                                        toheight = $('#main').height() + 25;
                                        totop =  '-' + ($('#container').height() - $('#header').height()) + 'px';

                                        $('.bigimage').animate({'height' : toheight , 'top' : totop}, 250, function() {
                                            $('#container').find('.bigimage').remove();
                                            $('body').find('.bigimage-nav').remove();
                                            $('#header, #footer').css({'visibility' : 'visible'});
                                        });
                                   });

                                   $('.next-image').click(function(e) {
                                       e.preventDefault();
                                       hzgal.nextImage();
                                   });

                                   $('.prev-image').click(function(e) {
                                       e.preventDefault();
                                       hzgal.prevImage();
                                   });
                                });
                            });
                        }
		});
	
 		$.extend(this, config, settings);
                
                hzgal = this;

                this.init_gallery();
                this.reg_big_image();
                
		return this;
	};
 })(jQuery);

$(document).ready(function () {
    jQuery('#content').css({'background' : '#101010'});
    $('#main').hzgallery({});
});
