window.addEvent('domready', function () {

	var thumns = $$(".thumbs li a");
	
	var gallery6 = new fadeGallery($('gallery'), {
	 	speed: 500,
 		autoplay: true,
 		duration: 4000,
 		onStart: function() {
 			thumns.removeClass("active");
 			thumns[this.current].addClass("active");
 		},
 		onPlay: function() { this.fireEvent('start');	}
	});
	thumns.each(function(el, i) {
		el.addEvent("click", function() {
			thumns.removeClass("active");
			this.addClass("active");
			gallery6.current = i;
			gallery6.play(true);
			return false;
		});
	});
	var buttonPanel = gallery6.holder.getElement(".control");
	gallery6.holder.addEvent("mouseenter", function() {
		buttonPanel.tween("top", 0);
		gallery6.prev.tween("left", 10);
		gallery6.next.tween("right", 10);
	});
	gallery6.holder.addEvent("mouseleave", function() {
		buttonPanel.tween("top", -40);
		gallery6.prev.tween("left", -50);
		gallery6.next.tween("right", -50);
	});
	
	// Periodical Ajax for trending videos
	var trendingCounter = 0;
	var trending = new Request({
		url: 'services.php?p=trending',
		method: 'get',
		onComplete: function(response) {
			$('trending').set('html',response);
		}
	});
	
	var getTrending = function() {
		trending.send();
		trendingCounter++;
		if(trendingCounter > 120) clearInterval(interval);
	};
	
	var interval = getTrending.periodical(5000);
	
});	

/* via @appden, Scott Kyle, http://appden.com/javascript/fun-with-custom-events-on-elements-in-mootools/ */
Native.implement([Element, Window, Document, Events], {
	oneEvent: function(type, fn) {
		return this.addEvent(type, function() {
			this.removeEvent(type, arguments.callee);
			return fn.apply(this, arguments);
		});
	}
});


//safety closure
(function($) {
	//domready event
	window.addEvent('domready',function() {
		//settings on top
		var initialPosts = '';
		var start = 5;
		var desiredPosts = 5;
		var loadMore = $('load-more');
		
		//NEW!
		var spy;
		var spyContainer = $('posts');
		
		var spyAct = function() {
			//var min = spyContainer.getScrollSize().y - spyContainer.getSize().y - 150 /* tolerance */;
			
			var min = spyContainer.getCoordinates().bottom - window.getHeight() - 500;
			
			//console.log('setting min to:  ' + min);
			spy = new ScrollSpy({
				container: spyContainer,
				min: min,
				onEnter: function() {
					loadMore.fireEvent('click');
					console.log('enter');
				}
			});
		};
		//wait for first load...
		window.oneEvent('load',function() {
			spyAct();
		});
		
		
		//function that creates the posts
		var postHandler = function(responseTree) {
			$('posts').adopt(responseTree);
			
		};
		
		//place the initial posts in the page
		//postHandler(initialPosts); // initial posts are already added by PHP script
		
		//ajax!
		var request = new Request.HTML({
			url: 'services.php', //ajax script -- same page
			method: 'get',
			link: 'cancel',
			noCache: true,
			onRequest: function() {
				//add the activate class and change the message
				loadMore.addClass('activate').set('text','Loading...');
			},
			onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
				//reset the message
				loadMore.set('text','Load More');
				//increment the current status
				start += desiredPosts;
				//add in the new posts
				postHandler(responseTree);

				//spy calc!
				spyAct();
			},
			onFailure: function() {
				//reset the message
				loadMore.set('text','Oops! Try Again.');
			},
			onComplete: function() {
				//remove the spinner
				loadMore.removeClass('activate');
			}
		});
		
		
		//add the "Load More" click event
		loadMore.addEvent('click',function(){
			//begin the ajax attempt
			request.send({
				data: {
					'p': 'topvids',
					'start': start,
					'desiredPosts': desiredPosts
				}
			});
		});
	});
	
	
})(document.id);
