/*
function AttrRenamer(obj, attr_name){
	if (obj.length){
		var renamed = obj.attr(attr_name).replace(/-/g,'.')
		obj.attr(attr_name,renamed)
	}
}
AttrRenamer($('#form-widgets-idea'), 'id')
AttrRenamer($('#formfield-form-widgets-idea label'), 'for')
AttrRenamer($('#form-widgets-why'), 'id')
AttrRenamer($('#formfield-form-widgets-why label'), 'for')
*/

$(function(){
	$('#header').attr('labeledby','dhs')
	
	/* RATINGS */
	/*
	function Status(msg){
		if (!$('.rate-msg').length){
			$('<p class="rate-msg" role="alert">'+msg+'</p>').insertAfter('.UserRating .star-rating')
		} else {
			$('.rate-msg').text(msg)
		}
	}
	
	$('.UserRating .star-rating a').bind('click', function(event){
		Status('Thanks for rating!')
	})
	*/
	/* END RATINGS */
	
	/* 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','')
	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'},
				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]
	})
})