var objHtmlBox;
var uploadID;
var blnFirstShow = true;
var isAdmin = false;
$(document).ready(function(){
	$('body').on('click', '#wbMode', function(e) {
		e.preventDefault();
		whiteboardMode();
	});
	$('body').on('click', '.admin_wb_change', function(e) {
		e.preventDefault();
		whiteboardChanges($(this).attr("rel"));
	});
});
function whiteboardMode() {
	disableLoadedInterface();
	$('#wbMode,#wbModeLinkContainer').remove();
	//$('.content').appendTo('body');
	$('
').appendTo('body').siblings().hide();
	//$('.admin_attachmentListing').hide();
	$('.whiteboard').append('\
		\
		\
			' + $('.content').html() + '\
		
\
	');
	$('body').addClass('admin_wb');
	whiteboardChanges('font_normal');
	whiteboardChanges('white_black');
}
function whiteboardChanges(strChange) {
	if (strChange != '') {
		switch (strChange) {
			case 'font_normal': $('.admin_wb').attr('class', 'admin_wb admin_wb--normal'); break;
			case 'font_med': $('.admin_wb').attr('class', 'admin_wb admin_wb--medium'); break;
			case 'font_lg': $('.admin_wb').attr('class', 'admin_wb admin_wb--large'); break;
			case 'white_black':
				$('.admin_wb_content,.admin_wb_content *').css({
					'background-color':'#fff',
					'color':'#000'
				});
				break;
			case 'yellow_blue':
				$('.admin_wb_content,.admin_wb_content *').css({
					'background-color':'#FEF57E',
					'color':'#002351'
				});
				break;
			case 'black_yellow':
				$('.admin_wb_content,.admin_wb_content *').css({
					'background-color':'#000',
					'color':'#F4EF00'
				});
				break;
			case 'blue_blue':
				$('.admin_wb_content,.admin_wb_content *').css({
					'background-color':'#81CAFF',
					'color':'#002351'
				});
				break;
			case 'exit_wb':
				window.location.reload();
				break;
		}
	}
}
function uploadResponse() {
	$.ajax({
		url: '/admin/vle/client_side/assignments/feedback.asp',
		dataType: 'html',
		type: 'POST',
		data: { u: uploadID, fb: ''+$('#content__homework__list__item--' + uploadID).closest('li').find('textarea').val() },
		success: function(data) {
			if (data === 'success') {
				alert("Your feedback has been received and saved.");
			} else {
				alert("There was a problem saving your feedback, please try again.");
			}
		}
	});
}
savePupilPage = function() {
	$("#cmsEditableMainContent").trigger('save-preFetch.e4e');
	if (CKEDITOR.instances.cmsEditableMainContent) { $("#strItemContent").html( CKEDITOR.instances.cmsEditableMainContent.getData() ); }
	else { $("#strItemContent").html( $("#cmsEditableMainContent").html() ); }
	var strPostURL = "/admin/vle/client_side/savePupilPage.asp";
	var aryPostData = "intItemID="+$('#intItemID').text() + 
		"&itemTitle=" + escape($('#strItemTitle').text()) + 
		"&itemContent=" + escape($("#strItemContent").html()) + 
		"&intReviewerID=" + $('#intReviewerID').text() + 
		"&intPageParentID=" + $('#intPageParentID').text() + 
		"&pageStyleSheet=" + escape($('#pageStyleSheet').text()) + 
		"&commentsOn=" + $('#commentsOn').text();
	$.ajax({
		url: strPostURL,
		data: aryPostData,
		type: 'POST',
		dataType: 'json',
		beforeSend: function() {
			if ($.e4eLoaderScreen.getProgress === false) {
				$.e4eLoaderScreen.show("Saving your page", undefined, undefined, true);
			}
		},
		success: function(retdata) {
			$.e4eLoaderScreen.setProgress(100);
			window.location.reload();
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			var errorMessage = 'Sorry, there was a problem saving this page.
Please close this message and try again.';
			$.e4eLoaderScreen.setProgress('error', errorMessage);
		}
	});
}
savePupilDraftPage = function() {
	var strDeleteURL = '/admin/vle/client_side/deletePupilDraftPage.asp';
	var aryDeleteData = 'intItemID=' + $('#intItemID').text();
	$.ajax({
		url: strDeleteURL,
		data: aryDeleteData,
		type: 'POST',
		dataType: 'json',
		beforeSend: function() {
			$.e4eLoaderScreen.show("Saving your page", undefined, undefined, true);
		},
		success: function (data) {
			savePupilPage();
		},
		error: function (XMLHttpRequest, textStatus, errorThrown) {
			var errorMessage = 'Sorry, there was a problem deleting the existing draft. 
Please close this message and try again.';
			$.e4eLoaderScreen.setProgress('error', errorMessage);
		}
	});
}
$(document).ready(function () {
	$('body').on('click', '#savePupilPage', function(e) {
		e.preventDefault();
		savePupilPage();
	});
	$('body').on('click', '#savePupilDraftPage', function(e) {
		e.preventDefault();
		savePupilDraftPage();
	});
	$('#editPupilDraftPage').on('click', function(e) {
		e.preventDefault();
		var strPostURL = "/admin/vle/client_side/getPupilDraftPage.asp";
		var aryPostData = "intItemID="+$('#intItemID').text();
		// TODO : Log delete draft task.
		$.ajax({
			url: strPostURL,
			data: aryPostData,
			type: 'POST',
			dataType: 'html',
			success: function (retdata) {
				$('.vle_toolbar').empty();
				$('.vle_toolbar').append(' Save your page
');
				$('.vle_toolbar').append('');
				$('.vle_desc').html( retdata ).each(function() {
					$(this).removeClass('admin_editable--disabled').addClass('admin_editable admin_editable--region');
					$(this).attr('contenteditable', 'true').ckeditor({
						extraPlugins: '',
						sharedSpaces: { top: 'vle_toolbar_ckeditor' },
						toolbar: [
							{ name: 'undo', items: [ 'Undo', 'Redo' ] },
							{ name: 'basicstyles', groups: [ 'basicstyles', 'cleanup' ], items: [ 'Bold', 'Italic', 'Underline', 'Superscript', '-', 'RemoveFormat' ] },
							{ name: 'paragraph', groups: [ 'list', 'align', 'blocks' ], items: [ 'NumberedList', 'BulletedList', '-', 'JustifyLeft', 'JustifyCenter', 'JustifyRight', '-', 'Blockquote' ] },
							{ name: 'styles', items: [ 'Format' ] },
							{ name: 'developer', items: [ 'Source' ] }
						]
					});
				});
			},
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				alert('Sorry, there was a problem getting the contents of your draft page, please close this message and try again.');
			}
		});
	});
	$('#deletePupilDraftPage').on('click', function(e) {
		e.preventDefault();
		var blnConfirm = confirm("Are you sure you wish to delete this draft?");
		if (blnConfirm) {
			var strPostURL = "/admin/vle/client_side/deletePupilDraftPage.asp";
			var aryPostData = "intItemID="+$('#intItemID').text();
			$.e4eLoaderScreen.show("Deleting your draft page", undefined, undefined, true);
			$.ajax({
				url: strPostURL,
				data: aryPostData,
				type: 'POST',
				dataType: 'json',
				success: function (data) {
					$.e4eLoaderScreen.setProgress(100);
					window.location.reload();
				},
				error: function (XMLHttpRequest, textStatus, errorThrown) {
					var errorMessage = 'Sorry, there was a problem deleting the item. 
Please close this message and try again.';
					$.e4eLoaderScreen.setProgress('error', errorMessage);
				}
			});
		}
	});
	if ( $('.vle_avatar').length > 0 ) {
		$('.vle_avatar').e4eDialog({
			diagTitle: 'My Avatar',
			follow: 'static',
			staticLink: '/admin/vle/avatar.dialog.asp',
			height: 400,
			width: 550,
			resizeable: false,
			storeAJAX: false
		});
	}
	$('.admin_cke_inline').each(function() {
		if ($(this).hasClass('admin_editable--disabled') !== true) {
			$(this).addClass('admin_editable admin_editable--region');
			$(this).attr('contenteditable', 'true').ckeditor({
				extraPlugins: '',
				sharedSpaces: { top: 'vle_toolbar_ckeditor' },
				toolbar: [
					{ name: 'undo', items: [ 'Undo', 'Redo' ] },
					{ name: 'basicstyles', groups: [ 'basicstyles', 'cleanup' ], items: [ 'Bold', 'Italic', 'Underline', 'Superscript', '-', 'RemoveFormat' ] },
					{ name: 'paragraph', groups: [ 'list', 'align', 'blocks' ], items: [ 'NumberedList', 'BulletedList', '-', 'JustifyLeft', 'JustifyCenter', 'JustifyRight', '-', 'Blockquote' ] },
					{ name: 'styles', items: [ 'Format' ] },
					{ name: 'developer', items: [ 'Source' ] }
				]
			});
		}
	});
	$('#assignmentsUploadsContainer a#btnContainer').click(function (e) {
		e.preventDefault();
		SWFUploadObject.cancelQueue();
	})
	$('.assignmentList li').each(function () {
		$(this).not(':contains("View Response")').append('');
		if($(this).index() %2 == 0) $(this).addClass('odd');
	}).find('a.delete-submission').click(function (e) {
		e.preventDefault();
		$(this).hide();
		var con = $(this).parent();
		con.append('are you sure you want to delete this?
NoYes').addClass('confirm-delete');
		$('.confirm-delete-sbumission', con).click(function (e) {
			e.preventDefault();
			var submissionLink = con.closest('li').find('a:first').attr('href');
			$.ajax({
				url:'/admin/vle/submission.remove.json.asp',
				dataType: 'json',
				data : {
					submissionID : submissionLink.substr(submissionLink.indexOf('submissionID=') + 13),
					pid : intCurrentItemID
				},
				success : function (ret) {
					if (ret.status == 'true') {
						con.closest('li').slideUp(300);
					} else {
						alert(ret.message);
					}
				}
				
			});
		});
		$('.cancel-delete-sbumission', con).click(function (e) {
			e.preventDefault();
			con.find('>*:not(.delete-submission)').fadeOut(200, function () {
				$(this).remove();
				$('.delete-submission', con).show();
				con.removeClass('confirm-delete')
			});
		});
	})
	$('.ajaxLoadResponse').on('click',function(event){
		event.preventDefault();
		$this = $(this);
		var strHref = $this.attr("href");
		if($this.closest('li').children('div.content__homework__list__response').length > 0) {
			if($this.hasClass('open')) {
				$this.closest('li').children('div.content__homework__list__response').slideUp(300);
				$this.removeClass('open');
				$this.text('View Response');
			} else {
				$this.closest('li').children('div.content__homework__list__response').slideDown(300);
				$this.addClass('open');
				$this.text('Hide Response');
			}
		} else {
			if (strHref != '') {
				uploadID = $this.attr("rel");
				$.ajax({
					url: strHref,
					dataType: 'html',
					success: function(data) {
						var response = $('' + data + '
');
						if (isAdmin) response.append('Save this comment
');
						$this.closest('li').append( response );
						$this.addClass('open');
						$this.text('Hide Response');
						response.slideDown(300);
					},
					error: function(){				
						alert('There was a problem retrieving the teachers response, please try again.');
					}
				});
			}
		}
	});
});