function confirm_action(a){if(confirm(a)){return true}else{return false}}function confirm_location(b,a){if(confirm(b)){window.location=a}else{return false}}function cancel_edit(a){if(a==NULL){a="Are you sure you want to discard these changes?"}if(confirm(a)){javascript:history.back()}};


/**
 * Create live preview
 *
 * Deserializes a query string (taken for example from window.location.hash 
 * string) into the appropriate page elements
 *
**/
function do_live_preview(include_mutli) {
	if (window.location.hash.length == 0) {
		return false;
	}
	var data = window.location.hash.substring(1).split("&");
	var poll_intro, poll_question, option_label_a, option_label_b, option_label_c, option_label_d;
	for (var i = 0; i < data.length; i++) {
		data[i] = data[i].replace(/\+/g, ' ');
		var pair = decodeURIComponent(data[i]).split("=");
		switch (pair[0]) {
			case 'poll_intro':
				if (pair[1].length > 0) {
					poll_intro = '<strong>' + pair[1] + ': </strong>';
				} else {
					poll_intro = '<strong>Question: </strong>';
				}
				break;
			case 'poll_question':
				if (pair[1].length > 0) {
					poll_question = pair[1];
				} else {
					poll_question = 'Enter your question';
				}
				break;
			case 'answer_1':
				if (pair[1].length > 0) {
					option_label_a = pair[1];
				} else {
					option_label_a = 'Your first answer';
				}
				break;
			case 'answer_2':
				if (pair[1].length > 0) {
					option_label_b = pair[1];
				} else {
					option_label_b = 'Your second answer';
				}
				break;
			case 'answer_3':
				if (pair[1].length > 0) {
					option_label_c = pair[1];
				} else {
					option_label_c = 'Your third answer';
				}
				break;
			case 'answer_4':
				if (pair[1].length > 0) {
					option_label_d = pair[1];
				} else {
					option_label_d = 'Your fourth answer';
				}
				break;
		}
	}
	// Update question	
	$('#voting-options p').html(poll_intro + poll_question);
	
	// Update answers
	if (include_mutli == true) {
		$('#option_label_a span').html(option_label_a);
		$('#option_label_b span').html(option_label_b);
		$('#option_label_c span').html(option_label_c);
		$('#option_label_d span').html(option_label_d);
	}
}



		
	// Display characters left for comments
	function text_limit() {
		var chars_allowed = 250;
		var poll_length = $('#f-comment').val().length;
		var chars_remaining = chars_allowed - poll_length
		$('#char-count div').html(chars_remaining);
		if (chars_remaining < 0)
		{
			$('#f-submit').attr('src','/img/layout/comment-submit-disabled.gif');
			$('#f-submit').attr('disabled','disabled');
			$('#char-count div').css('background', '#ca0008');
		}
		else if (chars_remaining < 20)
		{
			$('#f-submit').attr('src','/img/layout/comment-submit.gif');
			$('#f-submit').attr('disabled','');
			$('#char-count div').css('background', '#ff8712');
		}
		else
		{
			$('#f-submit').attr('src','/img/layout/comment-submit.gif');
			$('#f-submit').attr('disabled','');
			$('#char-count div').css('background', '#8bcd45');
		}
	}
	// ------------------------------------------------------------------------


	// Process ajax validation & comment submission
	function add_comment() {
		var c = $('#f-comment').val();
		var n = $('#f-name').val();
	
		$.ajax({
			url: '/poll/send_comment',
			cache: false,
			type: 'POST',
			data: { 'f-comment': c, 'f-name': n },
			dataType: 'json',
			success: function(data) {
				$('#c-add').remove();
				$('#c-form').fadeOut(function(){
					$('#comments').append('<div id="c-msg">Thanks! Your comment may take a short time to appear while we review it.</div>');
					$('#comments').animate({
						height: 70
					}, function(){
						if (jQuery.browser.msie) {
							this.style.removeAttribute("filter");
						}
						$('#c-msg').fadeIn();
						$('#c-form').remove();
					});
				});
			}
		});
		return false;
	}
	// ------------------------------------------------------------------------
	
	
		
	// Get More comments
	function get_comments() {
		var offset = $('#recent_comments dd').length;
		var url = '/poll/get_comments/' + offset
		$.getJSON(url, function(json) {
			$.each(json.comments, function(i,comment){
				$('#recent_comments').append('<dt>&raquo;</dt><dd>' + comment.comment + '<span class="author">'+ comment.author +'</span></dd>');
			});
			
			// Remove the comments link if we've loaded all comments
			if (json.remove_link == 'true')
			{
				$('#load-more-comments').remove();
			}
		});
		return false;
	}
	// ------------------------------------------------------------------------
	
	
	
	//Check for cookie support
	function check_cookie_support() {
		var COOKIE_NAME = 'test_cookie';
		var date = new Date();
		date.setTime(date.getTime() + (3 * 24 * 60 * 60 * 1000));
		$.cookie(COOKIE_NAME, 'test', { path: '/', expires: date });
		var c = $.cookie(COOKIE_NAME);
		if (c == 'test')
		{
			$('#voting-poll fieldset').show();
			$('#cookie-warning').remove();
		}
	}
	
	
	
	
	// Display results using ajax
	function show_results() {
		// Should we load small or large results?
		if ($('#iframe').length) {
			if ($('body').hasClass('vertical')) {
				var results_url = '/poll/get_results/tiny';
			} else {
				var results_url = '/poll/get_results/small';
			}
			var load_comments = false;
		} else {
			var results_url = '/poll/get_results';
			var load_comments = true;
		}
		$('#voting-poll').fadeOut(400, function(){
			$('#voting-poll fieldset').remove();
			$('#voting-poll').load(results_url, function() {
				$('#voting-poll').fadeIn(400, function(){ 
					$(this).css('filter',''); // fixes IE's poor handling of animated text
				});
				$('#sharing-options').fadeIn(400, function(){ 
					$(this).css('filter',''); // fixes IE's poor handling of animated text
				});
			});	
		});
		
		if (load_comments == true) {
			show_comments();
		}
	}
	
	
	//Load comments
	function show_comments() { 
		$('#ajax-magic').append('<p class="recent_comments_intro">Comments</p>');
		$('#ajax-magic').append('<dl id="recent_comments"></dl>');
		//$('#comments').append('<div id="c-add"><a href="#"><img src="/img/layout/comment-add.gif" alt="Add Comment"/></a></div>');
		$.ajax({
			url: '/poll/get_comments/0',
			cache: false,
			dataType: 'json',
			error: function(a, b, c){
				//alert(a);
				//alert(b);
				//alert(c);
			},
			success: function(json) {
				if (json.comments != '')
				{
					$.each(json.comments, function(i,comment){
						$('#recent_comments').append('<dt>&raquo;</dt><dd>' + comment.comment + '<span class="author">'+ comment.author +'</span></dd>');
					});
					
					if (json.remove_link != 'true')
					{
						$('#ajax-magic').append('<p class="extra-info"><a href="/poll/<?php echo $id;?>" id="load-more-comments" onClick="return get_comments();">&raquo; See more comments</a></p>');
					}
				}
				else
				{
					$('p.recent_comments_intro').html('There are no comments');
				}
	
				$('#ajax-magic').animate({
					opacity:1
				});
			}
		
		});
	
				
				$('#c-add a').click(function(e) {
					$('#comments').animate({
						height:240
					});
					$('#c-add').fadeOut(function(){
						$('#c-form').fadeIn(250, function(){
							$(this).css('filter',''); // fixes IE's poor handling of animated text
							$('#f-comment').focus();
						});
					});
					return false;
				});
				$('#c-cancel-comment').click(function(e) {
					$('#c-form').fadeOut(function(){
						$(this).css('filter',''); // fixes IE's poor handling of animated text
						$('#c-add').fadeIn(250);
					});
					$('#comments').animate({
						height:40
					});
					return false;
				});
		
				// Set up character count for comments
				$('#f-comment').keyup(function(e) {
					text_limit();
				});
				$('#f-comment').click(function(e) {
					text_limit();
				});

	}
	
	
	
	
