var debug = true;
var ontstart = new OntologyStartPage();
jQuery(document).ready(function(){
	ontstart.init();
});


jQuery.fn.swapWith = function(to) {
    return this.each(function() {
        var copy_to = $(to).clone(true);
        var copy_from = $(this).clone(true);
        $(to).replaceWith(copy_from);
        $(this).replaceWith(copy_to);
    });
};

function OntologyStartPage(){
	
	this.init = function(){
		this.fixMaxStickersHeight();
		this.iniPrevNextBtn();
	};
	this.fixMaxStickersHeight = function() {
		var dontcrash = 50;
		while($('.showAll a').closest(".stickers").height() > 50 && dontcrash > 0) {
			$('.showAll a').parent().prev().hide();
			$('.showAll a').parent().swapWith($('.showAll a').parent().prev());
			dontcrash--;
		}
		if(dontcrash == 50) {
			$('.showAll a').hide();
		}
		$('.showAll a').click(function(e) {
			$(".stickers .sticker").show();
			$(e.target).hide();
			return false;
		});
	};	
	this.iniPrevNextBtn = function() {
		$('.cotwlWidget .showPrev').click(function(e) {
			var currentOffset = $('.cotwlWidget input[name=currentOffset]').val();
			 var bodyContent = $.ajax({
			      url: jsBaseUrl + "/wp-content/plugins/ontology-startpage-posts/templates/front.php",
			      global: false,
			      type: "POST",
			      data: ({offset : currentOffset+1}),
			      dataType: "html",
			      success: function(msg){
				 	$('.cotwlWidget').html(msg);
				 	ontstart.init();
		 			dinPro();
			      }
			   }
			).responseText;
			 return false;
		});
		
		$('.cotwlWidget .showNext').click(function(e) {
			var currentOffset = $('.cotwlWidget input[name=currentOffset]').val();
			 var bodyContent = $.ajax({
			      url: jsBaseUrl + "/wp-content/plugins/ontology-startpage-posts/templates/front.php",
			      global: false,
			      type: "POST",
			      data: ({offset : currentOffset-1}),
			      dataType: "html",
			      success: function(msg){
				 	$('.cotwlWidget').html(msg);
				 	ontstart.init();
		 			dinPro();
			      }
			   }
			).responseText;
			 return false;
		});
		
	};
}