
app = {
	currentState: '',
	initialLoad: false,
	
	
	locationInit: function(resize){
		$("#selectUSA").click(function(e){
			e.preventDefault();
			$("#CanadaList").hide();
			$("#USAList").show();
			$("#stateText").html('State');
		});
		
		$("#selectCanada").click(function(e){
			e.preventDefault();
			$("#CanadaList").show();
			$("#USAList").hide();
			$("#stateText").html('Province');
		});
		
		var f = $('#stateList').find('li').click(function(){
			if (resize === undefined) {
				app.selectState($(this).find('input.shortState').val());
			} else {
				app.selectState($(this).find('input.shortState').val(), 'FALSE');
			}
		});
		
	},
	
	cityInit: function(){
		var f = $('#cityList').find('li').click(function(e){
			app.selectCity($(this).find('input.shortCity').val());
		});
	},
	
	serviceInit: function(){
		var f = $('#serviceList').find('li').click(function(e){
			app.selectService($(this).find('input.shortService').val());
		});
	},
	
	selectState: function(state, resize){
		if(state == 'QC'){
			$("#selectQC").show();
		}
		else{
			$("#selectQC").hide();			
		}
		
		$.ajax({
				url: '/locations/cities/'+state,
				dataType: "json",
				success: function(data) {
					app.clearCities();
					if(!app.initialLoad){
						app.clearServices();						
					}
					else{
						app.initialLoad = false;
					}
					app.clearService();
					app.currentState = state;
					if(data.cityList.length > 0){
						$('p.selectState').hide();
						for(var i=0;i<data.cityList.length;i++){
							$('#cityList').append('<li class="cityCont"><a href="#">'+data.cityList[i]+'</a><input type="hidden" class="shortCity" value="'+data.cityList[i]+'" /></li>');
						}
						if (resize === undefined) {
							if(data.cityList.length > 25){
								var length = data.cityList.length;
								var items = ((length-25) * 30) + 700;
								var height = items+'px';

								$("#pageContent").css('height', height);
							} else {
								$("#pageContent").css('height', '700px');
							}
						}
						app.cityInit();
					}
					else{
						$('p.selectState').show();
						$('p.selectState').html('<div style="text-align: center"><h4>There are no services in this area.</h4></div>');
					}
				}
		});
	},
	
	
	
	
	selectCity: function(city){
		var state = app.currentState;
		var description;
		
		//show zip code
		$('p#userCity').show();
		
		$.ajax({
				url: '/locations/services/'+state+'/'+city,
				dataType: "json",
				success: function(data) {
					$('p#userServices').show().text('Cleaning Service(s) Based on State/City');
				
					app.clearServices();
					app.clearService();
					if(data.serviceList.length > 0){
						for(var i=0;i<data.serviceList.length;i++){
							
							if (data.serviceList[i].description != '') {
								description = ' (' + data.serviceList[i].description + ')';		
							} else {
								description = '';
							}
							
							if(data.serviceList[i].full != 1) {
								$('#serviceList').append('<li class="serviceListItem"><a href="#resultInfo">'+data.serviceList[i].service+'</a><span style="font-size:10px; color:#333;">'+description+'</span><input type="hidden" class="shortService" value="'+data.serviceList[i].serviceID+'" /></li>');
							} else {
								$('#serviceList').append('<li class="serviceListItem">'+data.serviceList[i].service+description+' - FULL</li>');
							}
							
						}
						
						if(data.serviceList.length > 1){
							var length = data.serviceList.length;
							var items = (length * 40) + 700;
							var height = items+'px';
							
							$("#pageContent").css('height', height);
						}
						if(data.full == 1) {
							var htmlText = encodeURI($('span#serviceFullText').html());
							//alert(htmlText);
							$("#serviceList").append('<li class="serviceListItem">&nbsp;</li><li class="serviceListItem" style="text-align: center"><h4><strong>' + decodeURI(htmlText) + '</strong></h4></li>');
							//$('span#serviceFull').show();
						}
						app.serviceInit();
					}
				}
		});		
		
	},

	selectService: function(id){		
		$.ajax({
				url: '/locations/service/'+id,
				success: function(data) {
					app.clearService();
					$('#resultInfo').html(data);
				}
		});		
		
	},


	clearCities: function(){
		$('#cityList').children().remove();
	},
	
	clearServices: function(){
		//$('#serviceList').children().remove();
		$("#serviceList").html('');
	},
	
	clearService: function(){
		$('#resultInfo').html('');
	}
	
	
};

