var PublicationsPrototype = Class.create();

PublicationsPrototype.prototype = {
	initialize: function() {
	},
	showAddForm: function(categoryId, type)
	{
		$('addPublicationForm').style.display = "block";
		$('addPublicationHref').style.display = "none";
		$('addPublicationH1').scrollTo();
		if(type == '1') {
			var oFCKeditor = new FCKeditor('publicationText') ;
			__FCKeditorNS = null;
			FCKeditorAPI  = null;
			oFCKeditor.ToolbarSet = 'Frontend';
			oFCKeditor.BasePath	 = '/cms/js/fckeditor/';
			oFCKeditor.ReplaceTextarea();
		}
	},
	initAddPublication: function(link)
	{
		this.uploadFlag = 0;
		this.isUpload = 'off';
		this.title = '';
		this.photo = '';
		this.text = '';
		this.categoryId = '';
		this.link = link;
		this.bottomsArr = new Array('publicationSaveButton');
	},
	uploadInit: function()
	{
		if (this.uploadFlag == 1) {
			this.uploadFlag = 0;
 
			var errors = new Array ('bigSize', 'notUploaded', 'wrongMime', 'partial');	  
			var errorUploading = Xms.getCookie('err');
			this.photoObj.removeClassName('x-form-invalid');
			Xms.clearErrors(null, errors);
			if (errorUploading != 'false' && this.isUpload == 'on') {
				this.photoObj.addClassName('x-form-invalid');
				Xms.lockUnLockBottons(this.bottomsArr,'unlock');
				switch (errorUploading) {
					case 'BigSize' :
						Xms.showErrors(this.photoObj, 'bigSize');
					break;
					case 'Partial' :
						Xms.showErrors(this.photoObj, 'partial');
					break;
					case 'NoFile' :
						Xms.showErrors(this.photoObj, 'notUploaded');
					break;
					case 'WrongMime' :
						Xms.showErrors(this.photoObj, 'wrongMime');
					break;
				}
				Xms.deleteCookie('err','/','');
				this.isUpload = 'off';
			} else {
			alert('asd');
				 this.sendPublication();
			}
		}
	},
	addPublication: function(categoryId, titleAdd, photoAdd, type)
	{
		err = false;
		Xms.lockUnLockBottons(this.bottomsArr,'lock');
		
		if(titleAdd == '1') {
			var titleErr = new Array('titleWrongSymbols');
			var titleObj = $('publicationTitle');
			this.title = titleObj.getValue();
			Xms.clearErrors(new Array('publicationTitle'), titleErr);
			if(this.title != '' && !Validate.freeform(this.title)) {
				err = Xms.showErrors(titleObj,'titleWrongSymbols');
			}
		}
		
		if(photoAdd == '1') {
			var photoErr = new Array('adressError', 'extError');
			this.photoObj = $('photo');
			this.photo = this.photoObj.getValue();
			Xms.clearErrors(new Array('photo'), photoErr);
			if(this.photo != '' && !Validate.imageExt(this.photo)) {
				err = Xms.showErrors(this.photoObj,'extError');
			} else if(this.photo != '' && !Validate.pathToFile(this.photo)) {
				err = Xms.showErrors(this.photoObj,'adressError');
			}
		}
		
		var textErr  = new Array('textEmpty');
		var textObj  = $('publicationText');
		if(type == '1') {
			var oFCKeditor = FCKeditorAPI.GetInstance('publicationText');
			this.text = oFCKeditor.GetHTML();					   
		} else {
			this.text = Filter.nl2br($F('publicationText'));
		}
		Xms.clearErrors(textObj, textErr);
		
		if(this.text == '') {
			err = Xms.showErrors(textObj,'textEmpty');
		}
		
		if(err) {
			Xms.lockUnLockBottons(this.bottomsArr,'unlock');
		} else {
			this.categoryId = categoryId;
			if(this.photo != '') {
			   document.forms.addPhoto.submit();
			   this.isUpload = 'on';
			   this.uploadFlag = 1;
			} else {
				this.sendPublication();
			}
		}
	},
	sendPublication: function()
	{
		var argsObj = new Object();
		argsObj["operation"]		= "savePublication";
		argsObj["categoryId"]	   = this.categoryId;
		argsObj["text"]			 = this.text;
		argsObj["itemTitle"]		= this.title;
		if(this.photo != '') {
			argsObj["photo"]		= Xms.getCookie('photo');
			Xms.deleteCookie('photo','/','');
		}
		ajaxCall('Publications/showPublication', argsObj);
	},
	redirect: function(categoryId, articleId)
	{
		window.location = this.link + 'pubcategory/' + categoryId + '/' + articleId + '/1/1/';
	},
	showComments: function(articleId)
	{
		$('commentsHref_' + articleId).style.display = "none";
		$('showComments_' + articleId).style.display = "block";
		$('addCommentHref_' + articleId).style.display = "block";
		$('slash_' + articleId).style.display = "none";
		$('showComments_' + articleId).scrollTo();
	},
	showAddCommentForm: function(articleId)
	{
		$('addCommentArea_' + articleId).style.display = "block";
		$('addCommentHref_' + articleId).style.display = "none";
		$('slash_' + articleId).style.display = "none";
		$('addCommentArea_' + articleId).scrollTo();
	},
	commentSend: function(articleId)
	{
		var err = false;
		var commentErr = new Array('commentEmpty_' + articleId, 'commentLong_' + articleId);
		
		commentObj = $('commentTextForm_' + articleId); 
		comment = commentObj.getValue(); 
		
		Xms.clearErrors(new Array('commentTextForm_' + articleId), commentErr);
		
		if(comment == '') {
			err = Xms.showErrors(commentObj,'commentEmpty_' + articleId);
		} else if(comment.length > 2000) {
			commentObj.setValue(comment.substr(0, 2000));
			err = Xms.showErrors(commentObj,'commentLong_' + articleId); 
		}
		if(!err) {
			commentObj.setValue('');
			var argsObj = new Object();
			argsObj["operation"]	  = "sendComment";
			argsObj["itemId"]		 = articleId;
			argsObj["textStripTags"]  = comment;
			ajaxCall('Publications/showPublication', argsObj);
		}
	},
	commentPager: function(itemId, page)
	{
		var argsObj = new Object();
		argsObj["operation"] = "getComments";
		argsObj["itemId"]	= itemId;
		argsObj["page"]	  = page;
		ajaxCall('Publications/showPublication', argsObj);
	}
}

var Publications = new PublicationsPrototype();
