// Picture Dialog
function res_RunPictureDialog(wsroot, BasePath) {

	var path= "UI/Resources/PictureDialog/PictureDialog1.aspx";
	var picture= res_InitPicture();
	var res= window.showModalDialog(path + "?BasePath=" + BasePath, picture, 'dialogHeight:600px;dialogWidth:430px;scroll:no;resizable:no;status:no;help:no;');
	if (res) return picture;
	return null;
}

function res_RunUserPictureDialog(wsroot) {
	return res_RunPictureDialog(wsroot, wsroot + "Content/UserImages");
}

function res_InitPicture() {	
	var picture= new Object();
	picture.Title= "";
	picture.Src= "";
	picture.Width= "100";
	picture.Height= "40";
	return picture;	
}

function res_GetPictureTag(wsroot, BasePath) {
	var picture= res_RunPictureDialog(wsroot, BasePath);
	if (picture != null) return "<img src='"+picture.Src+"' width='"+picture.Width+"' height='"+picture.Height+"'>";
	return null;
}

function res_GetPictureSrc(wsroot, BasePath) {
	var picture = res_RunPictureDialog(wsroot, BasePath);
	if (picture != null) return picture.Src;
	return null;
}

// Font Dialog
function res_RunFontDialog(wsroot, font) {
	var path= wsroot + "UI/Resources/SelectFont/SelectFont.aspx";
	if (font == null) font = res_InitFont();
	if (window.showModalDialog( path, font, "dialogWidth:380px;dialogHeight:350px;help:no;status:no;resizable:no")) {
		var fontFamily = "font-family: " + font.fontName;
		var fontStyle = "font-style: " + ((font.fontStyle=="i" || font.fontStyle=="bi") ? "italic" : "normal");
		var fontWeight = "font-weight: " + ((font.fontStyle=="b" || font.fontStyle=="bi") ? "bold" : "normal"); 
		var fontSize = "font-size: " + font.fontSize + font.fontUnit;
		var fontDecoration= "text-decoration: " + (font.isUnderline ? "underline " : "") + (font.isOverline ? "overline " : "") + (font.isStrikeout ? "line-through" : "");
		var fontColor= "color: " +((font.foreColor=="" || font.foreColor==null) ? "#000000" : font.foreColor);
		var fontBackColor= "background-color : "+ ((font.backColor=="" || font.backColor==null) ? "transparent" : font.backColor);
		return fontFamily+";"+fontStyle+";"+fontWeight+";"+fontSize+";"+fontDecoration+";"+fontColor+";"+fontBackColor;
	}
	else return null;
}

function res_InitFont() {
	var font = new Function();
	font.fontName		= "Tahoma";
	font.fontStyle		= "r";
	font.fontSize		= "8";
	font.fontUnit		= "pt";
	font.isOverline		= false;
	font.isUnderline	= false;
	font.isStrikeout	= false;
	font.foreColor		= "#000000";
	font.backColor		= "transparent";
	return font;
}
// Color Dialog
function res_RunColorDialog(wsroot, InitialColor) {
	var path= wsroot + "UI/Resources/SelectColor/SelectColor.aspx";
	var args = new Function();
	args.color = InitialColor;
	var colorValue = window.showModalDialog(path, args, "dialogHeight:400px;dialogWidth:600px;status:no;help:no;scroll:no;");
	if  (colorValue!=undefined) return colorValue; else return null;
}

// Palette Class Chooser
function res_RunPaletteClassChooser(wsroot, Initial) {
	var path= wsroot + "UI/Resources/PaletteClassChooser/default.aspx";
	var args= new Object();
	args.InputClass=Initial;
	window.showModalDialog(path,args,	"dialogWidth:500px;dialogHeight:350px;scroll:no;resizable:no;status:no;help:no;");
	if (args.OutputClass!=undefined) return args.OutputClass; else return null;
}

// Html Editor
function res_RunHtmlEditor(wsroot, baseurl, htmlstr, url, ImagePath) {

	var path= "UI/Resources/HtmlEditor/HtmlEditor.aspx";
	var width= 800;
	var height= 500;
	return res_InnerRunHtmlEditor(wsroot, path, baseurl, htmlstr, url, ImagePath, width, height);
}

function res_RunUserHtmlEditor(wsroot, baseurl, htmlstr, url) {
	return res_RunHtmlEditor(wsroot, baseurl, htmlstr, url, wsroot + "Content/UserImages");
}

function res_InnerRunHtmlEditor(wsroot, path, baseurl, htmlstr, url, ImagePath, width, height) {
	if (url == null) url= ""; // TEMP
	var args= new Object();
	args.url= url;
	args.htmlstr= htmlstr;
	args.baseurl= baseurl;
	args.WorkspaceRoot= wsroot;
	args.ImagePath= ImagePath;
	if(window.navigator.appName == "Microsoft Internet Explorer"){

	if (window.showModalDialog(path, args, "dialogHeight:" + height + "px;dialogWidth:" + width + "px;scroll:no;resizable:yes;status:no;help:no;")) {
		return args.htmlstr;
	}
        }
	else{

window.open(path, args, "height=" + height + ",width=" + width + ",status=yes,toolbar=no,menubar=no,location=no");
	}
	return null;
}

// Css Editor
function res_RunCssEditor(wsroot, ImagePath, cssStr) {
	var path= wsroot + "UI/Resources/CssEditor/startTree.aspx";
	var width= 800;
	var height= 550;
	return res_InnerRunCssEditor(path, ImagePath, cssStr, width, height);

}

function res_RunUserCssEditor(wsroot, cssStr) {
	return res_RunCssEditor(wsroot, wsroot + "/Content/UserImages", cssStr);
}

function res_InnerRunCssEditor(path, ImagePath, cssStr, width, height) {
	var args= new Object();
	args.ImagePath= ImagePath;
	cssStr= res_String2CssEditor(cssStr);
	var cssText= showModalDialog(path + "?cssText=" + cssStr, args, "dialogHeight:" + height + "px;dialogWidth:" + width + "px;status:no;help:no;scroll:no;");
	if (cssText == undefined) return null;
	return cssText;
}

function res_String2CssEditor(str) {
	return str.replace(/#/gi, "%23");
}

// Show Modal Dialog
function res_ShowModalDialog(url, query, params, width, height) {

	return window.showModalDialog(url + "?" + query, params, "status:no;help:no;scroll:no;dialogHeight:" + height + "px;dialogWidth:" + width + "px");
}

// analog of format function
function Format() {
	var len= arguments.length;
	if (len < 1) return null;
	var format= arguments[0];
	var re= new RegExp("(\{(.+?)\})", "ig"); // ? - non-gready matching
	var arr;
	var result= "";
	var index, si, ei;
	ei= 0;
	while ((arr= re.exec(format)) != null) {
		si= arr.index;
		result+= format.substring(ei, si);
		ei= arr.lastIndex;
		index= parseInt(RegExp.$2);
		if (index + 1 < len) result+= arguments[index + 1];
	}
	result+= format.substr(ei);
	return result;
}

// Modal WebPart Runner
function res_RunModalWebPart(wsroot, wpName, wpPath, wpClass, width, height, aux_query) {
	if (wsroot == null) wsroot= "";
	if (wpClass == null) wpClass= "";
	var url= wsroot + "ModalPageRunner.aspx";
	var Title= "Web Snippet";
	var Mode= 1; // modal webpart
	var query= Format("wpMode={0}&wpName={1}&wpPath={2}&wpClass={3}&Title={4}&{5}", Mode, wpName, wpPath, wpClass, Title, aux_query);

	return res_ShowModalDialog(url, query, null, width, height);
}

function res_RunModalControl(wsroot, CtlPath, width, height, aux_query, Title) {
	var url= wsroot + "ModalPageRunner.aspx";
	if (Title == null) Title= "Modal Control";
	var Mode= 0; // modal control
	var query= Format("wpMode={0}&CtlPath={1}&Title={2}&{3}", Mode, CtlPath, Title, aux_query);
	return res_ShowModalDialog(url, query, null, width, height);
}

// Calendar 
function res_CalendarArguments() {
	var dtDate;
	var strType;
}	

function res_OpenCalendar(wsroot, date2Open) {
	var path= wsroot + "UI/Resources/Calendar/LoadCalendar.aspx";
	var objArgs= window.showModalDialog(path + "?dType=eventdate&dOpen=" + date2Open, null, "font-family:Verdana; font-size:12; dialogHeight:275px;dialogWidth:230px;status:no;help:no;scroll:no;");
	if(objArgs == null) return null;
	return objArgs.dtDate;
}

