/**
 * MessageBox - Easy popup boxes v1.5 by Strong
 * Copyright (c) 2010 Strong - http://www.code-masters.org
 * Under GPL license.
 * Date: 09/03/2010
 * @author Strong
 * @version 1.5
 *
 * http://www.code-masters.org
 */
 
 // Initialize dialogs
 $(document).ready(function() {
	popup.dialog.init();					   
 });

// Create new dialog object
var popup = new Object();

popup.dialog = function(options) {
	// Set default box options
	popup.defaults = {
		title         : 'Внимание',
		content       : '',
		width         : 400,
		height        : 'auto',
		showOverlay   : true,
		overlayColor  : '#000',
		fixedPosition : false,
		opacity       : 0,
		buttons       : '',
		onShow        : '',
		onClose       : ''
	};
	
	popup.options = $.extend({}, popup.defaults, options);	
	
	popup.overlayWidth  = getWidth();
	popup.overlayHeight = getHeight();
	
	// If pressed ESC key, box close
	$(document).bind('keyup', function(e) {
			if (e.keyCode == 27) {
				popup.dialog.close();
			}
	});	
	
	popup.boxTitle.empty().html( popup.options.title );
	popup.boxContent.empty().html( popup.options.content );
	popup.boxButtons.empty().html( popup.options.buttons );
	
	// If overlay not null
	if(popup.options.opacity > 0) {
		// Set overlay background color
		popup.overlay.get(0).style.background = popup.options.overlayColor;
		// Set overlay opacity
		if($.browser.msie) {
			// IE opacity
			popup.overlay.get(0).style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity="+(popup.options.opacity*100)+")";
		} else {
			// Other "good" browsers (CSS 3.0 support)
			popup.overlay.get(0).style.opacity    = popup.options.opacity;
		}
	}
	
	// Set box width & height
	popup.boxContainer.get(0).style.width = typeof(popup.options.width) == 'string' ? popup.options.width : popup.options.width + 'px';
    popup.boxContainer.get(0).style.height = typeof(popup.options.height) == 'string' ? popup.options.height : popup.options.height + 'px';
	// Set overlay position
	popup.overlay.get(0).style.top  = 0;
	popup.overlay.get(0).style.left = 0;
	if($.browser.msie && $.browser.version <= 7) {
	popup.overlay.get(0).style.width  = popup.overlayWidth;
	}
	popup.overlay.get(0).style.height = popup.overlayHeight;
	// Show popup box
	popup.dialog.show();
}

popup.dialog.show = function() {
	if($.isFunction(popup.options.onShow)) {
		popup.options.onShow.apply();
	}
	
	if(popup.options.fixedPosition) {
		popup.dialog.position();
	}
	
	popup.dialog.align();
	if(popup.options.showOverlay) {
		popup.overlay.show();
	}
	popup.isOpen = true;
	popup.boxContainer.show();
}

popup.dialog.close = function() {
	if($.isFunction(popup.options.onClose)) {
		popup.options.onClose.apply();
	}
	
	popup.boxContainer.fadeOut( "fast", function() {
	popup.overlay.hide();
	
	// If overlay not null
	if(popup.options.opacity > 0) {
		// Set overlay background color
		popup.overlay.get(0).style.background = '';
		// Set overlay opacity
		if($.browser.msie) {
			// IE opacity
			popup.overlay.get(0).style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity="+parseInt(0)+")";
		} else {
			// Other "good" browsers (CSS 3.0 support)
			popup.overlay.get(0).style.opacity    = 0;
		}
	}
	
	$(document).unbind('keyup');
	$(window).unbind();
	popup.boxTitle.empty();
	popup.boxButtons.empty();
	popup.boxContent.empty();
	
	delete popup.options;
	
	popup.isOpen = false;
	
	});
}

popup.dialog.init = function() {	
	var overlay, box, boxTitle;
	// 
	overlay = '<div id="strongBoxOverlay"></div>';
	// Append to DOM overlay object
	$(document.body).append( overlay );
	// 
	box = '<div id="strongBox">'
	    + '<div class="transparency">&nbsp;</div>'
		+ '<div class="strongBoxContentWrapper">'
		+ '<div id="strongBoxTitle"></div>'
		+ '<div id="strongBoxContent"></div>'
		+ '<div id="strongBoxButtons"></div>'
		+ '</div>'
		+ '</div>';
	// Append to DOM box object
	$(document.body).append( box );
	// Confirm buttons
	popup.confirmButtons = '';
	// Close button
	popup.closeButton    = '';
	// Assets overlay   
	popup.overlay        = $('#strongBoxOverlay');
	// Assets box
	popup.boxContainer   = $('#strongBox');
	// Assets box title
	popup.boxTitle       = $('#strongBoxTitle');
	// Assets box buttons
	popup.boxButtons     = $('#strongBoxButtons');
	// Assets box content
	popup.boxContent     = $('#strongBoxContent'); 
	// Hide overlay object
	popup.overlay.hide();
	// Hide box object
	popup.boxContainer.hide();
}

popup.dialog.align = function() {
	var top, left, boxWidth, boxHeight;
	
	boxWidth  = parseInt(popup.boxContainer.width());
	boxHeight = parseInt(popup.boxContainer.height());
	
	// Get screen center coordinates
	left = Math.round($(document).scrollLeft() + ($(window).width() - boxWidth-20) / 2);
	top  = Math.round($(document).scrollTop()  + ($(window).height() - boxHeight-20) / 2);
	
	popup.boxContainer.css('top',  top  + 'px');
    popup.boxContainer.css('left', left + 'px');	
}

popup.dialog.position = function() {
	var t, t2;
	$(window).bind('scroll', function () {
			t=setTimeout(						   
			popup.dialog.align(), 1);
			})
		    .bind('resize', function () {
			t2=setTimeout(						  
			popup.dialog.align(), 1);
			});	
}

popup.alert = function(title, callback) {
	popup.dialog({
	title      : 'Внимание',
	content    : '<p align="center">'+title+'</p>',
	onClose    : callback,
	width      : 500,
	buttons    : '<div class="fb_button_yes">'
	             + '<div onclick="popup.dialog.close()">Закрыть</div>'
				 + '</div>'
	});
	
	return false;
}

function getHeight() {
		// handle IE 6
		if ($.browser.msie && $.browser.version < 7) {
			var scrollHeight = Math.max(
				document.documentElement.scrollHeight,
				document.body.scrollHeight
			);
			var offsetHeight = Math.max(
				document.documentElement.offsetHeight,
				document.body.offsetHeight
			);

			if (scrollHeight < offsetHeight) {
				return $(window).height() + 'px';
			} else {
				return scrollHeight + 'px';
			}
		// handle "good" browsers
		} else {
			return $(document).height() + 'px';
		}
}

function getWidth() {
		// handle IE 6
		if ($.browser.msie && $.browser.version < 7) {
			var scrollWidth = Math.max(
				document.documentElement.scrollWidth,
				document.body.scrollWidth
			);
			var offsetWidth = Math.max(
				document.documentElement.offsetWidth,
				document.body.offsetWidth
			);

			if (scrollWidth < offsetWidth) {
				return $(window).width() + 'px';
			} else {
				return scrollWidth + 'px';
			}
		// handle "good" browsers
		} else {
			return $(document).width() + 'px';
		}
}