$(document).ready(function(){
	$(document).ready(function(){
		var modelhtml='';
		blockIt();
		$.ajax({
			url: "/customerservice/getmodels",
			global: false,
			type: "GET",
			data: ({}),
			dataType: "json",
			async: false,
			success: function(json){
				$.each(json, function(i,item){
					modelhtml=modelhtml+"<option value='"+i+"'>"+item.name+"</option>\n";
				});
				$('.model').hide().html('Model: <select class="modeldd" name="model">'+modelhtml+'</select>').fadeIn();
				loadkeywords($('.modeldd').val());
				var modelobj=document.forms['faqsearch'].model;
				$('#modelselected').text(modelobj.options[modelobj.selectedIndex].text);
				$('.modeldd').change(function(){
					loadkeywords($(this).val());
					$('#modelselected').text(modelobj.options[modelobj.selectedIndex].text);
				})
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) { 
				$('.model').hide().html("error "+errorThrown+" "+textStatus).fadeIn('slow');
			} 
		});	
		unblockIt();
		function blockIt(){
			$('#main').block({ 
				message: "Loading...",
				centerY: false,
		        fadeIn: 0, 
		        fadeOut: 700,
		        overlayCSS:  { 
		            background: '#efefef',
		            cursor: 'wait'
		        }, 
			    css: { 
			        width: '200px', 
			        top: '30%', 
			        left: '', 
			        right: '10px', 
			        border: 'none', 
			        padding: '5px', 
			        backgroundColor: '#000', 
			        '-webkit-border-radius': '10px', 
			        '-moz-border-radius': '10px', 
			        opacity: .9, 
			        color: '#fff',
			        cursor: 'wait'
			    }
			}); 
		}
		function unblockIt(){
			$('#main').unblock({
				onUnblock: function(){
					//$('#slider').slider('destroy');
				}
			});
		}
		function loadkeywords(selected){
			$("#answers").hide().html('');
			blockIt();
			$("#keyword").unbind();
			$.ajax({
				url: "/customerservice/getkeywords",
				global: false,
				type: "GET",
				data: ({ model: selected }),
				dataType: "text",
				async: false,
				success: function(text){
					if(text!=""){
						var autocompletedata = text.split("~");
						$("#keyword").autocomplete(autocompletedata);
					}else{
						$("#keyword").unbind();
					}
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) { 
					$("#keyword").unbind();
				} 
			});	
			unblockIt();
		}
		$("#faqsearch").click(function(){
			blockIt();
			var selected=$('.modeldd').val();
			var keywords=$('#keyword').val();
			var newhtml="";
			$.ajax({
				url: "/customerservice/getanswers",
				global: false,
				type: "GET",
				data: ({ model:selected,keywords:keywords}),
				dataType: "json",
				async: false,
				success: function(json){
					if(json){
						var q=0;
						$.each(json, function(i,item){
							newhtml=newhtml+"<div class='strong_branded_borderless'>"+item.question+"<a href='/customerservice/faqanswer/?id="+i+"'><img src='/images/link.png' border='0' title='Link to this answer' class='tt'></a></div>"+
							"<p>"+item.answer+" [ <span class='seefull' id='"+i+"'>see full answer</span> ]</p>";
							q++;
						});
						var plural="";
						if(q==0 || q>1){
							plural="s";
						}
						newhtml="<p style='color: #999; size: 10px;'>"+q+" result"+plural+" found:</p>"+newhtml;//$('.modeldd').val()
						$("#answers").hide().html(newhtml).fadeIn('slow'); 
						$(".seefull").unbind().click(function(){
							unidstr=$(this).attr('id');
							$.ajax({
								url: "/customerservice/getanswer",
								global: false,
								type: "GET",
								data: ({unid:unidstr}),
								dataType: "json",
								async: false,
								success: function(json){
									$.each(json, function(i,item){
										txt="<p><span id='backsearch'>Back to search results</span></p><div class='strong_branded_borderless'>"+item.question+"<a href='/customerservice/faqanswer/?id="+item.unid+"'><img src='/images/link.png' border='0' title='Link to this answer' class='tt'></a></div>"+
										"<p>"+item.answer+"</p>";
									});
									$("#answers").hide().html(txt).fadeIn('slow');
									$('#backsearch').click(function(){
										$('#faqsearch').trigger('click');
									});
								},
								error: function(XMLHttpRequest, textStatus, errorThrown) { 
									$("#answers").hide().html("<span style='color: #999;'>Our apologies, there was an error. Please contact the site administrator.</span>").fadeIn('slow')
								}
							});
						});
					}else{
						$("#answers").hide().html("<span style='color: #999;'>Our apologies, no FAQs found for the keywords provided.</span>").fadeIn('slow')
						$('#questionsubmit').fadeIn('slow');
					}
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) { 
					$("#answers").hide().html("<span style='color: #999;'>Our apologies, no FAQs found for the keywords provided.</span>").fadeIn('slow');
					$('#questionsubmit').fadeIn('slow');					
				} 
			});
			unblockIt();
		});
		$('#submitquestion').click(function(){
			var questiontxt=$('#suggest').val();
			var modeltxt=$('.modeldd').val();
			if(questiontxt==""){
				alert("Please provide a question before submitting.");
			}else{
				$.ajax({
					url: "/customerservice/putquestion",
					global: false,
					type: "POST",
					data: ({ question: questiontxt, model: modeltxt }),
					dataType: "text",
					async: false,
					success: function(txt){
						if(txt=="complete"){
							$('#submitquestion').hide();
							$('#suggestreplace').hide().html("<div style='background-color: #e0e0e0; padding: 15px;'><p>"+
							"<strong>Thank you very much for your suggestion!</strong>"+
							"<br>Your suggestion will be reviewed shortly for inclusion in our Frequently Asked Questions database.</p>"+
							"<p><button id='suggestanother'>Suggest another</button></p></div>").fadeIn('slow');
						}else{
							alert("Our apologies, there was an unexpected error. Please contact the site administrator.");
						}
						$('#suggestanother').unbind().click(function(){
							var modelobj=document.forms['faqsearch'].model;
							$('#suggestreplace').hide().html('<p id="suggestreplace">Submit a question for review to the FAQs database for <strong>Model <span id="modelselected">'+
									modelobj.options[modelobj.selectedIndex].text+'</span></strong>:<input type="text" name="suggest" style="width: 100%; height: 45px;" id="suggest">').fadeIn('slow');
							$('#submitquestion').fadeIn();
						});
					},
					error: function(XMLHttpRequest, textStatus, errorThrown){
					} 
				});
			}
		});
	});
});
