
function successfulRating(questionid)
{
	span_name = 'rating'+questionid;
	document.getElementById(span_name).innerHTML = "Thanks for your rating feedback!";

}

function errorOnRating(questionid)
{
	span_name = 'rating'+questionid;
	document.getElementById(span_name).innerHTML = "Sorry, but there was a problem.";
	
}

function bindRatingThumbs()
{
	$('.rating').click(function() { 
			var questionid = $(this).attr('id');
			var choice = $(this).attr('choice');
			var ratingtxt = $(this).html();
		//	alert(ratingtxt);
			
			if (choice == 'up') { var rating = 1; } else { var rating = 0; }
			var values = 'question_id='+questionid+'&rating='+rating;
			

			$.ajax({
                       type:'GET',
                       url:'/includes/php/save_rating.php',
                       dataType:'json',
                       data:values,
                       processData:true,
                       timeout:60000,
                       success:successfulRating(questionid)
                 //      success:function(data){{ if(data.result == true){ span_name = 'rating'+data.questionid; document.getElementById(span_name).innerHTML = "Thanks for your rating feedback!"; }
                 //  	error:errorOnRating(questionid)
              });

			});
}


function unbindRatingThumbs()
{
	$('.rating').unbind();
}

$(document).ready (
	function() { 				
		bindRatingThumbs();
	}
);

q =
{
	values:{},
	init: function()
	{
		// -------------
		// SUBMISSION OF A QUESTION
		// -------------
		$('#ask input').keypress(q.submit);
		$('#ask .submit').click(q.submit);
		// -------------

		// -------------
		// SHOW FORMS
		// -------------
		$('a.comment').click(q.show_business_form);
		// -------------

		// -------------
		// THIS IS CRAZY.
		// WHEN SOMEONE CLICKS ON A COMMENT LINK (MAYBE MORE) WE HAVE TO BIND THE UNLOAD BEHAVIOR TO THICKBOX (DYNAMIC HTML IS A PAIN)
		// -------------
		$('a.comment').click(q.bind_clear_form);
		// -------------

		// -------------
		// UPDATE THE NUMBER OF CHARACTERS LEFT FOR THE QUESTION
		// -------------
		$('#ask .question').keypress(q.question_character_max);
		$('#ask .question').blur(q.question_character_max);
		$('#ask .submit').click(q.question_character_max);
		// -------------
	},
	bind_clear_form:function()
	{
		// -------------
		// LET US RESET EVERYTHING WHEN THEY CLOSE THE THICKBOX
		// -------------
		$("#TB_window").bind('unload',q.clear_form);
		// -------------
	},
	clear_form:function()
	{
		$('#ask .question, #ask .email').val('');
		$('#ask .website, #ask .business').addClass('hidden');
		$('#ask .type').val('general');
		$('#ask .number').text('256');
	},
	submit:function(e)
	{

		if(!$(this).hasClass('submit') && e.keyCode != 13)
		{
			return;
		}
		
		
		q.values			= {};
		q.values.area			= $('#ask .area').val();
		q.values.question		= $('#ask .question').val();
		q.values.email			= $('#ask .email').val();
		q.values.type			= $('#ask .type').val();
		q.values.keywords		= $('#search').val();
		/*
		alert(q.values.area);
		alert(q.values.question);
		alert(q.values.email);
		alert(q.values.type);
		alert(q.values.keywords);
		*/		
		if(q.values.type == 'website')
		{
			q.values.title			= $('#ask .website .title').val();
			q.values.url			= $('#ask .website .url').val();
			if(q.values.title.length == 0)
			{
				alert('please enter a title');
				return;
			}
			if(q.values.url.length == 0)
			{
				alert('please enter a url');
				return;
			}
		}
		else if(q.values.type == 'business')
		{
			q.values.name			= $('#ask .business .name').val();
			q.values.listingid		= $('#ask .business .listingid').val();
			q.values.latitude		= $('#ask .business .latitude').val();
			q.values.longitude		= $('#ask .business .longitude').val();
		}

		if(q.values.question.length == 0)
		{
			//alert('please ask a quesion');
			return;
		}
		if(q.values.email.length == 0)
		{
			//alert('please enter your email');
			return;
		}
		if(!util.validate_email(q.values.email))
		{
			//alert('invalid email!');
			return;
		}

		$.ajax({
			type:'GET',
			url:'/ajax/set/new/question.php',
			dataType:'json',
			data: q.values,
			processData:true,
			timeout:60000,
			success:q.success,
			error:q.failure
		});
	//	q.clear_form();
	//	$('#TB_closeWindowButton').click();
	},
	success:function(data)
	{

		if(data.result == true)
		{
			q.clear_form();
			$('#TB_closeWindowButton').click();
			
	//		var new_question_id = data.question_id;


			/** 
			* FOR CROSS-BROWSER PURPOSES, AND TO BE ABLE TO PASS PARAMS IN, 
			* SETTING FUNCTION AS A VAR AND CALLING FROM THERE 
			**/
			var func = function () { q.open_new_tb(data.question_id)}
			//wait a bit to try and open a new tb
			setTimeout(func, 800);

		//	alert("Your question will be reviewed soon. Thank you.");
		}
		else
		{
			$('#TB_closeWindowButton').click();
			q.failure(data.message);
		}
	},
	failure:function(message)
	{
//		alert('failure message = '+message);
		if(message == undefined)
		{
			alert("We're having some trouble with our ajax!");
		}
		else
		{
			alert(message);
		}
	},
	open_new_tb:function(new_question_id)
	{
		q.get_question(new_question_id);
		var tburl = "/includes/html/forms/question_categorize.php?KeepThis=true&question="+q.values.question+"&questionid="+new_question_id+"&TB_iframe=true&height=500&width=675";
		tb_show("", tburl);
	},
	get_question:function(question_id)
	{
		$.ajax({
			type:'GET',
			url:'/ajax/get/question.php',
			dataType:'json',
			data:
			{
				'question_id':question_id
			},
			processData:true,
			timeout:60000,
			success:q.update_question,
			error:q.failure
		});
	},
	update_question:function(data)
	{
		if(data.result == true)
		{
			/* ADDS THE QUESTION ASKED TO THE RECENT Q&A AREA AND THEN REBINDS THE THUMB RATING LINKS */
			$('#exact_topic').after(data.html).removeClass('hidden');
			$('#exact_topic').attr("style",""); //remove display:none style
			a.init();
			tb_init('a.thickbox');
			unbindRatingThumbs();
			bindRatingThumbs();
					
			//SEND EMAIL AND LOG IT - WAITING UNTIL NOW TO AVOID SLOW USER EXPERIENCE
			values = "type=question&question_id="+data.question_id;
		
			$.ajax({
				type:'GET',
				url:'/includes/php/send_email.php',
				dataType:'json',
				data:values,
				processData:true,
				timeout:60000,
				success:function() {}
			});		
			
			
		}
		else
		{
			q.failure(data.message);
		}
	},
	toggle_website_form:function()
	{
		// ---------------
		// SHOW THE WEBSITE FORM
		// ---------------
		$('#ask .website').toggleClass('hidden');
		// ---------------
		// FLIP THE BUTTON DISPLAY
		// ---------------
		$('#ask .add_website').toggleClass('hidden');

		// ---------------
		// HIDE THE BUSINESS FORM (CAN'T HAVE BOTH)
		q.hide_business_form();
		// SET THE CURRENT TYPE TO WEBSITE SO WE KNOW WHAT WE'RE PROCESSING
		if($('#ask .website').hasClass('hidden'))
		{
			$('#ask .type').val('general');
		}
		else
		{
			$('#ask .type').val('website');
		}
		// ---------------
	},
	show_business_form:function()
	{
		// ---------------
		// GRAB ALL THE VALUES FOR THE FORM AND MAP FUNCTIONS
		// ---------------
		var name	= $(this).attr('name');
		var latitude	= $(this).attr('latitude');
		var longitude	= $(this).attr('longitude');
		var listingid	= $(this).attr('listingid');
		// ---------------

		// ---------------
		// SET ALL THE VALUES FOR THE FORM
		// ---------------
		$('#ask .business .name').val(name);
		$('#ask .business .latitude').val(latitude);
		$('#ask .business .longitude').val(longitude);
		$('#ask .business .listingid').val(listingid);
		// ---------------

		// ---------------
		// PAN TO THE BUSINESS ON THE MAP
		// ---------------
		// q.center_yahoo_map(latitude,longitude);
		// ---------------

		// ---------------
		// HIDE WEBSITE FORM && SET FORM TYPE TO BUSINESS
		// ---------------
		q.hide_website_form();
		$('#ask .type').val('business');
		// ---------------

		// ---------------
		// FLASH THAT BUSINESS FORM AND KICK BACK FALSE TO NOT FOLLOW THE LINK
		// ---------------
		u.grow_thickbox(u.thickbox_change);
		$('#ask .business').removeClass('hidden');
		return false;
		// ---------------
	},
	hide_business_form:function()
	{
		u.grow_thickbox(-u.thickbox_change);
		$('#ask .business').addClass('hidden');
		$('#ask .type').val('general');
	},
	hide_website_form:function()
	{
		$('#ask .website').addClass('hidden');
		$('#ask .type').val('general');
	},
	question_character_max:function()
	{
		var my_length = $('#ask .question').val().length;
		if(my_length > 256)
		{
			$('#ask .question').val($('#ask .question').val().substring(0,256));
			var my_length = $('#ask .question').val().length;
			$('#ask .number').text(256 - my_length);
		}
		else
		{
			$('#ask .number').text(256 - my_length);
		}
	},
	center_yahoo_map:function(lat,long)
	{
		// ---------------
		// ASSUMES THE YAHOO MAP HAS BEEN INITIALIZED
		// WITH THE VAR BEING 'map'
		// ---------------
		var geoPoint	= new YGeoPoint(lat,long);
		q.values.map.panToLatLon(geoPoint);
		// ---------------

		// ---------------
		// THE POP UP DOESN'T GET CLEARED ON PAN AND IT CAN LOOK A BIT ODD IN CERTAIN SITUATIONS
		// ---------------
		u.remove_map_pop_up();
		// ---------------
	}
}
a =
{
	values:{},
	init:function()
	{
		// ---
		$('.reply').unbind('a.initialize_answer_form').click(a.initialize_answer_form);

		// ---
		// SET UP THE WEBSITE & BUSINESS BUTTONS
		// ---
		$('#answer .website.add').unbind().click(a.show_website_form);
		$('#answer .business.add').unbind().click(a.show_business_form);
		// ---
		$('#answer .website.remove').unbind().click(a.hide_website_form);
		$('#answer .business.remove').unbind().click(a.hide_business_form);
		// ---

		// ---
		// SET UP THE ENTER/RETURN BUTTON SUBMITTING THE FORM
		// ---
		$('#answer input.email, #answer input.title, #answer input.url, #answer input.business_name').unbind().keypress(a.keyboard_submit_form);
		$('#answer input.submit').unbind().click(a.submit);
		$('#answer input.cancel').unbind().click(function(){$('#TB_closeWindowButton').click();});
		// ---
	},
	initialize_answer_form:function()
	{
		// ---
		// SET UP NEED VALUES
		// ---
		$('#answer .question_id').val($(this).attr('question_id'));
		// ---
	},
	keyboard_submit_form:function(e)
	{
		// ---
		// IF IT'S NOT THE RETURN CARRIAGE, KICK BACK
		// ---
		if(e.keyCode != 13)
		{
			return;
		}
		// ---

		a.submit();
	},
	show_business_form:function()
	{
		if(!$('#answer div.form.website').hasClass('hidden'))
		{
			a.hide_website_form(-u.thickbox_change);
		}
		u.grow_thickbox(u.thickbox_change);

		// ---
		// SHOW FORM AND SWITCH BUTTONS
		// ---
		$('#answer .business.add').addClass('hidden');
		$('#answer .business.remove').removeClass('hidden');
		$('#answer div.form.business').removeClass('hidden');
		// ---
	},
	hide_business_form:function()
	{
		// ---
		// HIDE FORM AND SWITCH BUTTONS
		// ---
		$('#answer .business.add').removeClass('hidden');
		$('#answer .business.remove').addClass('hidden');
		$('#answer div.form.business').addClass('hidden');
		// ---

		u.grow_thickbox(-u.thickbox_change);
	},
	show_website_form:function()
	{
		if(!$('#answer div.form.business').hasClass('hidden'))
		{
			a.hide_business_form(-u.thickbox_change);
		}
		u.grow_thickbox(u.thickbox_change);

		// ---
		// SHOW FORM AND SWITCH BUTTONS
		// ---
		$('#answer .website.add').addClass('hidden');
		$('#answer .website.remove').removeClass('hidden');
		$('#answer div.form.website').removeClass('hidden');
		// ---
	},
	hide_website_form:function()
	{
		// ---
		// HIDE FORM AND SWITCH BUTTONS
		// ---
		$('#answer .website.add').removeClass('hidden');
		$('#answer .website.remove').addClass('hidden');
		$('#answer div.form.website').addClass('hidden');
		// ---

		u.grow_thickbox(-u.thickbox_change);
	},
	reset_form:function()
	{
		$('#answer input[type=text], #answer textarea').val('');
		a.hide_website_form();
		a.hide_business_form();
	},
	submit:function()
	{
		var parent = $('#answer');

		a.values.type			= 'general';

		a.values.area			= $('#in').val();
		a.values.answer			= parent.find('textarea.answer').val();
		a.values.question_id		= parent.find('input.question_id').val();
		a.values.email			= parent.find('input.email').val();

		a.values.website		= [];
		a.values.website.url		= '';
		a.values.website.title		= '';
		a.values.business		= [];
		a.values.business.name		= '';

		// ---
		// ALWAYS NEED AN ANSWER
		// ---
		if(a.values.answer.length == 0)
		{
			//alert('please write an answer before submitting');
			return;
		}

		// ---
		// ALWAYS NEED AN EMAIL
		// ---
		if(a.values.email.length == 0)
		{
			//alert('please enter an email address');
			return;
		}

		// ---
		// ALWAYS NEED A QUESTION_ID
		// ---
		if(isNaN(a.values.question_id))
		{
			alert('Oops, we had a problem, please contact support.');
			return;
		}

		// ---
		// IF THE WEBSITE IS BEING SHOWN, WE'RE TRYING TO SUBMIT IT
		// ---
		if(!parent.find('div.website').hasClass('hidden'))
		{
			a.values.website.url		= parent.find('.website input.url').val();
			a.values.website.title		= parent.find('.website input.title').val();
			if(a.values.website.title.length == 0) 
			{
				//alert('please add a title before submitting');
				return;
			}
			if(a.values.website.url.length == 0)
			{
				//alert('please add a website address before submitting');
				return;
			}
			a.values.type			= 'website';
		}

		// ---
		// IF THE BUSINESS IS BEING SHOWN, WE'RE TRYING TO SUBMIT IT
		// ---
		if(!parent.find('div.business').hasClass('hidden'))
		{
			a.values.business.name		= parent.find('.business input.business_name').val();
			if(a.values.business.name.length == 0) 
			{
				//alert('please add a business name before submitting');
				return;
			}
			a.values.type			= 'business';
		}

		// ---
		// VALIDATE THEIR EMAIL
		// ---
		if(!util.validate_email(a.values.email))
		{
			//alert('invalid email!');
			return;
		}

		// ---
		// CLOSE THE ANSWER FORM
		// ---
		$('#TB_closeWindowButton').click();
		// ---

		// ---
		// HIDE THE BUSINESS/WEBSITE EXTRA FORM TYPES AND CLEAR THEIR VALUE
		// ---
		a.reset_form();
		// ---

		// ---
		// FIRE THE AJAX OFF
		// ---
		$.ajax({
			type:'GET',
			url:'/ajax/set/new/answer.php',
			dataType:'json',
			data:
			{
				"area":a.values.area,
				"type":a.values.type,
				"url":a.values.website.url,
				"title":a.values.website.title,
				"business_name":a.values.business.name,
				'email':a.values.email,
				'question_id':a.values.question_id,
				'answer':a.values.answer
			},
			processData:true,
			timeout:60000,
			success:a.success,
			error:a.failure
		});
	},
	success:function(data)
	{
		if(data.result == true)
		{
			a.get_answer(data.answer_id);
		}
		else
		{
			a.failure(data.message);
		}
	},
	failure:function(message)
	{
		if(message == undefined)
		{
			alert("We're having some trouble with our ajax!");
		}
		else
		{
			alert(message);
		}
	},
	get_answer:function(answer_id)
	{
		$.ajax({
			type:'GET',
			url:'/ajax/get/answer.php',
			dataType:'json',
			data:
			{
				'answer_id':answer_id
			},
			processData:true,
			timeout:60000,
			success:a.update_answer,
			error:a.failure
		});
	},
	update_answer:function(data)
	{
		if(data.result == true)
		{
			$('#'+data.question_id+' ul').append(data.html);
			
			//SEND EMAIL AND LOG IT - WAITING UNTIL NOW TO AVOID SLOW USER EXPERIENCE
			values = "type=answer&question_id="+data.question_id+"&answer_id="+data.answer_id;
			
			$.ajax({
				type:'GET',
				url:'/includes/php/send_email.php',
				dataType:'json',
				data:values,
				processData:true,
				timeout:60000,
				success:function() {}
			});	
			
		}
		else
		{
			a.failure(data.message);
		}
	}
}
u =
{
	values:[],
	thickbox_change:50,
	init: function()
	{
		// -----------------------------------
		// HIDE ALL AD LINKS
		// -----------------------------------
		u.hide_ad_links();
		// -----------------------------------

		// -----------------------------------
		// UPDATE THE CONTENT INPUT WITH THE RELEVANT INFORMATION TO SEND TO A TEXT MESSAGE
		// -----------------------------------
		$('.text_message').click(u.text_message_content);
		$('#send_to_phone .submit').click(u.text_message_submit);
		$('#send_to_phone input.phone').keypress(u.text_message_submit);
		// -----------------------------------

		// -----------------------------------  
		// MAP IT  
		// -----------------------------------  
		$('.mapit a').click(u.show_address_window);
		$('.map_it .submit').click(u.map_it_submit);
		$('.map_it .start_address').keypress(u.map_it_submit);
		$('.map_it .cancel').click(u.map_it_cancel);
		// -----------------------------------  

		// -----------------------------------  
		// SET UP THE TABS FOR THE RIGHT NAV  
		// -----------------------------------  
		$('#content ul.tab-ul').tabs();
		// -----------------------------------  

		// -----------------------------------  
		// CREATE BOOKMARK LINKS  
		// -----------------------------------  
		$('.bookmark a').click(function(){CreateBookmarkLink();return false;});
		// -----------------------------------  

		// -----------------------------------
		// REMOVE THE LOCAL LISTING
		// -----------------------------------
		$('.remove a').click(u.remove_listing);
		// -----------------------------------
	},
	remove_listing:function()
	{
		var listingid = $(this).attr('listingid');
		$('.js_listing[listingid='+listingid+']').remove();
		return false;
	},
	grow_thickbox:function(number)
	{
		$('#TB_ajaxContent').css('height',$('#TB_ajaxContent').height()+number);
	},
	remove_map_pop_up:function()
	{
		$('.map_remove').remove();
	},
	show_address_window:function(e)
	{
		// ---
		// GET THE ADDRESS AND INSERT THE VALUE INTO THE MAP FORM
		// ---
		var map_var	= $("#map_it");
		var address = $(this).attr('address');
		map_var.find('.end_address').val(address);
		// ---
	},
	map_it_submit:function(e)
	{
		if(!$(this).hasClass('submit') && e.keyCode != 13)
		{
			return;
		}

		var start_address	= $('#map_it .start_address').val();
		if(start_address.length == 0)
		{
			alert('Please enter a starting address to map');
			return;
		}
		var end_address		= $('#map_it .end_address').val();
		window.open("http://maps.google.com/maps?f=d&hl=en&geocode=&saddr="+start_address+"&daddr="+end_address);
		$('#TB_closeWindowButton').click();
	},
	text_message_submit:function(e)
	{
		if(!$(this).hasClass('submit') && e.keyCode != 13)
		{
			return;
		}

		// GET THE PHONE NUMBER && STRIP OUT ALL NON-NUMERIC CHARACTERS
		var phone	= $('#send_to_phone').find('.phone').val().replace(/[^0-9]/g,'');
		if(phone.length != 10)
		{
			alert('Please enter a 10 digit phone number! 555-555-5555');
			return;
		}
		var provider	= $('#send_to_phone').find('.provider').val();
		var content	= $('#send_to_phone').find('.content').val();
		$.ajax({
			type:'GET',
			url:'/ajax/set/new/text_message.php',
			dataType:'json',
			data:
			{
				"phone":phone,
				"provider":provider,
				"content":content
			},
			processData:true,
			timeout:60000,
			success:u.text_message_success,
			failure:function(){alert("Message not sent, please try back later.");$('#TB_closeWindowButton').click();}
		});
	},
	text_message_success:function(data)
	{
		if(data.result == true)
		{
			alert('Thank you. Message sent');
			$('#TB_closeWindowButton').click();
		}
		else
		{
			alert("Message not sent, please try back later.");
		}
	},
	text_message_content:function()
	{
		$('#send_to_phone').find('.content').val($(this).attr('content')+' '+$(this).attr('phone'));
	},
	hide_ad_links:function()
	{
		$('.ads-inline a, .ads-col a').mouseover(function(){window.status='';return true;});
	}
}
s =
{
	values:{},
	init:function()
	{
		$('#subscribe input').keypress(s.check_form);
		$('#subscribe-submit').click(s.check_form);
	},
	check_form:function(e)
	{
		if($(this).attr('id') != 'subscribe-submit' && e.keyCode != 13)
		{
			return;
		}
		s.values.subscription_type	= $("#subscribe input[@name='subscribe-radio']:checked").val();
		s.values.email			= $("#subscribe input.email-text").val();
		s.values.name			= $("#subscribe input.name-text").val();
		s.values.email_type		= $("#subscribe input[@name='format-radio']:checked").val();
		s.values.search_term		= $("#subscribe label.subscribe."+s.values.subscription_type).text();
		if(!util.validate_email(s.values.email))
		{
			alert('invalid email!');
			return;
		}
		$.ajax({
			type:'GET',
			url:'/ajax/set/new/subscription.php',
			dataType:'json',
			data: s.values,
			processData:true,
			timeout:60000,
			success:s.success,
			error:s.failure
		});
	},
	success:function(data)
	{
		if(data.result == true)
		{
			$('#subscribe').html(data.message);
		}
		else
		{
			alert(data.message);
		}
	},
	failure:function()
	{
		alert('Please contact support.');
	}
}

// QUESTION
$(document).ready(q.init);
// ANSWER
$(document).ready(a.init);
// USER INTERFACE
$(document).ready(u.init);
// SUBSCRIBE
$(document).ready(s.init);


util = 
{
	validate_email:function(email)
	{
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		return reg.test(email);
		// {
	}
}

function CreateBookmarkLink()
{
	var title = "DTA"; 
	try
	{
		var url = location.href;
	}
	catch(err)
	{
		var url = "http://www.dotellall.com";
	}
	// Mozilla Firefox Bookmark
	if (window.sidebar)
	{
		window.sidebar.addPanel(title, url,"");
	}
	// IE Favorite
	else if(window.external)
	{
		window.external.AddFavorite( url, title);
	}
	// Opera Hotlist
	else if(window.opera && window.print)
	{
		return true;
	}
}
