(function ($) {
    /**
     * Id of the Form
     * */
    var formId = 'lLForm';
	var LocationList = function(params) {
		var dealer = params.dealer;
		var zipCode = params.zipCode;
		var state = params.state;
		var brand = params.franchise;
		var franchises = params.franchises;
		var i;

		/**
		 * Takes an array and romoves duplicate data, and empty strings
		 * @params an array
		 * @returns  array of unique values, with no empty strings
		 *
		 * */
		var arrayScrub = function(array) {
			var i;
			var test;
			var testArray = [];
			for (i = 0; i < array.length; i++) {
				test = 0;
				for (var x = 0; x < testArray.length; x++) {
					if (array[i] == testArray[x]) {
						test = 1;
					}
				}
				if (test === 1) {
					test = 0;
				} else {
					if (array[i].length > 0) {
						testArray.push(array[i]);
					}
				}
			}
//            console.log(testArray)
			return testArray;
		};
		/**
		 * Takes an array of us state of canadian poviential abbvs, and
		 * Builds out a selectboxes
		 * name = region
		 * value = abbreviation
		 * key = full name of state/proveince
		 * @params an array
		 * @returns outputs select boxes to dom element with name = 'region'
		 * */
		var buildRegionList = function(array) {
			var i, id = $('#' + formId);
            for (i = 0; i < array.length; i++) {
				switch (array[i]) {
					case 'AL':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("Alabama", "AL");
						break;
					case 'AK':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("Alaska", "AK");
						break;
					case 'AZ':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("Arizona", "AZ");
						break;
					case 'AR':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("Arkansas", "AR");
						break;
					case 'CA':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("California", "CA");
						break;
					case 'CO':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("Colorado", "CO");
						break;
					case 'CT':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("Connecticut", "CT");
						break;
					case 'DE':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("Delaware", "DE");
						break;
					case 'DC':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("District of Columbia", "DC");
						break;
					case 'FL':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("Florida", "FL");
						break;
					case 'GA':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("Georgia", "GA");
						break;
					case 'HI':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("Hawaii", "HI");
						break;
					case 'ID':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("Idaho", "ID");
						break;
					case 'IL':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("Illinois", "IL");
						break;
					case 'IN':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("Indiana", "IN");
						break;
					case 'IA':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("Iowa", "IA");
						break;
					case 'KS':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("Kansas", "KS");
						break;
					case 'KY':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("Kentucky", "KY");
						break;
					case 'LA':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("Louisiana", "LA");
						break;
					case 'ME':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("Maine", "ME");
						break;
					case 'MD':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("Maryland", "MD");
						break;
					case 'MA':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("Massachusetts", "MA");
						break;
					case 'MI':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("Michigan", "MI");
						break;
					case 'MN':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("Minnesota", "MN");
						break;
					case 'MS':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("Mississippi", "MS");
						break;
					case 'MO':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("Missouri", "MO");
						break;
					case 'MT':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("Montana", "MT");
						break;
					case 'NE':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("Nebraska", "NE");
						break;
					case 'NV':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("Nevada", "NV");
						break;
					case 'NH':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("New Hampshire", "NH");
						break;
					case 'NJ':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("New Jersey", "NJ");
						break;
					case 'NM':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("New Mexico", "NM");
						break;
					case 'NY':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("New York", "NY");
						break;
					case 'NC':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("North Carolina", "NC");
						break;
					case 'ND':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("North Dakota", "ND");
						break;
					case 'OH':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("Ohio", "OH");
						break;
					case 'OK':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("Oklahoma", "OK");
						break;
					case 'OR':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("Oregon", "OR");
						break;
					case 'PA':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("Pennsylvania", "PA");
						break;
					case 'RI':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("Rhode Island", "RI");
						break;
					case 'SC':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("South Carolina", "SC");
						break;
					case 'SD':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("South Dakota", "SD");
						break;
					case 'TN':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("Tennessee", "TN");
						break;
					case 'TX':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("Texas", "TX");
						break;
					case 'UT':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("Utah", "UT");
						break;
					case 'VT':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("Vermont", "VT");
						break;
					case 'VA':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("Virginia", "VA");
						break;
					case 'WA':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("Washington", "WA");
						break;
					case 'WV':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("West Virginia", "WV");
						break;
					case 'WI':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("Wisconsin", "WI");
						break;
					case 'WY':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("Wyoming", "WY");
						break;
					case 'AB':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("Alberta", "AB");
						break;
					case 'BC':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("British Columbia", "BC");
						break;
					case 'MB':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("Manitoba", "MB");
						break;
					case 'NB':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("New Brunswick", "NB");
						break;
					case 'NL':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("Newfoundland and Labrador", "NL");
						break;
					case 'NT':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("Northwest Territories", "NT");
						break;
					case 'NS ':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("Nova Scotia", "NS");
						break;
					case 'NU':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("Nunavut", "NU");
						break;
					case 'ON':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("Ontario", "ON");
						break;
					case 'PE':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("Prince Edward Island", "PE");
						break;
					case 'QC':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("Quebec", "QC");
						break;
					case 'SK':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("Saskatchewan", "SK");
						break;
					case 'YT':
						$('select[name=region]', id)[0].options[$('select[name=region]', id)[0].options.length] = new Option("Yukon", "YT");
						break;
					default:
						break;
				}
			}
		};
		/**
		 * Builds selectboxes in the dom from a data obj
		 * e.g.
		 * obj.name = name of select
		 * obj.id = id of form
		 * obj.array = array of values to add as options, currently coded to use the array value as both
		 *           value and key in the selectbox
		 *
		 * @params data object
		 * @returns dom
		 * */
		var toSelectBox = function(obj) {
			var i;
			var id = obj.id;
			var name = obj.name;
			var array = obj.array;
			/*
				  console.log('id:'+obj.id)
				  console.log('name:'+obj.name)
				  console.log('array:'+obj.array)
   */
			var selectBox = document.forms[id].elements[name];
			var data = arrayScrub(array).sort();
			if (name === 'region') {
				buildRegionList(data);
			} else {
				for (i = 0; i < data.length; i++) {
					if (data[i].length > 0) {
						selectBox.options[selectBox.options.length] = new Option(data[i], data[i]);
					}
				}
			}
		};


		var behavior = function() {
			$('#lLForm select').change(function () {
				var chName = $(this).attr('name');
				var id = formId;
				for (var i = 0; i < document.forms[id].elements.length; i++) {
					if (!!document.forms[id].elements[i].value && (document.forms[id].elements[i] != document.forms[id].elements[chName])) {
						document.forms[id].elements[i].value = document.forms[id].elements[i].options[0].value;
					}
				}
				$('#lLForm select').not(this).val(':eq0');

				var selected = $(this).val();
				//console.log(selected)
				if (this.getAttribute("name") == "franchise") {
					$("#locationList tbody").find("tr").each(function() {
						if (selected != "all") {
							if (this.getAttribute("franchises") && this.getAttribute("franchises").indexOf(selected) != -1){ 
								$(this).removeClass("hidden");
							}else{
								$(this).addClass("hidden");                                                                 
                            }
						} else if (selected === 'all') {
							$(this).removeClass("hidden");
						}
					});
				} else if (selected === 'all') {
					$('#locationList tbody tr').removeClass('hidden');
				} else {
					$('#locationList tbody tr').addClass('hidden');
					$('#locationList td').each(function() {
						if ($(this).text() == selected) {
							$(this).parent().removeClass('hidden');
						}
					});
				}
			});
			$('#locationList thead th span').hover(function() {
				$(this).addClass('lLSpanhover');
			}, function() {
				$(this).removeClass('lLSpanhover');
			});
		};
		$('#locationList tbody tr').live('click',function(e){
			var that = this;
			var url = $(this).find('a').attr("href");
			if(!$(e.target).parent().is('a')){
				if($(that).find('a').attr('rel')){
					//alert('opening new widow')
					window.open(url);
				} else {
					window.location = url;
				}
			}

		});
		$('.vAll').click(function() {
			$('#locationList tbody tr').removeClass('hidden');
			return false;
		});


		var init = function() {
			/*Here we check for pramas passed in from location form*/
			if (dealer !== 'All' && dealer !== '') {
				//alert('dealer:' + dealer)
				$('#locationList tbody tr').addClass('hidden');
				$('#' + dealer + '').removeClass('hidden');
			}
			if (brand !== 'All' && brand !== '') {
				//alert('brand:' + brand)
				$("#locationList tbody").find("tr").each(function() {
					if (this.getAttribute("franchises") && this.getAttribute("franchises").indexOf(brand) != -1){ 
						$(this).removeClass("hidden");
					 }else{ 
						$(this).addClass("hidden");
                    }
				});
			}
			if (state !== 'All' && state !== '') {
				//alert('state:' + state)
				$('#locationList tbody tr').addClass('hidden');
				$('.region').each(function() {
					if ($(this).text() === state) {
						$(this).parent().removeClass('hidden');
					}
				});
			}
			if (zipCode !== 'All' && zipCode !== '') {
				//alert('zipCode: '+zipCode)
				$('#locationList tbody tr').addClass('hidden');
				$('.postal-code').each(function() {
					if ($(this).text() === zipCode) {
						$(this).parent().removeClass('hidden');
					}
				});
			}

			/*Gather info and build out selectBoxes*/
			$('#lLForm select').each(function() {
				var array = [];
				var thisName = $(this).attr('name');
				if (thisName === 'franchise') {
					array = [];
					for (i = 0; i < franchises.length; i++) {
						$("#locationList tbody tr").each(function() {
							if (this.getAttribute("franchises") && this.getAttribute("franchises").indexOf(franchises[i]) != -1) {
								array.push(franchises[i]);
							}
						});
					}
					toSelectBox({id:formId, array:array, name: thisName});
				} else {
					array = [];
					$('.' + thisName + '').each(function() {
						array.push($(this).text());
					});
					toSelectBox({id:formId, array:array, name:thisName});
				}
			});
			behavior();
		};
		init();
	};

	window.DDC.LocationList = LocationList;
})(jQuery);
