$('.news .tags .switch').live('click',function(e){
	$(this).hide().siblings().show().children(':gt(3)').hide();
	e.preventDefault()
});

function focus_search_field(obj, focus)
{
	if (focus && obj.value == obj.getAttribute('default'))
		obj.value = '';
	else if (!focus && obj.value == '')
		obj.value = obj.getAttribute('default')
}

function promijeni_izvor(id)
{
	window.location = id > 0 ? '/vijesti/izvor-' + id + '/' : '/'
}

function disable_fields(slct, flds)
{
	for(var i = 0; i < flds.length; ++i)
		$('#'+flds[i]).attr('disabled', slct.value != '0' ? true : false)
}

function disable_fields_unconditional(flds)
{
	for(var i = 0; i < flds.length; ++i)
		$('#'+flds[i]).attr('disabled', true)
}

function enable_fields_unconditional(flds)
{
	for(var i = 0; i < flds.length; ++i)
			$('#'+flds[i]).attr('disabled', false)
}

// AJAX
var ajax_population_field = null;
var ajax_indicator = null;

function post_ajax(url, indicator_field, form, population_field)
{
	if(indicator_field != null && indicator_field != undefined && indicator_field != '')
		indicator_field.style.display = 'block';

	ajax_population_field = population_field;
	ajax_indicator = indicator_field;

	url += '&ajax=1';
	if (form != undefined && form != null)
		var post_body = get_form_values(form);
	else
		var post_body = '';

	new ajax(url, { postBody:post_body, onComplete: process_ajax})
}

function process_ajax(req)
{
	if(ajax_population_field != null)
		ajax_population_field.innerHTML = req.responseText;
	else alert('Wrong population field given, AJAX cannot execute correctly!');
	
	if(ajax_indicator != null && ajax_indicator != undefined && ajax_indicator != '')
		ajax_indicator.style.display = 'none';
}

function get_form_values(form)
{
	var types = ['input', 'select'];
	var fval = [];
	
	for(var i = 0; i < types.length; i++)
	{
		var els = form.getElementsByTagName(types[i]);
		for(var j = 0; j < els.length; j++)
		{
			if(types[i] == 'select')
			{
				fval[fval.length] = els[j].name+'='+els[j].options[els[j].selectedIndex].value;
			}
			else if(types[i] == 'input')
			{
				if((els[j].type == 'checkbox' || els[j].type == 'radio'))
				{
					if(els[j].checked == true)
						fval[fval.length] = els[j].name+'='+els[j].value;
				}
				
				else if(els[j].value != '' && els[j].type != 'submit')
					fval[fval.length] = els[j].name+'='+els[j].value;
			}	
		}
	}
	
	fval = fval.join('&');
	
	return fval
}

function newsletter_additional(data)
{
	document.getElementById('additional').style.display = data == true ? 'block' : 'none'
}

function newsletter_validation(email_poruka)
{
	var reg = /([a-z,0-9,_]{1,64})@([a-z,0-9,_]{1,64})\.([a-z]{1,6})/;
	var counter = 0;
	
	if (reg.exec(document.getElementById('newsletter_email').value) == null)
	{
		alert(email_poruka);
		counter++
	}
	
	return (counter > 0 ? false : true)
}