(function($) {
	$.fn.gallerySlide = function(options){

		// default configuration properties
		var defaults = {
			speed: 			800
		};
                currentState = 0;

		var options = $.extend(defaults, options);

		return this.each(function() {
			var mainObj = $(this);
                        var childObjects = $("div.galleryFoto", mainObj);
                        var aChildObjects = $("a.state", $('div.gpages', mainObj));
			var s = childObjects.length;
                        var timeOut = 0;
                        var currentObject;
                        var maxHeight=0;

                        for(i=0; i<s; i++){
                            var selectedObj = $(childObjects[i]);
			    $(aChildObjects[i]).bind('click mouseover', function (){ setCurrent(this); } )
			    $(aChildObjects[i]).bind('mouseout', function (){ runTimer(); } )
			    $(aChildObjects[i])[0].currCount = i;
                            if(i>0){
                                selectedObj.hide();
                            }else{
                                currentObject = selectedObj;
                                $(aChildObjects[i]).addClass('current');
                            }
			    lastImage = $($("img", selectedObj)[1]);
                            if(maxHeight<selectedObj.height()){
                               maxHeight = selectedObj.height();
                            }
                        }
			lastImage.bind('load',
				function(){
					recalculate();
		                        runTimer();
				});
			function recalculate(){
        	                for(i=0; i<s; i++){
	                            var selectedObj = $(childObjects[i]);
	                            if(maxHeight<selectedObj.height()){
        	                       maxHeight = selectedObj.height();
                	            }
				}
	                        mainObj.css('height', maxHeight+35);
			}
			function setCurrent(obj){
				count = obj.currCount;
	                        clearTimeout(timeOut);
				currentObject.stop();

	                        $(aChildObjects[currentState]).removeClass('current');

				currentObject.fadeOut("fast");
				currentObject = $(childObjects[count])
				currentObject.fadeIn("fast");

				currentState = count;
	                        $(aChildObjects[currentState]).addClass('current');
			}

                        function animate(){
                            $(aChildObjects[currentState]).removeClass('current');
                            if((currentState+1)==s){
                                currentState = 0;
                            }else{
                                currentState++;
                            }
                            currentObject.fadeOut("slow",
                                function(){
                                    $(aChildObjects[currentState]).addClass('current');
                                    currentObject = $(childObjects[currentState]);
                                    currentObject.fadeIn("slow",
                                        function (){
                                            runTimer();
                                        });
                                }
                            );
                        }

                        function runTimer(){
                            clearTimeout(timeOut);
                            timeOut = setTimeout(
                                function(){
                                    animate();
                                },
                            1000);
                        }
		});

	};

})(jQuery);
