var st_D=new Date();
function set_i_am(obj,no){
	var opt=obj.options[obj.selectedIndex].value.split(':');
	document['RF'+no].i_am.value=opt[0];
	document['RF'+no].looking_for.value=opt[1];
}
function is_taken(v){
	if(v=='Choose a display name') return;
	var D=new Date();
	if (D.getTime()-st_D.getTime()<900) return;
	document.getElementById('usr_taken').src="?page=helpers/ajax_username_taken&login="+escape(v)+"&skip=1";
}
function visual_is_taken(y){
	var msg=['',"Display name/Username is taken","All ready in use please enter a different one",
		 		"Too short","Invalid - Use letter and digits only"];
	$('.login_div .example').html(y?'<div id=err>'+msg[y]+'</div>':'This will appear on your profile; no spaces');
	if(y) $('.login_div input').addClass('err'); else $('.login_div input').removeClass('err');
}
function term_pop(a,sizes){
    if (!sizes) sizes='width=750, height=600,scrollbars=1';
    w=window.open(a,'TERMS',sizes);
}

function try_submit(n){
	var F;
	if (n){
		F=document['RF'+n];
		if (!F.agree.checked) return $('.terms_err').show();
		$('.terms_err').hide();
		$('.div_input input, .div_input select').removeClass('err');
		if (F.email.title==F.email.value || F.email.value.length<6 || F.email.value.indexOf('@')<2) return $('.email_div input').addClass('err');
		if (F.pass.title==F.pass.value || F.pass.value.length<6 ) return $('.pass_div input').addClass('err');
		if (F.login.title==F.login.value || F.login.value.length<4) return $('.login_div input').addClass('err');
	} else {
		F=document.F;
		$('.errormessage').hide();
		$('.text, .inputs span').removeClass('err');
		if (F.city.title==F.city.value || F.city.value.length<2) {
			$('.err_city').html("<span class=nub></span>Enter your City Name").show();
			return $('input[name$="city"]').addClass('err');
		}
		if ($('select[name$="country"]').val()==840){
			if (F.state.title==F.state.value || F.state.value.length<2) {
				$('.err_city').html("<span class=nub></span>Enter 2 letter State").show();
				return $('input[name$="state"]').addClass('err');
			}
			if (F.zip.title==F.zip.value || F.zip.value.length<5) {
				$('.err_city').html("<span class=nub></span>Enter 5 digit Zip code").show();
				return $('input[name$="zip"]').addClass('err');
			}
		}
		var bad=0,a=['marital_status','body_type','race'];
		for(i=0;i<a.length;i++){
			if ($('select[name$="'+a[i]+'"]').val()<=0) {
				bad++;
				$('.err_'+a[i]).html("<span class=nub></span>select one (What is your "+a[i].replace('_',' ')+")").show();
				$('select[name$="'+a[i]+'"]').parent().addClass('err');
			}
		}
		if (bad) return;
		if (F.pr_header.title==F.pr_header.value || F.pr_header.value.length<4) {
			$('.err_pr_header').html("<span class=nub></span>Required field - enter few words as your profile 'one liner'").show();
			return $('input[name$="pr_header"]').addClass('err');
		}
		if (F.general_info.value.length<25) {
			$('.err_general_info').html("<span class=nub></span>Require minimum of 25 char in length").show();
			return $('textarea[name$="general_info"]').addClass('err');
		}
	}
	F.submit();
}

$(document).ready(function () {
//show the notification or blink it.
	$(".actions a").live('click', function (e) {
		e.preventDefault();
		if ($("#notification").is(":visible")) {
			for(a='',i=0;i<8;i++) a+='.fadeTo(100,1,function(){$(this).addClass("blink")}).fadeTo(100,1,function(){$(this).removeClass("blink")})';
			eval('$("#notification")'+a);
		}
		else {
			$('html, body').animate({scrollTop: 0}, 250,function(){
				$("#notification").hide().delay(200).slideDown(100);
			});
		}
	});
//close the notification
	$("#closenotification").click(function (e) {
		e.preventDefault();
		$(this).parent().slideUp(100);
	});
// add some hover class to submit buttons
	$("#form_r1_submit, #form_r1_pop_submit, #form_r2_submit, #form_login_submit, #form_login_pop_submit, #form_help_submit, #form_help_pop_submit").mouseenter(function () {
		$(this).addClass("hover");
	}).mouseleave(function () {
		$(this).removeClass("hover");
	});
// add some class to make this list play nicely.
	$(".r2_box li:nth-child(5n)").addClass("fifth");
	$("#newreg_thumbs li:nth-child(3n)").addClass("third");
// use form input titles as labels / remove on focus / return on blur
	$("input:text").focus(function () {
		if ($(this).val() == $(this)[0].title) {
			$(this).val("");
		}
	});
	$("input:text").blur(function () {
		if ($(this).val() == "") {
			$(this).val($(this)[0].title);
		}
	});
	$("input:text").blur();
// switch back and forth between login and help forms
	$("#form_login a").click(function (e) {
		e.preventDefault();
		$("#form_login").fadeOut(200);
		setTimeout(function () {
			$("#form_help").fadeIn(200);
		}, 400);
	});
	$("#form_help a").click(function (e) {
		e.preventDefault();
		$("#form_help").fadeOut(200);
		setTimeout(function () {
			$("#form_login").fadeIn(200);
		}, 400);
	});
	
	$("#form_r2 .checkbox").click(function () {
        $(this).toggleClass('checked');
        var ckbox = $(this).find(':checkbox');
        ckbox.attr('checked', !ckbox.attr('checked'));
    });
	
// switch back and forth between login and help forms within the popup
	$("#form_login_pop a").click(function (e) {
		e.preventDefault();
		$("#form_login_pop").fadeOut(200);
		setTimeout(function () {
			$("#form_help_pop").fadeIn(200);
		}, 400);
	});
	$("#form_help_pop a").click(function (e) {
		e.preventDefault();
		$("#form_help_pop").fadeOut(200);
		setTimeout(function () {
			$("#form_login_pop").fadeIn(200);
		}, 400);
	});
// tipsy stuff
	$('.facebook').tipsy({	title: 'name',	gravity: 'nw'	});
	$('.twitter').tipsy({	title: 'name',	gravity: 'nw'	});
	$('.myspace').tipsy({	title: 'name',	gravity: 'n'	});
	$('.digg').tipsy({		title: 'name',	gravity: 'n'	});
	$('.youtube').tipsy({	title: 'name',	gravity: 'n'	});
	$('.tumblr').tipsy({	title: 'name',	gravity: 'ne'	});
	$('.blogger').tipsy({	title: 'name',	gravity: 'ne'	});
// append action icons to .entry so we dont need to have it 15 times in the source code
	$('div.entry').append('<div class="actions clearfix"><ul><li><a href="#"><span class="icon message"></span>Send Mail</a></li><li><a href="#"><span class="icon imchat"></span>Start Chat</a></li><li><a href="#"><span class="icon flirt"></span>Send Flirt</a></li></ul></div>');
// toggle the login pulldown
	$("#login_tab").toggle(function () {
		$("#login_panel").animate({
			top: "0px"
		}, "fast");
		$("#login_tab").animate({
			top: "125px"
		}, "fast");
		$("#login_tab").addClass("open");
	}, function () {
		$("#login_panel").animate({
			top: "-125px"
		}, "fast");
		$("#login_tab").animate({
			top: "0px"
		}, "fast");
		$("#login_tab").removeClass("open");
	});
	$('.entry').mouseenter(function (e) {
		$(this).addClass('hover');
		$(this).find('p').stop().animate({
			top: '2'
		}, 100);
		$(this).find('.actions').stop().animate({
			marginTop: '-20'
		}, 100);
	}).mouseleave(function (e) {
		$(this).removeClass('hover');
		$(this).find('p').stop().animate({
			top: '10'
		}, 100);
		$(this).find('.actions').stop().animate({
			marginTop: '1'
		}, 100);
	});
// gender tabs changer
	$("#thumb_grid h1 .right a").click(function (e) {
		e.preventDefault();
		$(this).siblings("a").removeClass("selected");
		$(this).addClass("selected");
	});

//popups
	$('a.pop, .thumb').click(function(){ pop_reg($(this).attr('rel')); });
	$('a.close').live('click', function() {
		$('#fade , .pop_block').fadeOut(function() {
			$('#fade, a.close').remove();
		});
		return false;
	});
});

function pop_reg(popID){
	$('#' + popID).fadeIn('fast').append('<a href="#" class="icon close">X</a>');
	var popMargTop = ($('#' + popID).height()) / 2 + 80;
	var popMargLeft = ($('#' + popID).width()) / 2 +20;
	$('#' + popID).css({
		'margin-top' : -popMargTop,
		'margin-left' : -popMargLeft
		});
	$('body').append('<div id="fade"></div>');
	$('#fade').css({'filter' : 'alpha(opacity=90)'}).fadeIn('fast');
	return false;	
}
