(function(){
    var $ = jQuery,
    bdPopupDefaults = {		 
		setCookie: 'blank',  
        cookieExpires: 9999, 
        width: 300, 
		height: 300, 
		top: 30, 
		offset: 0, 
		loadUrl: 'iframe.htm', 
		autoClose: false, 
		autoCloseTime: 60, 
		openDelay: false, 
		openDelayTime: 60, 
		titleBarText: '',
		blockId: 'bdPopup',
		showEffect: 'show',
		showDuration: 0,
		closeEffect: 'hide',
		closeDuration: 0
	};   		
    $.bdPopup = function(options){
		var options = $.extend({}, bdPopupDefaults, options);	
		
		if (document.cookie.indexOf('displayPopup'+ options.blockId +'=false') != -1 && (options.setCookie == 'close' || options.setCookie == 'view')) {
			return false;
		}
		
		//loadUrl, titleBarText, blockId
		if( $('#' + options.blockId).length ) {
			$('#' + options.blockId).remove();
		}
		
		var popup = $(
			'<div id="'+ options.blockId +'" class="popupBox">' +
				'<div class="popupTitleBar">' +
					'<div class="bdPopupTitle">' +
						options.titleBarText +
					'</div>' +
					'<div class="popupClose">' +
					'</div>' +
				'</div>' +
				'<div class="bdPopupBody">' +
					'<div class="iframeH"></div>' +
					'<iframe src="' + options.loadUrl + '" name="popupIframe">' + 
					'</iframe>' +
				'</div>' +
			'</div>').hide().appendTo('body');

		
		
		//setCookie, cookieExpires 
	   	var date = new Date();
		date.setTime(date.getTime() + options.cookieExpires * 1000 * 60 * 60 * 24);
		var cookieExpires = date.toUTCString();
		switch(options.setCookie) {
			case 'close':
				popup.find('.popupClose').bind('click', function(){
					document.cookie = 'displayPopup'+ options.blockId +'=false; expires=' + cookieExpires + ';';
				});
				break;
			case 'view':
				document.cookie = 'displayPopup'+ options.blockId +'=false; expires=' + cookieExpires + ';';
				break;
			case 'blank':
				document.cookie = 'displayPopup'+ options.blockId +'=true; expires=-1;';
				break;
		}



		
		
		//width, height, top, offset 
		popup.width(options.width);
		popup.height(options.height);
		popup.top(options.top);
		popup.left( $(document).width()/2 - options.width/2 + options.offset );
		
		popup.find('iframe').height(options.height - popup.find('.popupTitleBar').height() );
		
		// For dragging
		popup.find('.iframeH')
			.hide()
			.height(options.height - popup.find('.popupTitleBar').height())
			.width('100%')
			.css({'position':'absolute', 
				'z-index':'2',
				'background':'red', // special for ie
				'opacity': 0, // special for ie
				'top':popup.find('.popupTitleBar').height()
			});
		
				

		//drag
		var popupZIndex = popup.css('z-index');
		popup.mousedown(function(e){
			popup.css({'z-index':100, 'cursor':'move'})
			popup.find('iframe').css({'position':'absolute', 'z-index':'1'});
			popup.find('.iframeH').show();
			var beginPageX = e.pageX,
				beginPageY = e.pageY,
				beginLeft = popup.left(),
				beginTop = popup.top();
			$(document).bind('mousemove', function(e){
				popup.left(beginLeft + e.pageX - beginPageX);
				popup.top(beginTop + e.pageY - beginPageY);
			});
		}).add(document).mouseup(function(){
			popup.css({'z-index':popupZIndex, 'cursor':'default'});
			popup.find('iframe').css({'position':'static', 'z-index':'auto'});
			popup.find('.iframeH').hide();
			$(document).unbind('mousemove');
		});
		
		
		// Show and close functions
		var showPopup = function() {
			$.bdPopupShow(options.blockId, options.showEffect, options.showDuration);
		}
		
		var closePopup = function() {
			$.bdPopupClose(options.blockId, options.closeEffect, options.closeDuration);
		}
		
		// Close event
		popup.find('.popupClose').bind('click', function(){
			closePopup();
		});
		
		// Open Delay
		if (options.openDelay) {
			setTimeout(function() {
				showPopup();
			}, options.openDelayTime*1000)
		} else {
			showPopup();
		}
		
		// Autoclose
		if (options.autoClose) {
			setTimeout(function(){
				closePopup();
			}, (options.autoCloseTime + ((options.openDelay) ? options.openDelayTime : 0))*1000);
		}
    }
	
	$.bdClear = function() {
		for(var i = 0; i < arguments.length; i++) {
			document.cookie = 'displayPopup'+ arguments[i] +'=true; expires=-1;';
		}
	}
	
	$.bdPopupClose = function(blockId, effect, duration) {
		if(!$.isArray(effect)) {
			effect = [effect];
		}
		for (var i = 0; i < effect.length; i++) {
			$('#' + blockId)[effect[i]]({duration:duration, queue:false});
		}
		setTimeout(function(){
			$('#' + blockId).remove();
		},duration);
	}
	
	$.bdPopupShow = function(blockId, effect, duration) {
		if(!$.isArray(effect)) {
			effect = [effect];
		}
		for (var i = 0; i < effect.length; i++) {
			$('#' + blockId)[effect[i]]({duration:duration, queue:false});
		}
	}
	
	$.positions = function(p) {
		$.fn[p] = function() {
			switch(arguments.length) {
				case 0:
					var pc = $(this).css(p);
					return (pc == 'auto') ? 0 : parseInt(pc);
				case 1:
					$(this).css(p, arguments[0]);
			}
		}
	}
	
	$.positions('left');
	$.positions('top');
	
})();
