support_requests = {
	save: function(){
		data = $('submit-support-request-form').serialize();
		
		name = $('name').value;
		email = $('email').value;
		comments = $('comments').value;

		if(name == ''){
			$('name').addClassName('missing');
		}
		if(email == ''){
			$('email').addClassName('missing');
		}
		if(comments == ''){
			$('comments').addClassName('missing');
		}
		
		if(name == '' || email == '' || comments == ''){
			$('top').update('<div class="msg">Ooops! Please complete all required fields');
		}else{
			$('top').update('<div class="msg">Sending support request, please wait...</div>');
			$$('input').each(function(s){
				s.disabled = true;
				s.addClassName('disabled');
				s.removeClassName('missing');
			});
			$$('textarea').each(function(s){
				s.disabled = true;
				s.addClassName('disabled');
				s.removeClassName('missing');
			});
			$$('button').each(function(s){
				s.disabled = true;
			});
			setTimeout(function(){
				new Ajax.Request('/help/submit_support_request/save',{
					method: 'post',
					parameters: data,
					onSuccess: function(t){
						$('top').update(t.responseText);
						setTimeout('support_requests.reset_form()',6500);
					}
				});
			},1500);
		}
	},
	reset_form: function(){
			$$('input').each(function(s){
				s.disabled = false;
				s.value = '';
				s.removeClassName('disabled');
			});
			$$('textarea').each(function(s){
				s.disabled = false;
				s.value = '';
				s.removeClassName('disabled');
			});
			$$('button').each(function(s){
				s.disabled = false;
				s.removeClassName('disabled');
			});
			$('top').update('');
	}
}

faqs = {
	questions: new Array(),
	truncated: new Array(),
	matching: new Array(),
	matchingLen: 0,
	fullLen: 0,
	category: '',
	init: function() {
		if($('faqs')) {
			if(arguments.length == 1) {
				faqs.category = arguments[0];				
			}
			this.get_list('/help/faqs/all');
		}
	},
	
	get_list: function(url) {		
		new Ajax.Request(url, {
			onSuccess: function(t){
				var json = t.responseText.evalJSON();
				faqs.questions = json; //complete array of object data
				faqs.fullLen = json.length;
				if(faqs.category == '') {
					var len = Math.min(20, json.length);
					for(var i = 0; i<len; i++) {
						faqs.matching.push(json[i]);
					}
				} else {
					for(var i = 0; i<faqs.fullLen; i++) {
						if(json[i].cat_slug == faqs.category) {
							faqs.matching.push(json[i]);
						}
					}				
				}
				
				for(var i = 0; i<faqs.fullLen; i++) {
					var q = json[i];
					faqs.truncated.push(q.question.toLowerCase()+q.answer.toLowerCase()); //array of just q / a data
				}
				var template = "{{#matching}}<li><a ='question' href='/help/faqs/view/{{slug}}'>{{question}}</a><p class='answer'>{{answer}}</p>{{/matching}}</li>";
				var html = Mustache.to_html(template,faqs);
				$('faq-list').update(html);
				
			},
			onException: function(request, ex) {
	            alert(ex);
	        }
		});		
	},
	get_matches: function(question) {
		question = question.toLowerCase();
		if(question.length > 2) {
			var question_parts = question.split(" ");
			var parts_length = question_parts.length;
			if(parts_length == 0) {
				question_parts.push(question);
			}
			faqs.matching = new Array();
			for(var i = 0; i<faqs.fullLen; i++) {
				//for each question
				var item = faqs.truncated[i];
				var score = 0;
				var regt = '';
				for(var j=0; j< parts_length; j++) {
					var part_length = question_parts[j].length;
					if(part_length > 2) {
						var searchX = new RegExp('\\b'+question_parts[j], "gi");
						var pos = item.search(searchX);
						if(pos != -1) {
							score += part_length*part_length/(pos+1);
							regt += '(\\b'+question_parts[j]+')|';
						}
					}
				}
				
				if(score > 0) {
					regt = regt.replace(/\|$/, '');
					var regx = new RegExp(regt, "gi");
					var qa = faqs.questions[i];
					faqs.matching.push({'score':score, 'question':qa.question.replace(regx, '<b>$&</b>'), 'answer':qa.answer.replace(regx, '<b>$&</b>'), 'slug':qa.slug});
				}
				faqs.matchingLen = faqs.matching.length;
			}
			//then resort by score DESC
			faqs.matching.sort( function(x,y) { 
			  var a = x.score;
		      var b = y.score; 
		      if (a > b) return -1 
		      if (a < b) return 1 
		      return 0; 
		    });
			
			var html = '';
			for(var i = 0; i < faqs.matchingLen; i++) {
				//mustache escapes all our html :-/
				html += '<li><a class="question" href="/help/faqs/view/'+faqs.matching[i].slug+'">'+faqs.matching[i].question+'</a><p class="answer">'+faqs.matching[i].answer+'</p></li>';
			}
			$('faq-list').update(html);
			
		} else {
			//lets just show em top or category questions till they type in enough
			faqs.matching = new Array();
			
			if(faqs.category == '') {
				var len = Math.min(20, faqs.fullLen);
				for(var i = 0; i<len; i++) {
					faqs.matching.push(faqs.questions[i]);
				}
			} else {
				for(var i = 0; i<faqs.fullLen; i++) {
					if(faqs.questions[i].cat_slug == faqs.category) {
						faqs.matching.push(faqs.questions[i]);
					}
				}				
			}
			
			var template = "{{#matching}}<li><a class='question' href='/help/faqs/view/{{slug}}'>{{question}}</a><p class='answer'>{{answer}}</p>{{/matching}}</li>";
			var html = Mustache.to_html(template,faqs);
			$('faq-list').update(html);
			
		}
	},
	
	add_blinds: function() {
		$$('#faq-list a').each(function(s) {
			s.observe('click', function(a) {
				new Effect.BlindDown($(this).next(), {duration: .4});
			});
		});
	},
	
	thumbs_up: function(id) {
		new Ajax.Request('/help/faqs/thumbs_up', {
			parameters: 'thumbs=1&id='+id,
			method: 'post',
			onSuccess: function(t){
				var success_text = '<p><b>Thanks for the feedback!</b><br />Thanks for helping us improve our FAQs.</p>';
				$('helpful').update(success_text);
			}
		});
	},
	
	thumbs_down: function(id) {
		new Ajax.Request('/help/faqs/thumbs_down', {
			parameters: 'thumbs=0&id='+id,
			method: 'post',
			onSuccess: function(t){
				var success_text = '<p><b>Thanks for the feedback!</b><br />Thanks for helping us improve our FAQs.</p>';
				$('helpful').update(success_text);
			}
		});
	}
}
