$(document).ready(
	//function()
	//{
	//	qa.init();

		//if (document.getElementById('searchinput') != undefined)
		//{
		//	$("#search :input:visible:enabled:first").focus();
		
		//	if (document.getElementById('searchinput').value === '')
		//	{
		//		document.getElementById('searchinput').style.color = '#ccc';
		//		document.getElementById('searchinput').value = 'Start your search';
		//	}
	    //    $("#searchinput").click(function() {
	    //    	if (document.getElementById('searchinput').value == 'Start your search')
	    //    	{
	    //    		document.getElementById('searchinput').value = '';
	    //    	}
	    //    	document.getElementById('searchinput').style.color = '#000';
	    //    });
	    //}


        $("#save").click(function() {
                $("#input").attr("value", "Hella Cool!");
                $("#input").attr("align", "bottom");
                alert($("form").clone().html());
        });

		if (document.getElementById('description_text') != undefined)
		{
			//document.getElementById('description_text').style.maxHeight == '200px';
			document.getElementById('description_text').style.overflow = 'hidden';
			var DivHeight = document.getElementById('description_text').offsetHeight;
			if (DivHeight > 200)
			{
				document.getElementById('description_text').style.height = '206px';
				document.getElementById('readmore').style.display = 'inline';
			}
		}
		function submit_search()
		{
			var search_value = $('#search input.text').val();
			var current_url = window.location.href;
			var base_url = "/home/shopping/search/query/";
			if (current_url.search('/local/') > 0)
			{
				base_url = "/home/local/search/query/";
			}

			if (current_url.search('uvx=') > 0)
			{
				base_url = "/home/search/index/query/";
			}

			var new_location = base_url+search_value;
			if (current_url.search('/mkt/uk') != -1)
			{
				mkt_url = '/mkt/uk';
			}
			else
			{
				mkt_url = '';
			}
			window.location = base_url+search_value+mkt_url;
			return false;
		}
		$('#search .btn').click(submit_search);
		$("#search input.search").keyup(
			function(e)
			{
				if(e.keyCode == 13)
				{
					submit_search();
				}
			}
		);
		/*
		document.getElementById('description_text').style.max-height = '200px';
		document.getElementById('description_text').style.overflow = 'hidden';
		*/
	}
);

qa =
{
	values:{},
	init:function()
	{
		tb_init('a.thickbox.post_answer');
		// ---
		// CLICK ON ANSWER LINK, POPULATE QUESTION_ID
		// ---
		$('.post_answer').click(
			function()
			{
				var question_id = $(this).attr('question_id');
				$('.answer_form input[name="question_id"]').val(question_id);
			}
		);

		// ---
		// ANSWER FORM SUBMISSION
		// ---
		$('.answer_form form').submit(qa.submit_answer);

		// ---
		// QUESTION FORM SUBMISSION
		// ---
		$('.question_form form').submit(qa.submit_question);
	},
	submit_answer:function()
	{
		// CURRENTLY NOT GETTING THE QUESTION_ID
		var question_id	= $('.answer_form input[name="question_id"]').val();
		var email	= $('.answer_form input[name="email"]').val();
		var name	= $('.answer_form input[name="name"]').val();
		var keyword	= $('.answer_form input[name="keyword"]').val();
		var answer	= $('.answer_form textarea[name="answer"]').val();
		var names	= name.split(' ',2);

		if(email.length == 0)
		{
			alert("Please enter an email");
			return false;
		}
		else if(!util.validate_email(email))
		{
			alert("Please enter a valid email. username@domain.com");
			return false;
		}

		if(name.length == 0)
		{
			alert("Please enter a name");
			return false;
		}

		if(answer.length == 0)
		{
			alert("Please enter an answer");
			return false;
		}

		var first_name	= '';
		var last_name	= '';
		if(names[0] && names[1])
		{
			first_name = names[0];
			last_name = names[1];
		}


		$.ajax({
			type:'GET',
			url:'/home/content/index/',
			dataType:'json',
			data: {'type':'answer','email':email,'question_id':question_id,'answer':answer,'first_name':first_name,'last_name':last_name,'keyword':keyword},
			processData:true,
			timeout:60000,
			success:qa.success_answer,
			error:qa.failure_answer
			});
		return false;
	},
	success_answer:function(data)
	{
		if(data.result == true)
		{
			$('.js_clear').val('');
			alert("Thank you. Your answer will be reviewed shortly.");
			$('#TB_closeWindowButton').click();
		}
		else
		{
			alert("Sorry, we're having some technical difficulties. Please contact support and/or retry. Thank you");
		}
	},
	failure_answer:function()
	{
		alert("Sorry, we're having some technical difficulties. Please contact support and/or retry. Thank you");
		$('#TB_closeWindowButton').click();
	},
	submit_question:function()
	{

		var email	= $('.question_form input[name="email"]').val();
		var name	= $('.question_form input[name="name"]').val();
		var keyword1	= $('.question_form input[name="keyword"]').val();
		var keyword2	= $('.question_form select[name="keyword"]').val();
		var question	= $('.question_form textarea[name="question"]').val();

		if(email.length == 0)
		{
			alert("Please enter an email");
			return false;
		}
		else if(!util.validate_email(email))
		{
			alert("Please enter a valid email. username@domain.com");
			return false;
		}

		if(name.length == 0)
		{
			alert("Please enter a name");
			return false;
		}

		if(typeof(keyword1) == 'undefined')
		{
			if(typeof(keyword2) == 'undefined')
			{
				alert("Sorry, we're having technical difficulties. Please contact support.");
				return false;
			}
			else
			{
				keyword = keyword2;
			}
		}
		else
		{
			keyword = keyword1;
		}

		if(question.length == 0)
		{
			alert("Please enter a question");
			return false;
		}

		var names	= name.split(' ',2);
		var first_name	= '';
		var last_name	= '';
		if(names[0] && names[1])
		{
			first_name = names[0];
			last_name = names[1];
		}
		$.ajax({
			type:'GET',
			url:'/home/content/index/',
			dataType:'json',
			data: {'type':'question','email':email,'keyword':keyword,'question':question,'first_name':first_name,'last_name':last_name},
			processData:true,
			timeout:60000,
			success:qa.success_question,
			error:qa.failure_question
			});

		return false;
	},
	success_question:function(data)
	{
		if(data.result == true)
		{
			$('.js_clear').val('');
			
			/** 
			* FOR CROSS-BROWSER PURPOSES, AND TO BE ABLE TO PASS PARAMS IN, 
			* SETTING FUNCTION AS A VAR AND CALLING FROM THERE 
			**/
			var func = function () { qa.open_new_tb(data.question_id)}
			//wait a bit to try and open a new tb
			setTimeout(func, 800);	
			
		//	alert("Thank you. Your question will be reviewed shortly.");
		}
		else
		{
			alert("Sorry, we're having some technical difficulties. Please contact support and/or retry. Thank you");
		}
	},
	failure_question:function()
	{
		alert("Sorry, we're having some technical difficulties. Please contact support and/or retry. Thank you");
	},
	open_new_tb:function(new_question_id)
	{
	//	alert(new_question_id);
		var tburl = "/home/questionanswer/categorizequestion?KeepThis=true&questionid="+new_question_id+"&TB_iframe=true&height=535&width=675";
		tb_show("", tburl);
	}
}
util =
{
	validate_email:function(email)
       {
	       var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	       return reg.test(email);
	       // {
       }
}

