(function( $ ){
	var methods = {
		init : function( options ) {
			var defaults = { 
				timeBetween: 4000,
				timeAnim:"slow"
			};  
			var options = $.extend(defaults, options);

			return this.each(function() {
				var $this = $(this);
				$this.data('portfolio', {
					els : $(this).children(),
					count: $(this).children().length,
					cur : -1,
					timeout: 0,
					timeBetween: options.timeBetween,
					timeAnim: options.timeAnim,
					isHover: false,
					ready: false,
					visible: false,
					preloadStarted: false,
					nav: $(this).closest(".portfolio_element").find(".portfolio_slider_nav")
				});
				var d = $this.data('portfolio');
				if(d.els!=null) {
					var l = $('<div class="portfolio_slider_loading"></div>');
					$this.append(l);
				}
				$this.portfolio('preload');
				/*$this.hover(
					function() {
						$(this).data().slider.isHover = true;
						clearTimeout( $this.data().slider.timeout );
					},function() {
						$(this).data().slider.isHover = false;
						if($(this).data().slider.ready) {
							$this.data().slider.timeout = setTimeout(function() {$this.slider('next');},$this.data().slider.timeBetween);
						}
					}
				);
				$(document).ready(function() { 
					$this.slider("scrollEvent");
				});
				$(window).scroll(function() {
					$this.slider("scrollEvent");
				});*/
			});
		}, 
		/*scrollEvent: function() {
			var wt = $(window).scrollTop();
			var wh = $(window).height();
			var t = $(this).offset().top;
			var h= $(this).outerHeight();
			
			if((t+h >= wt && t <=wt+wh)) {
				
				$(this).data().slider.visible=true;
				if( ! $(this).data().slider.preloadStarted ) {
					$(this).slider("preload");
				}
			} else {
				$(this).data().slider.visible=false;
			}
		
		},*/
		preload: function() {
			$(this).data().portfolio.preloadStarted = true;
			var $this = $(this);
			var d = $this.data('portfolio');
			var loaded = 0;
			for(var i=0; i<d.count; i++) {
				var im = $(d.els[i]).find("img");
				$(im).load(function() {
					loaded++;
					if(loaded == d.count) {
						setTimeout(function() { $this.portfolio('start') },200);
					}
				});
				$(im).error(function() {
					//console.log('error');
				});
				
			}
		},
		start: function() {
			var $this = $(this);
			
			for(i=0; i<$this.data().portfolio.count;i++) {
				var a = $('<a href="#" rel="'+i+'">'+(i+1)+'</a>');
				a.click(function() { $this.portfolio('gotoslide',parseInt($(this).attr("rel"))); return false; });
				a.appendTo($this.data().portfolio.nav);
			}
			$this.data().portfolio.els.css("top", $this.height());
			$this.find(".portfolio_slider_loading").fadeOut("slow",function() { $(this).remove(); });
			$this.data().portfolio.ready = true;
			$this.portfolio('next');
		},
		next: function() {
			var d = $(this).data('portfolio');
			var i = (d.cur + 1) %(d.count);
			$(this).portfolio('gotoslide',i);
			
		},
		gotoslide: function(i) {
			var $this = $(this);
			//clearTimeout( $this.data().slider.timeout );
			var d = $this.data('portfolio');
			var t = d.timeAnim
			if(d.cur!=i) {
				if(d.cur>=0) {
					$(d.els[d.cur]).animate({top: -1*($this.height())},t);
					d.nav.find("a").removeClass("on");
				}
				$this.data('portfolio').cur = i;
				d.nav.find("a").eq(i).addClass("on");
				$(d.els[i]).css("top", $this.height()).animate({top: 0},t, function() {
					//if(! $this.data().portfolio.isHover )
					//{
						//$this.data('slider').timeout = setTimeout(function() {$this.slider('next');},$this.data().slider.timeBetween);
					//}
				});
			}
		}
	}
	$.fn.portfolio = function( method ) {
		if ( methods[method] ) {
		  return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
		} else if ( typeof method === 'object' || ! method ) {
		  return methods.init.apply( this, arguments );
		} else {
		  $.error( 'Method ' +  method + ' does not exist on jQuery.slider' );
		}    
  };
})( jQuery );

var ajax_loader = new Array();

function DoladujPortfolio(el) {
	var url = $(el).attr("href");
	if(-1 == $.inArray(url,ajax_loader)) {
		ajax_loader.push(url);
		$(el).empty().addClass("loading");
		//setTimeout(function() { 
		$.ajax({
			url: url,
			data: {ajax:1},
			success: function(response) {
				$(el).before(response).remove();
				Cufon.refresh('.portfolio_www_nazwa');
				if($.browser.msie && $.browser.version < 8) {
					$("#bottom").hide();
					$("#bottom").show();
				}
			}
		});
		//},500);
	}
}
$(".portfolio_next").live("click",function() { DoladujPortfolio(this); return false; });

function scrollEvent()
{
	if ( $(".portfolio_next").length ) {
		var wt = $(window).scrollTop();
		var wh = $(window).height();
		$(".portfolio_next").each(function() {
			var t = $(this).offset().top;
			var h= $(this).outerHeight();
			
			if((t+h >= wt && t <=wt+wh)) {
				
				DoladujPortfolio(this);
			} else {
				
			}
		
		});
	}
}
$(window).bind('scroll',scrollEvent);
