var Reg = Class.create({
	initialize: function(){
	
	var _autotypeaheadArray = [];
	
	log("selected country="+countryTitleMapParse);
	var _tmpcnt=0;
	Titlemap.list.each(function(item){
		var discId,name,_edition,_country;
		//var buildautocategoriesListArray=[];
		
		item.list.each(function(arg){ 
			if(arg.discId != undefined)
			{
				discId = arg.discId;
			}
			if(arg.edition != undefined)
			{
				_edition = arg.edition; 
			}
			if(arg.name != undefined)
			{	name = arg.name;
				
			}
			arg.country.each(function(c){
				if(c.id != undefined)
				{
					if(c.id == countryTitleMapParse)
					{_country = c.id;
					}
				}
			});
		});

		
		//buildautocategoriesListArray.push({title:name, id:discId});
		//myArray.push(buildautocategoriesListArray);
			if(countryTitleMapParse == _country)	
			{	_tmpcnt+=1;
				//log(name +" : " + discId + " : " + _edition + "count of found items" + _tmpcnt);
				_autotypeaheadArray.push({title:name, id:discId,edition:_edition});
			
			}
		});
	this.contentArray = [];
	this.selectedTitle;
	//this.categories.sort();
	this.categories = _autotypeaheadArray;
	this.alertOther = false;
	window.onclick=function(event)
	{	
		if(event.target.id != "typeahead" || event.target.id != "hints" )
		{	
			if($("hints"))
				$("hints").hide();
			//$('hints').fade({ duration: 1.0, from: 1, to: 0 });

			
		}
	}
	
	$("autodvdtitle").observe("focus",this.onFocusDistrict);
	$("autodvdtitle").observe("keyup",this.onKeyEntry);
	
	//$("autodvdtitleNext").observe("focus",this.onFocusName);
	//$("divRegForm").observe("click",this.onClickContainer);
	
  },
  exactMatch: function(value){
	  value = value.split(" (");
	  for(var i=0; i< this.categories.length; i++){

		  if(value[0].toLowerCase() == this.categories[i].title.toLowerCase()){
			  this.selectedTitle = this.categories[i];
			  return true;
		  }
	  }

	  return false;
  },
  matchCategory: function(value){
	  if(!value || value.length < 1)
		  	return [];

	  var regExp = new RegExp("^"+value+"| "+value, "i");

	  //console.log(value);

	  var array = [];
	  this.contentArray = [];
	  
	  for(var i=0; i< this.categories.length; i++){
		  var isMatch = regExp.test(this.categories[i].title);
		  if(isMatch == true){
			  array.push(this.categories[i]);
			 // this.contentArray.push(this.categories[i]);
		  	}
	  }
	  //log("MatchCategory array output" + array);
	  this.contentArray = array;
	  return array;
  },
  onFocusDistrict: function(event){
	  $(test).alertOther = true;
  },
  onClickContainer: function(event){
	  $("hints").hide();
	  //$('hints').fade({ duration: 1.0, from: 1, to: 0 });
	  //$("formDeptSelect").show();
  },
  onFocusName: function(event){

	 if( $("autodvdtitle").value.length == 0){
		 return;
	 }

	 if(!$(test).exactMatch($("autodvdtitle").value) && $(test).alertOther ){
		 var value = confirm("You have enterred a Theater Circuit that is not in our list.\n\nClick OK to confirm, or Cancel to change.");
		 if(!value){
			 $("autodvdtitle").value = "";
			 setTimeout(function(){$("autodvdtitle").focus();}, 1);
		 }else{
			 setTimeout(function(){$("autodvdtitleNext").focus();}, 1);
		 }
		 $(test).alertOther = false;
	 }

  },
  onKeyEntry: function(event){
	  //$("autodvdtitle").value
	  var list = [];
	  list = $(test).matchCategory($("autodvdtitle").value);
	  log("onKeyEntry list length : "+list.length);
	  if(list.length > 0){
		 
		contentArray = list;
		//alert(list.join());
		if($("hintsContainer")){
			$("hintsContainer").remove();
		}
		var markup = "<div id='hintsContainer'>"+$(test).createHints(list)+"</div>";
		//console.log(markup);
		$("hints").insert(markup);
		$("hints").show();
		//$("hints").appear({ duration: 1.0, from: 0, to: 1 });
		//$("formDeptSelect").hide();
	  }
	  else
	  {
		$("hints").hide();
		//  $('hints').fade({ duration: 1.0, from: 1, to: 0 });
		//$("formDeptSelect").show();
	  }
  },
  onClickHint: function(value){
	 //log(this.contentArray.length + "content array length");
	  $("autodvdtitle").value = this.contentArray[value].title +" ("+this.contentArray[value].edition +")";
	  this.selectedTitle = this.contentArray[value];
	  //this.selectedTitle = "Blah nigga";
	  $("hints").hide();
	  //$('hints').fade({ duration: 1.0, from: 1, to: 0 });
	  //$("formDeptSelect").show();
  },
  createHints: function(array){
	  var hints = "";
	  for(var i=0; i< array.length; i++){
		 // hints+='<div id="typeahead" class="hint" onclick="test.onClickHint(\''+array[i].title+','+i+'\');">'+array[i].title+' ('+array[i].edition +')</div>';
		  hints+='<div id="typeahead" class="hint" onclick="test.onClickHint(\''+i+'\');">'+array[i].title+' ('+array[i].edition +')</div>';

	  }
	  return hints;
  }
});
var test;
var countryTitleMapParse;
function onTypeBAhead(country){
	countryTitleMapParse = country;
	test = new Reg();
}
function noTypeBAhead(){
		$("divRegForm").stopObserving("click",this.onClickContainer);
		test = null;
}


