// SIMPLE FUNCTIONS_____________________________________________________________

// confirm
function conf(msg)
{
	return confirm(msg ? msg : 'Megerősíted a műveletet?');
}

var page_backup = '';
var title_backup = '';
function pagemax()
{
	if (page_backup == '')
	{
		page_backup = $('body').html();
		$('body').html('<div id="box_center_maximized">' + $('#box_center').html() + '</div>');
		title_backup = $('#btn_pagemax').attr('title');
		$('#btn_pagemax').attr('title', 'Vissza a teljes oldalhoz');
	}
	else
	{
		$('body').html(page_backup);
		page_backup = '';
		$('#btn_pagemax').attr('title', title_backup);
	}
}



// PREVIEW______________________________________________________________________

// preview in new window
function preview(url)
{
	window.open(url, '', 'dependent=yes,resizable=yes,directories=no,location=no,menubar=no,status=no,titlebar=no,toolbar=no,scrollbars=yes,width=800,height=600,' + center(800, 600));
	return false;
}

// used by preview()
function center(width, height)
{
	if (window.screen)
	{
		xc = (screen.availWidth - 10 - width) / 2;
		yc = (screen.availHeight - 30 - height) / 2;
		return 'left=' + xc + ',screenX=' + xc + ',top=' + yc + ',screenY=' + yc;
	}
	return '';
}



// IMAGE GALLERY________________________________________________________________

// image click handler
function img_click(img, img_path_resized, img_path, leiras, size)
{
	$('#ii').html((img_path != '' ? '<a href=\'' + img_path + '\' title=\'Kattints a képre, hogy eredeti méretben láthasd!\'>' : '') + '<img src=\'' + img_path_resized + '\' alt=\'' + leiras + '\'>' + (img_path != '' ? '</a>' : ''));
	$('#tt').html(leiras);
	$('#ss').html('(' + size + ')');
	return false;
}



// DATE CONTROL_________________________________________________________________

var month_text = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'];

// leading zeros
function lzero(i)
{
	var t = '' + i;
	while (t.length < 2)
	{
		t = '0' + t;
	}
	return t;
}

// return the number of days for the specified year/month
function days(year, month)
{
	if (month == 0 || month == 2 || month == 4 || month == 6 || month == 7 || month == 9 || month == 11)
	{
		return 31;
	}
	if (month == 3 || month == 5 || month == 8 || month == 10)
	{
		return 30;
	}
	if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
	{
		return 29;
	}
	else
	{
		return 28;
	}
}

// create years combo
function create_ycombo(yf, mf, df, hf, mif, sf, datef, year, delta1, delta2)
{
	cury = new Date().getFullYear();
	i=0;
	for (y = cury + delta1; y <= cury + delta2; y++)
	{
		opt = new Option(y, y);
		yf.options[i++] = opt;
	}
	yf.options.selectedIndex = year - (cury + delta1);
	yf.onchange = function()
	{
		create_dcombo(yf, mf, df, hf, mf, sf, datef, df.value, days(yf.value, mf.value));
		create_ycombo(yf, mf, df, hf, mif, sf, datef, yf.value, delta1, delta2);
		save(yf, mf, df, hf, mif, sf, datef);
	}
}

// create months combo
function create_mcombo(yf, mf, df, hf, mif, sf, datef, month)
{
	for (m = 0; m < 12; m++)
	{
		mf.options[m] = new Option(month_text[m], m);
	}
	mf.options.selectedIndex = month;
	mf.onchange = function()
	{
		create_dcombo(yf, mf, df, hf, mif, sf, datef, df.value, days(yf.value, mf.value));
		save(yf, mf, df, hf, mif, sf, datef);
	}
}

// create days combo
function create_dcombo(yf, mf, df, hf, mif, sf, datef, day, nr)
{
	day = day < nr ? day : nr;
	for (i = 0; i < nr; i++)
	{
		df.options[i] = new Option(lzero(i + 1), i + 1);
	}
	df.options.selectedIndex = day - 1;
	df.options.length = nr;
	df.onchange = function()
	{
		save(yf, mf, df, hf, mif, sf, datef);
	}
}

// create hours combo
function create_hcombo(yf, mf, df, hf, mif, sf, datef, hours)
{
	for (i = 0; i < 24; i++)
	{
		hf.options[i] = new Option(lzero(i), i);
	}
	hf.options.selectedIndex = hours;
	hf.onchange = function()
	{
		save(yf, mf, df, hf, mif, sf, datef);
	}
}

// create minutes combo
function create_micombo(yf, mf, df, hf, mif, sf, datef, minutes)
{
	for (i = 0; i < 60; i++)
	{
		mif.options[i] = new Option(lzero(i), i);
	}
	mif.options.selectedIndex = minutes;
	mif.onchange = function()
	{
		save(yf, mf, df, hf, mif, sf, datef);
	}
}

// create seconds combo
function create_scombo(yf, mf, df, hf, mif, sf, datef, seconds)
{
	for (i = 0; i < 60; i++)
	{
		sf.options[i] = new Option(lzero(i), i);
	}
	sf.options.selectedIndex = seconds;
	sf.onchange = function()
	{
		save(yf, mf, df, hf, mif, sf, datef);
	}
}

// save change to the date field 
function save(yf, mf, df, hf, mif, sf, datef)
{
	datef.value = yf.value + '-' + lzero(mf.value - 0 + 1) + '-' + lzero(df.value) + ' ' + lzero(typeof(hf) != 'number' ? hf.value : hf) + ':' + lzero(typeof(mif) != 'number' ? mif.value : mif) + ':' + lzero(typeof(sf) != 'number' ? sf.value : sf);
}

// convert to sql date format 
function sql_date(d)
{
	return new Date(d.substring(0, 4), d.substring(5, 7) - 1, d.substring(8, 10), d.substring(11, 13), d.substring(14, 16), d.substring(17, 19));
}

// constructor for date control 
function date_selector(yfield, mfield, dfield, hfield, mifield, sfield, datefield, ydelta1, ydelta2)
{
	var datef = document.getElementById(datefield);
	var date = datef.value == '' ? new Date() : sql_date(datef.value);
	var yf = document.getElementById(yfield);
	var mf = document.getElementById(mfield);
	var df = document.getElementById(dfield);
	var hf = hfield ? document.getElementById(hfield) : date.getHours();
	var mif = mifield ? document.getElementById(mifield) : date.getMinutes();
	var sf = sfield ? document.getElementById(sfield) : date.getSeconds();
	create_ycombo(yf, mf, df, hf, mif, sf, datef, date.getFullYear(), ydelta1, ydelta2);
	create_mcombo(yf, mf, df, hf, mif, sf, datef, date.getMonth());
	create_dcombo(yf, mf, df, hf, mif, sf, datef, date.getDate(), days(yf.value, mf.value));
	if (typeof(hf) != 'number')
	{
		create_hcombo(yf, mf, df, hf, mif, sf, datef, date.getHours());
	}
	if (typeof(mif) != 'number')
	{
		create_micombo(yf, mf, df, hf, mif, sf, datef, date.getMinutes());
	}
	if (typeof(sf) != 'number')
	{
		create_scombo(yf, mf, df, hf, mif, sf, datef, date.getSeconds());
	}
	save(yf, mf, df, hf, mif, sf, datef);
}



// ADD/DEL ELEMENT______________________________________________________________

// insert new element
function add_elem(ref, code)
{
	$('#'+ref).before('<li>'+$('#'+code).html()+'</li>');
}

// insert new element 2
function add_elem2(ref, code, fa_index)
{
	$('#'+ref).before('<li>'+$('#'+code).html().replace(/XXX/g, fa_index++)+'</li>');
	return fa_index;
}

// insert new element 3
function add_elem3(ref, code)
{
	rid = randomString();
	h = $('#'+code).html().replace(/ta_szoveg/, rid);
	$('#'+ref).before('<div>'+h+'</div>');
	tinyMCEconfigAdvanced('none');
	tinyMCE.execCommand('mceAddControl', true, rid);
}

// insert new element 4
function add_elem4(ref, code)
{
	rid = randomString();
	$('#'+ref).before('<li>'+$('#'+code).html().replace(/XXX/g, rid)+'</li>');
	date_selector('ev1_'+rid,'honap1_'+rid,'nap1_'+rid,false,false,false,'datum1_'+rid,-1,1);
	date_selector('ev2_'+rid,'honap2_'+rid,'nap2_'+rid,false,false,false,'datum2_'+rid,-1,1);
}

function randomString()
{
	var str = '';
	for (var i = 0; i < 10; i++)
	{
		str += String.fromCharCode(97 + Math.round(Math.random() * 25));
	}
	return str;
}



// KITKER_______________________________________________________________________

// toggle kitker
function nyit_csuk_kitker(speed)
{
	if (nyitva)
	{
		nyitva = false;
		$('#kereso_nyitva').val('');
		$('#btn_kitker_be').show();
		$('#btn_kitker_ki').hide();
		$('#kitker').slideToggle(speed);
	}
	else
	{
		nyitva = true;
		$('#kereso_nyitva').val('igen');
		$('#btn_kitker_ki').show();
		$('#btn_kitker_be').hide();
		$('#kitker').slideToggle(speed);
	}
}

// clean search box
function clean_searcbox()
{
	$('#kitker').children().not('.active').remove();
	$("#kereso_nyitva[value='']").remove();
}



// COMMENTS_____________________________________________________________________

// send comment
function send_hozzaszolas()
{
	$.ajax({
		type: 'POST',
		url: '/ajax_hozzaszolas-hozzaad',
		async: false,
		data: {
			'hozzaszolas_tipus': $('#hozzaszolas_tipus').val(),
			'hozzaszolas_tipus_id': $('#hozzaszolas_tipus_id').val(),
			'hozzaszolas': $('#hozzaszolas').val(),
			'hozzaszolo_nev': $('#hozzaszolo_nev').val()
		}
	});
	$('#hozzaszolas').val('');
	$('#hozzaszolas_elonezet').html('');
	$('#hik').hide();
	$('#hozzaszolas_elonezet').hide();
	get_hozzaszolas_lista();
}

// show comment
function show_hozzaszolas()
{
	$('#hik').show();
	$('#hozzaszolas_elonezet').show();
	$.ajax({
		type: 'POST',
		url: '/ajax_hozzaszolas-elonezet',
		data: {
			'hozzaszolas': $('#hozzaszolas').val()
		},
		success: function(msg) {
			$('#hozzaszolas_elonezet').html(msg);
		}
	});
}

// update comments
function update_hozzaszolasok()
{
	if ($('#autofriss').attr('checked'))
	{
		get_hozzaszolas_lista();
	}
	setTimeout("update_hozzaszolasok()", 5000); // 5 seconds
}

// get comments list
function get_hozzaszolas_lista()
{
	$.ajax({
		type: 'POST',
		url: '/ajax_hozzaszolas-lista',
		async: false,
		data: {
			'hozzaszolas_tipus': $('#hozzaszolas_tipus').val(), 
			'hozzaszolas_tipus_id': $('#hozzaszolas_tipus_id').val(), 
			'request_uri': $('#request_uri').val(),
			'utolso_hozzaszolas_id' : $('#utolso_hozzaszolas_id').val()
		},
		success: function(msg) {
			if (msg != '')
			{
				v_split = msg.split('#@#', 2);
				$('#utolso_hozzaszolas_id').val(v_split[0]);
				$('#hozzaszolasok_doboz').html(v_split[1]);
			}
		}
	});
}



// SEARCHING____________________________________________________________________

// request search result
function request_search_result()
{	
	if (_search_field != null)
	{
		$.ajax({
			type: 'POST',
			url: document.location.href,
			data: {
				text_to_find: $('#'+_search_field).val().replace(/^\s+|\s+$/g,''), 
				search_field: _search_field, 
				search_all: _search_all
			},
			success: fill_search_result
		});
		_search_field = null;
		_search_all = false;
	}
	setTimeout("request_search_result()", 100);
}

// display search results
function fill_search_result(msg)
{
	if (msg != '')
	{
		var code = '';
		var split_sci = msg.split('##');
		var search_field = split_sci[0];
		var count = split_sci[1];
		if (count == 0)
		{
			code = '<option value="0">&nbsp;</option>';
		}
		else
		{
			var items = split_sci[2];
			var split_fa = items.split('@@');
			for (i = 0; i < split_fa.length; i++)
			{
				var split_in = split_fa[i].split('~~');
				code += '<option value="' + split_in[0] + '">' + split_in[1] + '</option>';
			}
		}
		$('#'+search_field+'_div').html('<select id=\"' + search_field + '_select\" onChange=\"select_search_result(\'' + search_field + '\');\" class="select" style="width:140px">' + code + '</select> <span class=\"search_res_count\">' + count + ' db.</span>');
	}
}

// select a search result
function select_search_result(sf)
{
	if ($('#'+sf+'_select').val() == 'more')
	{
		_search_all = true;
		_search_field = sf;
	}
	else
	{
		fill_selected_result(sf);
	}	
}

// fill selected result
function fill_selected_result(sf)
{
	$('#'+sf).val($('#'+sf+'_select option:selected').text());
	$('#'+sf+'_id').val($('#'+sf+'_select option:selected').val());
	search_field_close(sf,false);
}

// key handler
function search_field_keypress(sf, e, can_be_new)
{
	if (e.keyCode == 13)
	{
		if ($('#'+sf+'_select').children().size() > 0 && $('#'+sf+'_select').val() != '0')
		{
			fill_selected_result(sf);
		}
		else
		{
			if ($('#'+sf).val().replace(/^\s+|\s+$/g,'') != '' && can_be_new)
			{
				$('#'+sf).val($('#'+sf).val().replace(/^\s+|\s+$/g,''));
				$('#'+sf+'_id').val('0');
				search_field_close(sf,true);
			}
		}
		return false;
	}
	return true;	
}

// close the search field
function search_field_close(sf,is_new)
{
	$('#'+sf).attr('readonly', 'true');
	$('#'+sf+'_div').hide();
	$('#'+sf+'_add').show();
	$('#'+sf+'_del').show();
	if (is_new)
	{
		$('#'+sf+'_new').show();
	}
	if ($('#'+sf+'_next'))
	{
		$('#'+sf+'_next').focus();
	}
}

//close the search field
function search_field_open(sf)
{
	$('#'+sf).removeAttr('readonly');
	$('#'+sf+'_div').show();
	$('#'+sf+'_add').hide();
	$('#'+sf+'_del').hide();
	$('#'+sf+'_new').hide();
}

// generate options
function generate_options(select, array, selected, empty_option)
{
	if (empty_option !== false)
	{
		$('#'+select).append('<option value="-">'+empty_option+'</option>');
	}
	for (var i = 0; i < array.length; i = i + 2)
	{
		$('#'+select).append('<option value="'+array[i]+'">'+array[i+1]+'</option>');
	}
	$('#'+select).val(selected);
}



// RIGHTS_______________________________________________________________________

// check rights
function check_jogok(checked, val)
{
	$('#jogok').val(parseInt($('#jogok').val()) + (checked ? +val : -val));
}


// IE_BUTTON_HACK_______________________________________________________________

function ie_button_fix()
{
	$('button').click(function()
	{
		$('button').not(this).removeAttr('name');
	});
}


// ONLOAD_______________________________________________________________________

$(document).ready(function(){
	$(".autofocus:last").focus();
	if ($.browser.msie) ie_button_fix();
});