var $=jq;

$(function(){
	$('#header').attr('labeledby','dhs')
	
	/* COMMENTS */
	
	$("#add-comment-form").bind("submit", function() { return false; }) // disable form submitting
	
	var submit = $('#add-comment-form .form-actions input') // existing submit button
	var submit_value = submit.attr('value')
	
	submit.replaceWith('<input type="button" value="'+submit_value+'" class="add-comment"/>') // replace submit with button
	
	var add_button = $('#add-comment-form .form-actions .add-comment')
	var comment_input = $('#add-comment-form textarea')
	
	
	/*
	function ToggleSubmit(){  // disable add button if no input in textarea
		if (!comment_input.val()){
			add_button.attr('disabled', 'disabled')
		} else {
			add_button.attr('disabled', '')
		}
	}
	
	ToggleSubmit() // set default state - browser may of prefilled content
	
	comment_input.bind('keyup', function(event){ // check whether there is content to submit
		ToggleSubmit()
	})
	*/
	var here = window.location+''
	here = here.replace('idea-view','')
	here = here.replace(/#.*/, '')
	add_button.bind('click', function(event){
		event.preventDefault()
		if (comment_input.val()){
			$.post(here+'/++conversation++default/add-comment?deliv_notheme', {text:comment_input.val(), ____ajax:'ajax', 'comment-submit':'submit'},
				function(data){
					$('#discussion-viewlet #add-comment-form').before(data)
					RemoveSubmit($('#discussion-viewlet .comment:last'))
					comment_input.val('') // blank input
					$('#submit-idea a').focus() // fix for ie8
					comment_input.focus() //
				}
			)
		} else {
			// please enter a comment // disable / enable submit button
		}
		//ToggleSubmit()
	})
	
	function RemoveSubmit(ele){
		$(ele).each(
			function(){
				var remove_title = $(this).find('input').attr('value')
				$(this).find('input').replaceWith('<input type="button" class="delete-comment" value="'+remove_title+'" />')
			}
		)
	}
	RemoveSubmit($('form.delete'))
	
	$('.delete-comment').live('click', function(event){
		event.preventDefault()
		var ele = $(this)
		ele.parent().parent().parent().fadeOut("slow")
		//ele.parent().parent().parent().remove()
		$.post(ele.parent().parent().attr('action'),{},
			function(data){}
		)
	})
	/* END COMMENTS */
	
	$('#skip a').bind("click", function(event) {
		event.preventDefault()
		document.location.hash = $(this).attr('href').split('#')[1]
	})
	
	// reporting ideas
	if($('#report_link').length >0){
		old_href = $('#report_link').attr('href');
		$('#report_link').attr('href', old_href.replace(/report_confirm/,'#'));
		$('#report_link').bind("click", function(event){
			if(confirm("An email will be sent notifying the moderator of this idea.")){
				url = String(document.location).replace(/idea-view\/?$/, '');
				url = url.replace(/\/$/,'') +"/report_to_moderator"
				$.post(url, {ajax:"1"}, handleReportLinkClick);
			}
			return false
		});
	}
	
	//AJAX updating of ideas counts
	if($("#idea-count").length > 0) //only on the home page
		var ideas_update = setInterval("updateIdeascounts()", 10000);
})

function processIdeasCountsData(data){
	arr_missions = data.split(',');
	for(key in arr_missions){
		mission = arr_missions[key];
		arr_path_and_number = mission.split(':');
		path = arr_path_and_number[0];
		number = arr_path_and_number[1];
		if(path == 'total'){ //special case for the total
			$('#idea-count .count').text(number);
			continue
		}
		mission_name = path.match(/[^\/]+(?=\/ideas)/); //e.g. get 'borders' from '/site/borders/ideas'
		jquery_str = '#'+ mission_name +' .count';
		$(jquery_str).text(number);
	}
}

function updateIdeascounts(){
	$.get("get_ideas_count", {ajax:"1"}, processIdeasCountsData);
}



function handleReportLinkClick(data){
	if(String(data) == '1'){
		$('#report_link_p').text("Thanks, a moderator has been alerted.");
		$('#report_link_p').addClass('portalMessage info');
	}
	else{
		$('#report_link_p').text("Sorry, there was a problem trying to report this idea.");
	}
}





