//events.addHandler(document, 'ready', initBlog);

// initialize blog
function initBlog () {

  var window_addpicture = dom.byId ('window_addpicture');
	if (window_addpicture)
		widget.window.init (window_addpicture);
		
	var content = dom.byId('content');
	if (content) {

		// add comment blog
		var addcomments = dom.byClassName (content, 'addcomment', 'form');
		for (var i = 0; i < addcomments.length; i++) {
      events.addHandler (addcomments[i], "submit", function(e) {
				var params = ajax.getParams (e.target);
				widget.status.show ('<h1 class="loading">loading...</h1>');
				ajax.post (e.target.action, params, function(req){
					if (req.responseText) {
						if (req.responseText.indexOf ('poprawnie dodany') != -1) {
	      			widget.dialog.show ('Operation succeed', req.responseText, true);
						} else {
	      			widget.dialog.show ('Error occured', req.responseText);
						}
						widget.status.hide ();
					}
				});
				events.prevent(e);
			});
		}

		// add picture blog
		/*var addpicture = dom.byId ('window_addpicture');
		if (addpicture) {
			events.addHandler (addpicture.submit, "click", function(e) {
				widget.status.show ('<h1 class="loading">uploading...</h1>');
				w.timeout_iframe = w.setTimeout (function() {
					var iframe = dom.byId ('iframe').contentWindow.document;
				  var iframe_body = iframe.getElementsByTagName('body')[0].innerHTML;
				  if (iframe_body) {
				    w.clearTimeout (window.timeout_iframe);
						dom.byId (dom.byId ('mem').value).comment.value += iframe_body;
						widget.hide (dom.byId ('window_addpicture'));
						widget.status.hide ();
					}
	      }, 500);
			});
		}*/
		
	}
}

// function set html tags to textarea
function set_html(el, taginside, param, closed) {
	var tag = '<' + el.innerHTML;
	if (param) {
		arraypar = param.split(',');
		for (var i=0; i<arraypar.length; i++ ) {
      tag += ' ' + arraypar[i] + '=""';
		}
	}
	if (!closed) {
		tag += '>';
		if (taginside) tag += '<' + taginside +'>test</' + taginside +'>';
		tag += 'text</' + el.innerHTML + '>';
	} else {
		tag += '/>';
	}
	dom.byId (el.parentNode.parentNode.parentNode.id).comment.value += tag;
  return false;
}

// function generate view of textarea
function view(form,view,textarea) {
	var par1 = dom.byId (view);
	var par2 = dom.byId (textarea);

	if (par1.style.display != 'block') {
		var text = dom.byId (form).comment.value;
		// replacement string
    text = text.replace(/<blockquote>/g,"</p><blockquote>");
    text = text.replace(/<\/blockquote>/g,"</blockquote><p>");
    text = text.replace(/<cite>/g,"</p><cite>");
    text = text.replace(/<\/cite>/g,"</cite><p>");
    text = text.replace(/<ul>/g,"</p><ul>");
		text = text.replace(/<\/ul>/g,"</ul><p>");
		text = text.replace(/\n\n/g,"</p><p>");
		text = "<p>" + text + "</p>";

		par1.innerHTML = text;
		par1.style.border = '1px solid #E99B5B';
		par1.style.display = 'block';
		par2.style.display = 'none';
	} else {
		par2.style.display = 'block';
		par1.style.display = 'none';
	}
}

// view window where add pictures
function view_add_pic (form) {
	var win_picture = dom.byId ('window_addpicture');
  widget.show (win_picture);
	widget.center (win_picture);
	dom.byId('mem').value = form;
}