var lang = null;
var w_xml = null;
var google = 'http://www.google.com/';
var wtext = '';

$(document).ready(function(){
	$.get(path + 'data/language_js_' + LANG + '.xml', function(xml){
		lang = xml;
		var date = new Date();
		$.get(path + 'latest_work.php?lang=' + LANG, function(xml){			
			$('#newest-content').hide();
			var txt = '';
			$('item',xml).each(function(index, item){
				txt += '<div class="item">';
				txt += '<div class="img-holder"><img src="' + $(this).find('photo').text() + '" width="80" height="60"/></div>';
				txt += '<div class="body">';			
				txt += '<div class="title"><a href="' + path + LANG + '/ref/' + $(this).find('id').text() + '">' + $(this).find('title').text() + '</a></div>';
				txt += '<div class="text">' + $(this).find('text').text() + '</div>';			
				txt += '</div>';
				txt += '<div class="clear"></div>';
				txt += '</div>';				
			});				
			$('#newest-content').html(txt);			
			$('#newest-content').show();
			//$('#newest-content').slideToggle();

			// véletlen fotók
			$('#photo-content').hide();
			var txt = '', img_count = 0;
			$('foto',xml).each(function(index, item){
				txt += '<a href="' + path + LANG + '/fotos/' + $(this).find('album_id').text() + '"><img src="' + $(this).find('small').text() + '" width="60" height="60"/></a>';				
			});		
			txt += '<div class="clear"></div>';
			$('#photo-content').html(txt);
			$('#photo-content').show();
			/*
			$('#photo-content img').bind('load',function(){
				img_count++;				
				if(img_count == $('#photo-content img').length)
				{
					//$('#photo-content').slideToggle();
				}
			});	
			*/
		});

		// weather
		$('#weather-place').val('Sopron');
		loadForecast();

		$('#weather-place').bind('keypress', function(e){		
			if(enterKey(e))
			{		
				loadForecast();
				return false;
			}		
		});

		// Random photos	
		/*
		$.get(path + 'photolist.php?todo=list', function(xml){
			$('#photo-content').hide();
			var txt = '', img_count = 0;
			$('photo',xml).each(function(index, item){
				txt += '<a href="' + path + LANG + '/fotos/' + $(this).find('album_id').text() + '"><img src="' + $(this).find('small').text() + '" width="60" height="60"/></a>';				
			});		
			txt += '<div class="clear"></div>';
			$('#photo-content').html(txt);
			$('#photo-content img').bind('load',function(){
				img_count++;				
				if(img_count == $('#photo-content img').length)
				{
					$('#photo-content').slideToggle();
				}
			});			
		});		
		*/
		$('.city-links a').attr('target','_blabk');
	});
});

function loadForecast()
{
	$.get(path + 'get_weather.php?city=' + encodeURIComponent($('#weather-place').val()) + '&lang=' + LANG, function(xml){
		w_xml = xml;
		if(!$('forecast_information',xml).length)
		{
			$('#w-forecast').html($(lang).find("lang[name='w.noresult']").text());
			$('#w-describe').show();
			$('#w-layer').hide();
			$('#w-layer').html('');
			$('#w-forecast').show();
		}
		else
		{
			var dc = $('current_conditions',xml).find('temp_c').attr('data');
			var df = $('current_conditions',xml).find('temp_f').attr('data');
			var humidity = $('current_conditions',xml).find('humidity').attr('data');
			var icon = $('current_conditions',xml).find('icon').attr('data');
			var wind = $('current_conditions',xml).find('wind_condition').attr('data');

			var txt = '';
			txt += '<img src="' + google + icon + '"/>';
			txt += '<div class="w-holder">';
			txt += '<div class="w-item">' + dc + '&#176;C | ' + df + '&#176;F</div>';
			txt += '<div class="w-item">' + wind + '</div>';
			txt += '<div class="w-item">' + humidity + '</div>';
			txt += '</div>';
			txt += '<div class="clear"></div>';

			var ftxt = '<span>' + $(lang).find("lang[name='w.forecast']").text() + ' </span>';
			ftxt += '<span class="w-act">' + $(lang).find("lang[name='w.today']").text() + '</span>';
			$('forecast_conditions',xml).each(function(index, item){
				ftxt += '<span class="w-day" id="fc_' + index + '">' + $(this).find('day_of_week').attr('data') + '</span>';
			})										
			ftxt += '<div class="clear"></div>';
					
			wtext = txt;
			$('#w-forecast').html(ftxt);
			$('#w-describe').hide();
			$('#w-layer').hide();
			$('#w-layer').html(txt);
			$('#w-layer').fadeIn();
			$('#w-forecast').show();
			setForecast();
		}
	});
}

function setForecast()
{
	$('#w-forecast span').bind('click',function(){
		var txt = '';
		if($(this).attr('class') == 'w-day')
		{
			var index = $(this).attr('id').split('_')[1];
			var low = $('forecast_conditions', w_xml).eq(index).find('low').attr('data');
			var high = $('forecast_conditions', w_xml).eq(index).find('high').attr('data');			
			var icon = $('forecast_conditions', w_xml).eq(index).find('icon').attr('data');
			var wind = $('forecast_conditions', w_xml).eq(index).find('wind_condition').attr('data');
			var cond = $('forecast_conditions', w_xml).eq(index).find('condition').attr('data');
			
			txt += '<img src="' + google + icon + '"/>';
			txt += '<div class="w-holder">';
			txt += '<div class="w-item">' + cond + '</div>';
			txt += '<div class="w-item">' + low + ' - ' + high + ' &#176;C</div>';			
			txt += '</div>';
			txt += '<div class="clear"></div>';
		}

		if($(this).attr('class') == 'w-act')
		{
			txt = wtext;
		}

		$('#w-layer').hide();
		$('#w-layer').html(txt);
		$('#w-layer').fadeIn();
	})
}

function enterKey(e)
{
	var key;
	if(e.which)
		key = e.which;
	else
		key = e.keyCode; 
	if(key == 13) return true;
	else return false;
}