//multi-select drop downs
//$("#practice_area, #population_represented, #geographical_region, #area_of_focus").multiSelect({selectAll: false});

//var $=jq;


//$("#formfield-form-widgets-idea textarea, #formfield-form-widgets-why textarea, #archetypes-fieldname-text textarea").wymeditor({
//	//options
//	updateSelector: ":submit"
//});

$(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);
	
	
	
	//	*****	automatic checking of 'other' option when you enter text in 'if other please specify' boxes	*****
	
	arr_inputs = new Array();
	arr_inputs['practice_area_other'] = ['#practice_area input[value=Other]','checked']
	arr_inputs['population_represented_other'] = ['#population_represented input[value=Other]','checked'],
	arr_inputs['geographical_region_other'] = ['#geographical_region input[value=Other]','checked'],
	arr_inputs['org_type_other'] = ['#org_type option[value=Other]','selected'],
	arr_inputs['role_in_org_other'] = ['#role_in_org option[value=Other]','selected']
	
	for(input in arr_inputs){
		$('#'+input).keyup(//when contents of the text <input> is changed
			function(){
				this_id = $(this).attr('id');
				if($(this).val() != ''){//if some text has been entered in the 'please specify' box
					$(arr_inputs[this_id][0]).attr(arr_inputs[this_id][1], arr_inputs[this_id][1]);
				}
			}
		);
	}
	
	
	
	
	// shift key tracking
	var shiftKey = false; // default - not held down
	$(document).keyup(function(event){
		if (event.keyCode == 16) {
			shiftKey = false;
		}
	});
	
	$(document).keydown(function(event){
		if(event.keyCode == 16){ // shift key pressed
			shiftKey = true;
		}
	});
	// end 

	$("#formfield-form-widgets-idea input.sidehide").attr('id', 'idea-wym-hoax');
	
	$("#formfield-form-widgets-why input.sidehide").attr('id', 'why-wym-hoax');
	
	
	$(".wym_tools_strong a").text('Bold CTRL+b');
	$(".wym_tools_strong a").attr('title','Bold CTRL+b');
	$(".wym_tools_emphasis a").text('Italic CTRL+i');
	$(".wym_tools_emphasis a").attr('title','Italic CTRL+i');
	
	$("#idea-wym-hoax").keydown(
		function(event){
			if (event.keyCode != 9 && event.keyCode != 16){ // not tab key i.e. user starts typing answer // AND not shift key, allows user to shift tab
				// change the focus to wsyiwyg
				$("#formfield-form-widgets-idea iframe").contents().find("body").focus();
				$("#formfield-form-widgets-idea iframe").contents().find("html").focus();
				$("#formfield-form-widgets-idea iframe").attr('id', 'idea-iframe');
				document.getElementById('idea-iframe').contentWindow.focus();
				
				if(!$.browser.msie){ // not IE
					var key_pressed = String.fromCharCode(event.keyCode)
					if (!shiftKey){ 
						key_pressed = key_pressed.toLowerCase()
					}
					var old_text = $("#formfield-form-widgets-idea iframe").contents().find("body").text();
					$("#formfield-form-widgets-idea iframe").contents().find("body").text(key_pressed+old_text);
				}
			} 
		}
	);
	$("#idea-wym-hoax").focus(
		function(){
			if(!shiftKey){
				$("#formfield-form-widgets-idea iframe").css({'outline':'1px dotted grey'});
			}
		}
	);
	$("#idea-wym-hoax").blur(
		function(){
			$(this).foccused = false
			$("#formfield-form-widgets-idea iframe").css({'outline':'none'});
		}
	);
	$("#why-wym-hoax").keydown(
		function(event){
			if(event.keyCode != 9 && event.keyCode != 16){ // not tab key i.e. user starts typing answer // AND not shift key, allows user to shift tab
				// change the focus to wsyiwyg
				$("#formfield-form-widgets-why iframe").contents().find("body").focus();
				$("#formfield-form-widgets-why iframe").contents().find("html").focus();
				$("#formfield-form-widgets-why iframe").attr('id', 'why-iframe');
				document.getElementById('why-iframe').contentWindow.focus();
				
				if(!$.browser.msie){ // not IE
					var key_pressed = String.fromCharCode(event.keyCode)
					if (!shiftKey){ 
						key_pressed = key_pressed.toLowerCase()
					}
					var old_text = $("#formfield-form-widgets-why iframe").contents().find("body").text();
					$("#formfield-form-widgets-why iframe").contents().find("body").text(key_pressed+old_text);
				}
			} 
		}
	);
	var stop_second_jump = false
	$("#why-wym-hoax").focus(
		function(){
			if(!shiftKey){
				$("#formfield-form-widgets-why iframe").css({'outline':'1px dotted grey'});
			}
		}
	);
	$("#why-wym-hoax").blur(
		function(){
			$(this).foccused = false
			$("#formfield-form-widgets-why iframe").css({'outline':'none'});
		}
	);
})




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.");
	}
}










