var loginIntervalID = 0;

$(document).ready(function(){
	//###############   User Controls Popup Callback   ###############
	$("#user-nav li a").prettyPhoto({
		allowresize: true,
		callback: function(){
			if ( window.location.href.search(/#open-reg/) >= 0 ) {
				var currentURL = new Array();
				currentURL = window.location.href.split('#');
				window.location.href = currentURL[0] + "#";
//				window.location.href = window.location.href + "#";
				$("#user-nav li:eq(1) a").trigger('click');
			}
			clearInterval( loginIntervalID );
			return false;
		}
	});

	//###############   Login Listener (for close links)   ###############
	$("#user-nav li a").mouseup(function () {
		clearInterval( loginIntervalID );
		var currentURL = new Array();
		currentURL = window.location.href.split('#');
		window.location.href = currentURL[0] + "#";
//alert("1="+currentURL[0]+" 2="+window.location.href);
//		window.location.href = window.location.href + "#";
		loginIntervalID = setInterval ("CheckPopupClose()", 500);
	});

	//###############   Login Popup - Register Link   ###############
	$("#login #register-link a").click(function () {
		var currentURL = new Array();
		currentURL = top.location.href.split('#');
		top.location.href = currentURL[0] + "#open-reg";
//		top.location.href = top.location.href + "#open-reg";
//		top.location.href = "http://www.pallantbookshop.com/#open-reg";

		return false;
	});

	//###############   Login Popup - Password Reminder Link   ###############
/*	$("#login #register-link a").click(function () {
		var currentURL = new Array();
		currentURL = top.location.href.split('#');
		//top.location.href = currentURL[0] + "#password-reminder";
		window.location.href = "/user/password-reminder";

		return false;
	});*/

	//###############   Register Popup - Links off   ###############
	$("#register #register-form a").click(function () {
		//###   Redirect to another URL   ###
		top.location.href = "http://www.pallantbookshop.com" + $(this).attr("href");
		clearInterval( loginIntervalID );

		return false;
	});

	//###############   Logout   ###############
	$("#logout-link").click(function () {
		var currentURL = new Array();
		currentURL = top.location.href.split('#');
		//var urlStart = (("https:" == top.location.protocol) ? "https:" : "http:");
//"http://www.pallantbookshop.com" + 
		$.ajax({
			url: "/index.php?ACT=10",
			success: function (html) {
				$("#user-nav li:gt(1)", window.parent.document).addClass('hidden');
				$("#user-nav li:lt(2)", window.parent.document).removeClass('hidden');
			},
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				//alert(textStatus + " " + errorThrown);
			},
			complete: function (XMLHttpRequest, textStatus) {
				//alert(textStatus);
			}
		});

		top.location.href = currentURL[0] + "#close-pp-delay-logout";
//		top.location.href = top.location.href + "#close-pp-delay";

		return false;
	});

}); //###   End of DOM Ready   ###


//###############   Function called on Interval to close popup windows when location.hash has changed   ###############
function CheckPopupClose() {
//if ( window.location.href.search(/#close-pp/) >= 0 ) {
	var currentURL = new Array();
	currentURL = window.location.href.split('#');

	if ( window.location.hash == "#close-pp-delay-signin" ) {
		window.location.href = currentURL[0] + "#";
//		window.location.href = window.location.href + "#";
		clearInterval( loginIntervalID );
		$("#user-nav li:gt(1)").removeClass('hidden');
		$("#user-nav li:lt(2)").addClass('hidden');
		setTimeout( '$.prettyPhoto.close()', 6000 );

	} else if ( window.location.hash == "#close-pp-delay" ) {
		window.location.href = currentURL[0] + "#";
		clearInterval( loginIntervalID );
		setTimeout( '$.prettyPhoto.close()', 6000);

	} else if ( window.location.hash == "#close-pp-delay-logout" ) {
		window.location.href = currentURL[0] + "#";
		clearInterval( loginIntervalID );
		setTimeout( 'top.location.href = "http://www.pallantbookshop.com"', 6000);

	} else if ( window.location.hash == "#close-pp" ) {
		window.location.href = currentURL[0] + "#";
//		window.location.href = window.location.href + "#";
		$.prettyPhoto.close();
		clearInterval( loginIntervalID );

	} else if ( window.location.hash == "#open-reg" ) {
		$.prettyPhoto.close();
		clearInterval( loginIntervalID );
	}

	return false;
}
