(function($){
	window.popupWindow={};
	var pw=window.popupWindow;
	pw.extend=$.extend;
	pw.extend({
		opened: false,
		windowEventsBinded: false,
		xhrobj: null,

		open: function(options){
			window.BannerHandler.hideAll();
			if(window._gaq) window._gaq.push(['_trackPageview']);
			pw.stop();
			pw.options={
				ajax: false,
				width: 640,
				height: 'auto',
				content: false,
				box: false,
				opened: function(){},
				completed: function(){},
				closed: function(){}
			};
			pw.options=$.extend(pw.options,options || {});
			pw.openLayer(pw.options.width,pw.options.height);

			if(pw.options.content){
				$('#sitePopup .site-popup-content').html(
					pw.options.content
				)
			}
			if(pw.options.box){
				pw.options.box=$.extend({title:'',content:pw.options.content?pw.options.content:'',symbol:'dandelion'},pw.options.box);
				$("#sitePopup .site-popup-content").html(
					$( "#popupBoxTemplate" ).tmpl( pw.options.box )
				)
				if(pw.options.height!='auto'){
					$("#sitePopup .site-popup-content .box-content").css(
						{
							height: pw.options.height-50,
							overflow: 'auto'
						}
					);
				}
			}
			
			if(pw.options.ajax){
				pw.options.ajax=$.extend(pw.options.ajax,{
					type:'POST'
				});
				pw.xhrobj=$.ajax(options.ajax);
				pw.xhrobj.complete(pw.resizeLayer);
				if(!(typeof(pw.options.ajax.popup_update_disable)!='undefined' && pw.options.ajax.popup_update_disable))
				{
					$(pw.options.box?'#sitePopup .box-content':'#sitePopup .site-popup-content')
								.html("<div style='height: 100px;' class='site-popup-content-loader'></div>");
					window.ajaxLoader.addSlotLoader('#sitePopup .site-popup-content-loader');
					pw.xhrobj.complete(
						function(req){
//							window.ajaxLoader.removeSlotLoaders();
							window.ajaxLoader.removeSlotLoader('#sitePopup .site-popup-content-loader');
							$(pw.options.box?'#sitePopup .box-content':'#sitePopup .site-popup-content')
								.html(req.responseText);
							pw.resizeLayer();
						}
					);
				}
				pw.xhrobj.complete(pw.options.completed);
			}

			$(document).trigger("myvip-popup-opened");
			pw.options.opened();
		},
		stop: function(){
			if(pw.xhrobj)pw.xhrobj.abort();
			$("#sitePopup").stop(true,true).hide();
			pw.opened=false;
		},
		openLayer: function(width,height){
			var sizeOptions={
				width: $(document).width(),
				height: $(document).height()
			};
			if(!isModernBrowser()){
				$("#sitePopup .site-popup-bg").css('background','transparent');
			}
			$("#sitePopup").stop(false,false).fadeIn();
			$("#sitePopup .site-popup-bg").css(sizeOptions);
			$("#sitePopup .site-popup-container").css(sizeOptions);
				//.click(function(){pw.close()});

			var mTop=(($(window).height()-height)/2)+$(window).scrollTop();
			mTop=(mTop<0)?0:mTop;

			$("#sitePopup .site-popup-content").css({
				width: width,
				height: height,
				marginTop: mTop
			});
			pw.resizeLayer();
			pw.opened=true;

			if(!pw.windowEventsBinded) {
				$(window).bind('resize',function() {
						if(pw.opened) {
							pw.resizeLayer();
						}
					}
				);
			}
		},
		resizeLayer: function() {
			var height=$("#sitePopup .site-popup-content").height();

			$("#sitePopup .site-popup-bg").css({width: 'auto'});
			$("#sitePopup .site-popup-container").css({width: 'auto'});

			var neww=$(document).width()>$(window).width()?$(window).width():$(document).width();

			$("#sitePopup .site-popup-bg").css(
				{
					width: neww,
					height: $(document).height()
				}
			);

			$("#sitePopup .site-popup-container").css(
				{
					width: neww,
					height: $(document).height()
				}
			);

			var mTop=(($(window).height()-height)/2)+$(window).scrollTop();
			mTop=(mTop<0)?0:mTop;

			$("#sitePopup .site-popup-content").css({
				marginTop: mTop
			});
		},
		close: function(){
			if(pw.opened){
//				$("#sitePopup").stop(false,false).hide();
//				pw.opened=false;
				pw.stop();
				$(document).trigger("myvip-popup-closed");
				pw.options.closed();
				window.BannerHandler.showAll();
			}
		}
	});
})(jQuery)
