//var loading_anim = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0" width="16" height="16"><param name="allowFullScreen" value="true" /><param name="wmode" value="transparent" /><param name="quality" value="high" /><param name="movie" value="name.swf" /><embed type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer_de" width="16" height="16" quality="high" wmode="transparent" allowFullScreen="true" src="images/loading.swf" /></object>';
var loading_anim = '<img src="images/loading.gif">';
var admTimer = null;

// numbersOnly()
// copyright 1999 Idocs, Inc. http://www.idocs.com
// Distribute this script freely but keep this notice in place
function numbersonly(myfield, e, dec)
{
	var key;
	var keychar;

	if (window.event)
	{
		key = window.event.keyCode;
	}
	else if(e)
	{
		key = e.which;
	}
	else
	{
		return true;
	}
	
	keychar = String.fromCharCode(key);

	// control keys
	if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27))
	{
		return true;
	}

	// numbers
	else if((("0123456789.").indexOf(keychar) > -1))
	{
		return true;
	}

	// decimal point jump
	else if (dec && (keychar == "."))
	{
		myfield.form.elements[dec].focus();
		return false;
	}
	else
	{
		return false;
	}
}


function scrollToDiv(divId)
{
	var targetOffset = $('#'+divId).offset().top; 
	$('html,body').animate({scrollTop: targetOffset}, 800); 
}

function scrollToTop()
{
	$('html, body').animate({scrollTop:0}, 'slow');
	return false;
}

var sidebarScrollTop = 0;

$(document).ready(function() {
	var popTop = ($(window).height() / 2 - $("#popup_window").height() / 2);
	var popLeft = ($(window).width() / 2 - $("#popup_window").width() / 2);
	$("#popup_window").css({top: popTop, left: popLeft});
	
	var OssPopTop = ($(window).height() / 2 - $("#oss_popup_window").height() / 2);
	var OssPopLeft = ($(window).width() / 2 - $("#oss_popup_window").width() / 2);
	$("#oss_popup_window").css({top: OssPopTop, left: OssPopLeft});
	
	$("#content").css({minHeight: ($(window).height() - 268)});
	
	$(".notification").click(function() {
		$(this).slideUp(function() {
			sidebarScrollTop = $("#sidebar").offset();
		});
	});
	
	/* Sidebar */
	// TODO: Fix this.
	sidebarScrollTop = $("#sidebar").offset();
	
	if($(window).height() > 530)
	{
		$(window).scroll(function () { 
			//alert(sidebarScrollTop.top);
			var docScrollTop = $('body,html').scrollTop();
			//alert(docScrollTop);
			if(docScrollTop > sidebarScrollTop.top)
			{
				$("#sidebar").css({position: 'fixed', top: '0px'});
				$("#sidebar #sidebar_scroll_links").show(400);
				$("#sidebar #header_links_sidebar").fadeIn();
			}
			else
			{
				$("#sidebar").css({position: 'static'});
				$("#sidebar #sidebar_scroll_links").hide(400);
				$("#sidebar #header_links_sidebar").fadeOut();
			}
		});
	}
	/* END Sidebar */
	
	/* Dev progress page */
	$(".checklist_toggle_button").click(function() {
		$(this).html($(this).html() == "+" ? "-" : "+");
		$(this).next().slideToggle(400);
	});
	/* END Dev progress page */
	
	/* Driver upload page */
	// License selector stuff
	$("#license").change(function() {
		if($(this).children(":selected").hasClass("multiple_versions"))
		{
			$("#license_ver").attr('disabled', false);
			$("#license_ver").children().not("."+$(this).children(":selected").val()+"_license_ver").attr('disabled', true);
			$("#license_ver").children("."+$(this).children(":selected").val()+"_license_ver").attr('disabled', false);
			$("#license_version").animate({opacity: 1});
			$("#other_license").delay(405).attr('disabled', true);
			$("#other_license_container").animate({opacity: 0.5});
		}
		else if($(this).children(":selected").html() === "Other")
		{
			$("#license_ver").attr('disabled', true);
			$("#other_license").attr('disabled', false);
			$("#license_version").animate({opacity: 0.5});
			$("#other_license_container").animate({opacity: 1});
		}
		else
		{
			$("#license_ver").attr('disabled', true);
			$("#license_ver").children($(this).children(":selected").val()+"_license_ver").attr('disabled', true);
			$("#license_version").animate({opacity: 0.5});
			$("#other_license").delay(405).attr('disabled', true);
			$("#other_license_container").animate({opacity: 0.5});
		}
	});
	
	$("#driver_upload_form").find("textarea").elastic();
	$("#driver_upload_form").find("input, textarea, select, input").focus(function() {
		$(this).animate({backgroundColor: '#ffffff'});
	});
	/* END Driver upload page */
	
	/* Driver search page */
	$('#keywords, #manufacturer, #model').keyup(function(e) {
		//alert(e.keyCode);
		if(e.keyCode == 13) {
			$("#search_button").click();
		}
	});
	
	$("#search_button").click(function() {
		sidebarScrollTop = $("#sidebar").offset();
		
		// See if there is at least one form field filled in
		var values = $("#category").val() + $("#keywords").val() + $("#manufacturer").val() + $("#model").val();
		
		if(values.length == 0)		// No fields filled in
		{
			$("#fill_in_fields").show().css({color: '#f00'}).animate({color: '#ff5555'}, 800);
		}
		else		// At least one field is filled in
		{
			$("#fill_in_fields").fadeOut();
			$("#results_loading").fadeIn();
			
			// Search for drivers using AJAX
			$("#results_container").slideUp(function() {
				$.ajax({
					type: "POST",
					url: "content/drivers/drivers_search.php",
					data: {
							category: $("#category").val(), 
							keywords: $("#keywords").val(), 
							manufacturer: $("#manufacturer").val(), 
							model: $("#model").val()
						  },
					timeout: 1800,
					success: function(data)
					{
						//alert(data.error);
						var parsedData = jQuery.parseJSON(data);
						
						if(parsedData.error === false && parsedData.success === true)		// Success
						{
							$("#results_container").html(parsedData.msg).slideDown();
						}
						else if(parsedData.error === true)
						{
							$("#results_container").html("<div style='text-align: center; font-weight: bold;'>"+parsedData.msg+"</div>").slideDown();
						}
				
						$("#results_loading").fadeOut();
					},
					error: function(request, textStatus, errorCode)
					{
						if(textStatus === "error")
						{
							$("#results_container").html("<div style='text-align: center; font-weight: bold; color: red'>Could not complete request. Please try again.</div>").slideDown();
						}
						else if(textStatus === "timeout")
						{
							$("#results_container").html("<div style='text-align: center; font-weight: bold; color: red'>Request timed out. Please try again.</div>").slideDown();
						}
					
						$("#results_loading").fadeOut();
					}
				});
			});
		}
	});
	
	$(".toggle_button, .section_toggle_container strong").live('click', function(event) {
		if($(this).hasClass("toggle_button"))
		{ 
			$(this).html($(this).html() == '-' ? '+' : '-');
		}
		else
		{
			$(this).prev(".toggle_button").html($(this).prev(".toggle_button").html() == '-' ? '+' : '-');
		}
		$(this).parent().next(".info_section").slideToggle(400);
	});
	/* END Driver search page */	
	
	/* Feature request page */
	$("#feature_request_form").find("textarea").elastic();
	$("#software_title, #module, #short_desc, #description, #code").focus(function() {
		$(this).animate({backgroundColor: '#ffffff'});
	});
	sidebarScrollTop = $("#sidebar").offset();
	/* END Feature request page */
	
	/* Contact page */
	$("#send_mail").find("textarea").elastic();
	$("#send_mail input, #send_mail textarea").focus(function() {
		$(this).animate({backgroundColor: '#ffffff'});
	});
	/* END Contact page */
	
	/* News page */
	$(".post_image").each(function() {
		var height = $(this).height();
		$(this).next(".post_body").css("min-height", height);
	});
	
	$("#toggle_news_form span").click(function() {
		$("#return_msg").fadeOut();
		$(this).text($(this).text() == '-' ? '+' : '-');
		$("#news_form").slideToggle();
	});
	/* END News page */
	
	/* Administration page */	
	$("#user_search").keyup(function(e) {
		clearTimeout(admTimer);
		//$("#debug").html("Keyup");
		if($(this).val() == "")
		{
			$("#user_search_results").html('');
		}
		else
		{
			admTimer = setTimeout(function()
			{
				//$("#debug").append(" Started timeout for AJAX request");
				$.ajax({
					type: "POST",
					url: "admin/find_users.php",
					data: { username: $("#user_search").val() },
					timeout: 10000,
					success: function(data)
					{
						//alert(data);
						//$("#debug").append(" Success.");
						//alert(data.error);
						var parsedData = jQuery.parseJSON(data);
						//$("#debug").append(" Error: " + parsedData.error);
						if(parsedData.error === false && parsedData.success === true)		// Success
						{
							//$("#debug").append(" Appending results.");
							$("#user_search_results").html(parsedData.html);
							//$("#debug").append(" Appended results.");
						}
						else if(parsedData.error === true)
						{
							$("#user_search_results").html("<div style='text-align: center; font-weight: bold;'>"+parsedData.msg+"</div>").slideDown();
							$("#debug").append(" Error occured.");
						}
						//$("#debug").append(" Finished AJAX.");
					},
					error: function(request, textStatus, errorCode)
					{
						if(textStatus === "error")
						{
							$("#user_search_results").html("<div style='text-align: center; font-weight: bold; color: red'>Could not complete request. Please try again.</div>").slideDown();
						}
						else if(textStatus === "timeout")
						{
							$("#user_search_results").html("<div style='text-align: center; font-weight: bold; color: red'>Request timed out. Please try again.</div>").slideDown();
						}
					}
				});
			}, 1500);
		}
	});
	
	$("#user_search").keydown(function() {
		//$("#debug").html("Keydown. Cleared timeout.");
		clearTimeout(admTimer);
	});
	
	if($("#user_search").val())
	{
		$("#user_search").trigger('keyup');
	}
	/* END Administration page */
	
	/* Account page */
	$("#reenable #search_uname").click(function()
	{
		$.ajax({
			type: "POST",
			url: "content/account/ajax_search_uname.php",
			data: { username: $("#reenable #username").val() },
			timeout: 1800,
			success: function(data)
			{
				$("#reenable .questions").html(data).parent().slideDown();
			},
			error: function(request, textStatus, errorCode)
			{
				errorMessage('Your username could not be found. Please check your spelling and try again.', 0);
			}
		});
	});
	
	$("#reenable .answer").keyup(function() 
	{
		if($(this).val().length && $("#reenable .questions").html().length)
		{
			$("#reenable #reenable_submit").attr("disabled", false);
		}
		else
		{
			$("#reenable #reenable_submit").attr("disabled", true);
		}
	});
	
	$("#reenable #questions").change(function() 
	{
		if($(this).val().length)
		{
			$("#reenable #reenable_submit").attr("disabled", false);
		}
		else
		{
			$("#reenable #reenable_submit").attr("disabled", true);
		}
	});
	
	//echo "onFocus=\"if(this.value=='$text'){this.value='';}\" onBlur=\"if(this.value==''){this.value='$text';}\"";
	$("#about_us_description").focus(function() 
	{
		if($(this).html() == 'Description')
		{
			$(this).html('');
		}
	});
	
	$("#about_us_description").blur(function() 
	{
		if($(this).html() == '')
		{
			$(this).html('Description');
		}
	});
	/* END Account page */
	
	/* About Us page */
	$("#about_us_profiles").masonry({itemSelector: '.user_profile_container', isAnimated: true});
	/* END About Us page */
});

$(window).resize(function() {
	sidebarScrollTop = $("#sidebar").offset().top;
});

$(document).resize(function() {
	sidebarScrollTop = $("#sidebar").offset().top;
});

function errorMessage(mesg, scrollTo)
{
	$("#error_notification_message").html(mesg);
	$("#error_notification").slideDown(function() 
	{
		sidebarScrollTop = $("#sidebar").offset();
	});
	
	if(scrollTo)
	{
		var targetOffset = $("#notifications_scrollto").offset().top; 
		$('html,body').animate({scrollTop: targetOffset}, 800); 
	}
	
	sidebarScrollTop = $("#sidebar").offset();
}

function infoMessage(mesg, scrollTo)
{
	$("#info_notification_message").html(mesg);
	$("#info_notification").slideDown(function() 
	{
		sidebarScrollTop = $("#sidebar").offset();
	});
	
	if(scrollTo)
	{
		var targetOffset = $("#notifications_scrollto").offset().top; 
		$('html,body').animate({scrollTop: targetOffset}, 800); 
	}
	
	sidebarScrollTop = $("#sidebar").offset();
}

function cfrmMessage(mesg, scrollTo)
{
	$("#success_notification_message").html(mesg);
	$("#success_notification").slideDown(function() 
	{
		sidebarScrollTop = $("#sidebar").offset();
	});
	
	if(scrollTo)
	{
		var targetOffset = $("#notifications_scrollto").offset().top; 
		$('html,body').animate({scrollTop: targetOffset}, 800); 
	}
	
	sidebarScrollTop = $("#sidebar").offset();
}

function showMessage(mesg)
{
	$("#popup_text").html(mesg);
	$("#popup_window").fadeIn();
}

function showOSSBox()
{
	$("#oss_popup_window").fadeIn();
}

