var popup_check_auth = false;
var popup_auth_url = '';
var Popup = {

	init:function(name) {
	    $('a[rel=' + name + ']').live("click", function(e) {
	        //Cancel the link behavior
    	    e.preventDefault();

	        //Get the A tag
    	    var name = $(this).attr('name');
			if (popup_check_auth && !Auth.check()) {
				Auth.redirect(popup_auth_url);
				return false;
			}
			Popup.show(name);
			return false;
    	});

		return false;
	},

	show:function(name) {
   	    var id = '#' + name;
        //Get the screen height and width
   	    var maskHeight = $(document).height();
       	var maskWidth = $(window).width();

        //Set heigth and width to mask to fill up the whole screen
   	    $('#mask').css({'width':maskWidth,'height':maskHeight});

        //transition effect             
   	    $('#mask').fadeIn(100);        
       	$('#mask').fadeTo("fast",0.8);

        //Get the window height and width
   	    var winH = $(window).height();
       	var winW = $(window).width();

        //Set the popup window to center
   	    $(id).css('top',  winH/2-$(id).height()/2);
       	$(id).css('left', winW/2-$(id).width()/2);

        //transition effect
   	    $(id).fadeIn(200); 
/*		$(window).bind('scroll', function () {
			Popup.pos(name);
		});*/
		return false;
	},

	pos:function(name) {
   	    var id = '#' + name;
        //Get the screen height and width
   	    var maskHeight = $(document).height();
       	var maskWidth = $(window).width();

   	    var winH = $(window).height();
       	var winW = $(window).width();

        //Set the popup window to center
   	    $(id).css('top',  winH/2-$(id).height()/2);
       	$(id).css('left', winW/2-$(id).width()/2);

        //transition effect
   	    $(id).fadeIn(200); 
		return false;
	},

	open:function(name) {
	    //if close button is clicked
    	$('#close').live("click", function (e) {
        	//Cancel the link behavior
//	        e.preventDefault();
	        $('#mask').hide();
    	    $('.window').hide();
	    });                         

	},

	close:function(name) {
		el = $('#' + name);
	 	//if close button is clicked
		$('#close', el).live("click", function (e) {
        	//Cancel the link behavior
    	    var url = $(this).attr('href');
			if (url != '#') {
				$.getJSON(url, function(r) {
					if (r.status == 'OK' && r.redirect != '') {
						location.href = r.redirect;
					}
				});
			}
	        $('#mask').hide();
    	    $('.window').hide();
			return false;
	    });                         
	},

	check_auth:function(url) {
		popup_check_auth = true;
		popup_auth_url = url;
	}
};
