// JavaScript Document

	GJF.Class.create("GJF.Controls.Typer", "GJF.Controls.Control", 
		function(config) {
			var thisContext = this;
			GJF.Controls.Control.call(this, config);
			$(document).ready(function(){
			    thisContext.initialize();
			});
		}, 
		{

            backKeyIndex: 0,
            backKeyInterval: 0,
            backKeyPressPerSecond: .05,
            currentPhrase: "",
            currentPhraseIndex: 0,
            currentCharIndex: 0,
            defaultPhrase: "professionals.",
            lettersPerSecond: .12,
            phrases: [
                "Social Media Marketing.",
                "Alternative Advertising.",
                "Guerrilla Marketing.",
                "SEO Services.",
                "Online Reputation Management.",
                "Green Advertising.",
                "Branding.",
                "Optimized Web Publishing.",
                "Web Design.",
                "Web 2.0 Strategy.",
                "Public Relations.",
                "Experientail Marketing.",
                "Multimedia Production.",
                "Digital Distribution."
				"Profile Management."
				"Social Grocery."
				"Guerrilla Theatre."
				"2.0 User Experience."
            ],
            ponderLength: 3,
            ponderDeleteLength: 1.5,
            typeKeyInterval: 0,
            
            backKey: function() {
                this.backKeyIndex++;
                var phrase = this.currentPhrase || this.defaultPhrase;
                $("#Typed").html(this.getHighlightedPhraseHtml(phrase, this.backKeyIndex));
                if (this.backKeyIndex == phrase.length) {
                    this.backKeyIndex = 0;
                    this.cycleCurrentPhrase();
                    this.haltHighlighted();
                }
            },
            
            backKeyPress: function() {
                this.backKeyInterval = setInterval(GJF.Delegate.create(this, this.backKey), (this.backKeyPressPerSecond * 1000));
            },
            
            cycleCurrentPhrase: function() {
                if ((this.currentPhraseIndex + 1) < this.phrases.length) {
                    this.currentPhraseIndex++;
                } else {
                    this.currentPhraseIndex=0;
                }
                this.currentPhrase = this.phrases[this.currentPhraseIndex];
            },
            
            clearTyped: function() {
                $("#Typed").html("");
                this.typePress();
            },

            getHighlightedPhraseHtml: function(phrase, numHighlightedChars) {
                if (phrase.length-numHighlightedChars >= 0) {
                    var html = phrase.substr(0, (phrase.length-numHighlightedChars));
                    if (numHighlightedChars > 0) {
                        html += this.wrapInHighlightedHtml(phrase.substr((phrase.length-numHighlightedChars), phrase.length));
                    }
                    return html;
                } else {
                    return phrase;
                }
            },

            haltHighlighted: function() {
                clearInterval(this.backKeyInterval);
                this.ponderDeleting();
            },

            
            haltTyped: function() {
                clearInterval(this.typeKeyInterval);
                this.ponder();
            },
            
            initialize: function() {
                $("#Typed").html(this.defaultPhrase);
                this.ponder();
            },
            
            ponder: function() {
                setTimeout(GJF.Delegate.create(this, this.backKeyPress), (this.ponderLength * 1000));
            },
            
            ponderDeleting: function() {
                setTimeout(GJF.Delegate.create(this, this.clearTyped), (this.ponderDeleteLength * 1000));
            },
            
            typeKey: function() {
                if (this.currentCharIndex < this.currentPhrase.length) {
                    this.currentCharIndex++;
                    $("#Typed").html(this.currentPhrase.substr(0, this.currentCharIndex));
                } else {
                    this.currentCharIndex = 0;
                    this.haltTyped();
                }
            },
            
            typePress: function() {
                this.typeKeyInterval = setInterval(GJF.Delegate.create(this, this.typeKey), (this.lettersPerSecond * 1000));
            },
            
            wrapInHighlightedHtml: function(chars) {
                return "<span class='PhraseHighlighted'>" + chars + "</span>";
            }

		}
	);