$(function()
{
	var email;
	//Everything after the ?
	var params = window.location.search.substr(1).split('&');
	
	$.each(params, function(idx, item){
		
		var param = item.split('=');
		
		if(param[0] == 'email')
		{
			email = unescape(param[1]);
		}
	});
	
	if(email && email != '')
	{
		email_subscription_Popup(pathInfo.contextPath + '/util/email_subscription.jsp?email=' + email);
	}
});

function loginCheck() {
	$.getJSON(pathInfo.contextPath + '/json/loggedIn.jsp?randid=' + Math.random(), function(data) {
		logged_in = data.logged_in;
		
		if (data.logged_in) {
			$('#login_out').html('<a href="' + pathInfo.secureUri + pathInfo.contextPath + '/agent/my_account">My Account</a>');
		} else {
			$('#login_out').html('<a href="' + pathInfo.secureUri + pathInfo.contextPath + '/agent/login">Login</a>');
		}
	});
}

function wishlist_count() {
	$.getJSON(pathInfo.contextPath + '/list/show_lists?count=true&randid=' + Math.random(), function(data) {
		$('#wishlist_count').html('<a href="' + pathInfo.secureUri + pathInfo.contextPath + '/list/show_lists">Wish List (' + data.count + ')</a>');
	});
}

function userInfoUpdate() {
	$.getJSON(pathInfo.contextPath + '/json/userInfo.jsp?randid=' + Math.random(), function(data) {
		logged_in = data.logged_in;
		
		if (data.logged_in) {
			$('#login_out').html('<a href="' + pathInfo.secureUri + pathInfo.contextPath + '/agent/my_account">My Account</a>');
		} else {
			$('#login_out').html('<a href="' + pathInfo.secureUri + pathInfo.contextPath + '/agent/login">Login</a>');
		}
			
		item_count = data.item_count;
		$('#cart-link').html('Cart (' + item_count + ')');
		
	});
}

function email_subscription_Popup(servlet){
	$.ajax({ 
		url: servlet, 
		success: function(response){
			$('.popup').html(response);
			show_popup();
      	}
	});
}


function submit_email_subscription(){
	$.ajax({ 
		url: pathInfo.contextPath + '/servlet/EmailSubscriptionServlet', 
		data: get_form_values('email_signup'), 
		success: function(response){
			$('.popup').html(response);
			show_popup();
      	}
	});
}

function show_modal_cart(){
	hide_popups();
	fill_modal_cart_with_page(pathInfo.contextPath + '/shopping/show_cart/');
}
