// JavaScript Document
var AutoSuggest = Class.create();

AutoSuggest.prototype = {
    initialize: function(oTextbox /*:HTMLInputElement*/) {    
        this.updateList();
		this.cur /*:int*/ = -1;
		this.layer = $("results");
		this.provider = new Suggestions();
		this.textbox = $(oTextbox);
		var oThis = this;
		this.textbox.observe('keyup',(function (event) { this.handleKeyUp(event); }).bind(this));
		this.textbox.observe('keydown',(function (event) { this.handleKeyDown(event); }).bind(this));
		this.textbox.observe('blur',(function () { this.hideSuggestions(); }).bind(this));
		this.textbox.observe('focus',(function () { if(this.textbox.value != undefined) { this.provider.requestSuggestions(this, true); } }).bind(this));
		this.layer.observe('mousedown', (function(event) {
			oTarget = event.target || event.srcElement; this.textbox.value = oTarget.firstChild.nodeValue;
		}).bind(this));
		this.layer.observe('mouseup', (function(event) {
			oTarget = event.target || event.srcElement; this.textbox.focus();
		}).bind(this));
		this.layer.observe('mouseover', (function(event) {
			oTarget = event.target || event.srcElement; this.highlightSuggestion(oTarget);
		}).bind(this));
	},
	autosuggest: function (aSuggestions /*:Array*/, bTypeAhead /*:boolean*/) {
		
		//make sure there's at least one suggestion
		if (aSuggestions.length > 0) {
			if (bTypeAhead) {
			   this.typeAhead(aSuggestions[0]);
			}
			this.showSuggestions(aSuggestions);
		} else {
			this.hideSuggestions();
		}
	},
	
	/* GETS */
	getLeft: function() {
		var oNode = this.textbox;
		var IE = document.all?true:false;
		var iLeft = oNode.cumulativeOffset(oNode);
		return iLeft[0];
	},
	getTop: function() {
		var oNode = this.textbox;
		var IE = document.all?true:false;
		var iTop = oNode.cumulativeOffset(oNode);
		return iTop[1];
	},
	getNodes: function() {
		var ul = this.layer.down(0);
		if(ul != undefined) {
			return ul.childElements();
		}
		return 0;
	},

	/* GETS */
	
	/* HANDLES */
	handleKeyDown: function(oEvent) {
		switch(oEvent.keyCode) {
			case 38: //up arrow
				this.previousSuggestion();
				break;
			case 40: //down arrow 
				this.nextSuggestion();
				break;
			case 13: //enter
				var nodes = this.getNodes();
				if(nodes != 0)
				{
					var oNode = nodes[this.cur];
					this.textbox.value = oNode.innerHTML;
				}
				this.hideSuggestions();
				break;
		}
	},
	handleKeyUp: function(oEvent) {
		var iKeyCode = oEvent.keyCode;
	
		//for backspace (8) and delete (46), shows suggestions without typeahead
		if (iKeyCode == 8 || iKeyCode == 46) {
			this.provider.requestSuggestions(this, false);
			
		//make sure not to interfere with non-character keys
		} else if (iKeyCode < 32 || (iKeyCode >= 33 && iKeyCode < 46) || (iKeyCode >= 112 && iKeyCode <= 123)) {
			//ignore
		} else {
			//request suggestions from the suggestion provider with typeahead
			this.provider.requestSuggestions(this, true);
		}
	},
	/* HANDLES */
	/* List Functions */
	hideSuggestions: function() {
		this.layer.hide();
	},
	
	highlightSuggestion: function(suggestion) {
		var nodes = this.getNodes();
		nodes.each(function(node){
			node.setStyle({ 'background':'#fff', 'color':'#000' });
		});
		$(suggestion).setStyle({ 'background':'#003366', 'color':'#fff' });
	},

	nextSuggestion: function() {
		var nodes = this.getNodes();
		if(nodes.length > 0 && this.cur < nodes.length-1) {
			var node = nodes[++this.cur];
			this.highlightSuggestion(node);
			this.textbox.value = node.firstChild.nodeValue; 
		}
	},

	previousSuggestion: function() {
		var ul = this.layer.down(0);
		var nodes = ul.childElements();
		if(nodes.length > 0 && this.cur > 0) {
			var node = nodes[--this.cur];
			this.highlightSuggestion(node);
			this.textbox.value = node.firstChild.nodeValue; 
		}
	},
	selectRange: function(iStart /*:int*/, iLength /*:int*/) {
		//use text ranges for Internet Explorer
		if (this.textbox.createTextRange) {
			var oRange = this.textbox.createTextRange(); 
			oRange.moveStart("character", iStart); 
			oRange.moveEnd("character", iLength - this.textbox.value.length);      
			oRange.select();
			
		//use setSelectionRange() for Mozilla
		} else if (this.textbox.setSelectionRange) {
			this.textbox.setSelectionRange(iStart, iLength);
		}     
	
		//set focus back to the textbox
		this.textbox.focus();  
		//146
	},

	
	showSuggestions: function(aSuggestions /*:Array*/) {
		var ul = Builder.node("ul");
		this.layer.update();  //clear contents of the layer
		
		aSuggestions.each( function(Node) {
			ul.appendChild(Builder.node("li", Node ));
		} );
		this.layer.appendChild(ul);
		this.layer.show();
		this.layer.setStyle({ 'left':this.getLeft() + "px", 'top':(this.getTop()+this.textbox.offsetHeight) + "px" });
	},
	typeAhead: function(sSuggestion) {
		//check for support of typeahead functionality
		if (this.textbox.createTextRange || this.textbox.setSelectionRange){
			var iLen = this.textbox.value.length; 
			this.textbox.value = sSuggestion; 
			this.selectRange(iLen, sSuggestion.length);
		}
	},
	/* List Functions */
	updateList: function() {
        this.updateList = Prototype.emptyFunction;

        document.observe('click', (function(event){
            var target = event.findElement('input[name^=suggestions]');
            if (target) {
                event.stop();
                this.start(target);
            }
        }).bind(this));
	}
};

Suggestions = Class.create();

Suggestions.prototype = {
    initialize: function() {
		this.states = [
			"2 B Free",
	"2xist",
	"575 Denim",
	"7 Diamonds",
	"7 For All Mankind",
	"A.B.S.",
	"Abercrombie & Fitch",
	"ABS by Allen Schwartz",
	"Acne Jeans",
	"Adidas",
	"Adio",
	"Adrienne Vittadini",
	"Aeropostle",
	"Affliction",
	"AG Adriano Goldschmied",
	"AG Jeans",
	"AK Anne Klein",
	"Alexander McQueen ",
	"Alexandra Neel",
	"Alice & Olivia",
	"Allen Allen",
	"Allen B.",
	"Alpinestars",
	"Ambiguous",
	"American Apparel",
	"American Eagle Outfitters",
	"American Vintage",
	"AND 1",
	"Ann Taylor",
	"Anne Fontaine",
	"Anne Klein",
	"Antik Denim",
	"Aqua",
	"Armani",
	"Armani Exchange",
	"Arnette",
	"Atwater",
	"Baby Phat",
	"Badgley Mischka",
	"Balenciaga",
	"Bally",
	"Banana Republic",
	"Bang & Olufsten",
	"BCBGeneration",
	"BCBGirls",
	"BCBGMAXAZRIA",
	"Becca",
	"Betsey Johnson",
	"Billabong",
	"Blue Marlin",
	"Blumarine ",
	"Bottega Veneta",
	"Brian Atwood",
	"Brioni",
	"Brooks Brothers",
	"Bruno Magli",
	"Buffalo Jeans",
	"Burberry",
	"Burton",
	"C & C California",
	"Callaway",
	"Calvin Klein",
	"Canali",
	"Carmen Marc Valvo",
	"Carolina Herrera",
	"Catherine Malandrino",
	"Celine",
	"Champion",
	"CHANEL",
	"Chantelle",
	"Charles by Charles David",
	"Charles David",
	"Charlotte Ronson",
	"Charlotte Russe",
	"Chinese Laundry",
	"Chip & Pepper",
	"Chloe",
	"Christian Audigier",
	"Christian Dior",
	"Christian Louboutin",
	"Christian V. Siriano",
	"Christopher Blue ",
	"Circa",
	"Citizens of Humanity",
	"Club Monaco",
	"COACH",
	"Coexist",
	"Cole Haan",
	"Columbia",
	"Converse",
	"Cosabella",
	"Cream Soda",
	"CROCS",
	"Cynthia Rowley",
	"D.Squared",
	"Dakine",
	"Dana Buchman",
	"Da-Nang",
	"David Meister",
	"David Yurman",
	"DC Shoes",
	"Delia's",
	"Derek Lam ",
	"Diane Von Furstenberg",
	"Dickies",
	"Diesel",
	"Dina Bar-El",
	"Dior ",
	"Dirty English",
	"Diva",
	"DKNY",
	"Dolce & Gabbana",
	"Dolce Vita",
	"Donald J. Pliner",
	"Donna Karan",
	"Donna Karan Collection ",
	"Dooney & Burke",
	"Dunhill",
	"DVS",
	"Earnest Sewn",
	"Easy Spirit",
	"Eck? Unlimited",
	"Ed Hardy",
	"Eileen Fisher",
	"Electric",
	"Element",
	"Elie Tahari",
	"Elizabeth & James",
	"Ella Moss",
	"Ellen Tracy",
	"Emilio Pucci",
	"Emporio Armani",
	"Emu",
	"Envi",
	"Ermenegildo Zegna",
	"Escada",
	"Esprit",
	"Etnies",
	"Etro",
	"Evisu",
	"Express",
	"Ezekiel",
	"Faconnable",
	"Fashionista",
	"Fendi",
	"Forever 21",
	"Fossil",
	"Frankie B",
	"Free People",
	"Fred Perry",
	"French Connection",
	"Frye",
	"Fyasko",
	"Gap",
	"Giorgio Armani",
	"Giuseppe Zanotti",
	"Globe",
	"GRN Apple Tree",
	"GUCCI",
	"Guess",
	"G-Unit",
	"H&M",
	"Habitual",
	"Hanky Panky",
	"Hard Tail",
	"Havaianas",
	"Hermes",
	"Herve Leger",
	"Hickey",
	"Hogan",
	"Hollister Co",
	"Hot Topic",
	"House of Dereon",
	"Hoven",
	"Howe",
	"Hudson Jeans",
	"HUGO",
	"Hugo Boss",
	"Hurley",
	"Ike Behar",
	"Inca",
	"Independent",
	"Isaac Mizrahi",
	"J Brand",
	"J. Crew",
	"J. Lindeberg",
	"J. shoes",
	"Jacadi",
	"Jack Spade",
	"James Jeans",
	"James Perse",
	"Jay Godfrey",
	"Jean Paul Gaultier",
	"Jessica McClintock",
	"Jil Sander",
	"JIMMY CHOO",
	"Joe's Jeans",
	"John Hardy",
	"John Lobb",
	"John Varvatos",
	"Johnston & Murphy",
	"Joie",
	"Jones New York",
	"Jordache",
	"Jordan",
	"Joseph Abboud",
	"Juicy Couture",
	"Junk Food Tees",
	"Karl Lagerfeld",
	"Kashmere",
	"Kate Spade",
	"Kay Unger",
	"Kenneth Cole New York",
	"Kenneth Cole Reaction",
	"Kenzo",
	"KORS Michael Kors",
	"K-Swiss",
	"L Space",
	"L.A.M.B.",
	"La Perla",
	"La ROK",
	"Lacoste",
	"Lanvin",
	"Laundry",
	"Laundry by Design",
	"Laundry by Shelli Segal",
	"Lauren Conrad",
	"Lauren Raplh Lauren",
	"Le Mystere",
	"Le Tarte",
	"Le Tigre",
	"Lela Rose",
	"Levi's Premium",
	"Lilly Pulitzer",
	"Limited",
	"Lost",
	"Louis Vuitton",
	"LRG",
	"Lucky Brand Jeans",
	"M Missoni",
	"Mada",
	"Malia Mills",
	"Marc by Marc Jacobs",
	"Marc Ecko",
	"Marc Jacobs",
	"Marc New York",
	"Marni",
	"Matix",
	"Mavi Jeans",
	"Matthew Williamson",
	"MaxMara",
	"McQ By Alexander McQueen",
	"Members Only",
	"Michael Kors",
	"MICHAEL Michael Kors",
	"Michael Stars",
	"Miss Sixty",
	"Missoni",
	"Miu Miu",
	"Modern Amusement",
	"Moncler",
	"Moschino",
	"Moschino Cheap & Chic ",
	"Mossimo",
	"Nanette Lepore",
	"Nautica",
	"Neil Barrett",
	"New Balance",
	"Nicole Miller",
	"Nike",
	"Nine West",
	"Nixon",
	"No Fear",
	"Norma Kamali",
	"Oakley",
	"Obey",
	"Old Navy",
	"Oliver Peoples",
	"O'neill",
	"Original Penguin",
	"Oscar de la Renta",
	"Pac Sun",
	"Paige Black Label",
	"Paige Premium Denim",
	"Paper Denim & Cloth",
	"Paris Hilton",
	"Paul Frank",
	"Paul Smith",
	"Penguin",
	"Perry Ellis",
	"Phoebe Couture",
	"Piazza Sempione",
	"PJ Salvage",
	"Polo Ralph Lauren",
	"Prada",
	"Primp",
	"Pringle",
	"Pringle of Scotland ",
	"Priorities",
	"Project E",
	"Puma",
	"Qi",
	"Qi Cashmere",
	"Quiksilver",
	"Quiksilver Edition",
	"Quotation",
	"Rachel Pally",
	"Rachel Roy",
	"Rafe New York",
	"RALPH ",
	"Ralph Lauren",
	"Ralph Lauren Black Label",
	"Ralph Lauren Sport",
	"Rebecca Beeson",
	"Rebecca Taylor",
	"Red Carter",
	"Reebok",
	"Reef",
	"Report Signature",
	"Rip Curl",
	"Robert Talbott",
	"Roberto Cavalli",
	"RocaWear",
	"Rock & Republic",
	"Rocket Dog",
	"Roger Vivier",
	"Rogues Gallery",
	"Romeo & Juliet Couture",
	"Roxy",
	"Royal Underground",
	"Rusty",
	"RVCA",
	"Salvatore Ferragamo",
	"Sanctuary",
	"Sass & Bide",
	"Sean John",
	"See by Chloe",
	"Seven for All Mankind",
	"Shoshanna",
	"Single",
	"Spanx ®",
	"Sperry",
	"Splendid",
	"Split",
	"St. John",
	"Stella McCartney",
	"Steve Madden",
	"Stuart Weitzman",
	"Sweet Pea",
	"Sweetface",
	"Tadashi",
	"TAG Jeans",
	"Tahari",
	"Tart",
	"Taryn by Taryn Rose",
	"Taverniti So Jeans",
	"Tbags",
	"Ted Baker",
	"The North Face",
	"Theory",
	"Tiffany & Co.",
	"To Boot New York",
	"TOD'S",
	"Tom Ford",
	"Tommy Bahama",
	"Tommy Hilfiger",
	"Tory Burch",
	"Trina Turk",
	"Trovata",
	"True Religion",
	"Trunk LTD",
	"Twelfth St. By Cynthia Vincent",
	"UGG® Australia",
	"Under Armour",
	"United Colors of Benetton",
	"Urban Outfitters",
	"Valentino",
	"Vans",
	"Vera Wang",
	"Versace",
	"Vestal",
	"Via Spiga",
	"Victoria's Secret",
	"Vilebrequin",
	"Vince",
	"Vivien Westwood",
	"Vivienne Tam",
	"Vix",
	"Volcom",
	"Von Zipper",
	"Wet Seal",
	"William Rast",
	"Wrangler",
	"Yves Saint Laurent",
	"Zac Posen ",
	"Zanella",
	"ZARA",
	"Zegna",
	"Zegna Sport",
	"Zoo York"
	
		];
	},
	requestSuggestions: function (oAutoSuggestControl /*:AutoSuggestControl*/, bTypeAhead /*:boolean*/) {
		var aSuggestions = [];
		var sTextboxValue = oAutoSuggestControl.textbox.value;
		
		var capSugs = [];
		for(x in this.states)
		{
			capSugs[x] = this.states[x];
		}
		
		
		if (sTextboxValue.length > 0){
		
			for (var i=0; i < this.states.length; i++) { 
				if(this.states[i].toUpperCase().indexOf(sTextboxValue.toUpperCase()) == 0)
				{
					aSuggestions.push(this.states[i]);
				}
			}
		}
	
		//provide suggestions to the control
		oAutoSuggestControl.autosuggest(aSuggestions, bTypeAhead);
	}
};
