;(function($) {

$.DF = {
	version:	'1.0',
	copyright:	'Dating Factory'
};

$.extend($.DF, {
	scripts: [],
	
	require: function(src) {
		if(this.scripts.indexOf(src) == -1) {
			document.write('<script language="JavaScript" type="text/javascript" src="' + src + '"></scr' + 'ipt>');
			this.scripts.push(src);
		}
	},
	
	_blockElementOverlayMessage: {
		message:	null,
		overlayCSS: {
			backgroundColor:	'#FFFFFF',
			opacity:			'0.6'
		}
	}
});
	
})(jQuery);

$.DF.require('/scripts/libs/jquery.blockUI.js');

$.DF.Popup = {
	_popup: 	null,
	_query:		[],
	_opened:	false,
	
	open: function(html, callback) {
		html = html || '<div class="loading"></div>';
		
		if(!$.DF.Popup._opened) {
			
			$.DF.Popup._popup.find('div.content').html(html);
			$.blockUI({message:$.DF.Popup._popup});

			$.DF.Popup.align();
			
			$.DF.Popup._opened = true;
			if($.isFunction(callback)) callback();
		}
		else {
			$.DF.Popup._query.push({html:html, callback:callback});
		}
	},
	
	update: function(html, callback) {
		if($.DF.Popup._opened && html) {
			$.DF.Popup._popup.find('div.content').html(html);
			$.DF.Popup.align('slow', callback);
		}
	},
	
	close: function(event) {
		$.DF.Popup._popup.find('div.content').html('');
		$.unblockUI();
		
		$.DF.Popup._opened = false;
		
		if($.DF.Popup._query.length  > 0) {
			setTimeout(function() {
				var data = $.DF.Popup._query.shift();
				$.DF.Popup.open(data.html, data.callback);
			}, 150);
		}
	},
	
	align: function(speed, callback) {
		var content = this._popup.find('div.content').children(':first');
		
		var width	= content.width() + 10;
		var height	= content.height() + 10;
		
		var left	= Math.max(0, parseInt($(window).width() - width)/2);
		var top		= Math.max(0, parseInt($(window).height() - height)/2);
		
		var popup	= this._popup.parents('div.blockMsg');
		
		if(speed != 'slow') {
			popup.resizeTo(width, height);
			popup.moveTo(left, top);
		}
		else {
			popup.animate({
				left:		left,
				top:		top,
				width:		width,
				height:		height
			}, 300, function() {
				if($.isFunction(callback)) callback();
			});
		}
	}
}

$(document).ready(function() {
	// Special style for blockUI
	$.extend($.blockUI.defaults, {
		css: { 
			padding:        0, 
			margin:         0, 
			width:          'auto', 
			top:            '40%', 
			left:           '35%', 
			textAlign:      'center', 
			border:         'none', 
			backgroundColor:'#ffffff'/*, 
			cursor:         'normal' */
		},
		
		overlayCSS:  { 
			backgroundColor:'#000000', 
			opacity:        '0.6' 
		},
		
		fadeIn:		0, 
		fadeOut:	0
	});
	
	$.DF.Popup._popup = $('<div class="popup"><div class="head"></div><div class="content"></div></div>');
	$(document.body).append($.DF.Popup._popup);
});

window.alert = function(text) {
	$.DF.Popup.open(String.format('<div class="alert"><p>{0}</p><button type="button" class="submit common" onclick="$.DF.Popup.close()">Ok</button></div>', text));
}

$.DF.Sticker = {
	_queue:		[],
	_sticker:	null,
	
	options:	{
		timeout:	15 * 1000,
		close:		true
	},
	
	open: function(type, html, options) {
		var sticker = this.buildHTML(type);
		var index = this._queue.push(sticker) - 1;

		var content = sticker.find('div.content');
		content.html(html);
		
		sticker.css('left',		-1000);
		sticker.css('top',		-1000);
		
		var node = sticker.find('div.content');
		
		if($.browser.msie)
			node.css('float', 'left').children(':first').css('float', 'left');
			
		var width	= node.realWidth()	+ 12;
		var height	= node.realHeight()	+ 12 + 5;
		
		if($.browser.msie) 
			node.css('float', '').children(':first').css('float', '');
		if($.browser.msie && $.browser.version < 7) 
			sticker.find('div.popup').width(width - 12);
		
		if(this._queue.length == 1) {
			var left	= parseInt($(window).width() - width)/2;
			var top		= parseInt($(window).height() - height)/2;
		}
		else {
			var offset	= this._queue[0].offset();
			var left	= offset.left	+ (this._queue.length - 1) * 20;
			var top		= offset.top	+ (this._queue.length - 1) * 20;
		}
		
		left	+= $(window).scrollLeft();
		top		+= $(window).scrollTop();
		
		sticker.css('opacity',	0);
		sticker.css('left',		left);
		sticker.css('top',		top);
		sticker.css('width',	width);
		sticker.css('height',	height);
		
		sticker.animate({
			opacity:1
		}, 200, function() {
			sticker.css('filter', '');
			if($.browser.msie) sticker.bgiframe();
		});
		
		options = $.extend({}, this.options, options || {});
		
		if(options.timeout > 0) {
			sticker.timer = setTimeout(function() {
				$.DF.Sticker.close(sticker);
			}, options.timeout);
		}
		
		if(options.close) {
			$('<img alt="close" title="close" width="16" height="16" src="/theme/images/ico_close.gif" class="close" />')
				.appendTo( sticker.find('div.popup'))
				.click(function() {
					$.DF.Sticker.close(sticker);			
				})
		}
		
		return sticker;
	},
	
	close: function(sticker) {
		if(sticker) {
			sticker.animate({
				opacity: 0
			}, 200, function() {
				sticker.trigger('remove');
				sticker.remove();
			});
			
			clearTimeout(sticker.timer);
			
			// Remove this sticker from queue
			var index = this._queue.indexOf(sticker);
			
			if(index >= 0 && index < this._queue.length) {
				for(var i=index+1; i<this._queue.length; i++) {
					this._queue[i].animate({
						left:	'-=20px',
						top:	'-=20px'
					}, 200);
				}
				
				var q1 = this._queue.slice(0, index);
				var q2 = this._queue.slice(index+1, this._queue.length);
				this._queue = q1.concat(q2);
			}
		}
	},
	
	buildHTML: function(type) {
		var node = $(String.format('<div class="sticker {0}"><div class="shadow"></div><div class="popup"><div class="head"></div><div class="content"></div></div></div>', type));
		$(document.body).append(node);
		node.find('div.shadow').css('opacity', 0.5);
		
		return node;
	}
}