/**
* IndexController
* @argument {type} description
* @constructor name
* @returns true
*/
function IndexController() {
	var self = this;
	var inActiveCount = 120; //120 seconds
	this.inActiveInterval;
	
	/* !Init */
	this.init = function() {
		self.pngFix();
		//self.overlay();
		self.externalLinks();
		self.player();
		self.overview();
		self.overviewReset();
		self.songsForm();
	}
	
	this.pngFix = function() {
		//If the plugin exists, e.g-IE6
		if(typeof DD_belatedPNG == 'object') {
			DD_belatedPNG.fix('#header h1 a, #header h2 a, #overview, #overview h3, #overview a span, #videoBorder, .controls, .next span, .mute span, .unmute span, .play span, .pause span, #song_search input');
		}
	}
	
	this.isActive = function() {
		var defaultCount = inActiveCount;
		//Bind mousemove and keyup to reset inActiveCount
		$(document).bind('mousemove keyup', function() {
			inActiveCount = defaultCount;
		});
		$(window).bind('PLAYING', function() {
			$('#inActive').css('display', 'none'); //Hide the inactiv alert
			if (!self.inActiveInterval) { //If interval is not defined, then start it
				self.isInActive(); //Reset active check	
			}
		});
		$(window).bind('PAUSED', function() {
			clearInterval(self.inActiveInterval); //Clear the active check
			self.inActiveInterval = false; //Set the inActiveInterval to false when paused
		});
	}
	
	this.isInActive = function() {
		var defaultCount = inActiveCount;
		//Set interval to count down inactivity
		self.inActiveInterval = setInterval(function() {
			inActiveCount--;
			//If inactive longer than set time, then trigger alert
			if (inActiveCount <= 0) {
				clearInterval(self.inActiveInterval);
				self.inActiveInterval = false; //Set the inActiveInterval to false when paused
				player.sendEvent('PLAY','false');
				$('#inActive').css('display', 'block').children('.resume').bind('click', function(e) {
					e.preventDefault();
					inActiveCount = defaultCount;
					player.sendEvent('PLAY','true');
				});
			}
		}, 1000);
	}
	
	this.overlay = function() {
		$('.colorbox').colorbox({innerWidth:'673px', innerHeight:'377px', inline:true});
	}
	
	this.externalLinks = function() {
		// externalLinks
		$('#header').externalLinks({selector: '.external'});
		$('#footer').externalLinks({selector: '.external'});
	}
	
	this.player = function() {
		$('#skipIntro').click(function(e) { 
			player.sendEvent('SEEK', 40);
			e.preventDefault();
		});
		
		$('#next').click(function(e) { 
			if(playlistOrder[currentPlaylist] === '8fxgBoFR' || currentPlaylist ==='6igp4zWd') {
				switchPlaylist(currentPlaylist); 
			}
			e.preventDefault();
		});
		
		
		//custom playback controls
		$('#playback').bind('click', function(e) {
			if($(this).hasClass('play')) {
				player.sendEvent('PLAY','true');
				//$(this).removeClass('play').addClass('pause');
			} else {
				player.sendEvent('PLAY','false');
				//$(this).removeClass('pause').addClass('play');
			}
			e.preventDefault();
		});
		
		$('#sound').bind('click', function(e) {
			if($(this).hasClass('mute')) {
				player.sendEvent('MUTE','true');
				$(this).removeClass('mute').addClass('unmute');
			} else {
				player.sendEvent('MUTE','false');
				$(this).removeClass('unmute').addClass('mute');
			}
			e.preventDefault();
		});
		
		$('#videoBorder').bind('click', function(e) {
			$('#playback').trigger('click');
			e.preventDefault();
		});
	}
	
	this.overview = function() {
		$('#videoWrapper').hide();
		$('#overview a').bind('click', function(e) {
			$('#overview').fadeOut(900);
			$('#videoWrapper').delay(900).fadeIn(1200, function() {
				switchPlaylist('reset');
				//player.sendEvent("PLAY", "true");
			});
			e.preventDefault();
			self.isActive(); //Set the inactive check
		});
	}
	
	this.overviewReset = function() {
		$('h2 a').bind('click', function(e) {
			$('#overview').fadeIn(1200);
			$('#song_search').slideUp();
			$('#videoWrapper').hide();
			e.preventDefault();
			clearInterval(self.inActiveInterval); //Clear the inactive check
			self.inActiveInterval = false; //Set the inActiveInterval to false when paused
			player.sendEvent("STOP");
			$('#song').val("Enter Holiday Song").addClass('unfocused');
		});
	}
	
	this.songsForm = function() {
		$('#song_search').submit(function(e) {
			e.preventDefault();
		});
		$('#song').bind('focus', function() {
			if($('.auto_list').children().length <= 0) {
				$(this).val("").removeClass('unfocused');
			}
		}).bind('blur', function() {
			if($('.auto_list').children().length <= 0 && ($(this).val().length <= 0 || $(this).val() == "Enter Holiday Song") ) {
				$(this).val("Enter Holiday Song").addClass('unfocused');
			}
		}).autosuggest({
			dataUrl: '/index/songs',
			minCharLimit: 2,
			delay: 500,
			onInputSubmit: function(data) {
				//If data is a string, test for profanity
				if (typeof data === 'string' && data == 'holiday noms LOLZ!') {
					switchPlaylist('easteregg');
				} else if (typeof data === 'string' ) {
					$.ajax({
						url: '/index/profanity',
						data: 'query=' + data,
						dataType: 'json',
						type: 'post', 
						success: function(returnData) {
							//If true, profanity matched, key up PROFANITY vid
							if (returnData[0]) {
								switchPlaylist('profanity');
							//Else, no profanity, check for video again
							} else {
								$.ajax({
									url: '/index/songs',
									data: 'query=' + data,
									dataType: 'json',
									type: 'post', 
									success: function(returnData) {
										//If Video found, switch playlist
					        			if (returnData[0]) {
					        				switchPlaylist('song-' + returnData[0].key);
					        			//Else 404
					        			} else {
					        				switchPlaylist('404');
					        			}
					      			}
					      		});
							}
		      			}
		      		});
				//Else, if data is an object, key up SONG vid
				} else if (typeof data === 'object') {
					switchPlaylist('song-' + data.data('key'));
				}
			}
		});
	}
	
	this.init();
}
