$(document).ready(function(e) 
{
    $(".ui-button").button();
	$(".qtip").qtip({
	
		show: 'mouseover',
   		hide: 'mouseout',
		style:{tip:true}
		
	});
	
	$("#ticker-search").autocomplete({
		source:"/json-companies",
		minLength:2,
		select: function(event,ui)
		{
			document.location.href = "/"+ui.item.lowerepic+"-chat";
		}
	});
	
	$("#forum-company-search").autocomplete({
		source:"/json-companies",
		minLength:2,
		select: function(event,ui)
		{
			document.location.href = "/forum/"+ui.item.lowerepic;
		}
	});
	
	
});

var addthis_config = {data_track_clickback: false, data_track_addressbar: false};
var addthis_share = {
  templates: { twitter: 'Check out {{title}} @ {{url}}' }
};

function submit_review()
{
	
	
	$("#review-dialog").dialog({
		modal:true,
		resizable:false,
		height:500,
		width:490,
		title:"Submit Review"
	});	
}

function process_review_message()
{
	$("#captcha-callback").val("process_review_message()");
	
	$("<div class=\"no-title-dialog\" id=\"this-thread-message-submit-dialog\" style=\"text-align:center\"><p style=\"padding-top:20px\">Please wait...</p><p><img src=\"/global-assets/images/ajax-loaders/loader-1.gif\" alt=\"Loading...\"/></p></div>").dialog({
		modal:true,
		dialogClass:"no-title-dialog",
		resizable:false,
		close:function()
		{
			$(this).dialog("destroy").remove();	
		}
	});
	
	$.post("/ajax-submit-review/",$("#new-review-form").serialize(),function(data)
	{
		if(data=="-10")
		{
			$("#this-thread-message-submit-dialog").dialog("destroy").remove();
			$("#captcha-dialog").dialog({
				modal:true,
				width:348,
				height:380,
				close:function()
				{
					Recaptcha.reload();
					$("#captcha-error").hide();	
				}
			});
		}
		
		if(data=="-2")
		{
			show_review_error("Please enter a review summary.");	
		}
		
		if(data=="-7")
		{
			show_review_error("Please enter some content for the review.");	
		}
		
		if(data=="-9")
		{
			show_review_error("Please enter your name.");	
		}
		
		if(data=="-11")
		{
			show_review_error("Please enter a valid email.");	
		}
		
		if(data=="1")
		{
			$("#this-thread-message-submit-dialog").dialog("destroy").remove();
			$("#review-dialog").dialog("close");
			alert("Thank you for your review!");	
		}
	});
}

function show_review_error(e)
{
	$("#this-thread-message-submit-dialog").dialog("destroy").remove();
	$("#new-review-message-error").show();
	$("#new-review-message-error-text").html(e);
}

function review_question()
{
	$( "#review-request-dialog" ).dialog({
			resizable: false,
			height:195,
			width:470,
			modal: true,
			buttons: {
				"No thanks": function() {
					$( this ).dialog( "close" );
				},
				"Yes, no problem!": function() {
					$( this ).dialog( "close" );
					submit_review();
				}
			}
		});	
}

function vote_thread(what,mid)
{
	$("#captcha-callback").val("vote_thread('"+what+"',"+mid+")");
	
	var v = $(".vote-"+what+"-count-"+mid);
	votes = parseInt(v.html());	
	v.html("<img src=\"/global-assets/images/ajax-loaders/loader-1.gif\" alt=\"Submitting\" />");
	$.post("/ajax-submit-vote/",{"what":what,"mid":mid},function(data)
	{
		var json = $.parseJSON(data);
		if(json.code=="1")
		{
			votes++;
		}
		if(json.code=="-2")
		{
			// must be logged in
			$("<div title=\"Login Required\"><p>You must be logged in to perform this operation.</p></div>").dialog({
				modal:true,
				width:400,
				resizable:false,
				close:function()
				{
					$(this).dialog("destroy").remove();	
				}
			});
		}
		if(json.code=="-4")
		{
			// must be logged in
			$("<div title=\"Invalid Action\"><p>You can only vote for an item once.</p></div>").dialog({
				modal:true,
				width:400,
				resizable:false,
				close:function()
				{
					$(this).dialog("destroy").remove();	
				}
			});
		}
		if(json.code=="-5")
		{
			// bad captcha
			$("#captcha-dialog").dialog({
				modal:true,
				width:348,
				height:380,
				close:function()
				{
					Recaptcha.reload();
					$("#captcha-error").hide();	
				}
			});
		}
		
		v.html(votes);
	});
}
	

function what_are_insights()
{
	$("<div id=\"insights-dialog\" title=\"Introducting DTM Insights\"><div class=\"loading-div\"><p>Loading...</p><img src=\"/global-assets/images/ajax-loaders/loader-1.gif\" alt=\"Loading...\"/></div></div>").dialog(
	{
		width:810,
		height:720,  
		modal:true,
		open:function()
		{
			$(this).load("/ajax-insights-dialog/");	
		},
		close:function()
		{
			$(this).dialog("destroy").remove();
		}
	});
}

function register()
{
	$("<div id=\"register-dialog\" title=\"Register Today\"><div class=\"loading-div\"><p>Loading...</p><img src=\"/global-assets/images/ajax-loaders/loader-1.gif\" alt=\"Loading...\"/></div></div>").dialog(
	{
		width:440,
		height:570,  
		modal:true,
		open:function()
		{
			$(this).load("/ajax-register/");	
		},
		close:function()
		{
			$(this).dialog("destroy").remove();
		}
	});
}

function process_registration()
{
	$("#captcha-callback").val("process_registration()");
	var formdata = $("#register-form").serialize();
	$.post("/ajax-register/",formdata,function(data)
	{
		var user = $.parseJSON(data);
		if(user.code=="-10")
		{
			$("#captcha-dialog").dialog({
				modal:true,
				width:348,
				height:380,
				close:function()
				{
					Recaptcha.reload();
					$("#captcha-error").hide();	
				}
			});
		}
		if(user.code=="1")
		{
			document.location.href=document.location.href;
			return;	
		}

		var errors = Array();
		errors["E-9"] = "Your password is too long! Maximum 100 characters";
		errors["E-8"] = "Your username is too long! Maximum 25 characters";
		errors["E-7"] = "Your username is invalid. Alphanumeric characters only allowed";
		errors["E-5"] = "Your email addresses to not match";
		errors["E-4"] = "Your passwords do not match";
		errors["E-3"] = "You have supplied an invalid email address";
		errors["E-2"] = "You must specify a password";
		errors["E-1"] = "You must specify a username";
		errors["E-100"] = "Your username has already been register! Login instead?";
		errors["E-101"] = "Your email has already been registered! Login instead?";
	
		var msg = errors["E"+user.code];
		if(typeof(msg)!="undefined")
		{
			$("#registration-error").show();
			$("#registration-error-text").html(msg+".");
		}
	
	});
}

function submit_captcha()
{
	$.post("/ajax-submit-captcha/",$("#captcha-form").serialize(),function(data)
	{
		if(data=="1")
		{
			$("#captcha-dialog").dialog("close");
			eval($("#captcha-callback").val());
		}
		else
		{
			$("#captcha-error").fadeIn();
			Recaptcha.reload();
		}
	});
}

function login()
{
	$("<div id=\"login-dialog\" title=\"Login!\"><div class=\"loading-div\"><p>Loading...</p><img src=\"/global-assets/images/ajax-loaders/loader-1.gif\" alt=\"Loading...\"/></div></div>").dialog(
	{
		width:384,
		height:570,  
		modal:true,
		open:function()
		{
			$(this).load("/ajax-login/");	
		},
		close:function()
		{
			$(this).dialog("destroy").remove();
		}
	});
}

function process_login()
{
	$("#reminder-error").hide();
	$("#reminder-notice").hide();
	var formdata = $("#login-form").serialize();
	$.post("/ajax-login/",formdata,function(data)
	{
		if(data=="1")
		{
			document.location.href=document.location.href;
		}
		else
		{
			$("#login-error").show();
			$("#login-error-text").html("Invalid login details. Please try again.");
		}
	});
}

function process_reminder()
{
	$("#login-error").hide();
	var formdata = $("#reminder-form").serialize();
	$.post("/ajax-login/",formdata,function(data)
	{
		if(data=="1")
		{
			$("#reminder-details").val("");
			$("#reminder-error").hide();
			$("#reminder-notice").show();
			$("#reminder-notice-text").html("Your details have been sent to your email!");

		}
		else
		{
			$("#reminder-notice").hide();
			$("#reminder-error").show();
			$("#reminder-error-text").html("Sorry, we could not match your details.");
		}
	});
}
