var currentRequestId = 0;
var postsGroup = 2;
function getPosts()
{
    $(".loader").css('display', 'block');
    scrollable = false;
        //var categories = URLcategories;

        var dataString = '';

        //if (URLsearchPhrase) {
        //    dataString += 'phrase='+URLsearchPhrase+'&';
        //}

        dataString += 'page_no='+postsGroup+'&';
    if(category_id != null)
        dataString += 'category_id='+category_id+"&";
    if(time != '')
        dataString += 'time='+time+"&";
    if(timestamp != '')
        dataString += 'timestamp='+timestamp+"&";
    if(search_phrase != '')
        dataString += 'q='+search_phrase+"&";
    if(media_category_id != null)
        dataString += 'media_category_id='+media_category_id+"&";
    if(author_search != '')
        dataString += 'author_search='+author_search+"&";


        currentRequestId++;

        $.ajax({
			attachedData: {requestId: currentRequestId},
			url: "blog/ajax_get_posts",
			type: "POST",
			dataType: "json",
			data: dataString,
			success: function(result){

				// check if we performed another request in the mean time (and ignore this one if so...)
				if (this.attachedData.requestId != currentRequestId) {
					return false;
				}

				$(".loader").css('display', 'none');

				var posts_container = $('#posts');

				postsGroup++;

				var result_length = 0;
				for (var i in result) {result_length++;}
				if(result_length == 0) {load_posts = false;}

				var posts_counter = 0;

				for (var post_id in result) {

					var tag = $('<div class="post"></div>');
					var postIntro = $('<div class="post-intro"></div>');
					var imgContainer = $('<div class="img-container" id="' + result[post_id].entry_id + '"></div>');
					
					var header = $('<div class="post-title"></div>');
					
					if(access_level > 1)
						var title = $('<a href="'+ config["adminPostEditUrl"] + result[post_id].entry_id +'">' + result[post_id].title +'</a>');
					else
						var title = $('<a href="'+ result[post_id].url_path +'" onclick="goToWithFade(\'' + result[post_id].url_path + '\')">' + result[post_id].title +'</a>');

					var date = '';
					if('start_date' in result[post_id]){
							date = $('<div class="post-date">' + result[post_id].start_date.substr(0,5) + (('end_date' in result[post_id])? "–" + result[post_id].end_date.substr(0,5) : "" ) + '</div>');
					}
					
					var postIntroText = '';
                    var isIntro = ('wernisaz' in result[post_id]) && (result[post_id].wernisaz != 0) && ('start_time' in result[post_id]);
					if(isIntro){
							postIntroText = $('<div class="post-intro-inside">' +  result[post_id].start_time + '</div>');
					}

                    var catName = '';
					if('category_name' in result[post_id]){
                        if(isIntro){
							catName = $('<div class="category-name">'+result[post_id].category_name + '</div>');
                        }
                        else{
                            catName = $('<div class="category-name" style="margin-top: 4px;">'+result[post_id].category_name + '</div>');
                        }
					}
					
					var medias = '';
					if(result[post_id].medias.length > 0){

						if(result[post_id].medias[0].filetype === 'image'){
							if(access_level > 1)
								medias = $('<a href="'+ config["adminPostEditUrl"] + result[post_id].entry_id +'" style="height:' + result[post_id].medias[0].thumb_height + 'px;"><img src="var/images/thumbs/' + result[post_id].medias[0].path + '" alt="' + result[post_id].title + '" width="234" height="' + result[post_id].medias[0].thumb_height + '"/></a>');
							else
								medias = $('<a href="'+ result[post_id].url_path +'" style="height:' + result[post_id].medias[0].thumb_height + 'px;" onclick="goToWithFade(\'' + result[post_id].url_path + '\')"><img src="var/images/thumbs/' + result[post_id].medias[0].path + '" alt="' + result[post_id].title + '" width="234" height="' + result[post_id].medias[0].thumb_height + '"/></a>');
/*
							preloader.add(
								[ "var/images/thumbs/"+result[post_id].medias[0].path ], (function(dest, title) {
									return function(images) {
										$(dest)
										.html('<img src="' + images[0] + '" alt="' + title + '" style="display: none;" width="234" height="' + result[post_id].medias[0].thumb_height + '" />')
										.children()
										.fadeIn(200);
									}
								})(medias, result[post_id].title )
							);
*/
						}
						else{
							medias = '<iframe src="http://player.vimeo.com/video/'+result[post_id].medias[0].path + '?byline=0" width="234" height="132" frameborder="0"></iframe>';
						}
					}
					else{
						if(result[post_id].content != null)
							if(result[post_id].content.length > 180)
								medias = '<div class="no-image-text">' + result[post_id].content.substr(0, 180) + '...</div>';
							else
								medias = '<div class="no-image-text">' + result[post_id].content + '</div>';
					}

					header.append(title);
					postIntro.append(header);
					postIntro.append(date);
					postIntro.append(postIntroText);
					postIntro.append(catName);
					imgContainer.append(medias);
					tag.append(postIntro);
					tag.append(imgContainer);
			
					posts_container.append(tag);
					posts_counter++;
				}

				if(load_posts != false) {arrange();}
				scrollable = true;

				return;
			}
        });
}
