var blankFeedParams = {
	'postLanguages': '',
	'parties': '',
	'authorId': '',
	'feedTypes': '',
	'multiLanguage': '',
	'smallParties': ''
};

// grabbed at http://phpjs.org/functions/in_array:432
function in_array(needle, haystack, argStrict) {
    var key = '', strict = !!argStrict;

    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {
                return true;
            }
        }
    } else {
        for (key in haystack) {
            if (haystack[key] == needle) {
                return true;
            }
        }
    }

    return false;
}

function openFirstBlogGroup() {
	$('#postsList .blogGroup.hasPosts.page' + _feedPage).find('.blogGroupHead').trigger('click');
	
	if (_feedPage > 1) {
		// scrolling to newly opened group
		postOffsetY = $('#postsList .blogGroup.hasPosts.page' + _feedPage + ':first').find('.blogGroupHead').offset().top;
		$('html,body').animate({ scrollTop: postOffsetY }, 500);
	}
}

function loadPosts(resetPage, params) {
	if (resetPage) {
		_feedPage = 1;
	}
	
	var defaultFeedParams = {
		'language': LANGUAGE, 
		'postLanguages': _feedLanguages,
		'parties': _feedParties,
		'page': _feedPage,
		'authorId': _feedAuthorId,
		'feedTypes': _feedTypes,
		'groupBy': _feedGroupBy,
		'multiLanguage': _feedMultiLanguage,
		'smallParties': _feedSmallParties,
		'currentView': _currentView 
	};
	
	params = $.extend(defaultFeedParams, params);

	$.post('/inc/ajax/ajaxLoadListing.php', params, function(postsHtml) {
			if (_feedPage > 1) {
				$('#postsList').append(postsHtml);
				openFirstBlogGroup();
				return false;
			}
			
			$('#postsList').html(postsHtml);
			openFirstBlogGroup();
			return false;
		});		
}


$(function() {
	$('form#communeForm').ajaxForm({
		success: function(responseText) {
			if (responseText == 'OK') {
				window.location = COMMUNE_URL;
			} else {
				window.location = '/';
			}
		}
	});
	
	$('input[type=text]').hint();
	
	$('form#keywordForm').submit(function() {
		var keywords = $(this).find('input:text').val();
		window.location = KEYWORD_SEARCH_URL.replace(/%s/, escape(keywords));

		return false;
	});

	$('#communeButton a#communeEditLink').click(function() {
		$('#communeLabel').hide();
		$('#communeForm').show();
		$('#communeForm input[name=zip]').focus();
	});
	
	$('#communeForm input[name=zip]').blur(function() {
		if (_feedZip.length > 0) {
			$('#communeLabel').show();
			$('#communeForm').hide();
		}
		
		return true;
	});

});

//
// TODO: refactor / validate JS code below... 
//
$(function() {
	$('#globalNav .item.nb5').mouseover(showSubMenu);
	$("#subNav").mouseover(showSubMenu);
	$("#buttons").mouseout(hideSubMenu);
	$("#timeLine a").click(toggleTimeLine);
//	$("#autoCompleteBox").autocomplete(data);
	$(".drop").click(dropClick);
	$(".update").click(updateClick);	
});

// FUNCTIONS
////////////////////////////////////////////////////////////////////////////////

function initPostsFunctions(displayMorePostsLink) {
	$(".blogGroupHead").click(toggleBlogGroup);
	$(".blogGroupHead").click(toggleBlogGroupArrow);
	$(".blogIcon").click(toggleBlogContent);
	$(window).load(function () {
		$('#morePosts').toggle(displayMorePostsLink);
	});
}

function toggleBlogGroup() {
	blogGroupBody = $(this).parent().find(".blogGroupBody").slideToggle();
}

function toggleBlogContent() {
	blogGroupBody = $(this).parent().find(".firstAbstractLine").slideToggle();
	blogGroupBody = $(this).parent().find(".blogContent").slideToggle();
	
	if($(this).find("img").attr('src') == "/images/icon_plus.gif") {
		$(this).find("img").attr("src", "/images/icon_minus.gif");
	} else {
		$(this).find("img").attr("src", "/images/icon_plus.gif");
	}
}

function toggleBlogGroupArrow() {
	
	arrowSpan = $(this).find("span");
	
	if(arrowSpan.css("background-position") == "8px 1px") {
		arrowSpan.css("background-position", "8px -17px");
	}
	else {
		arrowSpan.css("background-position", "8px 1px");
	}
}


function toggleTimeLine() {
	$(this).parent().parent().find("a").attr("class","");
	$(this).attr("class","selected");
	return false;
}

function showSubMenu() {
   	$('#subNav').css('display', 'block');
}

function hideSubMenu() {
   	$('#subNav').css('display', 'none');
}
  
function updateClick(e){  
  	
    e.preventDefault();  
    e.stopPropagation();  
  
    var text = document.createElement("input");  
    text.setAttribute("type", "text");  
    text.setAttribute("value", $(this).text());
	$(text).css("width", $(this).css("width"));
  
    $(this).html(text); 
  
    $(text).click(function(e){ e.stopPropagation(); });      
    $(text).blur(fnUpdate);	
    text.focus();  
}
  
function dropClick(e){  
  	
    e.preventDefault();  
    e.stopPropagation();
	
	console.log($(this).siblings("#autoCompleteBox"));
	$(this).siblings("#autoCompleteBox").autocomplete();
}

function fnUpdate(e) {  
	e.stopPropagation();
    $(this).parent().html($(this).attr("value"));
}  