jQuery(function(){
jQuery('.findastore,.findastore2').html('<span style="height:18px;width:233px;position:absolute;top:8px;left:9px;background:white;padding:3px;"><a style="background:#d37c2c;color:white;font-weight:bold;line-height:18px;text-decoration:none;margin:0 auto;height:18px;width:233px;text-align: center;position: absolute;top:3px;" href="/locator/store_locator.php">Find a Store</a></span>');
});
jQuery(function(){

	$ = jQuery;
	var rooturl = 'http://wizkidsgames.com',
		userstate = 0
	;
	
	var wkredir = {
		"page-template-user.postredemption-php": [rooturl, 1],
		"page-template-user.redemption-php": [rooturl+"/buyitbythebrick/", 1],
		"page-template-user.menu-php": [rooturl, 1],
		"page-template-buyitbythebrick-php": [rooturl+"/user/redemption", 2]
	};
	
	jQuery('input:text[alt]').bind('focusin', function(){
		$that = $(this);
		if($that.val() === $that.attr('alt')){
			$that.val('');
		};
	}).bind('focusout', function(){
		$that = $(this);
		if($that.val() === ''){
			$that.val($that.attr('alt'));
		};
	});
	
	$('#forgotpass').bind('click', function(){
		
		/*
		$('<div><p>The Wizkids Email system is currently down for maintenance.  We apologize for any inconvenience this may have on your redemption process.  Our system is currently unable to send emails of any kind (including password resets).  We are working to correct the issue immediately.</p></div>').dialog({
			modal:true,
			width:380
		});
		return false;
		*/
		
		$('#forgotpassbox').remove();
		$('<div id="forgotpassbox" title="Password Help">'+
			'<div id="forgotpassbox-pre">'+
				'<p>Enter <strong><em>either</em></strong> your username or email address below, and we\'ll email you a temporary password that you can change once you log in.  <em>The email may appear in your spam folder.</em></p>'+
				'<div id="forgotpass-err"></div>'+
				'<form id="forgotpassform">'+
					'<table border="0" cellspacing="0" cellpadding="0">'+
					'<tr><td><label for="forgotpassuser">Username:</label></td><td><input type="text" id="forgotpassuser" name="forgotpassuser" /></td></tr>'+
					'<tr><td><strong><em>or</em></strong></td></tr>'+
					'<tr><td><label for="forgotpassemail">Email:</label></td><td><input type="text" id="forgotpassemail" name="forgotpassemail" /></td></tr>'+
					'</table>'+
					'<div><input type="submit" value="Request New Password" /></div>'+
					'<p style="padding-top:9px;">or <a id="resendactlink" href="">Resend me an activation email.</a></p>'+
				'</form>'+
			'</div>'+
			'<div id="forgotpassbox-post" class="hidden">'+
				
			'</div>'+
		'</div>').dialog({
			modal:true,
			width:380,
			close:function(){
				$('#forgotpassbox').dialog('destroy').remove();
			},
			open:function(){
			
				$('#resendactlink').bind('click', function(){
					var fpuser = $('#forgotpassuser').val(),
						fpemail = $('#forgotpassemail').val(),
						reg = /^\s*$/
					;
					if(!reg.test(fpuser)){
						dat = {username: fpuser};
					}else if(!reg.test(fpemail)){
						dat = {email: fpemail};
					}else{
						$('#forgotpass-err').empty().html('<p class="reqnotice">You must enter a username or email.</p>');
						return false;
					};
					$('#forgotpassbox-pre').hide();
					$('#forgotpassbox-post').show().empty().html('<img src="http://wizkidsgames.com/v3/images/common/ajax-loader-blue.gif" />');
					
					$.ajax({
						url:rooturl+'/players/send_activation/',
						type:'post',
						dataType:'json',
						data:dat,
						success:function(resp){
							if(resp.result!=="success"){
								$('#forgotpassbox-post').empty().html('<p>There was an error sending your activation email, please close this popup and try again.</p><p class="reqnotice">'+resp.result+'</p>');
							}else{
								$('#forgotpassbox-post').empty().html('<p>Success!  Please check your email for your activation link.  <em>The email may appear in your spam folder.</em></p>');
							};
						},
						error:function(resp){
							$('#forgotpassbox-post').empty().html('<p>There was an error sending your activation email, please close this popup and try again.</p>');
						}
					});
					return false;
				});
			
				$('#forgotpassform').bind('submit', function(){
					var fpuser = $('#forgotpassuser').val(),
						fpemail = $('#forgotpassemail').val(),
						reg = /^\s*$/
					;
					if(!reg.test(fpuser)){
						dat = {username: fpuser};
					}else if(!reg.test(fpemail)){
						dat = {email: fpemail};
					}else{
						$('#forgotpass-err').empty().html('<p class="reqnotice">You must enter a username or email.</p>');
						return false;
					};
					
					$('#forgotpassbox-pre').hide();
					$('#forgotpassbox-post').show().empty().html('<img src="http://wizkidsgames.com/v3/images/common/ajax-loader-blue.gif" />');
					
					$.ajax({
						url:rooturl+'/players/reset_password/',
						type:'post',
						dataType:'json',
						data:dat,
						success:function(resp){
							if(resp.result!=="Password Reset"){
								$('#forgotpassbox-post').empty().html('<p>There was an error resetting your password, please close this popup and try again.</p><p class="reqnotice">'+resp.result+'</p>');
							}else{
								$('#forgotpassbox-post').empty().html('<p>Success!  Please check your email for your password reset.  <em>The email may appear in your spam folder.</em></p>');
							};
						},
						error:function(resp){
							$('#forgotpassbox-post').empty().html('<p>There was an error resetting your password, please close this popup and try again.</p>');
						}
					});
					return false;
				});
			}
		});
		return false;
	});
	
	
	
	
	// check logged in state
	// --------------
	$.ajax({
		type:'post',
		dataType:'json',
		cache:false,
		url:rooturl+'/players/check_login/',
		success:function(resp){
			if(resp.logged_in==="yes"){
				for(var key in wkredir){
					if($(document.body).hasClass(key)){
						
						if(wkredir[key][1]===2){
							window.location = wkredir[key][0];
						};
					};
				};
				userstate=1;
				$('#quick-out, #quick-load').css({display:"none"});
				$('#quick-in').css({display:"block"});
				$('.top .username').text(resp.username);
				$.ajax({
					type:'get',
					dataType:'text',
					data:{"user_email":resp.email},
					url:rooturl+'/wp-content/themes/wizkids_v4/includes/gravatar.php',
					success:function(resp2){
						$('#quick-in .gravatar').html('<img src="http://www.gravatar.com/avatar/'+resp2+'?s=44&d=mm" />');
					}
				});
				uniqid=resp.unique_id;
				if($('#redeemform').length>0){$('#redeemform').prepend('<input type="hidden" name="custom" id="custom" value="'+uniqid+'" />');};
			}else{
				$('#quick-in, #quick-load').css({display:"none"});
				$('#quick-out').css({display:"block"});
				for(var key in wkredir){
					if($(document.body).hasClass(key)){
						if(wkredir[key][1]===1){
							window.location = wkredir[key][0];
						};
					};
				};
			};
		}
	});
	
	
	// logout action
	// ---------------
	$('#playerquick .logout').bind('click', function(){
		$.ajax({
			type:'post',
			dataType:'json',
			cache:false,
			url:rooturl+'/players/logout/',
			success:function(){
				window.location = rooturl;
			}
		});
		$('#quick-in').stop(true).animate({opacity:0}, function(){
			$('#quick-in').css({display:'none'});
			$('#quick-out').css({display:'block'}).stop(true).animate({opacity:1});
			$('#quick-in .gravatar').html('<img src="http://www.gravatar.com/avatar/0?s=44&d=mm" />');
		});
		return false;
	});
	
	$('.usermenu .logout').bind('click', function(){
		$.ajax({
			type:'post',
			dataType:'json',
			cache:false,
			url:rooturl+'/players/logout/',
			success:function(){
				window.location = rooturl;
			}
		});
		return false;
	});
	
	/*
	$('#playerquick .loginform').bind('submit', function(){
		$('#quick-out').animate({opacity:0}, function(){
			$('#quick-out').css({display:'none'});
			$('#quick-in').css({display:'block'}).animate({opacity:1});
		});
		return false;
	});
	*/
	
	$('#findastore form').bind('submit', function(){
		window.location = rooturl+"/stores/#address="+$('.store', '#findastore').val()+"&radius=20";
		return false;
	});
	
	
	// login action
	// ---------------
	$('#playerquick .loginform').bind('submit', function(){
		var user = $(this).find('.user').val(),
			pass = $(this).find('.password').val();
			
		$('#quick-out, #playerquick-error').css({display:"none"});
		$('#quick-load').css({display:"block"});
		
		$.ajax({
			type:'post',
			dataType:'json',
			cache:false,
			data:{username:user, password:pass},
			url:rooturl+'/players/login/',
			success:function(resp){
				if(resp.result==="success"){
					$('#quick-load').css({display:"none"});
					$('.top .username').text(user);
					$.ajax({
						type:'get',
						dataType:'text',
						data:{"user_email":resp.email},
						url:rooturl+'/wp-content/themes/wizkids_v4/includes/gravatar.php',
						success:function(resp2){
							$('#quick-in .gravatar').html('<img src="http://www.gravatar.com/avatar/'+resp2+'?s=44&d=mm" />');
						}
					});
					uniqid=resp.unique_id;
					if($('#redeemform').length>0){$('#redeemform').prepend('<input type="hidden" name="custom" id="custom" value="'+uniqid+'" />');};
					$('#quick-in').css({display:'block', opacity:0}).stop(true).animate({opacity:1});
				}else{
					$('#playerquick-error').stop(true).css({opacity:0, display:'block'}).animate({opacity:1});
					$('#quick-load').stop(true).css({display:"none"});
					$('#quick-out').stop(true).css({display:'block', opacity:0}).animate({opacity:1});
				};
			}
		});
		return false;
	});
});

