YUI().use('node', 'io', 'cookie', function(Y){
// NEWS CLASS
	var News = (function (){
		
		return{
			init: function(){
				//console.log('News Initializing...');
				this.offset = 0;
				this.activated = false;
				this.getNews(this.offset);
				//console.log('News Initialized');
			},
			
			getNews: function(offset){
				var callback = {
					timeout : 3000,
					on : {
						success : function ( transId, response) {
							News.populatePage(response);
							//console.log('News Retrieved');
						},
						failure : function (x,o) {
							alert("Async call failed!");
						}
					}
				}
				// Make the call to the server for HTML data
				Y.io("/actions/getNews.php?offset=" +offset, callback);
			},
			
			populatePage: function(response){
				var html = Y.Node.create(response.responseText);
				
				var newPages = html.all('#body .page');
				var newAnchors = html.all('#pager a');
				
				var oldPages = Y.all('#body .page');
				var oldAnchors = Y.all('#pager a');
				
				if ( oldPages.size() > 0 )
					oldPages.remove();
				if ( oldAnchors.size() > 0 )
					oldAnchors.remove();
				
				var body = Y.one('#body');
				var pager = Y.one('#pager');
				
				body.append(newPages);
				pager.append(newAnchors);
				
				//Y.one('#body')._node.className = 'page1';
				//Y.one('#pager')._node.className = 'page1';
				
				this.populated = true;
				//console.log('News Populated');
				this.activate();
				
			},
			
			activate: function(){
			// Pager Events
				var pageAnchors = Y.all('#pager a');
				pageAnchors.on('mouseover', function(e) {
					Y.one('#body')._node.className = e.currentTarget._node.className;
				});
				pageAnchors.on('mouseout', function(e) {
					var className = Y.one('#pager')._node.className;
					if ( className ) 
						Y.one('#body')._node.className = className;
					else
						Y.one('#body')._node.className = 'index';
				});
				pageAnchors.on('click', function(e) {
					e.preventDefault();
					e.currentTarget._node.blur();
					
					if ( e.currentTarget.hasClass('prev') ){
						if ( News.offset > 0 )
							News.offset--;
						News.getNews(News.offset);
						e.halt();
						Y.one('#body')._node.className = 'index';
					}
					if ( e.currentTarget.hasClass('next') ){
						News.offset++;
						News.getNews(News.offset);
						e.halt();
						Y.one('#body')._node.className = 'index';
					}
					else {
						Y.one('#body')._node.className = e.currentTarget._node.className;
						e.currentTarget._node.parentNode.className = e.currentTarget._node.className;
					}
				});
				
			// Nav Events
				if ( !this.activated ){
					var nav = new Array();
					nav['events'] = Y.one('#header .nav a.events');
					nav['tunes'] = Y.one('#header .nav a.tunes');
					nav['events'].on('click', function(e) {
						e.preventDefault();
						e.currentTarget._node.blur();
						Y.one('#body')._node.className = e.currentTarget._node.className;
						Y.one('#footer').setStyle('display', 'none');
						Y.Cookie.set('home', 'events', { expires: new Date('January 01, 2025') });
					});
					nav['tunes'].on('click', function(e) {
						e.preventDefault();
						e.currentTarget._node.blur();
						Y.one('#body')._node.className = 'index';
						Y.one('#footer').setStyle('display', 'block');
						Y.one('#footer #pager')._node.className = '';
						Y.Cookie.set('home', 'index', { expires: new Date("January 12, 2025") });
					});
					this.activated = true;
					
					Y.one('#footer .styleSwitch').on('click', function (e){
						e.preventDefault();
						
						var layout = Y.Cookie.get('layout');
						if ( layout == 'scroll' ){
							var style = Y.one('link[href="/styles/alternate.css"]');
							if ( style )
								style.remove();
							Y.Cookie.set('layout', 'no-scroll', { expires: new Date('January 01, 2025') });
						} else if ( layout == 'no-scroll' ){
							Y.one('#body')._node.className = 'page1';
							Y.Get.css('/styles/alternate.css');
							Y.Cookie.set('layout', 'scroll', { expires: new Date('January 01, 2025') });
						}
					});
					Y.one('#footer .disclaimer').on('click', function (e){
						e.preventDefault();
						
						var className = Y.one('#body')._node.className;
						if ( className == 'disclaimer' )
							Y.one('#body')._node.className = Y.one('#pager')._node.className;
						else
							Y.one('#body')._node.className = 'disclaimer';
					});
				}
				//console.log('News UI Activated');
				
			// zPlayer
				AudioPlayer.setup("/swf/player.swf", {
					width: 290,
					initialvolume: 100,
					transparentpagebg: "yes",
					leftbg: '00ccff',
					rightbg: 'f40054',
					volslider: 'f40054',
					tracker: '00ccff',
					loader: 'f40054',
					text: 'f40054'
				});
				AudioPlayer.embed("zPlayer");
				
				var audio = Y.all('a.mp3');
				audio.on('click', function(e){
					e.preventDefault();
					AudioPlayer.load('zPlayer', e.currentTarget.get('href'), e.currentTarget.get('text'));
					AudioPlayer.open('zPlayer');
				});
				////console.log('zPlayer Tracks Activated');
			}
			
	}})();
	News.init();
});
