	$(document).ready(function(){
		//var returnPage=document.location;
		//if ($("#locationBox").html().length == 0) 
		
		$("#loginLink").click(function(){
			if($(this).text() == 'log in'){
				//alert("logging in")
				if(tp){
					document.location = 'http://shopridleys.com/u/userSignIn.php';
				}else{
					login();
				}
				return false;
			}else{
				//log out
				//window.location = '/userLogout.php';
				$.ajax({
					type: "POST",
					url: "http://shopridleys.com/u/userLogout_ajax.php",  // Send the login info to this page
					success: function(msg){
						//alert("logging out");
						$("#loginLink").text('log in');
						$("#reglink").show('slow');
						//$('.wsLink').attr('href','javascript:login(\'x\');');
						$("#locationBox").fadeOut(300);
						setTimeout(3000, gotoHome());
					}  
				});  
			}
		});
		
		// When the form is submitted
		$("#status > form").submit(function(){  
			
			// Hide 'Submit' Button
			$('#submit').hide();
			$('#login_response').html('');
			
			// Show Gif Spinning Rotator
			$('#ajax_loading').show();
			
			// 'this' refers to the current submitted form
			var str = $(this).serialize();  
			
			// -- Start AJAX Call --
			
			$.ajax({
				type: "POST",
				url: "http://shopridleys.com/u/userSignUpCheck_ajax.php",  // Send the login info to this page
				data: str,
				success: function(msg){  
				
				//$("#status").ajaxComplete(function(event, request, settings){  
					
					// Show 'Submit' Button
					//$('#submit').show();
					
					// Hide Gif Spinning Rotator
					$('#ajax_loading').hide();  
					
					if(msg.indexOf('OK')!= -1 ){ // LOGIN OK?
						
						//alert(msg);
						//alert("logging in");
						var login_response = '<p>You have successfully logged in!</p>';
						$("#reglink").fadeOut();
						//change link to ad (gets set on new page loads in _head.php w/ Session)
						$('.wsLink').attr('href','ad.php');
						
						var vaddon = '';
						if (adredirect === true) {
							vaddon = 'You will be taken to your weekly ad.'
						}
						$('#login_response').html(login_response+vaddon);
						
						$('#login_form').delay(2000).fadeOut('slow');
						//set link 
						$('#loginLink').text('log out');
						
						var sto = msg.substring( msg.indexOf('|')+1, msg.indexOf('%') );
						
						$('#locationBox').html('<a href="http://shopridleys.com/u/index.php">Advantage Card Home</a><a href="http://shopridleys.com/u/userChangeInfo.php">My Account Info</a><a href="Javascript:overlay();" class="slist">My Shopping List</a></div>');
						
						$("#locationBox").delay(200).fadeIn('slow') ;
						jaxSL(null); 
						//$('#loginLink').attr('href','/userLogout.php');
						if (adredirect === true)
							setTimeout(3000, gotoAd());
						else
							setTimeout(3000, gotoAdv());
						
					}else{ // ERROR?
						
						// alert(msg);
						$('#login_response').html("Problem: "+msg.replace('no dup',''));
						$('#submit').show();
						// $("#locationBox").hide()
					}  
					
				//});  
				
				}  
			
			});  
			
			// -- End AJAX Call --
			
			return false;
		
		}); // end submit event
	
	});
	
	var adredirect = false;
	function login(f){
		if(f) {
			$('#login_response').html('Please <a href="http://shopridleys.com/u/userSignUp.php">Register</a> or log in to see the ad.');
			adredirect = true;
		}else{
			$('#login_response').html('');
			adredirect = false;
		}
		$('#login_form').show('fast');
		$('#email').focus()
		$('#ajax_loading').hide();				  
	
	}
	function gotoAd(){
		window.location = 'http://shopridleys.com/ad.php';
	}
	function gotoAdv(){
		window.location = 'http://shopridleys.com/u/index.php';
	}
	function gotoHome(){
		window.location = 'http://shopridleys.com/index.php';
	}
(function($) {
	var imgList = [];
	$.extend({
		preload: function(imgArr, option) {
			var setting = $.extend({
				init: function(loaded, total) {},
				loaded: function(img, loaded, total) {},
				loaded_all: function(loaded, total) {}
			}, option);
			var total = imgArr.length;
			var loaded = 0;
			
			setting.init(0, total);
			for(var i in imgArr) {
				imgList.push($("<img />")
					.attr("src", imgArr[i])
					.load(function() {
						loaded++;
						setting.loaded(this, loaded, total);
						if(loaded == total) {
							setting.loaded_all(loaded, total);
						}
					})
				);
			}
			
		}
	});
})(jQuery);
