(function($, $C){
	//Static Category Logger.
	var log = $.Logging.getLogger("AlphabetCity.Controllers.Necklace");
	var Necklace = AlphabetCity.Models.Necklace;
	/** -   
	*	Necklace Controller    - 
	*/
	$C.Necklace$Class = {
		steps:null,
		abcNecklace:null,
		maxBlocks:null,
		cartUrl: null,
		cursorPosition:0,
	    constructor: function(options){
	       $.extend( this, $C.Necklace$Class);
	       $.extend(true, this, options);
		   this.current = 0;
	    },
	    open: function(event, mvc){
	    	log.debug("Opening Necklace Customization Dialog");
	       	this.abcNecklace.$metal = $("#abc_metal").attr('value');
	    	mvc.m = {
	    		currentStep: this.current,
	    		totalSteps: this.steps.length,
	    		step: this.steps[this.current],
    			cost:Necklace.cost(this.abcNecklace),
    			necklace: Necklace.js(this.abcNecklace)
	    	};
	    	mvc.v += ".initialize";
	    	mvc.resolve();
	    },
	    startOver: function(event, mvc){
	    	log.debug("Restarting Necklace Customization.");
	    	this.current = 0;
	    	this.abcNecklace = new Necklace();
	    	this.abcNecklace.$metal = $("#abc_metal").attr('value');
	    	this.cursorPosition = 0;
    		mvc.m = {
	    		currentStep: this.current,
	    		totalSteps: this.steps.length,
    			step : this.steps[this.current],
    			cost:Necklace.cost(this.abcNecklace),
    			necklace: Necklace.js(this.abcNecklace)
			};
	    	mvc.v += ".beginStep";
	    	mvc.resolve();
	    },
	    previous: function(event, mvc){
	    	this.current = this.current-1;
	    	if(this.current === 0){
		    	this.abcNecklace = new Necklace();
		    	this.abcNecklace.$metal = $("#abc_metal").attr('value');
	    	}
    		mvc.m = {
	    		currentStep: this.current,
	    		totalSteps: this.steps.length,
    			step : this.steps[this.current],
    			cost:Necklace.cost(this.abcNecklace),
    			necklace: Necklace.js(this.abcNecklace)
			};
	    	mvc.v += ".beginStep";
	    	mvc.resolve();
	    },
	    chooseCollection: function(event, mvc){
	    	log.debug("Choosing Necklace Collection");
	    },
	    chooseLength: function(event, mvc){
	    	var check;
	    	//we dont want to handle a click on the drop down
	    	//arrow, just on the drop down options
	    	if(event.target.nodeName.toUpperCase() === 'OPTION'
	    	|| ( event.target.nodeName.toUpperCase() === 'SELECT' 
	    		&& $(event.target).attr('value') !== "")){
		    	log.debug("Choosing a Necklace Length");
		    	this.abcNecklace.$length = (event.target.nodeName.toUpperCase() === 'SELECT' )?
		    		$(event.target).attr('value'):
		    		$(event.target).parent().attr('value');
		    	check = Necklace.validate(this.abcNecklace, 'length');
		    	if(check.valid){
		    		log.debug("Necklace Length is Valid %s", this.abcNecklace.$length);
		    		this.current++;
		    	}else{
		    		log.info("Necklace Length is Invalid %s. \t\n%s", 
		    			this.abcNecklace.$length, check.message);
	    		}
	    		mvc.m = {
	    			currentStep: this.current,
	    			totalSteps: this.steps.length,
	    			step : this.steps[this.current],
    				necklace: Necklace.js(this.abcNecklace),
    				cost:Necklace.cost(this.abcNecklace),
    				check: check
				};
		    	mvc.v += ".beginStep";
		    	mvc.resolve();
    		}
	    },
	    chooseClasp: function(event, mvc){
	    	log.debug("Choosing a Necklace Clasp");
	    },
	    chooseCordColor: function(event, mvc){
	    	var check;
	    	if(event.target.nodeName.toUpperCase() === 'OPTION'
	    	|| ( event.target.nodeName.toUpperCase() === 'SELECT' 
	    		&& $(event.target).attr('value') !== "")){
		    	log.debug("Choosing a Necklace Cord Color");
		    	this.abcNecklace.$cord = (event.target.nodeName.toUpperCase() === 'SELECT' )?
		    		$(event.target).attr('value'):
		    		$(event.target).parent().attr('value');
		    	check = Necklace.validate(this.abcNecklace, 'cord');
		    	if(check.valid){
		    		log.debug("Necklace Cord Color is Valid %s", this.abcNecklace.$cord);
		    		this.current++;
		    	}else{
		    		log.info("Necklace Cord Color is Invalid %s. \n\t%s", 
		    			this.abcNecklace.$cord, check.message );
	    		}
	    		mvc.m = {
	    			currentStep: this.current,
	    			totalSteps: this.steps.length,
	    			step : this.steps[this.current],
    				necklace: Necklace.js(this.abcNecklace),
    				cost:Necklace.cost(this.abcNecklace),
    				check: check
				};
		    	mvc.v += ".beginStep";
		    	mvc.resolve();
    		}
	    },
	    modifyBlocks: function(event, mvc){
	    	var letter, guid;
		    if(event.target.nodeName.toUpperCase() === 'TD'){
		    	letter = $(event.target).text();
		    	if(letter.toUpperCase()=='DELETE' ){
		    		if(this.abcNecklace.$block.length > 0 && this.cursorPosition > 0){
			    		this.cursorPosition--;
		    			this.abcNecklace.$block.splice(this.cursorPosition, 1);
				    	mvc.m = {
				    		removeBlock: this.cursorPosition
				    	};
		    		}else{return;}
		    	}else if(letter.toUpperCase()=='BACK' ){
		    		if(this.abcNecklace.$block.length > 0 && this.cursorPosition > 0){
			    		this.cursorPosition--;
				    	mvc.m = {
				    		setCursor: this.cursorPosition,
				    		back:true
				    	};
		    		}else{return;}
		    	}else if(letter.toUpperCase()=='FORWARD' ){
		    		if(this.cursorPosition < this.abcNecklace.$block.length){
			    		this.cursorPosition++;
				    	mvc.m = {
				    		setCursor: this.cursorPosition
				    	};
		    		}else{return;}
		    	}else if(letter.replace(/\s/g,'')==''){
		    		return;
		    	}else{
			    	log.debug("Adding a Letter %s", letter);
			    	if(letter.toUpperCase().match('BLANK')){
			    		letter = "■";
			    	}
			    	//This is where we make sure it's valid to
			    	//add another.  Right now our business logic
			    	//is as simple as 'length less than n'.
			    	guid = Math.abs($.createGUID());
			    	if(this.abcNecklace.$block.length < this.maxBlocks){
			    		this.abcNecklace.$block.splice(this.cursorPosition, 0, {
			    			code	: letter, 
			    			metal	: this.abcNecklace.$metal,
			    			id	: guid
		    			});
			    		this.cursorPosition++;
				    	mvc.m = {
				    		newBlock: letter,
				    		newBlockId: guid
				    	};
			    	}else{
			    		mvc.m = {
			    			flash : ["At most " + this.maxBlocks + " may be selected."]
			    		}
			    	}
	    		}
	    		$.extend(true, mvc.m, {
	    			currentStep: this.current,
	    			totalSteps: this.steps.length,
	    			step : this.steps[this.current],
	    			necklace: Necklace.js(this.abcNecklace),
    				cost:Necklace.cost(this.abcNecklace),
	    			check: {valid:true}
    			});
				mvc.v += ".updateBlocks";
				mvc.resolve();
	    	}
	    },
	    modifyBlockMetal: function(event, mvc){
	    	var i, id;
	    	id = $(event.target).attr('name').replace(/(block\/|.abc)/g,'');
	    	log.debug("Modifying the Block Metal");
	    	if(event.target.checked == true){
				for(i=0;i<this.abcNecklace.$block.length;i++){
					if(this.abcNecklace.$block[i].id.toString() == id){
						this.abcNecklace.$block[i].metal = 
							(this.abcNecklace.$metal == 'SILVER')?'GOLD':'SILVER';
						mvc.m = {
							block: this.abcNecklace.$block[i]
						};break;
					}
				}	    		
	    	}else{
				for(i=0;i<this.abcNecklace.$block.length;i++){
					if(this.abcNecklace.$block[i].id.toString() == id){
						this.abcNecklace.$block[i].metal = 
							(this.abcNecklace.$metal == 'SILVER')?'SILVER':'GOLD';
						mvc.m = {
							block: this.abcNecklace.$block[i]
						};break;
					}
				}	
	    	}
    		$.extend(true, mvc.m, {
    			currentStep: this.current,
    			totalSteps: this.steps.length,
    			step : this.steps[this.current],
    			necklace: Necklace.js(this.abcNecklace),
    			cost:Necklace.cost(this.abcNecklace),
    			check: {valid:true}
			});
	    	mvc.v += '.updateBlockMetal';
	    	mvc.resolve();
	    },
	    confirm: function(event, mvc){
	    	log.debug("Confirming the Necklace Letters");
	    	var check = Necklace.validate(this.abcNecklace, 'block');
	    	if(check.valid){
	    		log.debug("Necklace Block Configuration is Valid %s", this.abcNecklace.$block);
	    		this.current++;
	    	}else{
	    		log.info("Necklace Block Configuration is Invalid %s. \n\t\%s", 
	    			this.abcNecklace.$block, check.flash.join("\n"));
    		}
    		mvc.m = {
    			currentStep: this.current,
    			totalSteps: this.steps.length,
    			step : this.steps[this.current],
    			necklace: Necklace.js(this.abcNecklace),
    			cost:Necklace.cost(this.abcNecklace),
    			check: check
			};
	    	mvc.v += ".beginStep";
	    	mvc.resolve();
	    },
	    addToCart: function(event, mvc){
	    	var _this = this;
	    	log.debug("Adding Necklace to Cart");
	    	Necklace.submit(this.abcNecklace, this.cartUrl, function(html){
	    		var mini_cart = $("#mini_cart", html);
	    		var flash = $("#flash", html);
	    		mvc.m = {
	    			cart:  mini_cart,
	    			flash: flash,
	    			currentStep: _this.current,
	    			totalSteps: _this.steps.length,
	    			step : _this.steps[_this.current],
	    			necklace: Necklace.js(_this.abcNecklace),
    				cost:Necklace.cost(_this.abcNecklace)
    			};
	    		mvc.v += ".showCartUpdate";
	    		mvc.resolve();
	    	});
	    	this.close();
    		mvc.v += ".showCartLoading";
    		mvc.resolve();
	    },
	    cancel: function(event, mvc){
	    	log.debug("Canceling Necklace Customization");
	    	$.$("#NecklaceView").dialog('cancel');
	    },
	    close: function(event, mvc){
	    	log.debug("Opening Necklace Customization Dialog");
	    	$.$("#NecklaceView").hide("clip", { 
		        direction: "horizontal"
		    }, 800, function(){
		    	$(this).dialog('close');
		    });
	    }
	};
	$C.Necklace   = $C.Necklace$Class.constructor;
})(jQuery, AlphabetCity.Controllers);