
/* vertCenterContainer(): for lofts, used to vertically center the Floor Plate
 * image's container, by setting a negative margin-top equal to 1/2 the floor
 * plate image height.  Could be used for most any element for which height()
 * returns something useful. */
(function($) {
        $.fn.vertCenterContainer = function(prop) {
            prop = (prop == 'padding')? 'padding-top' : 'margin-top';
            //console.log('vertCenterContainer() %x, parent=%x: ', $(this).height(), $(this).parent().height());
            return $(this).css(prop, ($(this).parent().height() - $(this).height())/2).addClass('loaded'); 
        };
})(jQuery);


var dosignin = function() {
    bxOverlay.show();
    $('#loginform').submit( function() {
            $.post($(this).attr('action'), $(this).serialize(), function(data, textStatus) {
                    if (textStatus == 'success' && data == '1') {
                        document.location = '/news.html';
                    }
                    else {
                        if ($('#loginform .error').length == 0) {
                            $('#loginform').prepend('<div class="error">Sorry, incorrect login</div>');
                        }
                    }
                });
                return false;
            });
    // $('#bxLightbox .login').corner("round 10px cc:#fc0"); // not working on top of the semi-transpareny overlay :( 
    return false;
}

$( function () {

         /* vertically center top text content by setting the top margin to 1/2 the height. */
         if ($('.topContentTxtW').wrapInner('<div id="topContentMagicW"></div>')) {
             $('#topContentMagicW').vertCenterContainer();
         }
         $('#specialProjectsContentContainer .ctxt').vertCenterContainer('padding');

         /* init jScrollPane for bottom content 
          * NOTE: msie7 (not 6 or 8) is barfing
          * on the special_projects pages, when the scroll is enabled. Nothing
          * shows up - maybe b/c this is the only one with a floated element.
          * who knows. Don't need scrolling here - yet. */
         if ($('#specialProjectsContentContainer').length == 0) {
             $('.scroll').jScrollPane({showArrows:true, scrollbarWidth:2, scrollbarMargin:0});
         }

         $('#siteNav').children(':last').bind('click', dosignin);

});


var bxOverlay = { 

    settings : { // Configuration related to overlay
        overlayBgColor: 		'#999',		
        overlayOpacity:			0.7
               },

    getBoxContent : function() {
                        return '<div class="login"><div class="logincornerstop"></div><div class="logincontent"><p>Please type in the password given to you by a Lowell Blake Associate below.</p><form id="loginform" action="/news/auth.html" method="post"><div>Password</div><input id="pw" type="password" name="sip_pass" /><div class="submit"><input type="submit" value="Go" /></div></form></div><div class="logincornersbot"></div></div>';
    },

    show : function () {
        if ($('#bxOverlay').length) { return false };
        //
        // Hime some elements to avoid conflict with overlay in IE. These elements appear above the overlay.
        $('embed, object, select').css({ 'visibility' : 'hidden' });
        this._add_nodes();

        this._doPositioningDance();

        // Style overlay and show it
        $('#bxOverlay').css({
            backgroundColor:	this.settings.overlayBgColor,
            opacity:			this.settings.overlayOpacity
        }).fadeIn();

        $('#bxLightbox').show();

        // Assigning click events in elements to close overlay
        $('#bxOverlay').click( this._finish );

        // If window was resized, calculate the new overlay dimensions
        $(window).resize( bxOverlay._doPositioningDance );
    },


    _doPositioningDance : function () {
        if ($('#bxOverlay').length) {
				// Get page sizes
				var arrPageSizes = bxOverlay.___getPageSize();
                //alert(arrPageSizes);
                //
				// Style overlay and show it
				$('#bxOverlay').css({
					//width:		arrPageSizes[0],
					height:		arrPageSizes[1]
				});
				// Get page scroll
				var arrPageScroll = bxOverlay.___getPageScroll();
                //console.log(arrPageScroll);
                //
                var H = $('#bxLightbox').height();
				// Calculate top and left offset for the jquery-lightbox div object and show it
				$('#bxLightbox').css({
                    top:	arrPageScroll[1] + ((arrPageSizes[3] - H) / 2),
					left:	arrPageScroll[0]
				});
        }
    },

    _add_nodes: function () {
        $('body').append('<div id="bxOverlay"></div>');
        $('body').append('<div id="bxLightbox">'+ this.getBoxContent() +'</div>');
    },

    _finish : function() {
			$('#bxLightbox').remove();
			$('#bxOverlay').fadeOut(function() { $('#bxOverlay').remove(); });
            //
			// Show some elements to avoid conflict with overlay in IE. These elements appear above the overlay.
			$('embed, object, select').css({ 'visibility' : 'visible' });
    },


		/**
		 / THIRD FUNCTION
		 * getPageSize() by quirksmode.com
		 *
		 * @return Array Return an array with page width, height and window width, height
		 */
    ___getPageSize : function () {
			var xScroll, yScroll;
			if (window.innerHeight && window.scrollMaxY) {	
				xScroll = window.innerWidth + window.scrollMaxX;
				yScroll = window.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;
		},
		/**
		 / THIRD FUNCTION
		 * getPageScroll() by quirksmode.com
		 *
		 * @return Array Return an array with x,y page scroll values.
		 */
    ___getPageScroll : function () {
			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;
		}

};
