var typingTimer;                //timer identifier
var doneTypingInterval = 200;  //time in ms
//var searchType = "union";
var tempValue_AUTOComplete = 'Enter Zip';
var searchType = "zip";

$(function(){
	$('.topNav_BG .searchBox .search').removeAttr("disabled"); 
	$('.topNav_BG .searchBox .search').bind('keydown', showHints);
	$('.topNav_BG .searchBox .search').bind('keyup', function(){
		typingTimer = setTimeout(search_stores_withHints, doneTypingInterval);
	});
	
	
	$('.topNav_BG .searchBox .search').bind('focus', function(){
		$(this).css('color', '#000000');
	}).bind('focusout', function(){
		var thisObj = $(this);
		if(thisObj.val() == tempValue_AUTOComplete || thisObj.val() == ''){
			thisObj.css('color', '#d0cece');
		}
		
	});
	
	
	$('#searchHints .searchHints_Body .searchHints_header a.closeHinds').bind('click', hideHints);
	
	//zip code click
	$('.zipBTN').live('click', changeSearchType);
	//$('.topNav_BG div.off').bind('click', changeSearchType);
});
function commitToSearch(){
	var thisSearch =  $('#autoComplete');
	var thisSearchVal = thisSearch.val();
	
	if(thisSearchVal != "Enter Zip" || thisSearchVal != "Enter Local"){
		
		if( thisSearchVal.length  == 5 && searchType != "union" && !isNaN(thisSearchVal) ){
			window.location = "/Find_My_Local?q="+thisSearchVal
		}else if(tempValue_AUTOComplete == 'Enter Zip'){
			alert("Please enter a valid zip code." );
		}
		/*if(searchType == "union"){
			//getClosestZip(thisSearchVal);
		}else{
			window.location = "/Find_My_Local?q="+thisSearchVal
		}*/
	}
}
function changeSearchType(event){
	var thisObj = $(this);
	var thisSearch = $('#autoComplete');
	var thisId = thisObj.attr('id');
	var tempSearch = '';
	var searchThis = false;
	
	if(thisSearch.val() != tempValue_AUTOComplete){
		tempSearch = thisSearch.val()
		searchThis = true;
	}
	
	
	if(thisObj.attr('searchType') == 'union'){
		searchType = "union";
		obj = "#union";
		tempValue_AUTOComplete = 'Enter Local';
		thisSearch.attr('name', 'local');
	}else{
		searchType = "zip";
		obj = "#zipcode";
		tempValue_AUTOComplete = 'Enter Zip';
		thisSearch.attr('name', 'q');
	}
	
	$('.zipBTN').removeClass('selected');
	//console.log(obj);
	$(obj).addClass('selected');
	
	if(tempSearch == ''){
		tempSearch = tempValue_AUTOComplete;
		searchThis = false;
	}
	$('#autoComplete').attr('tempValue', tempValue_AUTOComplete).attr('value', tempSearch);
	
	if(searchThis == true){
		search_stores_withHints();
	}
}

function showHints(e){
	clearTimeout(typingTimer);
	if (e.keyCode == '13') {
		commitToSearch()
	}
	var searchHint = $('#searchHints');
	searchHint.slideDown("slow");
}

function hideHints(){
	var searchHint = $('#searchHints');
	searchHint.slideUp("slow")
}

function searchLoading(){
	var populateArea = $('#hintDisplay');
	
	var tempHtml = "<img class='loadingScreen' src='/graphics/ajax-loader.gif' width='32' height='32' style='padding: 10px 0 10px 117px;' />";
	
	populateArea.html(tempHtml);
	
}

function getClosestZip(localVal){
	searchLoading();
	$.ajax({
		type: "get",
		url: "/_commongoal/API/StoreLocator/searchAutoComplete.cfc",
		dataType: "json",
		data: {
			method: "getZip",
			localVal: localVal
		},
		success: function(response) {
			for( i=0; i< response.length; i++){
				window.location = "/Find_My_Local?q=" + response[i].ZIP + "&local=" + response[i].LOCALNUMBER
			}
		},
		error: function(response) {
			return false;
		}
	});
	
}
function search_stores_withHints(){
	var searchInput = $('#header .header_right .topNav .searchBox input[type="text"]');
	var searchValue = searchInput.val();
	
	if(searchValue == "") {
		hideHints();
		return false;
	}
	
	searchLoading();
	$.ajax({
		type: "get",
		url: "/_commongoal/API/StoreLocator/searchAutoComplete.cfc",
		dataType: "json",
		data: {
			method: "getHints",
			searchValue: searchValue,
			searchType: searchType
		},
		success: function(response) {
			populateHints(data=response, error=false, searchValue)
		},
		error: function(response) {
			populateHints(data="Couldn't find any details for this locations.", error=true, searchValue)
		}
	});
}

function populateHints(data, error, searchValue){
	
	/*var populateArea = $('#hintDisplay');
	var populateSpecialAreas = $('#specialLocals');*/
	var populateArea = $('.searchHints_content');
	var tempHtml = "<div id='hintDisplay'>";
	var tempSpecialHtml = "<p class='hintQuestion'>Are you a MA specialty craft worker?</p><div id='specialLocals'><p>";
	var count = 0;
	if(!error){
		if(data.length > 0){
			$('.probablySearch').show();
			for( i=0; i< data.length; i++){
				
				// zip results
				if(searchType == "zip"){
					if(data[i].SPECIAL != 1){
						if(data[i].LOCALNUMBER != 1302 && data[i].LOCALNUMBER  != 3073 && data[i].LOCALNUMBER != 3196){
							if(count < 5){
								tempHtml += '<a href="/Find_My_Local?q=' +data[i].ZIP + '&local='+data[i].LOCALNUMBER+'">'
												+'<p class="hint"> Local #' + data[i].LOCALNUMBER 
													+ '<img src="/graphics/icon/searchHintArrow_icon.png" width="9" height="12"  />'
												+'</p>'
											+'</a>';
								count++;
							}
						}
					}else{
						tempSpecialHtml += data[i].NAME + ' <a href=/Find_My_Local?q=' +data[i].ZIP + '&local='+data[i].LOCALNUMBER+'>' 
											+ 'Local #' + data[i].LOCALNUMBER 
										+ '</a> <br />';;
					}
					
				// union results	
				}else{
					if(count < 5){
						tempHtml += '<a href="/Find_My_Local?q=' +data[i].ZIP + '&local='+data[i].LOCALNUMBER+'">'
										+'<p class="hint"> Local #' + data[i].LOCALNUMBER 
											+ '<img src="/graphics/icon/searchHintArrow_icon.png" width="9" height="12"  />'
										+'</p>'
									+'</a>';
						count++;
					}
				}
				
			}
			tempHtml += '</div>';
			
			var tempSearchValue = searchValue;
				tempSearchValue = tempSearchValue.split('');
				tempSearchValue = tempSearchValue[0] + tempSearchValue[1];
			if(tempSpecialHtml != "<p class='hintQuestion'>Are you a MA specialty craft worker?</p><div id='specialLocals'><p>"){
				if(tempSearchValue == 02 || tempSearchValue == 01 ){
					tempSpecialHtml += '</p></div>';
				}else{
					tempSpecialHtml = "";
				}
			}else{
				tempSpecialHtml = "";
			}
		}else{
			if(searchType == 'zip'){
				tempSearchType = 'union';
			}else{
				tempSearchType = 'zip';
			}
			
			$('.probablySearch').hide();
			tempHtml = '<br /><p class="error">Sorry, no results were found for ' + searchType + ' ' + searchValue + '.<br />Did you mean to search by <a href="javascript:;" searchType="' + tempSearchType + '" class="zipBTN">';
			
			tempHtml += tempSearchType;	
						
						
			tempHtml += '</a>?</p>';
			tempSpecialHtml = "";
		}
	}else{
			if(searchType == 'zip'){
				tempSearchType = 'union';
			}else{
				tempSearchType = 'zip';
			}
			$('.probablySearch').hide();
			tempHtml = '<br /><p class="error">Sorry, no results were found for ' + searchType + ' ' + searchValue + '.<br />Did you mean to search by <a href="javascript:;"  searchType="' + tempSearchType + '" class="zipBTN">';
				tempHtml += tempSearchType;	
			tempHtml += '</a>?</p>';
			
			tempSpecialHtml = "";
	}
	populateArea.html(tempHtml + tempSpecialHtml);
	/*populateArea.html(tempHtml);
	populateSpecialAreas.html(tempSpecialHtml);*/
}
