/* Догрузка данных при прокрутке */
var engine = {

	posts : [],
	target : null,
	busy : false,
	count : 1,

	render : function(obj){
		if (obj.img_view) {
			xhtml = obj.img_view;
		}
		if (obj.img_view == '<div id="category-container"><ul></ul></div>') {
			$('#loading').remove();
			xhtml = '';
		}
		return xhtml;
	},

	init : function(posts, target){

		if (!target)
			return;

		this.target = $(target);
		this.append(posts);

              var that = this;
	              $(window).scroll(function(){
						var scroll_top = $(document).scrollTop();//высота прокрученной области
						var page_height = $(document).height();//высота всей страницы
						var wind_height = $(window).height();//высота окна браузера

	                  if (page_height - wind_height <= scroll_top + 149) {
	                      that.scrollPosition = $(window).scrollTop();
	                      that.get();
	                  }
	              });

	},

	append : function(posts){
		posts = (posts instanceof Array) ? posts : [];
		this.posts = this.posts.concat(posts);

		for (var i=0, len = posts.length; i<len; i++) {
			this.target.append(this.render(posts[i]));
		}

              if (this.scrollPosition !== undefined && this.scrollPosition !== null) {
                  $(window).scrollTop(this.scrollPosition);
              }
	},

	get : function() {

			if (!this.target || this.busy) return;

                  if (this.posts && this.posts.length) {
  					var lastId = this.posts[this.posts.length-1].id_img;
                  } else {
                      var lastId = 0;
                  }


			this.setBusy(true);

   			var that = this;

			$.getJSON('../post.php', {count:this.count, last:lastId},
				function(data){
					if (data.length > 0) {
						that.append(data);

						// Обработка картинки по цвету
						$(".small_images img").hover(function(){
					    	//alert('1');
							if (!$(this).hasClass("preview_on")){
							    $(this).parent().find("img").removeClass("preview_on");
							    $(this).addClass("preview_on");

								var src = $(this).attr("alt");
								//$(this).parent().parent().find("a img").hide();
								$(this).parent().parent().find("a img").attr("src", src).load(function(){
								$(this).show();
								});

							}
						});
					}
					that.setBusy(false);
				}
                 );
	},

	showLoading : function(bState){
 		var loading = $('#loading');

   		if (bState) {
     		$(this.target).append(loading);
      		loading.show('slow');
        }
        else {
        	$('#loading').hide();
        }
	},

	setBusy : function(bState){
 		this.showLoading(this.busy = bState);
   	}
};
/* Конец : Догрузка данных при прокрутке */

$(document).ready(function(){

	/* Обработка картинки по цвету */
	$(".small_images img").hover(function(){
    	//alert('1');
		if (!$(this).hasClass("preview_on")){
		    $(this).parent().find("img").removeClass("preview_on");
		    $(this).addClass("preview_on");

			var src = $(this).attr("alt");
			//$(this).parent().parent().find("a img").hide();
			$(this).parent().parent().find("a img").attr("src", src).load(function(){
			$(this).show();
			});

		}
	});
});

