/**
* Commandline - lightweight text display and keyword/command tool
* Coded by Ben Greig
* Version 1.0
* www.bengreig.com
**/

(function($){	
	$.fn.commandline = function(options) {
		
		//------------------------------------------------------------------------------//
		//- Commandline Options --------------------------------------------------------//
		//------------------------------------------------------------------------------//
		// define defaults and override with options, if available						//
		// by extending the default settings, we don't modify the argument				//
		//------------------------------------------------------------------------------//
		
		set = jQuery.extend({
			debug: true,
			debugcss: true,
			keywords: new Object(),
			parent: 'textoverlay',
			object: 'container'
		}, options);
		
		
		//------------------------------------------------------------------------------//
		//- Create the Commandline Instance --------------------------------------------//
		//------------------------------------------------------------------------------//
		
		var theobjectstring = "#"+set.parent+" ."+set.object;
		
		if($(theobjectstring).parent().length > 0) {} else {
			$('body').prepend('<div id="'+set.parent+'" class="rows_10"><div class="'+set.object+'"></div></div>');
			$(theobjectstring).parent().hide();
		}
		
		
		//------------------------------------------------------------------------------//
		//- Commandline internal VARS --------------------------------------------------//
		//------------------------------------------------------------------------------//
		
		cl = {
			
			// - LOGGING VARS - //
			keylog: '',
			currentkey: null,
			contentoutput: null,
			object: $(theobjectstring), //Commandline Object (Where things get output)
			
			// - KEYWORD VARS - //
			keyhistory: '',
			keyhistory_escaped: '',
			
			disablekeywords: false,
			match: null,
			keywords_regex: null
		}
		
		
		//------------------------------------------------------------------------------//
		//- Predefined Keywords --------------------------------------------------------//
		//------------------------------------------------------------------------------//
		
		set.keywords.kwd_home 			= function(){ window.location.href="/"; };
		set.keywords.kwd_frontpage		= set.keywords.kwd_home;
		
		set.keywords.kwd_projects 		= function(){ window.location.href="/projects/"; };
		set.keywords.kwd_work 			= set.keywords.kwd_projects;
		set.keywords.kwd_folio 			= set.keywords.kwd_projects;
		set.keywords.kwd_portfolio 		= set.keywords.kwd_projects;
		set.keywords.kwd_pieces 		= set.keywords.kwd_projects;
		
		set.keywords.kwd_awards 		= function(){ window.location.href="/awards/"; };
		set.keywords.kwd_accolades		= set.keywords.kwd_awards;
		set.keywords.kwd_recognition	= set.keywords.kwd_awards;
		set.keywords.kwd_merits			= set.keywords.kwd_awards;
		set.keywords.kwd_trophies		= set.keywords.kwd_awards;
		
		set.keywords.kwd_news 			= function(){ window.location.href="/news/"; };
		set.keywords.kwd_blog			= set.keywords.kwd_news;
		set.keywords.kwd_headlines		= set.keywords.kwd_news;
		set.keywords.kwd_press			= set.keywords.kwd_news;
		
		set.keywords.kwd_contact 		= function(){ window.location.href="/contact/"; };
		set.keywords.kwd_address		= set.keywords.kwd_contact;
		set.keywords.kwd_telephone		= set.keywords.kwd_contact;
		set.keywords.kwd_phone			= set.keywords.kwd_contact;
		set.keywords.kwd_fax			= set.keywords.kwd_contact;
		set.keywords.kwd_facsimile		= set.keywords.kwd_contact;
		set.keywords.kwd_location		= set.keywords.kwd_contact;
		
		set.keywords.kwd_studio			= function(){ window.location.href="/studio/"; };
		set.keywords.kwd_about			= set.keywords.kwd_studio;
		
		set.keywords.kwd_make_it_black	= function(){ $('body').removeClass('white').addClass('black'); resetCommandline(); };
		set.keywords.kwd_make_it_white	= function(){ $('body').removeClass('black').addClass('white'); resetCommandline(); };
		set.keywords.kwd_color			= function(){ cl.keylog = ""; cl.object.html("No way!<br/> Only black or white here."); };
		set.keywords.kwd_red			= set.keywords.kwd_color;
		set.keywords.kwd_green			= set.keywords.kwd_color;
		set.keywords.kwd_blue			= set.keywords.kwd_color;
		set.keywords.kwd_pink			= set.keywords.kwd_color;
		set.keywords.kwd_grey			= set.keywords.kwd_color;
		set.keywords.kwd_gray			= set.keywords.kwd_color;
		set.keywords.kwd_yellow			= set.keywords.kwd_color;
		set.keywords.kwd_orange			= set.keywords.kwd_color;
		
		set.keywords.kwd_search			= function(){ cl.keylog = "What would you like to search for?<br/><br/>"; cl.object.html("What would you like to search for?<br/><br/>"); };  
		
		set.keywords.kwd_help			= function(){ cl.keylog = ""; cl.object.html("Stuck eh? You can search for projects by name, type page names such as projects, contact etc or even type in a type of architectural work like residential."); };
		set.keywords.kwd_lost			= set.keywords.kwd_help;
		
		
		set.keywords.kwd_fuckin_awesome	= function(){ cl.keylog = ""; cl.object.html("Yeah it is isn't it"); };
		set.keywords.kwd_awesome		= set.keywords.kwd_fuckin_awesome;
		set.keywords.kwd_i_win			= function(){ cl.keylog = ""; cl.object.html("So you might believe human."); };
		
		
		
		//-------------------------//
		//- DEBUG FUNCTIONS -------//
		//-------------------------//
		if(set.debug == true){
			
			//Attach the debug objects to the page
			$('body').append('<div id="cl_debug_keylog" class="cl_debug">Characters: <span>0</span><br/>Char Code: <cite></cite></div>');
			$('body').append('<div id="cl_debug_string" class="cl_debug"></div>');
			
			//Refresh the visible vars
			setInterval(function(){
			    $('#cl_debug_keylog span').html(cl.keylog.length);
			    $('#cl_debug_keylog cite').html(cl.currentkey);
			},50);
			
			//If true attach styles to the debug objects
			if(set.debugcss == true){
				$('.cl_debug').css({
					'display'	: 'block',
					'position'	: 'absolute',
					'z-index'	: 999999999,
					'color'		: '#fff',
					'font-size'	: '18px',
					'padding'	: '15px',
					'cursor'	: 'pointer',
					'border-radius' 		: '8px',
					'-moz-border-radius'	: '8px',
					'-webkit-border-radius'	: '8px'
					
				});
				$('#cl_debug_keylog').css({
					'top'		: '20px',
					'right'		: '20px',
					'background': '#7d0000',
					'background-color'	: 'rgba(125,0,0,0.8)'
				});
				$('#cl_debug_string').css({
					'top'		: '90px',
					'right'		: '20px',
					'background': '#007d00',
					'background-color'	: 'rgba(0,125,0,0.8)'
				});
				
			}
			
			$('#cl_debug_string').hide();
			
			//Onclick do this stuff
			$('#cl_debug_keylog').click(function(){
				$('#cl_debug_string').html(cl.keyhistory_escaped);
				$('#cl_debug_string').show();
				$('#cl_debug_string').delay(2000).animate({opacity: 0},500,function(){
					$(this).hide().css({opacity:1});
				});	
			});
			
		}
		
		
		//-------------------------//
		//- COMMON FUNCTIONS ------//
		//-------------------------//
		
		//Common function to hide/reset the textoverlay state
		function resetCommandline() {
			cl.object.html('');
			cl.object.parent().hide();
			cl.keylog = '';
			cl.keyhistory = cl.keyhistory_escaped = cl.match =  '';
		}
		
		function ifthekey(event,code,func) {
			//EVENT , THE CHARACTER CODE , THE FUNCTION TO CALL
			if (event.keyCode == code) {
				event.preventDefault();
	    		func.call();
		    }  
		}
		
		
		//-------------------------//
		//- EVENTS ----------------//
		//-------------------------//
		
		$(document).keypress(function(e) {
		    
		    //Get the printable characters
		    cl.keylog += String.fromCharCode(e.which);
		    cl.currentkey = e.which;
		    
		    cl.contentoutput = cl.keylog;
		    cl.contentoutput = cl.contentoutput.replace( new RegExp( "\\r", "g" ), '<br />');
		    cl.object.html(cl.contentoutput);
		    
		    //cl.object.append('<span class="cursor"></span>');
		    if(cl.keylog.length == 1){
		    	cl.object.parent().show();
		    }
		    
		    
		    
		    
		});
		
		$(document).keydown(function(e) {
		    
		    //-- Get the del character ----------------------------------------------------
			ifthekey(e,'8',function(){ resetCommandline(); });
		    
		    //-- Hide the text overlay and reset it's contents if ESC is hit ----------------------------------------------------
			ifthekey(e,'27',function(){ resetCommandline(); });
			
			//-- Goto Navigation if enter pressed only on the homepage ----------------------------------------------------
			if( $('body').hasClass('home') && cl.keyhistory == '' ){
				ifthekey(e,'13',function(){ window.location.href="/navigation/"; });
			}
			
		});
		
		
		
		
		/*------------------------------------------------------------------*/	
		
		/* Make a list of keys for cl.match() ---------*/
		for(var key in set.keywords) {
			var daKey = key.replace('kwd_', ''); //Strip out kwd_ prefix
			set.keywords_regex += daKey+"|";
			daKey = "";
		}
		set.keywords_regex = set.keywords_regex.substr(9);
		set.keywords_regex = set.keywords_regex.substr(0,set.keywords_regex.length-1);
		set.keywords_regex = new RegExp(set.keywords_regex, "i");
		
		/* Look for the cl.match on keypress ---------*/
		$(document).keypress(function(e) {
		
			cl.keyhistory += String.fromCharCode(e.which)
		    cl.keyhistory_escaped = cl.keyhistory;
		    cl.keyhistory_escaped = cl.keyhistory_escaped.toLowerCase();
		    cl.keyhistory_escaped = cl.keyhistory_escaped.replace(/ /g,"_");
		    cl.match = cl.keyhistory_escaped.match(set.keywords_regex); 
		    //cl.match = cl.keyhistory.cl.match(set.keywords_regex); 
		    if(cl.match) {
		        
		        //Call the function based on the cl.matched key
		        set.keywords["kwd_"+cl.match].apply(); //Strip out kwd_ prefix
		        
		        cl.keyhistory = cl.keyhistory_escaped = cl.match =  '';
		    }
		});
		
		
		
		 		
	};
})(jQuery);
