﻿var _curEditorWindowPrimary = 'jsPrimaryEditor';
var _curEditorWindowSecondary = 'jsSecondaryEditor';
var _curEditorObject = null;

function GetRadWindow()
{
   var oWindow = null;
   if (window.radWindow) oWindow = window.radWindow; //Will work in Moz in all cases, including classic dialog
   else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow;//IE (and Moz as well)
   
   if (oWindow == null) alert('window is null');
   return oWindow;
}

function CloseOnReload()
{
   GetRadWindow().Close();
}

function CloseOnReloadPrompt()
{
	if (confirm('Are you sure you want to abandon your work in this window?'))
	{
		GetRadWindow().Close();
	}
}

function RefreshParentPage()
{
   GetRadWindow().BrowserWindow.location.reload();
}				

function AdjustRadWindow()
{
	setTimeout(function(){GetRadWindow().autoSize(true)}, 200);
} 	

function openTelerikWindow(page, query, window, width, height, title)
{
	var qry = (query) ? '?' + query : '';
	var win = radopen(_siteRoot + '/' + page + qry, window);
	//win.set_height(height);
	//win.set_width(width);
	win.SetTitle(title);
	win.set_modal(true);
	win.center();
}

function openTelerikWindowXY(page, query, window, width, height, title, xVal, yVal, isModal)
{
	var qry = (query) ? '?' + query : '';
	var win = radopen(_siteRoot + '/' + page + qry, window);
	//win.set_height(height);
	//win.set_width(width);
	win.SetTitle(title);
	win.set_modal(isModal);
	win.MoveTo(xVal, yVal);
}

function CriticalNewsPopup()
{
	openTelerikWindow('Modals/News/ViewCriticalNews.aspx', '', null, 800, 650, 'Critical News and Announcements');
}

function OpenUrl(url)
{
	window.location.href = _siteRoot + '/' + url;
}

function EditProfile(type) 
{
	var win = radopen(_siteRoot + "/Modals/Portals/EditProfiles.aspx?type=" + type.toLowerCase(), null);
	if (type.toLowerCase() == "user") 
	{
		win.set_height(650);
		win.set_width(850);
		win.SetTitle('Editing User Profile');
	}
	else if (type.toLowerCase() == "faculty")
	{
		win.set_height(650);
		win.set_width(850);
		win.SetTitle('Editing Faculty Profile');
	}
	else if (type.toLowerCase() == "student")
	{
		win.SetTitle('Editing Student Profile');
		win.set_height(650);
		win.set_width(800);
	}
	
	win.set_modal(true);
	win.center();
}

/* Blocks */
function OnDeleteUserBlockCommand(sender, eventArgs)
{
	if (!confirm("Are you sure you want to delete this text block?")) eventArgs.set_cancel(true);
	else {
		// confirmed delete of text block, load page, delete, refresh window
		var idToDelete = sender.get_id().indexOf("_BlockID_");
		idToDelete = sender.get_id().substring(idToDelete + 9, sender.get_id().length)
		var win = radopen(_siteRoot + "/Modals/Portals/SubmitMediaBlock.aspx?type=user&mode=delete&tid=" + idToDelete, null);
		win.set_height(600);
		win.set_width(850);
		win.set_modal(true);
		win.center();
	}
}

function OnDeleteClassInfoBlockCommand(sender, eventArgs)
{
	if (!confirm("Are you sure you want to delete this text block?")) eventArgs.set_cancel(true);
	else {
		// confirmed delete of text block, load page, delete, refresh window
		var idToDelete = sender.get_id().indexOf("_BlockID_");
		idToDelete = sender.get_id().substring(idToDelete + 9, sender.get_id().length)
		var win = radopen(_siteRoot + "/Modals/Portals/SubmitMediaBlock.aspx?type=classinfo&mode=delete&tid=" + idToDelete, null);
		win.set_height(600);
		win.set_width(850);
		win.set_modal(true);
		win.center();
	}
}

function OnEditUserBlockCommand(sender, eventArgs) {
	var idToEdit = sender.get_id().indexOf("_BlockID_");
	idToEdit = sender.get_id().substring(idToEdit + 9, sender.get_id().length)
	var win = radopen(_siteRoot + "/Modals/Portals/SubmitMediaBlock.aspx?type=user&tid=" + idToEdit, null);
	win.set_height(600);
	win.set_width(850);
	win.set_modal(true);
	win.center();
}

function OnEditClassInfoBlockCommand(sender, eventArgs) {
	var idToEdit = sender.get_id().indexOf("_BlockID_");
	idToEdit = sender.get_id().substring(idToEdit + 9, sender.get_id().length)
	var win = radopen(_siteRoot + "/Modals/Portals/SubmitMediaBlock.aspx?type=classinfo&tid=" + idToEdit, null);
	win.set_height(600);
	win.set_width(850);
	win.set_modal(true);
	win.center();
}

// text block create button
function SubmitMediaBlock(type, classid, textid) 
{
	var win = radopen(_siteRoot + "/Modals/Portals/SubmitMediaBlock.aspx?type=" + type + "&id=" + classid + "&tid=" + textid, null);
	//win.set_height(600);
	//win.set_width(850);
	win.set_modal(true);
	win.center();
}

// text block delete button
function OnDockDeleteCommand(sender, eventArgs)
{
	if (!confirm("Are you sure you want to delete this text block?"))
	{
		eventArgs.set_cancel(true);
	}
	else
	{
		// confirmed delete of text block, load page, delete, refresh window
		var idToDelete = sender.get_id().indexOf("_RadDock");
		idToDelete = sender.get_id().substring(idToDelete + 8, sender.get_id().length)
		var win = radopen(_siteRoot + "/Modals/Portals/SubmitMediaBlock.aspx?dmb=" + idToDelete, null);
		win.set_height(600);
		win.set_width(850);
		win.set_modal(true);
		win.center();
	}
}

function OnDockEditCommand(sender, eventArgs)
{
	var idToEdit = sender.get_id().indexOf("_RadDock");
	idToEdit = sender.get_id().substring(idToEdit + 8, sender.get_id().length)
	var win = radopen(_siteRoot + "/Modals/Portals/SubmitMediaBlock.aspx?mb=" + idToEdit, null);
	win.set_height(600);
	win.set_width(850);
	win.set_modal(true);
	win.center();
}

function SubmitArticle() {
	var win = radopen(_siteRoot + "/pages/SubmitArticle.aspx", "SubmitArticle");
	win.set_height(600);
	win.set_width(850);
	win.set_modal(true);
	win.center();
}

function ShowClassNote(noteId) {
	var win = radopen(_siteRoot + "/pages/ClassNotes.aspx?noteid=" + noteId, "ClassNotes");
	win.set_height(500);
	win.set_width(450);
	win.set_modal(true);
	win.center();
}
	
/* Display the assignement window for the specified class - if the assignement id is provided then its an update */
function EditAssignment(mode, classid, facultyid, assignmentid) {
	var win = radopen(_siteRoot + "/Modals/Portals/ClassAssignments.aspx?mode=" + mode + "&cid=" + classid + "&fid=" + facultyid + "&id=" + assignmentid, null);
	win.set_height(600);
	win.set_width(850);
	if (mode == 'create') win.SetTitle('Adding an Assignment');
	else if (mode == 'edit') win.SetTitle('Editing an Assignment');
	win.set_modal(true);
	win.center();
}

function ViewAssignment(assignmentid) {
	var win = radopen(_siteRoot + "/Modals/Portals/ViewAssignment.aspx?aid=" + assignmentid, null);
	win.set_height(500);
	win.set_width(565);
	win.SetTitle('Viewing an Assignment');
	win.set_modal(true);
	win.center();
}

function EditCourse(mode, classid) {
	var win = radopen(_siteRoot + "/Modals/School/EditCourse.aspx?mode=" + mode + "&cid=" + classid, null);
	win.set_height(600);
	win.set_width(565);
	if (mode == 'create') win.SetTitle('Adding an Assignment');
	else if (mode == 'edit') win.SetTitle('Editing a Course Information');
	win.set_modal(true);
	win.center();
}

/* The assignment window is done and needs to be closed and we need to update */
function AssignmentUpdate() {
	InitiateAjaxRequest("Nothing");
}

/* Show the list of students for the specified class id */
function StudentClassList(classid) {
	var win = radopen(_siteRoot + "/Modals/Portals/ClassStudents.aspx?cid=" + classid, null);
	win.set_height(500);
	win.set_width(565);
	win.SetTitle('Class Roster');
	win.set_modal(true);
	win.center();
}

function get_editor(editorID)
{
	 var editor = $find(editorID);
    if (editor == null) 
    {
		//alert('getting from hidden va');
		editor = $find($('#'+_curEditorWindowPrimary).value);
	}
    if (editor == null) 
    {
		//alert(_curEditorObject);
		//alert('getting from object');
		editor = _curEditorObject;
    }
    return editor;
}

function pods_set_galleryselector(gallery, selector)
{
	if ($('#'+gallery) != null)
	{
		$('#'+gallery).val(selector);
	}
	openGallerySelector();
}

function pods_set_gallery(ctl, txt, id)
{
	if ($('#'+ctl) != null)
	{
		$('#'+ctl+'_litText').html(txt);
		$('#'+ctl+'_hdnID').val(id);
	}
	openGallerySelector();
}

/* Will be obsolete*/
function OpenMediaFile(media)
{
	openWindow('/pages/Media/MediaGet.aspx?id='+media, 'media', '');
}
