var groups = 20;
var people = 17;
var signup_timeout = '';
var signup_value = '';
var signup_value_sent = '';
var signup_submitted = 0;
var opened_popup;
var iphone_images = new Array();
$(document).ready(function() {
	// Nav Hovers
	var prefix = (location.href.indexOf('https://') != -1) ? 'https' : 'http';
	prefix += '://www.proboards.com';
	$('#nav_home').mouseover(function() {
		$(this).attr('src',prefix+'/resources/images/nav/home_over.png');
		}).bind('mouseleave',function() {
		$(this).attr('src',prefix+'/resources/images/nav/home.png');
	});
	
	$('#nav_blog').bind('mouseenter',function() {
		$(this).attr('src',prefix+'/resources/images/nav/blog_over.png');
		}).bind('mouseleave',function() {
		$(this).attr('src',prefix+'/resources/images/nav/blog.png');
	});
	
	$('#nav_support').bind('mouseover',function() {
		$(this).attr('src',prefix+'/resources/images/nav/support_over.png');
		}).bind('mouseout',function() {
		$(this).attr('src',prefix+'/resources/images/nav/support.png');
	});
	
	$('#nav_signup').bind('mouseover',function() {
		$(this).attr('src',prefix+'/resources/images/nav/signup_over.png');
		}).bind('mouseout',function() {
		$(this).attr('src',prefix+'/resources/images/nav/signup.png');
	});
	
	// Preload images
	var preload_images = new Array("nav/home_over.png","nav/blog_over.png","nav/support_over.png","nav/signup_over.png");
	
	if(location.href.indexOf('signup') != -1)
		preload_images.push("signup/searching.gif","signup/pw0.png","signup/pw1.png","signup/pw2.png","signup/pw3.png","signup/pw4.png","signup/pw5.png","signup/good.png","signup/bad.png");

	if(location.href.indexOf('login') != -1)
		preload_images.push("global/visit-hover.png");
	
	var loaded_images = new Array(preload_images.length);
	for(var i=0;i<preload_images.length;i++)
	{
		loaded_images[i] = new Image(1,1);
		loaded_images[i].src=prefix+'/resources/images/'+preload_images[i];
	}
	
	// Help Find URLs button
	$('.findurls').bind('click',function() {
		pop('popup/find_urls');
	});
	
	$('.tospopup').bind('click',function() {
		pop("/popup/tos");
	});
	
	$('#signup_button').bind('click',function() {
		if(!signup_submitted)
		{
			signup_submitted = 1;
			$('#signup_forum2').val($('#recaptcha_response_field').val());
			$('#signup_button').text('Creating Forum...');
			$('#signup_form')[0].submit();
		}
	});

	// Randomize groups / people photos
	/*
	$('.people').each(function() {
		var group = Math.floor(Math.random()*groups) + 1;
		group = 21;
		$(this).attr('src','/resources/images/people/group'+group+'.jpg');
	});

	$('.person').each(function() {
		var person = Math.floor(Math.random()*people) + 1;
		$(this).attr('src','/resources/images/people/p'+group+'.jpg');
	});
	*/
	
	// Signup Links
	$('.signup_link').bind('click',function() {
		location.href='create_free_forum';
	});

	// Report Abuse submit
	$('#report_abuse_submit').bind('click',function() {
		$('#report_abuse_form')[0].submit();
	});

	// Advertise Buttons	
	$('#advertise_targeted_button').bind('click',function() {
		location.href='advertise_targeted';
	});
	$('#advertise_selfserve_button').bind('click',function() {
		location.href='http://banners.proboards.com/';
	});

	// Targeted
	$('#advertise_email').html('<a href="mailto:tim'+'@pro'+'boards.com">tim'+'@pro'+'boards.com</a>');
	$('#advertise_targeted_submit').bind('click',function() {
		$('#advertise_targeted_form')[0].submit();
	});

	
	// Signup form - check if forum name is available
	$('#signup_input_forum').bind('keydown',function() {
		signup_value = $('#signup_input_forum').val();
	});
	$('#signup_input_forum').bind('keyup',function() {
		if($('#signup_input_forum').val() != signup_value)
			signup_checkforum();
	});

	// Signup form - password strength
	$('#signup_password').bind('keyup',function() {
		signup_password_strength();
	});
	
	if(location.href.indexOf('signup') != -1 && $('#signup_password').length)
		signup_password_strength();
		
	$('.graphics_table td').bind('click',function() {
		if($('img',this).length) {
			pop('/popup/graphics?image='+$('img',this).attr('id'));
			return false;
		}
	});
	
	$('.global_main_table tr.account').bind('mouseover',function() {
		$('img.visit_forum',this).attr('src','/resources/images/global/visit-hover.png');
	});
	$('.global_main_table tr.account').bind('mouseout',function() {
		$('img.visit_forum',this).attr('src','/resources/images/global/visit.png');
	});

	$('#global_login_submit').bind('click',function() {
		$('#global_login_form')[0].submit();
	});
	$('#global_register_submit').bind('click',function() {
		$('#global_register_form')[0].submit();
	});
	$('#global_addforum_submit').bind('click',function() {
		$('#global_addforum_form')[0].submit();
	});
	$('#global_activate_submit').bind('click',function() {
		$('#global_activate_form')[0].submit();
	});
	
	if(location.href.indexOf('iphone') != -1 || location.href.indexOf('android') != -1)
	{
		$('.feature').bind('mouseover',function() {
			$('#phone_threads').hide();
			$('#phone_'+this.id).show();
		});
		$('.feature').bind('mouseout',function() {
			$('#phone_'+this.id).hide();
			$('#phone_threads').show();
		});
	}
});

function signup_checkforum()
{
	if(signup_timeout)
		clearTimeout(signup_timeout);
	$('#signup_checkforum').html('<img src="/resources/images/signup/searching.gif" alt="Searching..." width="24" height="24" />')
	signup_timeout = setTimeout("signup_checkforum_go()",500);
}

function signup_password_strength()
{
	var messages = ["Poor","Still not good","Almost there...","Pretty good...","Great!","Excellent!"];
	var str = 0;
	var pass = $('#signup_password').val();
	if(pass.length >= 8)
		str++;
	if(pass.match(/[A-Z]/))
		str++;
	if(pass.match(/[a-z]/))
		str++;
	if(pass.match(/[1-9]/))
		str++;
	if(pass.match(/[^A-Za-z1-9]/))
		str++;

	if(pass.length <= 3)
		str = 0;
	else if(str > 1 && pass.length <= 5)
		str = 1;
	else if(str == 4 && pass.length >= 10)
		str = 5;
	
	var message = messages[str];
	
	if(pass.length > 20)
		message = '<span class="error">While your password may be strong,<br />Perhaps it is a tad too long.<br />And though it should not be too small,<br />You should choose one you can recall.</span>';

	if(pass == "password")
	{
		message = '<span class="error">Seriously, that is a terrible password.</span>';
		str = 0;
	}
	
	if(pass.length && pass == $('#signup_input_forum').val())
	{
		message = '<span class="error">Pick a different password.</span>';
		str = 0;
	}
	
	$('#signup_passwordstr').html(message+'<br /><img src="/resources/images/signup/pw'+str+'.png" width="200" height="12" alt="Password Strength" /> ');
}

function signup_checkforum_go()
{
	if($('#signup_input_forum').val() == "")
	{
		$('#signup_checkforum').html('');
		return;
	}
	signup_value_sent = $('#signup_input_forum').val();
	$.ajax({
		async    : true,
		url      : 'signup_checkforum.cgi',
		type     : 'POST',
		dataType : 'json',
		data     : {
			forum : $('#signup_input_forum').val()
		},
		success : function(data,txt,xhr) {
			
			if(data == null || !data.success)
				$('#signup_checkforum').html('<img src="/resources/images/signup/bad.png" width="24" height="24" alt="Error" /> There was an error checking if this URL is available.');
			else
			{
				if(data.invalid_username)
					$('#signup_checkforum').html('<img src="/resources/images/signup/bad.png" alt="Invalid URL" width="24" height="24" /> URLs may only contain alphanumeric characters and hyphens.')
				else if(data.taken)
					$('#signup_checkforum').html('<img src="/resources/images/signup/bad.png" alt="URL taken" width="24" height="24" /> This URL is already taken.')
				else
					$('#signup_checkforum').html('<img src="/resources/images/signup/good.png" alt="URL available" width="24" height="24" /> This URL is available!')
			}
		}
	});
}

function pop(url)
{
	if(opened_popup)
		opened_popup.close();
	opened_popup = window.open(url,"newwin","status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=1,scrollbars=1,width=500,height=400,top=50,left=50");
	opened_popup.focus();
}

