function addDate(id, select)
{
	for (var i=1; i<=31; i++)
	{
		i == select
			? $('#'+id).append($('<option></option>').val(i).attr('selected', 'selected').html(i))
			: $('#'+id).append($('<option></option>').val(i).html(i))
		;
	}
}

function addMonth(id, select)
{
	for (var i=1; i<=12; i++)
	{
		i == select
			? $('#'+id).append($('<option></option>').val(i).attr('selected', 'selected').html(i))
			: $('#'+id).append($('<option></option>').val(i).html(i))
		;
	}
}

function addYear(id, select)
{
	for (var i=1993; i>=1900; i--)
	{
		i == select
			? $('#'+id).append($('<option></option>').val(i).attr('selected', 'selected').html(i))
			: $('#'+id).append($('<option></option>').val(i).html(i))
		;
	}
}

function showPreview(id_location)
{
	$.post(ROOT + '/response/location.php', {getImages:id_location}, function(response)
	{
		$("#thumbList").html("");

		if (response.length > 0)
		{
			for (var i=0; i<response.length; i++) {
				$("#thumbList").append('<div class="fL mR05 mB05"><img class="thumb" src="' + response[i].img + '" alt="" /><br/><div class="pT03 aC"><a href="javascript:deleteLocationImage(\'' + response[i].md5 + '\')" onclick="return(askDelete())" ">Löschen</a></div></div>');
			}

			$("#thumbList").append('<div class="clear"></div>');
		}
	});
}


function deleteLocationImage(md5)
{
	$.post(ROOT + "/response/location.php", {deleteImage:md5}, function(response){
		showPreview(response);
	});
}

function askDelete()
{
	return confirm('Eintrag Löschen?');
}


function updateLocationItem(id)
{
	$.post(ROOT + "/response/location.php", {getItem:id}, function(response){
		$('li#item-'+id).html(response);
	});
}


function hideAjaxLoader(id)
{
	if (!id) {
		id = 'ajaxLoader';
	}

	$('#' + id).html('');
}



function showAjaxLoader(id)
{
	if (!id) {
		id = 'ajaxLoader';
	}

	$('#' + id).html('<img src="/shared/gfx/loader.gif" alt="" />');
}


function showAjaxIcon(id)
{
	$("label[for="+id+"] b").html('<img class="pL05" src="' + ROOT + '/gfx/ico-ajax-mini.gif" alt="" />');
}

function hideAjaxIcon(id)
{
	$("label[for="+id+"] b").html('');
}

	$("#btnUpload").click(function()
	{
		$('#formLocation').submit();
		/*$("#Ajax").dialog({
			modal: true,
			width: 300,
			height: 200,
			closeOnEscape: false,
			title: "Bild hochladen",
		});*/
	});

	function popUp()
	{
		var w = open('', 'category', 'resizable=0,scrollbars=1,width=300,height=600');
		w.location.href = 'category.php';
		w.focus();
	}

	function enableSelectbox(id, response, selected)
	{
		$(id).attr("disabled", false);
		$(id).removeClass("disabled");
		$(id).removeOption(/./);

		if (response)
		{
			var responseData = {};

			switch(id)
			{
				case('#frm_country'): responseData = toKeyValue(response, selected, 'geonames_id', 'country'); break;
				case('#frm_region'): responseData = toKeyValue(response, selected, 'geonames_id', 'region'); break;
				case('#frm_city'): responseData = toKeyValue(response, selected, 'geonames_id', 'city'); break;
				case('#frm_category_1'): responseData = response; break;
				case('#frm_category_2'): responseData = response; break;
				case('#frm_category_3'): responseData = response; break;
			}

			$(id).addOption(responseData, false);
		}

		if (selected) {
			$(id).selectOptions(selected, true);
		}
	}

	function disableSelectbox(id)
	{
		$(id).attr('selectedIndex', 0);
		$(id).addClass('disabled');
		$(id).attr('disabled', true);
	}

	function updateSelectbox(id, response, selected)
	{
		response.length == undefined
			? enableSelectbox(id, response, selected)
			: disableSelectbox(id)
		;
	}

	function getLocationCategories(id)
	{
		var split = id.split('-');

		alert(split.length);
	}

	function getLocationCategory(nr, category_id, selected)
	{
		showAjaxIcon('frm_category_' + nr);

		$.post(ROOT + '/response/location.php', {type:'category_'+nr, id_category:category_id}, function(response){
			updateSelectbox('#frm_category_' + nr, response, selected);
		});
	}

	function getLocationCountry(selected)
	{
		showAjaxIcon('frm_id_country');

		$.post(ROOT + '/response/location.php', {type:'country'}, function(response) {
			updateSelectbox('#frm_country', response, selected);
			hideAjaxIcon('frm_id_country');
		});
	}

	function getLocationRegion(country_id, selected)
	{
		disableSelectbox('#frm_region');
		disableSelectbox('#frm_city');

		if (country_id)
		{
			showAjaxIcon('frm_id_region');

			$.post(ROOT + '/response/location.php', {type:'region', id_country:country_id}, function(response) {
				updateSelectbox('#frm_region', response, selected);
				hideAjaxIcon('frm_id_region');
			});
		}
	}


	function getLocationCity(region_id, selected)
	{
		disableSelectbox('#frm_city');

		if (region_id)
		{
			showAjaxIcon('frm_id_city');

			$.post(ROOT + '/response/location.php', {type:'city', id_region:region_id}, function(response) {
				updateSelectbox('#frm_city', response, selected);
				hideAjaxIcon('frm_id_city');
			});
		}
	}


function toKeyValue(input, index, key, value)
{
	var obj = {};

	for (i in input) {
		obj[input[i][key]] = input[i][value];
	}

	return obj;
}


/**
 * LiveStream Popup
 */
function openLivestream(url)
{
	player.stopPlayback();

	livestream = open('', 'livestream', 'resizable=0,scrollbars=0,width=500,height=412');
	livestream.location.href = url;

	if (livestream.opener == null) {
		livestream.opener = self;
	}

	livestream.focus();
}

/**
 * FlashUploader
 */
function uploadComplete()
{
	top.location.reload();
}


function dumpObj(obj)
{
	for(i in obj)
	{
		alert(i + " = " + obj[i]);
	}
}
