

function changeSearchSelected(link) {
	var firstItem = $$("fieldset#topsearch .radio label.active").first();
	if (firstItem) firstItem.removeClassName('active');
	link.className = 'active it';
}

function openPopup(url,name,height,width,opt_show_scrollbars){
	var scrollbar_param=opt_show_scrollbars?",scrollbars=1":"";
	var newwindow=window.open(url,name,'height='+height+',width='+width+scrollbar_param);
	if(newwindow&&!newwindow.opener){newwindow.opener=window;}
	if(window.focus){newwindow.focus()}
	return false;
}

//
//	Trieda pre mazanie defaultnych textov v inputoch
//	 pri zanechani prazdneho inputu sa naspet vrati hodnota na podovnu
//
var InputCleaner = Class.create({
	inputId: '',
	input: null,
	oldValue: '',
	initialize: function(inputId) {
		this.inputId = inputId;
		this.input = $(this.inputId);
		if (this.input != null) {
			this.oldValue = this.input.getValue();
			this.input.observe('focus', this.onActivate.bindAsEventListener(this));
			this.input.observe('blur', this.onDeactivate.bindAsEventListener(this));
		}
	},
	onActivate: function(event) {
		if (this.input.getValue() == this.oldValue) {
			this.input.setValue('');
		}
	},
	onDeactivate: function(event) {
		if (this.input.getValue() == '') {
			this.input.setValue(this.oldValue);
		}
	}
});

//
//  Trieda pre nastavenie akcie inputu ktora umozni po klinkuti na input oznacit cely jeho obash
//  pouziva sa pri inputoch kde je embed kod videa, alebo url
//
var InputSelector = Class.create({
	inputId: '',
	intput: null,
	initialize: function(inputId) {
		this.inputId = inputId;
		this.input = $(this.inputId);
		if (this.input != null) {
			//this.input.observe('focus', this.onActivate.bindAsEventListener(this))
			this.input.observe('click', this.onActivate.bindAsEventListener(this))
		}
	},
	onActivate: function(event) {
		this.input.focus();
		this.input.select();
	}
});


var ArticleTools = Class.create({
	perexDiv: '',
	actual: '',
	initialize: function(perexDiv) {
		this.perexDiv = perexDiv;
	},
	showDefault: function() {
		if (this.actual) {
			var a = $(this.actual);
			if (a) a.style.display = 'none';
		}
		//$(this.perexDiv).style.display = 'block';
		$('pid').style.display = 'none';
	},
	show: function(elemId) {
		
		$('pid').style.display = 'block';
		
		if (this.actual) {
			$(this.actual).style.display = 'none';
		}
		var e = $(elemId);
		if (e) {
			//var p = $(this.perexDiv);
			//p.style.display = 'none';
			e.style.display = 'block';
			this.actual = elemId;
		}
	}
});


var FotoGalery = Class.create({
	imagesDiv: '',
	bigImagesDiv: '',

	initialize: function(imagesDiv, bigImagesDiv) {
		this.imagesDiv = imagesDiv;
		this.bigImagesDiv = bigImagesDiv;
		this.appendMethods();
	},

	appendMethods: function() {
		var d = $(this.imagesDiv);
		var images = d.getElementsByTagName('img');
		var i = 0;
		for (i = 0; i < images.length; i++) {
			images[i].observe('click', this.showImage.bindAsEventListener(this));
		}
	},

	showImage: function(image) {
		var d = $(this.bigImagesDiv);
		var images = d.getElementsByTagName('img');
		var i = 0;
		for (i = 0; i < images.length; i++) {
			images[i].style.display = 'none';
		}
		var imageId = str_replace('bar-image-', '', image.target.id);
		//alert(image);
		//alert(imageId);
		$('image-' + imageId).style.display = 'block';
	}
});

function str_replace(search, replace, subject) {
    var f = search, r = replace, s = subject;
    var ra = r instanceof Array, sa = s instanceof Array, f = [].concat(f), r = [].concat(r), i = (s = [].concat(s)).length;

    while (j = 0, i--) {
        if (s[i]) {
            while (s[i] = (s[i]+'').split(f[j]).join(ra ? r[j] || "" : r[0]), ++j in f){};
        }
    };

    return sa ? s : s[0];
}


//
//	Trieda pre pracu s kolektivnym vedomim na titulke
//
var CollectiveKnowledge = Class.create({
	textDiv: 'colective',
	formDiv: 'collective-form',
	initialize: function() {
		//
	},
	showForm: function() {
		$(this.textDiv).style.display = 'none';
		$(this.formDiv).style.display = 'block';
		$('collective_text').focus();
	},
	showOriginal: function() {
		$(this.textDiv).style.display = 'block';
		$(this.formDiv).style.display = 'none';
	},
	save: function() {
		new Ajax.Request('services/AddCollectiveKnowledge.xml', {
			method: 'post',
			parameters: {'message':$('collective_text').value, 'author':$('collective_author').value, 'uid':$('author_uid').value},
			onSuccess: this.parseResponse.bindAsEventListener(this),
			onFailure: function() {
				alert('Na serveri vznikla chyba. Nepodarilo sa pridať vašu myšlienku. Skúste to prosím znovu');
				this.showOriginal();
			},
			onLoading: function() {
				$('collective_text').value   = '';
				$('collective_author').value = '';
				//$('collective_save').disabled   = 'disabled';
			}
		});
	},
	parseResponse: function(response) {
		var xmlDoc = response.responseXML;
		if (!xmlDoc) {
			alert('Nepodarilo sa spracovat odpoved zo servera');
			return;
		}
		var errors = xmlDoc.getElementsByTagName('error');
		if (errors.length > 0) {
			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('message');
		var r = response[0];
		var text = r.attributes.getNamedItem("text").nodeValue;
		if (text == 'ok') {
			alert('Ďakujeme. Vaša myšlienka bola pridaná.');
		} else {
			alert('Vyskytla sa chyba. Nepodarilo sa pridať vašu myšlienku. Skúste to prosím znovu');
		}

		this.showOriginal();
	}
});

//
//	Trieda na posielanie mailu redakcii
//
var RedactionMail = Class.create({
	formDiv: 'funkcia-redakcia',
	initialize: function() {
		//
	},
	
	showOriginal: function() {
		$(this.formDiv).style.display = 'none';
	},
	
	send: function() {
		new Ajax.Request('services/SendMessageToEditors.xml', {
			method: 'post',
			parameters: {'message':$('user_text').value, 'email':$('redaction_user_mail').value},
			onSuccess: this.parseResponse.bindAsEventListener(this),
			onFailure: function() {
				alert('Na serveri vznikla chyba. Nepodarilo sa odoslať vašu pripomienku. Skúste to prosím znovu');
				this.showOriginal();
			}
		});
	},
	
	parseResponse: function(response) {
		var xmlDoc = response.responseXML;
		if (!xmlDoc) {
			alert('Nepodarilo sa spracovat odpoved zo servera');
			return;
		}
		
		var errors = xmlDoc.getElementsByTagName('error');
		if (errors.length > 0) {
			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('message');
		var r = response[0];
		var text = r.attributes.getNamedItem("text").nodeValue;
		if (text == 'ok') {
			alert('Ďakujeme. Váš mail bol odoslaný.');
		} else {
			alert(text);
		}

		this.showOriginal();
	}
	
});

var FavouriteArticle = Class.create({
	initialize: function() {
		//
	},
	
	add: function(article,user) {
		new Ajax.Request('services/SaveArticleToFavourites.xml', {
		method: 'post',
		parameters: {'article':article, 'user':user},
		onSuccess: this.parseResponse.bindAsEventListener(this),
			onFailure: function() {
				alert('Na serveri vznikla chyba. Nepodarilo sa uložiť článok medzi vaše obľúbené články. Skúste to prosím znovu');
			}
		});
	},
	
	parseResponse: function(response) {
		var xmlDoc = response.responseXML;
		if (!xmlDoc) {
			alert('Nepodarilo sa spracovať odpoveď zo servera');
			return;
		}
		
		var errors = xmlDoc.getElementsByTagName('error');
		if (errors.length > 0) {
			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('message');
		var r = response[0];
		var text = r.attributes.getNamedItem("text").nodeValue;
		if (text == 'ok') {
			alert('Článok bol uložený medzi vaše obľúbené články.');
		} else {
			alert(text);
		}
	}
});

var FriendMail = Class.create({
	initialize: function() {
		//
	},
	
	send: function() {
		
		// ak bol vybrany priatel zo zoznamu
		if ($('to_friend').value != -1)
		{
			new Ajax.Request('services/SendUrlToFriendByUid.xml', {
			method: 'post',
			parameters: {'from_name':$('from_name').value, 'user':$('to_friend').value, 'message':$('message_to_text').value, 'url':$('actual_url_input').value, 'article_uid':$('article_uid').value},
			onSuccess: this.parseResponse.bindAsEventListener(this),
				onFailure: function() {
					alert('Na serveri vznikla chyba. Nepodarilo sa odoslať link vášmu priatelovi. Skúste to prosím znovu');
				}
			});
		}
		
		// ak bol vpisany mail
		else
		{
			new Ajax.Request('services/SendUrlToFriend.xml', {
			method: 'post',
			parameters: {'from_name':$('from_name').value, 'email':$('to_email').value, 'message':$('message_to_text').value, 'url':$('actual_url_input').value, 'article_uid':$('article_uid').value},
			onSuccess: this.parseResponse.bindAsEventListener(this),
				onFailure: function() {
					alert('Na serveri vznikla chyba. Nepodarilo sa odoslať link vášmu priatelovi. Skúste to prosím znovu');
				}
			});
		}
	},
	
	parseResponse: function(response) {
		var xmlDoc = response.responseXML;
		if (!xmlDoc) {
			alert('Nepodarilo sa spracovať odpoveď zo servera');
			return;
		}
		
		var errors = xmlDoc.getElementsByTagName('error');
		if (errors.length > 0) {
			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('message');
		var r = response[0];
		var text = r.attributes.getNamedItem("text").nodeValue;
		if (text == 'ok') {
			alert('Link bol odoslaný vášmu priateľovi.');
		} else {
			alert(text);
		}
	}
});


var UserPopup = Class.create({
	userId: 0,
	anchor: null,

	initialize: function(userId) {
		this.userId = userId;
		this.anchor = $('user_' + this.userId);
		this.addActions();
	},
	addActions: function() {
		Event.observe(this.anchor, 'mouseover', this.onMouseOver.bindAsEventListener(this));
		Event.observe(this.anchor, 'mouseout', this.onMouseOut.bindAsEventListener(this));
	},
	onMouseOver: function(e) {
		var pop = $('user_pop_' + this.userId);

		//var pos = this.anchor.cumulativeOffset();
		//alert(pos['left']);
		//pop.relativize();

		//pop.style.left = pop.style.left - 300;

		//pop.style.top  = findPosY(document.getElementById(this.userId));
		//pop.style.left = findPosX(document.getElementById(this.userId));
		pop.style.visibility = 'visible';



		//pop.style.left = this.anchor.offsetLeft;
		//pop.style.top  = this.anchor.offsetTop;
	},
	onMouseOut: function(e) {
		var pop = $('user_pop_' + this.userId);
		pop.style.visibility = 'hidden';
	}
});

var UserFriend = Class.create({
	
	removedUsers : [],
	
	initialize: function() {
		//
	},
	
	add: function(loggedUser,addedUser) {
		new Ajax.Request('services/AddUserToFriends.xml', {
			method: 'post',
			parameters: {'loggedUser':loggedUser, 'addedUser':addedUser},
			onSuccess: this.parseResponse.bindAsEventListener(this),
			onFailure: function() {
				alert('Na serveri vznikla chyba. Nepodarilo sa uložiť užívateľa medzi vašich priateľov. Skúste to prosím znovu');
			}
		});
	},
	
	removeClick: function(element, userUid)
	{		
		//Effect.Shrink(element.up('a'));
		try
		{
			element.up('a').addClassName('user-removed');
		}
		catch (error) {}
		this.removeUser(userUid);
	},
	
	removeUser: function(userUid)
	{
		this.removedUsers.push(userUid);
		$('friends-remove-confirm').show();
	},
	
	confirmRemove: function()
	{
		$$('.user-removed').each( function(el) {Effect.Shrink(el)});
		
		new Ajax.Request('services/RemoveUsersFromFriends.xml', {
			method: 'post',
			parameters: {'removedUsers': this.removedUsers.toJSON()},
			onSuccess: this.parseResponse.bindAsEventListener(this),
			onFailure: function() {
				alert('Na serveri vznikla chyba. Nepodarilo sa odstrániť užívateľov z vašich priateľov. Skúste to prosím znovu');
			},
			onComplete: $('friends-remove-confirm').hide()
		});		
	},
	
	parseResponse: function(response) {
		var xmlDoc = response.responseXML;
		if (!xmlDoc) {
			alert('Nepodarilo sa spracovať odpoveď zo servera');
			return;
		}
		
		var errors = xmlDoc.getElementsByTagName('error');
		if (errors.length > 0) {
			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('message');
		var r = response[0];
		var text = r.attributes.getNamedItem("text").nodeValue;
		if (text == 'ok') {
			alert('Užívateľ bol uložený medzi vašich priateľov.');
		}
		try
		{
			var html = r.attributes.getNamedItem("html").nodeValue;
			$('friends-main-div2').replace(html);
		}
		catch(e){}
		
		
	}
});



function findPosY(obj)
{
  var curtop=0;
  if (obj.offsetParent) {
    while (obj.offsetParent) {
      curtop += obj.offsetTop
      obj=obj.offsetParent;
	 }
	}
	else if (obj.y)
    curtop += obj.y;
	return curtop;
}
function findPosX(obj)
{
  var curleft=0;
  if (obj.offsetParent) {
    while (obj.offsetParent) {
      curleft += obj.offsetLeft
		  obj=obj.offsetParent;
		}
	}
	else if (obj.x)
    curleft += obj.x;
	return curleft;
}

/*
Event.observe(window, 'load', function() {

	var userPhotos = $$('a[rel="userfoto"]');
	var i = 0;
	var wrapper = $$('body')[0];
	wrapper.innerHTML += '<div id="userPopUp"></div>';
	var actualPop = $('userPopUp');

	for (i = 0; i < userPhotos.length; i++) {

		//var actual = userPhotos[i];

		userPhotos[i].observe('mouseover', function() {

//			alert(actual);
			//var posx = findPosX(anchor);
			//alert(posx);
			//var posy = findPosY(anchor);
			//actualPop.innerHTML += '<div class="userPop; position:absolute; top:'+posy+'px; left:'+posx+'px">'+anchor.title+'</div>';


			alert('n');
			return false;
		});

		userPhotos[i].observe('mouseout', function() {
			actualPop.innerHTML += '';
			return false;
		});
	}
});
*/

