
DTP.Preview = {
	previewURL: '',
	adminURL: '',
	SID: ''
};

//------------------------------ Admin Side

DTP.Preview.open = function (uri) {
	var preview = window.open('', 'preview');
	if (uri) {
		// We assume & here since ? should have been established by SID
		preview.document.location = DTP.Preview.previewURL + '&uri=' + uri;
	}
	else if (!/^http:/i.test(preview.document.location)) {
		preview.document.location = DTP.Preview.previewURL;
	}
	preview.focus();
}

DTP.Preview.showTheme = function (id) {
	DTP.Preview.open('/?DTPPreviewTheme=' + id);
}


//------------------------------ Visitor Preview Side

DTP.Preview.returnToAdmin = function () {
	if (window.parent.opener.closed) {
		window.parent.opener = window.open(DTP.Preview.adminURL);
		window.parent.opener.preview = window;
	}
	else
		window.parent.opener.focus();
}

DTP.Preview.refresh = function () {
	var d = window.parent.content.document;
	d.location = d.location
}

DTP.Preview.home = function () {
	window.parent.content.document.location = '/content';
}

DTP.Preview.close = function () {
	window.parent.close()
}

DTP.Preview.update = function () {
	var d = window.parent.content.document;
	var loc = String(d.location);
	
	var preview = 0;
	var module = null;
	var theme = null;
	
	if (document.form.preview.selectedIndex > 0)
		preview = document.form.preview.options[document.form.preview.selectedIndex].value;
	
	if (document.form.module.selectedIndex > 0)
		module = document.form.module.options[document.form.module.selectedIndex].value;
	
	if (/DTPPreviewTheme=(\d+)/.test(parent.content.document.location))
		theme = RegExp.$1;
	
	var i = loc.indexOf('?');
	if (i != -1) loc = loc.substring(0, i);
	
	//if (preview || module || theme) 
	loc += "?SID=" + DTP.Preview.SID;
	if (preview) loc += "&DTPPreview=" + preview;
	if (module) loc += "&DTPPreviewModule=" + module;
	if (theme) loc += "&DTPPreviewTheme=" + theme;
	
	d.location = loc;
	window.parent.content.focus();
}

