
var CR = Class.create( { 
	
	initialize: function( category ) {
		this.category = category;
		if(this.category.length==0) this.category='all';
		
		//this.baseUrl = "http://localhost/creativeroll_rss/index.php";
		this.baseUrl = "http://www.creativeroll.com";
		
		this.currentPage = 0;
		this.loadDots = 0;
		
		this.readinglist = 'readinglist';
		this.itemList = 'contentlist';
		this.loader = 'itemlistloader';
		
		this.isLoading = false;
		
		this.start();
	},
	
	start: function() {
		var ref = this;
		this.interval = new PeriodicalExecuter(function(){ref.onTimer();}, 1);
	},
	
	stop: function() {
		this.interval.stop();
	},
	
	onTimer: function() {
		var preloadDistance = 250;
		var position = this.getPageHeight() - this.getScrollHeight();
		if( position < preloadDistance ) {
			this.loadMore();
		}
	},
	
	startLoader: function() {
		var ref = this;
		this.loaderInterval = new PeriodicalExecuter(function(){ref.updateLoader();}, 1);
	},
	
	stopLoader: function() {
		this.loaderInterval.stop();
	},
	
	updateLoader: function() {
		this.loadDots++;
		if(this.loadDots==4) this.loadDots = 0;
		var dots = '';
		while(dots.length<this.loadDots) dots += '.';
		$(this.loader).innerHTML = 'Loading more' + dots;
	},
	
	add: function( itemId ) {
		var ref = this;
		new Ajax.Request( this.baseUrl+'/data/marks/add/'+itemId,
			{ onComplete: function( result ) {
				ref.onAdd( result );
			}});
	},
	
	onAdd: function( result ) {
		var response = result.responseText;
		$(this.readinglist).insert( {top:response} );
	},
	
	loadMore: function() {
		this.currentPage++;
		var ref = this;
		this.stop();
		this.startLoader();
		$(this.loader).show();
		new Ajax.Request( this.baseUrl+'/data/posts/getMore/'+this.category+'/'+this.currentPage,
			{ onComplete: function( result ) {
				ref.onLoadMore( result );
			}});
	},
	
	onLoadMore: function( result ) {
		var response = result.responseText;
		$(this.itemList).insert( {bottom:response} );
		$(this.loader).hide();
		this.stopLoader();
		this.start();
	},
	
	getScrollHeight: function(){
	  var y;
	  // all except Explorer
	  if (self.pageYOffset) {
	      y = self.pageYOffset;
	  } else if (document.documentElement && document.documentElement.scrollTop) {
	      y = document.documentElement.scrollTop;
	  } else if (document.body)	{
	      y = document.body.scrollTop;
	  }
	  return parseInt(y)+this.getWindowHeight();
	},
	
	getWindowHeight: function(){
		var frameWidth;
		var frameHeight;
		if (self.innerWidth) {
			frameWidth = self.innerWidth;
			frameHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientWidth) {
			frameWidth = document.documentElement.clientWidth;
			frameHeight = document.documentElement.clientHeight; 
		} else if (document.body) {
			frameWidth = document.body.clientWidth;
			frameHeight = document.body.clientHeight;
		}
		return parseInt(frameHeight);
	},

	getPageHeight: function(){
		var y;
		var test1 = document.body.scrollHeight;
		var test2 = document.body.offsetHeight
		if (test1 > test2) {
			y = document.body.scrollHeight;
		} else {
			y = document.body.offsetHeight;
		}
		return parseInt(y);
	},
	
	track: function(type, id) {
		var ref = this;
		//alert( 'track ' + type + ' : ' + id );
		new Ajax.Request( '/data/track/'+type+'/'+id,
			{ asynchronous:false, onComplete: function( result ) {
				ref.onTrack( result );
			}});
	},
	
	onTrack: function(result){
		//alert( result.responseText );
		return true;
	}
	
} );