(function($) {
	$.fn.procedureNav = function(options){
		// default configuration properties
		var defaults = {
			speed: 			800
		};
                currentState = 0;

		var options = $.extend(defaults, options);

		return this.each(function() {
			var obj = $(this);
			var aObjects = $('div.page a', this);
			var divObjects = $('div.item', this);
                        var timeOut = 0;
                        var s = divObjects.length;
                        var currentObject=$(divObjects[0]);
                        var maxHeight=0;
			var currentState = 0;

			for(var i=0; i<aObjects.length; i++){
                            	var selectedObj = $(divObjects[i]);
                            	if(maxHeight<selectedObj.height()){
                	               maxHeight = selectedObj.height();
        	                }
				if($(aObjects[i]).html() != 'все') {
					$(aObjects[i]).attr('count', i);
					$(aObjects[i]).bind('click', function(){
						 $('div.item', obj).hide();
						 $('div.page a.current', obj).removeClass('current');
						 $(divObjects[$(this).attr('count')]).show();
						 $(this).addClass('current');
					});
				    	lastImage = $($("img", selectedObj)[0]);
				}
			}

			lastImage.bind('load',
				function(){
					recalculate();
		                        runTimer();
				});
			function recalculate(){
        	                for(i=0; i<s; i++){
	                            var selectedObj = $(divObjects[i]);
	                            if(maxHeight<selectedObj.height()){
        	                       maxHeight = selectedObj.height();
                	            }
				}
				$('div.item', obj).css('height', maxHeight);
	                        obj.css('height', maxHeight+70);
			}

                        function animate(){
                            $(aObjects[currentState]).removeClass('current');
                            if((currentState+1)==s){
                                currentState = 0;
                            }else{
                                currentState++;
                            }
                            currentObject.fadeOut("slow",
                                function(){
                                    $(aObjects[currentState]).addClass('current');
                                    currentObject = $(divObjects[currentState]);
                                    currentObject.fadeIn("slow",
                                        function (){
                                            runTimer();
                                        });
                                }
                            );
                        }

                        function runTimer(){
                            clearTimeout(timeOut);
                            timeOut = setTimeout(
                                function(){
                                    animate();
                                },
                            1000);
                        }
		});
	}
})(jQuery);

