﻿$(document).ready(function(){
	
	$('input, textarea').clearField();
	
	$('#share_main').click(function() {
		return false
	});	
	
	/*Ajax commune search (start)*/
	$('#commune').attr('autocomplete', 'off');
	
	var communeinput = false;
	
	$('#commune').focus(function(event){
		communeinput = true;
	});
	
	$('#commune').blur(function(event){
		communeinput = false;
	});
	
	$(document).keydown(function(event){
		if (event.keyCode == 13 || event.keyCode == 9) {
			if ($('.selected').html() == null) {
				var commune_id = $('.normal:first').attr('id');
				commune_id = commune_id.substring(commune_id.indexOf('_')+1);
			} else {
				var commune_id = $('.selected').attr('id');
				commune_id = commune_id.substring(commune_id.indexOf('_')+1);
			}
	
			$('#commune_id').val(commune_id);
	
			$('.scrollPanel').html('');
			$('.combobox').attr('class', 'combobox');
			$('#commune').focus();
			if (event.keyCode == 9) {
				return false;
			}
		}
	});
	
	$(document).keyup(function(event){
		
		var commune = $('#commune').val();
		if ($('.scrollPanel').find('.noresult').html() == null) {
			var hits = $('.scrollPanel').children().size(); 	
		} else {
			var hits = 0;
		}

		if (commune.length > 2 && communeinput) {
			if (event.keyCode != 40 && event.keyCode != 38 && event.keyCode != 13 && event.keyCode != 9) {
				var page = $('body').attr('class');
				$.ajax({
					type: 'POST',
					url: 'http://'+window.location.host+'/_ajax/ajax_communes.php',
					data: 'searchString='+commune+'&page='+page,
					success: function(html){
						if (html.length > 1) {
							$('#comboboxHolder').html(html);
							//$('#comboboxHolder').css('display', 'block');
						} else {
							//$('#comboboxHolder').css('display', 'none');
							$('.scrollPanel').html('');
							$('.combobox').attr('class', 'combobox');
						}
					}
				});
			} else {
				if (event.keyCode == 40 && hits > 0) {
					var nofSelected = 0;
					$(".selected").each(function() {
						if ($(this).next('.normal').html()) {
							$(this).next('.normal').addClass('selected');
							$('.selected:first').removeClass('selected');
							nofSelected++;
						} else {
							nofSelected = 0;	
						}
					});
					
					if (nofSelected == 0) {
						if (hits > 1) {
							$('.normal:first').addClass('selected');
							$('.normal:last').removeClass('selected');
						} else {
							$('.normal:first').addClass('selected');
						}
					}
					
					$('#commune').val($('.selected').html());

					var commune_id = $('.selected').attr('id');
					commune_id = commune_id.substring(commune_id.indexOf('_')+1);
					$('#commune_id').val(commune_id);
					
				} else if (event.keyCode == 38 && hits > 0) {
					var nofSelected = 0;
					$(".selected").each(function() {
						if ($(this).prev('.normal').html()) {
							$(this).prev('.normal').addClass('selected');
							$('.selected:last').removeClass('selected');
							nofSelected++;
						} else {
							nofSelected = 0;	
						}
					});
					
					if (nofSelected == 0) {
						if (hits > 1) {
							$('.normal:last').addClass('selected');
							$('.normal:first').removeClass('selected');
						} else {
							$('.normal:first').addClass('selected');
						}
					}
					
					$('#commune').val($('.selected').html());
					
					var commune_id = $('.selected').attr('id');
					commune_id = commune_id.substring(commune_id.indexOf('_')+1);
					$('#commune_id').val(commune_id);
					
				/*} else if (event.keyCode == 13 || event.keyCode == 9) {
					if ($('.selected').html() == null) {
						var commune_id = $('.normal:first').attr('id');
						commune_id = commune_id.substring(commune_id.indexOf('_')+1);
					} else {
						var commune_id = $('.selected').attr('id');
						commune_id = commune_id.substring(commune_id.indexOf('_')+1);
					}

					$('#commune_id').val(commune_id);

					$('.scrollPanel').html('');
					$('.combobox').attr('class', 'combobox');
					$('#commune').focus();
					if (event.keyCode == 9) {
						return false;
					}*/
				}
			}
		} else {
			//$('#comboboxHolder').css('display', 'none');
			$('.scrollPanel').html('');
			$('.combobox').attr('class', 'combobox');
		}
	});	
	
	$('.normal').live('mouseover', function(){
		$(this).addClass('selected');
	});
	
	$('.normal').live('mouseout', function(){
		$(this).removeClass('selected');
	});	
	
	$('.normal').live('click', function(){
		$('#commune').val($('.selected').html());
		
		var commune_id = $('.selected').attr('id');
		commune_id = commune_id.substring(commune_id.indexOf('_')+1);
		$('#commune_id').val(commune_id);
		
		//$('#comboboxHolder').css('display', 'none');
		$('.scrollPanel').html('');
		$('.combobox').attr('class', 'combobox');
		$('#commune').focus();
	});
	
	
	$('.submitButton').click(function() {
		$('#mainSearch').submit();
		return false;
	});
	
	$('.submitButton_small').click(function() {
		$('#communePageSearch').submit();
		return false;
	});
	
	/*Ajax commune search (end)*/
	
	/*Validation and form submit (start)*/
	
	$('#to_politician').validate();
	
	$('#to_politician').find('.submit').click(function() {
		if ($('#to_politician').valid()) {
			
			var politician_mail = $('#politician_mail').val();
			var your_name = $('#your_name').val();
			var your_mail = $('#to_politician').find('.your_mail').val();

			$.ajax({
				type: 'POST',
				url: 'http://'+window.location.host+'/_ajax/ajax_mail_politician.php',
				data: 'politician_mail='+politician_mail+'&your_name='+your_name+'&your_mail='+your_mail,
				success: function(html){
					//$('#to_politician').find('.submitHolder').html(html);
					$('#confirmation').find('.box').html(html);
					$("#confirmation").show();
					
					$('#politician_mail').val('');
					$('#your_name').val('');
					$('#to_politician').find('.your_mail').val('');
				}
			});
		} else {
			$("#field_error").show();
		}
		return false;
	});
	
	$('#to_hgf').validate();
	
	$('#to_hgf').find('.submit').click(function() {
		if ($('#to_hgf').valid()) {
			
			var your_commune = $('#your_commune').val();
			var your_message = $('#to_hgf').find('#your_message').val();
			var your_mail = $('#to_hgf').find('.your_mail').val();

			$.ajax({
				type: 'POST',
				url: 'http://'+window.location.host+'/_ajax/ajax_mail_hgf.php',
				data: 'your_commune='+your_commune+'&your_message='+your_message+'&your_mail='+your_mail,
				success: function(html){
					//$('#to_hgf').find('.submitHolder').html(html);
					$('#confirmation').find('.box').html(html);
					$("#confirmation").show();
					
					$('#your_commune').val('');
					$('#to_hgf').find('#your_message').val('');
					$('#to_hgf').find('.your_mail').val('');
				}
			});
		} else {
			$("#field_error").show();
		}
		return false;
	});
	
	$('#ask_politician').validate();
	
	$('#ask_politician').find('.submit').click(function() {
		if ($('#ask_politician').valid()) {
			$("#fraga").hide();
			var politician_id = $('#ask_politician').find('.politician_id').val();
			var your_name = $('#ask_politician').find('.your_name').val();
			var your_mail = $('#ask_politician').find('.your_mail').val();
			var commune_id = $('.politicians').attr('id');
			commune_id = commune_id.substring(commune_id.indexOf('_')+1);

			$.ajax({
				type: 'POST',
				url: 'http://'+window.location.host+'/_ajax/ajax_ask_politician.php',
				data: 'politician_id='+politician_id+'&your_name='+your_name+'&your_mail='+your_mail,
				success: function(html){
					//$('#ask_politician').find('.submitHolder').html(html);
					$('#confirmation').find('.box').html(html);
					$("#confirmation").show();
					
					$('#ask_politician').find('.your_name').val('');
					$('#ask_politician').find('.your_mail').val('');
					
					$.ajax({
						type: 'POST',
						url: 'http://'+window.location.host+'/_ajax/ajax_politicians.php',
						data: 'commune_id='+commune_id,
						success: function(html){
							$('.politicians').html(html);
						}
					});
				}
			});
		} else {
			$("#field_error").show();
		}
		return false;
	});
	
	$('#remind_politician').validate();
	
	$('#remind_politician').find('.submit').click(function() {
		if ($('#remind_politician').valid()) {
			$("#paminn").hide();
			var politician_id = $('#remind_politician').find('.politician_id').val();
			var your_mail = $('#remind_politician').find('.your_mail').val();
			var commune_id = $('.politicians').attr('id');
			commune_id = commune_id.substring(commune_id.indexOf('_')+1);

			$.ajax({
				type: 'POST',
				url: 'http://'+window.location.host+'/_ajax/ajax_remind_politician.php',
				data: 'politician_id='+politician_id+'&your_mail='+your_mail,
				success: function(html){
					//$('#remind_politician').find('.submitHolder').html(html);
					$('#remind_politician').find('.your_mail').val('');
					$('#confirmation').find('.box').html(html);
					$("#confirmation").show();
					
					$.ajax({
						type: 'POST',
						url: 'http://'+window.location.host+'/_ajax/ajax_politicians.php',
						data: 'commune_id='+commune_id,
						success: function(html){
							$('.politicians').html(html);
						}
					});
				}
			});
		} else {
			$("#field_error").show();
		}
		return false;
	});
	
	$('#comment_politician').validate();
	
	$('#comment_politician').find('.submit').click(function() {
		if ($('#comment_politician').valid()) {
			$("#confirmation").show();
			var politician_id = $('#comment_politician').find('.politician_id').val();
			var your_message = $('#comment_politician').find('#your_message').val();
			your_message = your_message.replace(/\n/g, "***newline***");
			var your_name = $('#comment_politician').find('.your_name').val();
			var your_mail = $('#comment_politician').find('.your_mail').val();
			var commune_id = $('.politicians').attr('id');
			commune_id = commune_id.substring(commune_id.indexOf('_')+1);

			$.ajax({
				type: 'POST',
				url: 'http://'+window.location.host+'/_ajax/ajax_post_comment.php',
				data: 'politician_id='+politician_id+'&your_message='+your_message+'&your_name='+your_name+'&your_mail='+your_mail,
				success: function(html){
					//$('#comment_politician').find('.submitHolder').html(html);
					$('#confirmation').find('.box').html(html);
									
					
					$('#comment_politician').find('#your_message').val('');
					$('#comment_politician').find('.your_name').val('');
					$('#comment_politician').find('.your_mail').val('');
					
					$.ajax({
						type: 'POST',
						url: 'http://'+window.location.host+'/_ajax/ajax_politicians.php',
						data: 'commune_id='+commune_id,
						success: function(html){
							$('.politicians').html(html);
						}
					});
				}
			});
		} else {
			$("#field_error").show();
		}
		return false;
	});
	
	$('#politician_answer').validate();
	
	$('#politician_answer').find('.submit').click(function() {
		if ($('#politician_answer').valid()) {
			
			var politician_id = $('#politician_answer').find('.politician_id').val();
			var comment_id = $('#politician_answer').find('.comment_id').val();
			var your_message = $('#politician_answer').find('#your_message').val();
			your_message = your_message.replace(/\n/g, "***newline***");
			var commune_id = $('.politicians').attr('id');
			commune_id = commune_id.substring(commune_id.indexOf('_')+1);

			$.ajax({
				type: 'POST',
				url: 'http://'+window.location.host+'/_ajax/ajax_post_answer.php',
				data: 'politician_id='+politician_id+'&comment_id='+comment_id+'&your_message='+your_message,
				success: function(html){
					//$('#politician_answer').find('.submitHolder').html(html);
					$("#confirm_posts").hide();
					$("#first_confirm_posts").hide();
					$('#politician_answer').find('#your_message').val('');
					
					$.ajax({
						type: 'POST',
						url: 'http://'+window.location.host+'/_ajax/ajax_comments.php',
						data: 'politician_id='+politician_id+'&p_view=1',
						success: function(html){
							$('.commentContent').html(html);
						}
					});
					
					$.ajax({
						type: 'POST',
						url: 'http://'+window.location.host+'/_ajax/ajax_politicians.php',
						data: 'commune_id='+commune_id,
						success: function(html){
							$('.politicians').html(html);
						}
					});
				}
			});
			
			/*var your_message = $('#your_message').val();
			
			$('#confirm_post').html('<p>Vill du posta följande meddelande?</p>'+your_message+'<p><a href="#" id="no">Nej</a> <a href="#" id="yes" href="">Ja</a></p>');
			$('#confirm_post').css('display', 'block');*/

			/*$.ajax({
				type: 'POST',
				url: '_ajax/ajax_post_answer.php',
				data: 'politician_id='+politician_id+'&your_message='+your_message,
				success: function(html){
					$('#politician_answer').find('.submitHolder').html(html);
				}
			});*/
		} else {
			$("#field_error").show();
		}
		return false;
	});
	
	
	$('#no').live('click', function(){
		$('#confirm_post').css('display', 'none');
		return false;
	});
	
	$('#yes').live('click', function(){
		$('#confirm_post').css('display', 'none');
		
		var politician_id = $('#politician_answer').find('.politician_id').val();
		var comment_id = $('#politician_answer').find('.comment_id').val();
		var your_message = $('#your_message').val();
		
		$.ajax({
			type: 'POST',
			url: 'http://'+window.location.host+'/_ajax/ajax_post_answer.php',
			data: 'politician_id='+politician_id+'&comment_id='+comment_id+'&your_message='+your_message,
			success: function(html){
				$('#politician_answer').find('.submitHolder').html(html);
				
				$.ajax({
					type: 'POST',
					url: 'http://'+window.location.host+'/_ajax/ajax_comments.php',
					data: 'politician_id='+politician_id+'&p_view=1',
					success: function(html){
						$('.commentContent').html(html);
					}
				});
			}
		});
		
		/*$.ajax({
			type: 'POST',
			url: '_ajax/ajax_comments.php',
			data: 'politician_id='+politician_id+'&p_view=1',
			success: function(html){
				$('.commentContent').html(html);
			}
		});*/
		
		$('#your_message').val('');
		
		return false;
	});
	/*Validation and form submit (end)*/


	/*Send politian_id to form on click (start)*/
	//$('.ask').click(function() {
	$('.ask').live('click', function(){
		$("#fraga").show();
		var politician_id = $(this).attr('id');
		var name = $(this).attr('name');
		politician_id = politician_id.substring(politician_id.indexOf('_')+1);
		if (politician_id.indexOf('?') != '-1') {
			politician_id = politician_id.substring(0, politician_id.indexOf('?'));
		}
		$('#ask_politician').find('.politician_id').val(politician_id);
		$('#fraga').find('h2').html('Hej '+name+'!');
		return false;
	});
	
	//$('.remind').click(function() {
	$('.remind').live('click', function(){
		$("#paminn").show();
		var politician_id = $(this).attr('id');
		var name = $(this).attr('name');
		politician_id = politician_id.substring(politician_id.indexOf('_')+1);
		if (politician_id.indexOf('?') != '-1') {
			politician_id = politician_id.substring(0, politician_id.indexOf('?'));
		}

		$('#remind_politician').find('.politician_id').val(politician_id);
		$('#paminn').find('.remind_name').html(name);
		return false;
	});
	
	//$('.read').click(function() {
	$('.read').live('click', function(){
		$("#diskutera").show();
		var politician_id = $(this).attr('id');
		politician_id = politician_id.substring(politician_id.indexOf('_')+1);
		if (politician_id.indexOf('?') != '-1') {
			politician_id = politician_id.substring(0, politician_id.indexOf('?'));
		}
		$('#comment_politician').find('.politician_id').val(politician_id);

		$.ajax({
			type: 'POST',
			url: 'http://'+window.location.host+'/_ajax/ajax_comments.php',
			data: 'politician_id='+politician_id,
			success: function(html){
				$('.commentContent').html(html);
			}
		});
		return false;
	});
	/*Send politian_id to form on click (end)*/
	
	/*Vote functionality (start)*/
	$('.good').live('click', function(){
		var parent = $(this).parent().parent();
		var comment_id = $(this).attr('href');
		comment_id = comment_id.substring(comment_id.indexOf('_')+1);
		if (comment_id.indexOf('?') != '-1') {
			comment_id = comment_id.substring(0, comment_id.indexOf('?'));
		}
		$.ajax({
			type: 'POST',
			//url: '_ajax/ajax_vote.php',
			url: 'http://www.byggframtiden.se/_ajax/ajax_vote.php',
			data: 'comment_id='+comment_id+'&vote=good',
			success: function(html){
				parent.html(html);
			}
		});
		return false;
	});
	
	$('.bad').live('click', function(){
		var parent = $(this).parent().parent();
		var comment_id = $(this).attr('href');
		comment_id = comment_id.substring(comment_id.indexOf('_')+1);
		if (comment_id.indexOf('?') != '-1') {
			comment_id = comment_id.substring(0, comment_id.indexOf('?'));
		}
		$.ajax({
			type: 'POST',
			url: 'http://'+window.location.host+'/_ajax/ajax_vote.php',
			data: 'comment_id='+comment_id+'&vote=bad',
			success: function(html){
				parent.html(html);
			}
		});
		return false;
	});
	/*Vote functionality (end)*/
	
	/*Report functionality (start)*/
	$('.report').live('click', function(){
		var parent = $(this).parent();
		var comment_id = $(this).attr('href');
		comment_id = comment_id.substring(comment_id.indexOf('_')+1);
		if (comment_id.indexOf('?') != '-1') {
			comment_id = comment_id.substring(0, comment_id.indexOf('?'));
		}

		$.ajax({
			type: 'POST',
			url: 'http://'+window.location.host+'/_ajax/ajax_report.php',
			data: 'comment_id='+comment_id,
			success: function(html){
				parent.html(html);
			}
		});
		return false;
	});	
	/*Report functionality (end)*/
	
	/*Choose question to reply to (start)*/
	$('.answer').live('click', function(){
		$("#confirm_posts").show();
		var comment_id = $(this).attr('href');
		
		if (comment_id.indexOf('?') != '-1') {
			comment_id = comment_id.substring(0, comment_id.indexOf('?'));
		}
		
		$('.comment_id').val(comment_id);
		$.ajax({
			type: 'GET',
			url: 'http://'+window.location.host+'/_ajax/ajax_question.php',
			data: 'comment_id='+comment_id,
			success: function(html){
				$('#question').html(html);
			}
		});
		return false;							   
	});
	/*Choose question to reply to (end)*/
	/*Share buttons (navigation)*/	
	$('.shareHover').live('mouseover', function(){
		$(this).find('.share_container').css('height', '65px');
		$(this).find('span.share_container').addClass('active_buttons');
		//$(this).find('.buttons').show();
	});
	$('.shareHover').live('mouseout', function(){
		$(this).find('.share_container').css('height', '10px');
		$(this).find('span.share_container').removeClass('active_buttons');
		//$(this).find('.buttons').hide();
	});
	/*Share buttons (start)*/
	$('.comment_share').live('mouseover', function(){
		//setTimeout("hideShare()", 3000);
		/*if ($(this).parent().parent().next('.shareholder').css('display') == 'block') {
			$(this).parent().parent().next('.shareholder').css('display', 'none');
		} else {*/
			$(this).parent().parent().next('.shareholder').css('display', 'block');
		//}
		//return false;
	});	
	
	$('.comment_share_f').live('click', function(){
		/*$('#shareButtons').css('display', 'none');
		$('.shareholder').css('display', 'none');*/
		var url = 'http://www.facebook.com/sharer.php?u='+$(this).attr('href');
		url = (url.substr(-1,1)=='/') ? url.substring(0,url.length-1) : url ;
		window.open(url,'','scrollbars=no,menubar=no,height=500,width=600,resizable=no,toolbar=no,location=no,status=no');
		return false;
	});
	
	$('.comment_share_t').live('click', function(){
		/*$('.shareholder').css('display', 'none');
		$('#shareButtons').css('display', 'none');*/
	});
	/*Share buttons (end)*/
	
	/*Inserts statistics (start)*/
	$('.siffror_popup').live('click', function(){
		$("#siffror_popup").show();
		var href = $(this).attr('href');
		if (href.indexOf('?') != '-1') {
			href = href.substring(0, href.indexOf('?'));
		}
		if (href == 'communes') {
			$.ajax({
				type: 'POST',
				url: 'http://'+window.location.host+'/_ajax/ajax_get_all_communes.php',
				success: function(html){
					$("#siffror_popup").find('h3').html('Mest aktiva kommuner');
					$("#siffror_popup").find('.content').html(html);
				}
			});
		} else if (href == 'politicians') {
			$.ajax({
			type: 'POST',
			url: 'http://'+window.location.host+'/_ajax/ajax_get_all_politicians.php',
			success: function(html){
				$("#siffror_popup").find('h3').html('Mest aktiva politiker');
				$("#siffror_popup").find('.content').html(html);
			}
		});			
		}
		return false;
	});
	/*Inserts statistics (end)*/
	
	/*Backup for when user hits return instead of clicking "Skicka" (start)*/
	$('#politician_answer').submit(function() {
		if ($('#politician_answer').valid()) {
			
			var politician_id = $('#politician_answer').find('.politician_id').val();
			var comment_id = $('#politician_answer').find('.comment_id').val();
			var your_message = $('#politician_answer').find('#your_message').val();
			var commune_id = $('.politicians').attr('id');
			commune_id = commune_id.substring(commune_id.indexOf('_')+1);

			$.ajax({
				type: 'POST',
				url: 'http://www.byggframtiden.se/_ajax/ajax_post_answer.php',
				data: 'politician_id='+politician_id+'&comment_id='+comment_id+'&your_message='+your_message,
				success: function(html){
					//$('#politician_answer').find('.submitHolder').html(html);
					$("#confirm_posts").hide();
					$("#first_confirm_posts").hide();
					$('#politician_answer').find('#your_message').val('');
					
					$.ajax({
						type: 'POST',
						url: 'http://'+window.location.host+'/_ajax/ajax_comments.php',
						data: 'politician_id='+politician_id+'&p_view=1',
						success: function(html){
							$('.commentContent').html(html);
						}
					});
					
					$.ajax({
						type: 'POST',
						url: 'http://'+window.location.host+'/_ajax/ajax_politicians.php',
						data: 'commune_id='+commune_id,
						success: function(html){
							$('.politicians').html(html);
						}
					});
				}
			});
		}
		return false;
	});
	
	
	
	
	
	$('#comment_politician').submit(function() {
		if ($('#comment_politician').valid()) {
			
			var politician_id = $('#comment_politician').find('.politician_id').val();
			var your_message = $('#comment_politician').find('#your_message').val();
			var your_name = $('#comment_politician').find('.your_name').val();
			var your_mail = $('#comment_politician').find('.your_mail').val();
			var commune_id = $('.politicians').attr('id');
			commune_id = commune_id.substring(commune_id.indexOf('_')+1);

			$.ajax({
				type: 'POST',
				url: 'http://'+window.location.host+'/_ajax/ajax_post_comment.php',
				data: 'politician_id='+politician_id+'&your_message='+your_message+'&your_name='+your_name+'&your_mail='+your_mail,
				success: function(html){
					//$('#comment_politician').find('.submitHolder').html(html);
					$('#confirmation').find('.box').html(html);
					$("#confirmation").show();				
					
					$('#comment_politician').find('#your_message').val('');
					$('#comment_politician').find('.your_name').val('');
					$('#comment_politician').find('.your_mail').val('');
					
					$.ajax({
						type: 'POST',
						url: 'http://'+window.location.host+'/_ajax/ajax_politicians.php',
						data: 'commune_id='+commune_id,
						success: function(html){
							$('.politicians').html(html);
						}
					});
				}
			});
		} else {
			$("#field_error").show();
		}
		return false;
	});
	/*Backup for when user hits return instead of clicking "Skicka" (end)*/
	
	
	$('#tell_me_button').click(function() {
		
		var error = false;								
		var patt = /(\w[-._\w]*\w@\w[-._\w]*\w\.\w{2,3})/;
		var email = $('#email').val();
		
		if (!patt.test(email)) {
			$("#field_error").show();
			error = true;
		}
		
		if (! error) {
			$('#tell_me_button_container').html('');
			$.ajax({
				type: 'POST',
				url: 'http://'+window.location.host+'/_ajax/ajax_tell_me.php',
				data: 'email='+email,
				success: function(html){
					$('#tell_me_form').html('<h4>'+html+'</h4>');
				}
			});
		}
		return false;
	});

});

function hideShare() {
	$('.shareholder').css('display', 'none');
}
