function alertPopUp(popUpAlert, forFB){
	
	$('embed, object, select').css({ 'visibility' : 'hidden' });
	
	$('body').append('<div id="overlay"></div>');
	
	var arrPageSizes = ___getPageSize()
	var arrPageScroll = ___getPageScroll();

	$('#overlay').css({
		position:			'absolute',
		top:				0,
		left:				0,
		'z-index':			9000,
		display:			'none'			
	});
	
	$('#overlay').css({
		backgroundColor:	'#000',
		opacity:			0.8,
		width:				arrPageSizes[0],
		height:				arrPageSizes[1],
		display:			'none'			
	}).fadeIn(800);
	
	popUpAlert.css({'display': 'block'});
	
	var windowWidth = $(window).width() / 2;
	var windowHeight = $(window).height() / 2;
	if (forFB == true) {
	    windowHeight = 200;
	}
	var xPos = (windowWidth - popUpAlert.width() / 2);
	if (forFB === true) {
	    xPos = (260 - popUpAlert.width() / 2);
	}
	var arrPageScroll = ___getPageScroll()
	var yPos = (windowHeight - popUpAlert.height() / 2) + (arrPageScroll[1]);
	popUpAlert.css({'left': xPos + 'px',
							'top': yPos + 'px'});
							
	$(window).scroll(function(){
		var arrPageScroll = ___getPageScroll()
		yPos = (windowHeight - popUpAlert.height() / 2) + (arrPageScroll[1]);
		popUpAlert.css({'left': xPos + 'px',
							'top': yPos + 'px'})
	});
	
	$('.patpUtilityClose').click(function(){
		_finish(popUpAlert);
		return false;	
	})
	
}

function ___getPageSize() {

	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
};	
			
function ___getPageScroll() {
	var xScroll, yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}
	arrayPageScroll = new Array(xScroll,yScroll);
	return arrayPageScroll;
};

function _finish(popUpAlert) {
	$('#overlay').fadeOut(function() { $('#overlay').remove(); });
	popUpAlert.hide();
	// Show some elements to avoid conflict with overlay in IE. These elements appear above the overlay.
	$('embed, object, select').css({ 'visibility' : 'visible' });
}

$(document).ready(function() {
	renderUtilityBar();

    $('.bookNowBanner').click(function() {
        alertPopUp($('#bookPopUp'), true);
        return false;
    });

    $('.bookNow').click(function() {
        alertPopUp($('#bookPopUp'), true);
        return false;
    });
    
    $('.register').click(function() {
        alertPopUp($('#registerPopUp'));
        return false;
    });

    $('.downloadVoucher h2, .downloadVoucher .download').click(function() {
        alertPopUp($('#voucherPopUp'), true);
        return false;
    });

    $('#grabAGiftLogin a').click(function() {
        alertPopUp($('#patpUtilityPopupLoginForGrabAGift'));
        return false;
    });

    $('.transferToPap').click(function() {
        var target = $(this).attr('href');
        $('#targetlink').attr('href', target);
        alertPopUp($('#TransferToPatpPopUp'), null);
        return false;
    });

    $('.bookNowParty').click(function() {
        alertPopUp($('#bookPopUpParty'), true);
        return false;
    });
    
    $('.bookNowArea').click(function() {
        alertPopUp($('#bookPopUpArea'), true);
        return false;
    });
    
    $('.bookNowKaraoke').click(function() {
        alertPopUp($('#bookPopUpKaraoke'), true);
        return false;
    });
});

var SSODomain = 'http://www.partyatthepub.co.uk';


function renderUtilityBar() {

    if ($('#patpUtility').length > 0) {

        $.ajax({
            type: 'GET',
            dataType: 'jsonp',
            url: SSODomain + '/General/General/UtilityBarJson/',
            success: function(data) {

                $('#patpUtility').replaceWith(data["html"]);

                $('#patpUtilityLogin').click(function() {
                    alertPopUp($('#patpUtilityPopupLogin'));
                    return false;
                });

                $('.patpUtilityLogin').click(function() {
                    alertPopUp($('#patpUtilityPopupLogin'));
                    return false;
                });

                $("#patpUtilityLogout").click(function() {
                    $.ajax({
                        type: 'GET',
                        dataType: 'jsonp',
                        url: SSODomain + '/Members/MemberAuthentication/QuickLogout/',
                        success: function(data) {
                            $.ajax({
                                type: 'GET',
                                url: '/Members/MemberAuthentication/QuickLogout/',
                                success: function(data) {
                                    top.location.href = '/';
                                }
                            });
                        }
                    });
                });

                $('#patpUtilityMember').click(function() {
                    alertPopUp($('#patpUtilityPopupRegister'));

                    return false;
                });

                $('.transferToPap').click(function() {
                    var target = $(this).attr('href');
                    $('#targetlink').attr('href', target);
                    alertPopUp($('#TransferToPatpPopUp'), null);
                    return false;
                });
            }
        });
    }
}


