//
//	Javascript pre handlovanie veci suvisiacich s posielanie postov
//


Event.observe(window, 'load', function() {
	postLoadEvents();
});

function postLoadEvents()
{
	// nastavi highlajtovanie
	$$('.highlight').each(function(item) {
		item.observe('focus', function(){
			//item.style.backgroundColor = "#FDFFDE";
			item.setStyle({'backgroundColor': "#FDFFDE"});
			//item.setStyle({'background': "#FDFFDE"});
		});
		item.observe('blur', function(){
			/*item.style.backgroundColor = "#ffffff";
			item.style.background = "#ffffff";*/
			item.setStyle({'backgroundColor': "#ffffff"});
//			item.setStyle({'background': "#ffffff"});
		});
		new InputCleaner(item);
	});

	// nastavy elementom s expand classou automaticke vertiklane zvacsovanie
	var elements = $$('.expand');
	if (elements.size() > 0) {
		for (var index = 0, length = elements.size(); index < length; ++index)	{
			autoExpandContract(elements[index]);
		}
	}
}


var Post = Class.create({
	labelItem:    'label_add_post',
	formItem:    'new_post',
	newItem:    'posts_list',
	//addPostUrl: document.location.href.substring(0, document.location.href.indexOf('.sk/') + 4) + 'services/AddPost.xml',
	addPostUrl: 'services/AddPost.xml',
	formId:     'f_comment',
	
	initialize: function() {
	/*
		if (document.location.href.indexOf('localhost') != -1)
		{
			this.addPostUrl = 'http://localhost/joj/services/AddPost.xml';
		}
		*/
	},
	submit: function() {
		var pars = $(this.formId).serialize();
		pars += '&href=' + escape(document.location.href);
		//alert(pars);
		new Ajax.Request(this.addPostUrl, {method: 'post', parameters: pars, onLoading: this.onStartAdd.bindAsEventListener(this),
			onComplete: this.onFinisAdd.bindAsEventListener(this), onFailure: this.onError.bindAsEventListener(this) });
		return false;
	},

	onStartAdd: function() {
		// nothing
	},
	onFinisAdd: function(response) {
		
		var xmlDoc = response.responseXML;
		if (!xmlDoc) {
			alert('Nepodarilo sa spracovat odpoveď zo servera');
			return;
		}

		var errors = xmlDoc.getElementsByTagName('error');
		if (errors.length > 0) {
			if (errors.length == 1) {
				var error = errors[0].attributes.getNamedItem('message').nodeValue + "\n";
				var parts = error.split("\n");
				if (parts.length == 3) {
					alert(parts[0]);
					$('captcha_wrapper').innerHTML = parts[1];
					
				}
			} else {
				var result = '';
				for (var i = 0; i < errors.length; i++) {
					result += errors[i].attributes.getNamedItem('message').nodeValue + "\n";
				}
				alert(result);
				return;
			}
		}

		var response = xmlDoc.getElementsByTagName('postHtml');
		var newPost = "<li class=\"first\">" + response[0].textContent + "</li>";
		
		var liItem = $(this.newItem);
		var lis = liItem.getElementsByTagName('li');
		if (lis.length > 0)
		{
			lis[0].setAttribute("class", "");
		}
		liItem.innerHTML = newPost + liItem.innerHTML;
		//liItem.update(newPost + liItem.innerHTML);
		
		var fItem = $(this.formItem);
		fItem.style.display = 'none';
		
		var lItem = $(this.labelItem);
		lItem.show();
		
	},
	onError: function() {
		alert('error');
	}
});


//
//	funkcie pre automaticke vertiklane zvacsovanie textarei
//

function autoExpandContract(el) {

	// get height of the element
	var __heightFromElement = el.offsetHeight;

	// get height of the element set in the styles
	var __heightFromCSS = parseInt(getStyleFromCSS(el, 'height'));

	// If the height style is set in the CSS and it's bigger than 0px, resize the element to that height
	if (__heightFromCSS > 0) {
		__heightFromElement = __heightFromCSS;
	}

	// adjust the textarea, for all good browsers: overflow:hidden to lose the scrollbars,
	// for IE use overflowX:auto to let that browser decide whether to show scrollbars
	$(el).setStyle({overflow: 'hidden', overflowX: 'auto'});

	// set the width and height to the correct values
	el.style.width = getStyleFromCSS(el, 'width')+'px';
	el.style.height = getStyleFromCSS(el, 'height')+'px';

	// create a new element that will be used to track the dimensions
	var dummy_id = Math.floor(Math.random()*99999) + '_dummy';
	var div = document.createElement('div');

	// we use setAttribute() here instead of writeAttribute which is a prototype method due to IE6
	div.setAttribute('id',dummy_id);
	document.body.appendChild(div);
	var dummy = $(dummy_id);

	// match the new elements style to the el
	dummy.style.fontFamily = getStyleFromCSS(el, 'font-family');
	dummy.style.fontWeight = getStyleFromCSS(el, 'font-weight');
	dummy.style.fontSize = getStyleFromCSS(el, 'font-size')+'px';

	// fornicate with IE (not a check for IE7 though, just IE6)
	if (navigator.userAgent.indexOf('MSIE') !=-1) {
		dummy.style.width = getStyleFromCSS(el, 'width');

	// Play  nice with the good browsers
	} else {
		dummy.style.width = getStyleFromCSS(el, 'width')+'px';
	}
	dummy.style.padding = getStyleFromCSS(el, 'padding');
	dummy.style.margin = getStyleFromCSS(el, 'margin');
	dummy.style.overflowX = 'auto';
	// hide the created div away
	dummy.style.position = 'absolute';
	dummy.style.top = '0px';
	dummy.style.left = '-9999px';
	dummy.innerHTML = '&nbsp;42';

	var __lineHeight = dummy.offsetHeight;

	var checkExpandContract = function(){
		// place text inside the element in a new var called html
		var html = el.value;
		html = html.replace(/\n/g, '<br />');
		if (dummy.innerHTML != html) {
			dummy.innerHTML = html;
			var __dummyHeight = dummy.offsetHeight;
			var __elHeight = el.offsetHeight;

			// if the height from our element is not the same as the height from our dummy we just made...
			// example: 150px != 250px
			if (__elHeight != __dummyHeight) {

				// example: 250px > 200px
				if (__dummyHeight > __heightFromElement) {

					// example: height = (250+0)px
					el.style.height = (__dummyHeight+__lineHeight) + 'px';
				} else {
					el.style.height = __heightFromElement+'px';
				}
			}
		}
	}

	var expandElement = function()	{
		interval = window.setInterval(function() {checkExpandContract()}, 250);
	}

	var contractElement = function() {
		clearInterval(interval);
	}

	// Put eventListeners to our elements
	$(el).observe('focus', expandElement);
	$(el).observe('blur', contractElement);
	checkExpandContract();
} // end autoExpandContract()

function getStyleFromCSS(el, style) {

	// get styles from our CSS
	var value = $(el).getStyle(style);

	// if styles are not defined in our CSS
	if(!value) {

		// for other browsers. Actually this equals 'window'. Use that if Opera fails on you.
		if(document.defaultView) {
			//	getComputedStyle() requires two parameters.
			//		The first is a reference to the element.
			//		The second is either the name of a pseudo element ('before', 'after', 'first-line'),
			//		or null just for the element itself
			// getPropertyValue()
			//		returns the value of the property if it has been set.
			// 		Returns an empty string if the property has not been set.
			value = document.defaultView.getComputedStyle(el, null).getPropertyValue(style);

	   // for IE
		} else if(el.currentStyle) {
			//	As well as being an actual usable value,
			//	the returned value for several styles may be their
			// 	default values, such as 'auto', 'normal', 'inherit',...
			value = el.currentStyle[style];
			if (value.substring(value.length-2,value.length) == "px") {
				alert('style' + style);
				value = value+'px';
			}
		}
	}

	// if the value we got from our element has more than 0 characters...
	if(value.length > 0){

		// if the value returned has the word px in it, we check for the letter x
		if (value.charAt(value.length-1) == "x") {

			// substring()
			//	get all characters from the 0th place to the total String length - 2
			// parseInt()
			//	Only the first number in the string is returned!
			//  Leading and trailing spaces are allowed.
			//  If the first character cannot be converted to a number, parseInt() returns NaN.
			value = parseInt(value.substring(0,value.length-2))
		}
	}
	return value;

} // end getStyleFromCSS()
