var EntryPopup = {
	'wrapper': null,
	'overlay': null,
	'popup': null,
	
	'state': 'init',
	
	'init': function () {
		$(function(){
			EntryPopup.create();
		});
	},
	
	'create': function () {
		if ( !('createElement' in document) ) {
			return;
		}
		
		var cookie = 'cookie' in EntryPopup ? EntryPopup.cookie : 'showEntryPopup';
		if (document.cookie.indexOf(cookie) < 0) {
			var date = new Date();
			date.setTime(date.getTime() + 7200000);
			document.cookie = cookie + '=1; expires=' + date.toGMTString() + ' ; path=/';
		}
		else if ( !('useCookie' in EntryPopup) || (EntryPopupData.useCookie) ) {
			return;
		}
		
		var wrapper = document.createElement('div');
		wrapper.id = 'EntryPopupWrapper';
		
		if (EntryPopupData.showOverlay) {
			var overlay = document.createElement('div');
			overlay.id = 'EntryPopupOverlay';
			overlay.innerHTML = '&nbsp;';
			wrapper.appendChild(overlay);
			EntryPopup.overlay = overlay;
		}
		
		var popup = document.createElement('div');
		popup.id = 'EntryPopup';
		popup.style.width = EntryPopupData.width + 'px';
		popup.style.height = EntryPopupData.height + 'px';
		popup.style.marginLeft = '-' + (EntryPopupData.width / 2) + 'px';
		popup.style.marginTop = '-' + (EntryPopupData.height / 2) + 'px';
		if (EntryPopupData.backgroundImage) {
			popup.style.backgroundImage = 'url(' + EntryPopupData.backgroundImage + ')';
		}
		popup.innerHTML = EntryPopupData.html;
		wrapper.appendChild(popup);
		EntryPopup.popup = popup;
		
		document.body.appendChild(wrapper);
		EntryPopup.wrapper = wrapper;
		
		setTimeout(EntryPopup.open, EntryPopupData.timeout);
	},
	
	'open': function () {
		if (EntryPopup.wrapper != null && EntryPopup.popup != null) {
			EntryPopup.wrapper.style.visibility = 'visible';
			EntryPopup.wrapper.style.display = 'block';
			EntryPopup.state = 'opened';
			if ('scrollTo' in window) {
				window.scrollTo(0, 0);
			}
		}
	},
	
	'close': function () {
		if (EntryPopup.wrapper != null) {
			EntryPopup.wrapper.style.display = 'none';
			EntryPopup.state = 'closed';
		}
	},
	
	'closeHandler': function (e) {
		EntryPopup.close();
		return false;
	}
};
