var actTab_i = 1;
var page_size = 3;
var feeds_length = 0;
var fbUser = null;
var search_txt = '';
var firsttime = 0;
var uuid = '';
if (content_page=='download') actTab_i = 5;
	
$(document).ready(function(){
	$("a.submitbyhref").live("click", function(){
		$($(this).attr("href")).submit();
		return false;
	});
	
	if (content_page=='mytrip' && back_id=='' && $('#feeds_tab_3').attr('id')) actTab_i = 3;
	
	var page = {1:0, 2:0, 3:0, 4:0, 5:0};
	var more = {1:true, 2:true, 3:true, 4:true, 5:true};
	var uuids = {1:[], 2:[], 3:[], 4:[], 5:[]};
	
	// back
	if (back_id!='') {
		$.ajax({
			url: '/get_back.php',
			data: {'back_id': back_id},
			type: 'POST',
			async: false,
			dataType: 'json',
			success: function(data) {
				var back = data;
				actTab_i = back.actTab_i;
				if (back.search_txt != undefined && back.search_txt != '') {
					$('#search_txt').val(back.search_txt);
					search_txt = back.search_txt;
					$('#feeds_tab_5').show();
					if (content_page=='download') {
						$('.feeds_top > h2 > span').html(back.search_txt);
						$('select[name="destination"] option:contains(\''+back.search_txt+'\')').attr('selected', 'selected');
						$('select[name="destination"]').change();
					}
				}
				page = back.page;
				more = back.more;
				uuids = back.uuids;
				uuid = back.uuid;
				firsttime = back.firsttime;
				$.each(more, function(i) {
					if (more[i]=='false' && uuids[i]==undefined) {
						drawEmpty(i);
					} else if (uuids[i]!=undefined && uuids[i].length>0) {
						getData('/feeds.php',{
							'type':'uuidFeeds', 
							'params': {
								'uuids':uuids[i],
								'tab_i':i
							}
						}, i);
					}
				});
				if (more[actTab_i] && more[actTab_i]!='false') setMoreFeedsLink(true);
				else setMoreFeedsLink(false);
			}
		});
	}
	
	$('#search_txt').focus(function() {
		if ($('#search_txt').val()==dst) {
			$('#search_txt').val('');
		}
	});
	$('#search_txt').blur(function() {
		if ($('#search_txt').val()=='') {
			$('#feeds_tab_5').hide();
			$('#search_txt').val(dst);
			$('#feeds_tab_1').click();
		}
	});
	$('#search_btn').click(function() {
		$('#searchForm').submit();
		return false;
	});
	$('#searchForm').submit(function () {
		search_txt = $('#search_txt').val();
		searchFeeds();
		return false;
	});
	$(".feed_more").live("click", function() {
		uuid = $(this).parent().find('a:first').attr('name');
		back_id = saveBack();
		$(this).attr('href', $(this).attr('href')+'&bid='+back_id);
	});
	function saveBack() {
		var back = new Object();
		back.actTab_i = actTab_i;
		if (search_txt!='') back.search_txt = search_txt;
		back.page = page;
		back.more = more;
		back.uuids = uuids;
		back.uuid = uuid;
		back.firsttime = firsttime;
		var back_id = $.ajax({
			url: "/save_back.php",
			type: "POST",
			data: back,
			async: false
		}).responseText;
		return back_id;
	}
	function searchDestionation(value, destination) {
		if (value!=-1) {
			page[5] = 0;
			more[5] = true;
			$('#feeds_div_5').html('');
			search_txt = destination;
			$('.feeds_top > h2 > span').html(destination);
			searchFeeds();
		} else {
			$('#feeds_top_image').hide();
			$('.contentbox_feeds').hide();
		}
		return false;
	}
	function searchFeeds() {
		page[5] = 0;
		more[5] = true;
		uuids[5] = [];
		$('#feeds_div_5').html('');
		$('#feeds_tab_5').show();
		$('#feeds_tab_5').click();
	}
	$('#more_btn').click(function() {
		getFeeds(true);
		return false;
	});
	$('#feeds_menu > li > a').click(function() {
		actTab = $(this);
		if (content_page=='download') actTab_i = 5;
		else actTab_i = $('#feeds_menu > li').index($(actTab.parent()))+1;
		setActTab(actTab_i);
		getFeeds(false);
		return false;
	});
	function setActTab(tab_i) {
		$('#feeds_menu li a.act').removeClass('act');
		$('#feeds_tab_'+tab_i).addClass('act');
		$('.feeds_div').hide();
		$('#feeds_div_'+tab_i).show();
	}
	function setMoreFeedsLink(more_exists) {
		if (more_exists) {
			$('#more_btn').show();
			$('#no_more_btn').hide();
		} else {
			$('#more_btn').hide();
			$('#no_more_btn').show();
		}
	}
	function getFeeds(needMore) {
		if (more[actTab_i] && more[actTab_i]!='false') setMoreFeedsLink(true);
		else setMoreFeedsLink(false);
		if (page[actTab_i]==0 || needMore) {
			page[actTab_i]++;
			var feed_list_type = '';
			var params = {
				'size': page_size,
				'page': page[actTab_i],
				'tab_i': actTab_i
			};
			if (actTab_i==1) var feed_list_type = 'ALLfeedList';
			else if (actTab_i==2) var feed_list_type = 'FRIENDSfeedList';
			else if (actTab_i==3) var feed_list_type = 'MYfeedList';
			else if (actTab_i==4) var feed_list_type = 'FAVfeedList';
			else if (actTab_i==5) {
				var feed_list_type = 'SEARCHfeedList';
				params.search = search_txt;
			}
			getData('/feeds.php',{
					'type': feed_list_type, 
					'params': params
				}, actTab_i);
		}
	}
	function getData(url, params, tab_i) {
		if (params.type!='newFeed' && !params.topfeeds) drawLoading(tab_i);
		$.ajax({
			url: url,
			data: params,
			type: "post",
			async: true,
			dataType: "json",
			success: drawFeedList
		});
	}
	function drawFeedList(data) {
		if (data.firsttime && data.firsttime!='0') firsttime = data.firsttime;
		checkFbUser(data.user);
		if (data.feeds!=null && data.feeds.length) {
			feeds_length = data.feeds.length;
			if (data.type=='TOPfeedList' && feeds_length==1) var needRemoveLast = true;
			else var needRemoveLast = false;
			if (content_page=='download') {
				$('#feeds_top_image').show();
				$('.contentbox_feeds').show();
			}
			$.each(data.feeds, function(i) {
				drawFeed(data.feeds[i], data.type, data.tab_i, needRemoveLast);
			});
			if (data.type=='TOPfeedList') {
				$.each(data.uuids, function(i) {
					uuids[data.tab_i].unshift(data.uuids[i]);
				});
			} else {
				$.each(data.uuids, function(i) {
					uuids[data.tab_i].push(data.uuids[i]);
				});
			}
		} else if (content_page=='download') {
			$('#feeds_top_image').hide();
			$('.contentbox_feeds').hide();
		} else if (data.type!='newFeed')  {
			feeds_length = 0;
			if ($('#feeds_div_'+data.tab_i+' > .feed_item:not(.loading_feed)').length==0) {
				drawEmpty(data.tab_i);
			}
		}
		if (feeds_length<page_size && data.type!='newFeed' && data.type!='TOPfeedList' && data.type!='uuidFeeds') {
			setMoreFeedsLink(false);
			$("#feeds_div_"+data.tab_i).find('div.loading_feed').remove();
			more[data.tab_i] = false;
		}
	}
	function checkFbUser(data) {
		fbUser = data;
		if (!fbUser) $('#feeds_tab_2, #feeds_tab_3, #feeds_tab_4').hide();
		else $('#feeds_tab_2, #feeds_tab_3, #feeds_tab_4').show();
	}
	function drawFeed(feed_html, type, tab_i, needRemoveLast){
		var position = 'last';
		if (type=='newFeed' || type=='TOPfeedList') {
			$('#feeds_div_'+tab_i).prepend(feed_html);
			position = 'first';
		} else $('#feeds_div_'+tab_i).append(feed_html);
		$('#feeds_div_'+tab_i+' .feed_item:'+position+' .feed_rating span').prepend(rating_txt);
		var aktUuid=$('#feeds_div_'+tab_i+' .feed_item:'+position+' a:first').attr('name').substr(2);
		$('#feeds_div_'+tab_i+' .feed_more:'+position).attr('href', feed_landing_href+'&uuid='+aktUuid);
		$('.edit_feed_txt').text(edit_feed_txt);
		$("#feeds_div_"+tab_i).find('div.loading_feed').remove();
		$('#feeds_div_'+tab_i+' > .feed_item:'+position).slideDown(1000, function() {
			// pozícionálás uuid-re
			var patt = new RegExp(uuid, 'gi');
			if (tab_i==actTab_i && uuid!='' && feed_html.search(patt)!=-1) {
				var top = $('a[name="'+uuid+'"]').offset().top-20;
				$('html,body').animate({scrollTop: top + 'px'}, 200);
				uuid = '';
			}
		});
		if (needRemoveLast) {
			$('#feeds_div_1 .feed_item:last:not(.loading_feed)').slideUp(1000, function() {
				$(this).remove();
			});
		}
		return true;
	}
	function drawEmpty(tab_i) {
		var feed_html  = '<div class="feed_item empty_feed">'+no_feeds_txt+'<div class="clear"></div></div>';
		$('#feeds_div_'+tab_i).append(feed_html);
		$("#feeds_div_"+tab_i).find('div.loading_feed').remove();
		$('#feeds_div_'+tab_i+' > .feed_item:last').show();
		setMoreFeedsLink(false);
		more[tab_i] = false;
	}
	function drawLoading(tab_i) {
		var feed_html  = '<div class="feed_item loading_feed">&nbsp;<div class="clear"></div></div>';
		$('#feeds_div_'+tab_i).append(feed_html);
		$("#feeds_div_"+tab_i).find('div.loading_feed').show();
	}
	if (content_page!='download') $('#feeds_tab_'+actTab_i).click();
	else {
		$('select[name="destination"]').change(function(){
			searchDestionation($(this).val(), $(this).find(':selected').text());
		});
	}
	setInterval(function() {
			if (content_page=='front' && actTab_i==1 && fbUser) {
				getData('/feeds.php',{
					'type':'newFeed', 
					'firsttime':firsttime,
					'params': {
						'size':1,
						'page':1,
						'tab_i':1
					}
				}, actTab_i);
			}
		}, 30000
	);
	setInterval(function() {
			if (content_page=='front' && actTab_i==1 && page[1]==1 && !fbUser && uuids[1].length>0) {
				uuids[1] = uuids[1].slice(0,2);
				getData('/feeds.php',{
					'type':'ALLfeedList',
					'topfeeds':true,
					'params': {
						'size':1,
						'page':1,
						'tab_i':1,
						'uuids':uuids[1]
					}
				}, 1);
			}
		}, 5000
	);
});
