

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;
		}
	}
});

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 otazkami pre hosti
//

var GuestQuestion = Class.create({
	buttonDiv: 'new-question-button',
	formDiv: 'new-question',
	initialize: function() {
		//
	},
	showForm: function() {
		$(this.buttonDiv).style.display = 'none';
		$(this.formDiv).style.display = '';
		$('guest-question-text').focus();
	},
	showOriginal: function() {
		$(this.buttonDiv).style.display = '';
		$(this.formDiv).style.display = 'none';
		$('guest-question-text').value   = '';
		if ($('guest-question-user-uid').value == '')
		{
			$('guest-question-user').value = '';
			$('guest-question-email').value = '';
		}
	},
	add: function() {
		new Ajax.Request('services/AddGuestQuestion.xml', {
			method: 'post',
			parameters: {'question':$('guest-question-text').value, 'user_uid':$('guest-question-user-uid').value, 'user_name':$('guest-question-user').value, 'user_email':$('guest-question-email').value, 'question-storage':$('guest-question-storage').value},
			onSuccess: this.parseResponse.bindAsEventListener(this),
			onFailure: function() {
				alert('Na serveri vznikla chyba. Nepodarilo sa pridať vašu otázku. Skúste to prosím znovu.');
				this.showOriginal();
			}
		});
	},
	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('Ďakujeme. Vaša otázka bola pridaná.');
		} else {
			alert(text);
		}

		this.showOriginal();
	}
})

var UserAnkete = Class.create({
	initialize: function() {
		//
	},
	addQuestion: function() {
		new Ajax.Request('services/AddUserAnkete.xml', {
			method: 'post',
			parameters: {'name':$('name').value, 'email':$('email').value, 'question':$('question').value, 'answer1':$('answer1').value, 'answer2':$('answer2').value, 'answer3':$('answer3').value, 'answer4':$('answer4').value, 'answer5':$('answer5').value, 'answer6':$('answer6').value},
			onSuccess: this.parseResponse.bindAsEventListener(this),
			onFailure: function() {
				$('error').innerHTML = "<strong>Na serveri vznikla chyba. Nepodarilo sa pridať vašu anketu. Skúste to prosím znovu.</strong>";
			}
		});
	},
	parseResponse: function(response) {
		var xmlDoc = response.responseXML;
		if (!xmlDoc) {
			$('error').innerHTML = "<strong>Nepodarilo sa spracovať odpoveď zo servera.</strong>";
			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 + "<br />";
			}
			$('error').innerHTML = "<strong>" + result + "</strong>";
			return;
		}

		var response = xmlDoc.getElementsByTagName('message');
		var r = response[0];
		var text = r.attributes.getNamedItem("text").nodeValue;
		if (text == 'ok') {
			$('error').innerHTML = "<strong>Ďakujeme. Vaša anketa bola pridaná.</strong>";
			$('name').value   = '';
			$('email').value   = '';
			$('question').value   = '';
			$('answer1').value = '';
			$('answer2').value = '';
			$('answer3').value = '';
			$('answer4').value = '';
			$('answer5').value = '';
			$('answer6').value = '';
		} else {
			$('error').innerHTML = "<strong>" + text + "</strong>";
			
		}
	}
})

var SMSCode = Class.create({
	initialize: function(options) {
		Object.extend(this, options || {});
	},
	send: function() {
		new Ajax.Request('services/SendSMSCode.xml', {
			method: 'post',
			parameters: {'code':$('smscode').value, 'uid':$('articleUid').value},
			onSuccess: this.parseResponse.bindAsEventListener(this),
			onFailure: function() {
				alert('Na serveri vznikla chyba.');
				this.showOriginal();
			}
		});
	},
	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('Váš SMS kód bol prijatý.');
			this.showOkMessage();
			var bodytext = r.attributes.getNamedItem("bodytext").nodeValue;
			if ($('bodytext')) {
				$('bodytext').innerHTML = bodytext;
			}
		} else {
			alert(text);
		}
	},
	showOkMessage: function() {
		
	}
})

var Voting = Class.create({
	initialize: function(options) {
		Object.extend(this, options || {});
	},
	vote: function(result_id, votes) {
		new Ajax.Request('services/Vote.xml', {
			method: 'post',
			parameters: {'result_id':result_id, 'votes':votes},
			onSuccess: this.parseResponse.bindAsEventListener(this),
			onFailure: function() {
				$('message').innerHTML = 'Na serveri vznikla chyba.';
				this.showOriginal();
			}
		});
	},
	parseResponse: function(response) {
		var xmlDoc = response.responseXML;
		if (!xmlDoc) {
			$('message').innerHTML = '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 + "<br />";
			}
			$('message').innerHTML = result;
			return;
		}

		var response = xmlDoc.getElementsByTagName('message');
		var r = response[0];
		
		var text = r.attributes.getNamedItem("text").nodeValue;
		
		if (text == 'ok') {
			var average = r.attributes.getNamedItem("average").nodeValue;
			var total = r.attributes.getNamedItem("total").nodeValue;
			var bar = r.attributes.getNamedItem("bar").nodeValue;
		
			$('message').innerHTML = "<strong>Ďakujeme za váš hlas</strong>";
			$('average').innerHTML = average;
			$('total').innerHTML = total;
			$('bar').innerHTML = '<span style="width:' + bar + '%;">';
			
		} else {
			$('message').innerHTML = "<strong>" + text + "</strong>";
		}
	},
	showOkMessage: function() {
		
	}
})


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;
}

Array.prototype.has = function(needle)
{
	for (var i=0;i<this.length;i++)
	{
		if(this[i] == needle)
		{
			return true;
		}
	}
	return false;
}

function changeCountyCinema()
{
	var county;
	var i;
	county = $('county').value;
	
	var city = $('city');
	var cinema = $('cinema');
	
	var cities = city.childNodes;
	var cinemas = cinema.childNodes;
	
	city.value = 0;
	cinema.value = 0;
	
	if (county == 0)
	{
		for (i=0; i < cities.length; i++)
		{
			if (cities[i] == '[object HTMLOptionElement]')
			{
				cities[i].style.display  = '';
			}
		}
		for (i=0; i < cinemas.length; i++)
		{
			if (cinemas[i] == '[object HTMLOptionElement]')
			{
				cinemas[i].style.display  = '';
			}
		}
	}
	
	else
	{
		var citiesId = new Array();
		for (i=0; i < cities.length; i++)
		{
			if (cities[i] == '[object HTMLOptionElement]')
			{
				if (cities[i].value != 0 && cities[i].getAttribute('class') != county)
				{
					cities[i].style.display  = 'none';
					citiesId[citiesId.length] = cities[i].value;
				}
				else
				{
					cities[i].style.display  = '';
				}
			}
		}
		
		for(i=0; i < cinemas.length; i++)
		{
			if (cinemas[i] == '[object HTMLOptionElement]')
			{
				if (cinemas[i].value != 0 && citiesId.has(cinemas[i].getAttribute('class')))
				{
					cinemas[i].style.display  = 'none';
				}
				else
				{
					cinemas[i].style.display  = '';
				}
			}
		}
	}
}

function changeCityCinema()
{
	var city = $('city').value;
	var cinema = $('cinema');

	cinema.value = 0;
	
	var cinemas = cinema.childNodes;

	if (city == 0)
	{
		for (i=0; i < cinemas.length; i++)
		{
			if (cinemas[i] == '[object HTMLOptionElement]')
			{
				cinemas[i].style.display  = '';
			}
		}
	}
	else
	{
		for(i=0; i < cinemas.length; i++)
		{
			if (cinemas[i] == '[object HTMLOptionElement]')
			{
				if (cinemas[i].value != 0 && cinemas[i].getAttribute('class') != city)
				{
					cinemas[i].style.display  = 'none';
				}
				else
				{
					cinemas[i].style.display  = '';
				}
			}
		}
	}
}

function changeCountyTheatre()
{
	var county;
	var i;
	county = $('county').value;
	
	var city = $('city');
	var theatre = $('theatre');
	
	var cities = city.childNodes;
	var theatres = theatre.childNodes;
	
	city.value = 0;
	theatre.value = 0;
	
	if (county == 0)
	{
		for (i=0; i < cities.length; i++)
		{
			if (cities[i] == '[object HTMLOptionElement]')
			{
				cities[i].style.display  = '';
			}
		}
		for (i=0; i < theatres.length; i++)
		{
			if (theatres[i] == '[object HTMLOptionElement]')
			{
				theatres[i].style.display  = '';
			}
		}
	}
	
	else
	{
		var citiesId = new Array();
		for (i=0; i < cities.length; i++)
		{
			if (cities[i] == '[object HTMLOptionElement]')
			{
				if (cities[i].value != 0 && cities[i].getAttribute('class') != county)
				{
					cities[i].style.display  = 'none';
					citiesId[citiesId.length] = cities[i].value;
				}
				else
				{
					cities[i].style.display  = '';
				}
			}
		}
		
		for(i=0; i < theatres.length; i++)
		{
			if (theatres[i] == '[object HTMLOptionElement]')
			{
				if (theatres[i].value != 0 && citiesId.has(theatres[i].getAttribute('class')))
				{
					theatres[i].style.display  = 'none';
				}
				else
				{
					theatres[i].style.display  = '';
				}
			}
		}
	}
}

function changeCityTheatre()
{
	var city = $('city').value;
	var theatre = $('theatre');

	theatre.value = 0;
	
	var theatres = theatre.childNodes;

	if (city == 0)
	{
		for (i=0; i < theatres.length; i++)
		{
			if (theatres[i] == '[object HTMLOptionElement]')
			{
				theatres[i].style.display  = '';
			}
		}
	}
	else
	{
		for(i=0; i < theatres.length; i++)
		{
			if (theatres[i] == '[object HTMLOptionElement]')
			{
				if (theatres[i].value != 0 && theatres[i].getAttribute('class') != city)
				{
					theatres[i].style.display  = 'none';
				}
				else
				{
					theatres[i].style.display  = '';
				}
			}
		}
	}
}

function changeCountyMusic()
{
	var county;
	var i;
	county = $('county').value;
	
	var city = $('city');
	var place = $('place');
	
	var cities = city.childNodes;
	var places = place.childNodes;
	
	city.value = 0;
	place.value = 0;
	
	if (county == 0)
	{
		for (i=0; i < cities.length; i++)
		{
			if (cities[i] == '[object HTMLOptionElement]')
			{
				cities[i].style.display  = '';
			}
		}
		for (i=0; i < places.length; i++)
		{
			if (places[i] == '[object HTMLOptionElement]')
			{
				places[i].style.display  = '';
			}
		}
	}
	
	else
	{
		var citiesId = new Array();
		for (i=0; i < cities.length; i++)
		{
			if (cities[i] == '[object HTMLOptionElement]')
			{
				if (cities[i].value != 0 && cities[i].getAttribute('class') != county)
				{
					cities[i].style.display  = 'none';
					citiesId[citiesId.length] = cities[i].value;
				}
				else
				{
					cities[i].style.display  = '';
				}
			}
		}
		
		for(i=0; i < places.length; i++)
		{
			if (places[i] == '[object HTMLOptionElement]')
			{
				if (places[i].value != 0 && citiesId.has(places[i].getAttribute('class')))
				{
					places[i].style.display  = 'none';
				}
				else
				{
					places[i].style.display  = '';
				}
			}
		}
	}
}

function changeCityMusic()
{
	var city = $('city').value;
	var place = $('place');

	place.value = 0;
	
	var places = place.childNodes;

	if (city == 0)
	{
		for (i=0; i < places.length; i++)
		{
			if (places[i] == '[object HTMLOptionElement]')
			{
				places[i].style.display  = '';
			}
		}
	}
	else
	{
		for(i=0; i < places.length; i++)
		{
			if (places[i] == '[object HTMLOptionElement]')
			{
				if (places[i].value != 0 && places[i].getAttribute('class') != city)
				{
					places[i].style.display  = 'none';
				}
				else
				{
					places[i].style.display  = '';
				}
			}
		}
	}
}

function changeTab(tab)
{
	var a = $('klasicke-predplatne');
	var b = $('on-line-vydanie');
	var header = $('predplatne-header').getElementsByTagName('a');
	
	if (tab == 1)
	{
		a.style.display = '';
		b.style.display = 'none';
		header[0].setAttribute("class","sel");
		header[1].setAttribute("class","");
	}
	else
	{
		a.style.display = 'none';
		b.style.display = '';
		header[0].setAttribute("class","");
		header[1].setAttribute("class","sel");
	}
}