;(function($){
	var timer;
	$(document).ready(function(){
		max_zindex();
		if(isset('popup_domination_delay') && popup_domination_delay > 0){
			timer = setTimeout(function(){
				$('#popup_domination_lightbox_wrapper').fadeIn('fast');
				center_it();
			},(popup_domination_delay*1000));
		}
		if(isset('popup_domination_center') && popup_domination_center == 'Y')
			init_center();
		if(check_cookie()){
			close_box();
		}
		if(isset('popup_domination_defaults')){
			var defaults = popup_domination_defaults;
			for(var i in defaults){
				if($.trim(defaults[i]) != ''){
					$('#popup_domination_lightbox_wrapper form :text[name="'+i+'"]')
						.data('default_value',defaults[i])
						.focus(function(){
							var $this = $(this);
							if($this.val() == $this.data('default_value'))
								$this.val('');
						}).blur(function(){
							var $this = $(this);
							if($this.val() == '')
								$this.val($this.data('default_value'));
						});
				}
			}
		}
		$('#popup_domination_lightbox_close').click(function(){
			close_box();
			return false;
		});
		$('#popup_domination_lightbox_wrapper form').submit(function(){
			var checked = true;
			$('#popup_domination_lightbox_wrapper :text').each(function(){
				var $this = $(this), val = $this.val();
				if($this.data('default_value') && val == $this.data('default_value')){
					if(checked)
						$this.val('').focus();
					checked = false;
				}
				if(val == ''){
					checked = false;
				}
			});
			if(checked){
				close_box();
				return true;
			}
			return false;
		});
	});
	function center_it(){
		$('.popup-dom-lightbox-wrapper .lightbox-main').css({
			position:'absolute',
			left: ($(window).width() - $('.popup-dom-lightbox-wrapper .lightbox-main').outerWidth())/2,
			top: ($(window).height() - $('.popup-dom-lightbox-wrapper .lightbox-main').outerHeight())/2
		});
	}
	function init_center(){
		center_it();
		$(window).resize(center_it);
	};
	function max_zindex(){
		var maxz = 0;
		$('body *').each(function(){
			var cur = parseInt($(this).css('z-index'));
			maxz = cur > maxz ? cur : maxz;
		});
		$('#popup_domination_lightbox_wrapper').css('z-index',maxz+10);
	};
	function close_box(){
		clearTimeout(timer);
		$('#popup_domination_lightbox_wrapper').fadeOut('fast');
		set_cookie();
	};
	function set_cookie(){
		if(isset('popup_domination_cookie_time') && popup_domination_cookie_time > 0){
			var date = new Date();
			date.setTime(date.getTime() + (popup_domination_cookie_time*86400*1000));
			window.document.cookie = ['popup_domination_hide_lightbox=Y','expires='+date.toUTCString()].join('; ');
		}
		return false;
	};
	function check_cookie(){
		var cookie = window.document.cookie;
		if(cookie.length > 0){
			var c_start = cookie.indexOf('popup_domination_hide_lightbox=');
			if(c_start !== -1){
				c_start = c_start + 31;
				var c_end = cookie.indexOf(';',c_start);
				if(c_end === -1)
					c_end = cookie.length;
				if(unescape(cookie.substring(c_start,c_end)) == 'Y')
					return true;
			}
		}
		return false;
	};
	function isset(varname){
		if(typeof varname == 'string'){
			if(typeof window[varname] === 'undefined' || window[varname] === null){
				return false;
			}
		}
		return true;
	};
})(jQuery);