$(document).ready(function(){

	replace_items = [
		{
			selector: ".body_home.partner_ivoteyouvote h1.title", 
			url: "fonts/replace_splashtitle.php"
		},
		{
			selector: ".body_user.partner_ivoteyouvote h1.title, .body_user h2.replace", 
			url: "fonts/replace_pagetitle_white.php"
		},		
		{
			selector: ".body_interior.partner_ivoteyouvote h1.title, .body_interior h2.replace", 
			url: "fonts/replace_pagetitle.php"
		}
	];
	doReplacement();

	$("#topnav ul li a").empty();

	$("ul").each(function() {
		$(this).children("li:first").addClass('first');	
		$(this).children("li:last").addClass('last');
	});

	$("tr").each(function() {
		$(this).find("td:first").addClass('first');	
		$(this).find("th:first").addClass('first');
	});


	$(".minheight").each(function() {
		if (parseInt($(this).height()) < parseInt($(this).css('min-height').replace('px', ''))) {
			$(this).height(parseInt($(this).css('min-height').replace('px', '')));
		}
	});

	if (parseInt($("#left").height()) > parseInt($("#right").height())) {
		$("#right").height($("#left").height());
	}


	$(".autofill").focus(function() {
		if ($(this).val() == $(this).attr('title')) { $(this).val(''); }
	}).blur(function() {
		/* if ($(this).val() == '') { $(this).val($(this).attr('title')); } */
	});


	$("#edit-log").parent().addClass('hidden_form_item');

	$("#topnav li.active_section").each(function() {
		bubble = $("<div></div>");
		bubble.addClass('active_section_bubble');
		$(this).append(bubble);
	});

	need_parent_classes = ['getstarted_voter_firstname']
	$("form input").each(function() {
		my_id = $(this).attr('id');
		if (my_id) {
			$(this).parents('.form-item').addClass('div-' + my_id);
		}
		for (i in need_parent_classes) {
			if ($(this).hasClass(need_parent_classes[i])) {
				$(this).parents('.form-item').addClass('div-' + need_parent_classes[i]);
			}
		}
	});
	$("fieldset legend").each(function() {
		my_id = $(this).text().toLowerCase().replace(/([^0-9a-z])/g,"");
		$(this).parents('fieldset').addClass('fieldset-' + my_id);
	});

	$("#edit-field-different-address-keys").click(function() {
		$(".fieldset-yourmailingaddress").toggle('slow');
	});



	$('.getstarted_remove_voter').click(function() {
		removeVoter($(this));
	});

	$(".getstarted_add_voter_trigger").click(function() {
		voter = $("#getstarted_first_voter").clone();
		add_button = $(this).parent();

		voter.attr('id', '');
		voter.find("input, select, textarea").each(function() {
			current_voter = $("#getstarted_first_voter").parent().find(".getstarted_voter_fieldset").length;
			my_name = $(this).attr('name').replace('[0]', '[' + current_voter + ']');
			$(this).attr('name', my_name);
			$(this).val('');
			$(this).removeClass('error');
		});

		voter_remove = $("<div class='getstarted_remove_voter'><a>Remove</a></div>").click(function() {
			removeVoter($(this));
		});

		voter.prepend(voter_remove);

		voter.hide();
		add_button.before(voter);
		voter.show('slow');
	});

	stripeTable($('table'));

	$(".header_all_link").click(function() {
		my_value = $(this).attr('id').replace('header_all_link_', '');
		$("input[value=" + my_value + "]").click();
	});




	$(".myvoters_button input[type=button], .myvoters_button input[type=image]").click(function() {
		button = $(this).parents('.myvoters_button');
		overlay = button.find('.myvoters_ajax_form_overlay');
		form = button.find('.myvoters_ajax_form');

		w = document.body.clientWidth;

		overlay.css('opacity', 0.75);
		overlay.fadeIn('slow');

		form.css('left', Math.round((w - form.width())/2) + "px");
		form.fadeIn('slow');
	});

	$(".myvoters_ajax_form_cancel").click(function() {
		button = $(this).parents('.myvoters_button');
		button.find('.myvoters_ajax_form').hide();		
		button.find('.myvoters_ajax_form_overlay').hide();

	});

	$(".myvoters_select_form select").change(function() {
		$(this).parents('form').submit();
	});
	$(".myvoters_select_form").ajaxForm().submit(function() {
		nid = $(this).find('input[name=nid]').val();

		saved = $("<div>Saved.</div>").addClass('myvoters_select_form_update').attr('id', 'myvoters_select_form_update_' + nid);
		$(this).after(saved);
	
		setTimeout("$('#myvoters_select_form_update_" + nid + "').fadeOut('slow');", 5000);		
	});


	$(".myvoters_ajax_form").ajaxForm().submit(function() {
		$(this).find('.myvoters_ajax_form_cancel').click();
		nid = $(this).find('input[name=nid]').val();

		if ($(this).hasClass('myvoters_nudge_form')) {
			nudged = $("<div>Nudged!</div>").addClass('myvoters_ajax_form_update').attr('id', 'myvoters_ajax_form_update_' + nid);
			$(this).before(nudged);
			setTimeout("$('#myvoters_ajax_form_update_" + nid + "').fadeOut('slow');", 5000);
		}
		else if ($(this).hasClass('myvoters_remove_form')) {
			$(this).parents('tr').fadeOut('slow', function() {
				$(this).remove();
			});
			stripeTable($('table'));
		}
	});
	
	/*
	$(".form-submit").each(function() {
		name = $(this).attr('value');
		better = $("<div class='button_container_outer'><div class='button_container_inner'>" + name + "</div></div>");
		better.click(function () {
			$(this).parents('form').submit();
		});
		$(this).after(better);
		$(this).hide();
	});
	*/

	$("#splashform").submit(function() {
		r = $("#q option:selected").text();
		$("#r").val(r);
	});


	$("#user-edit > div").attr('id', 'user-edit-inner');

	$(".body_user ul.secondary li.active").each(function() {
		$(this).append("<img src='sites/all/themes/ivoteyouvote/white.gif' id='tab_hide_line' />");
	});

	$("#toggle_email").click(function() {
		$("#edit-field-email-address").toggle();
	});
	$("#toggle_mobile").click(function() {
		$("#edit-field-mobile-phone").toggle();
	});

	$("#edit-custom-email-text").charCounter(500, {container: '#custom_email_counter'});

	$(".skip_this_step").click(function() {
		$(this).parents('form').submit();
	});

	$(".ivyv_tooltip_container").each(function() {
		my_offset = $(this).offset();
		parent_offset = $("#content_inner").offset();
		parent_width = $("#content_inner").width();
		
		if ((my_offset.left - parent_offset.left) > (parent_width/2)) {
			$(this).find('.ivyv_tooltip').addClass('ivyv_tooltip_right');
		}
		else {
			$(this).find('.ivyv_tooltip').addClass('ivyv_tooltip_left');
		}		
	}).hover(function () {
		$(this).find('.ivyv_tooltip').show();
	}, function () {
		$(this).find('.ivyv_tooltip').hide();
	});


	$("#user-register input").each(function() {
		$(this).parent().addClass('form-item-' + $(this).attr('id'));
	});


	$(".messagetone_overlay_link").click(function() {
		button = $(this).parents('.overlay_parent');
		overlay = button.find('.messagetone_overlay');
		box = button.find('.messagetone_overlay_box');

		w = document.body.clientWidth;

		overlay.css('opacity', 0.75);
		overlay.fadeIn('slow');

		box.css('left', Math.round((w - box.width())/2) + "px");
		box.fadeIn('slow');
	});

	$(".messagetone_overlay_close_link").click(function() {
		button = $(this).parents('.overlay_parent');
		button.find('.messagetone_overlay').hide();		
		button.find('.messagetone_overlay_box').hide();

	});


	$(".pretty_table th, .pretty_table td").attr('valign', 'middle');


	$("input[type=submit], input[type=button]").addClass('button');




	$(".album_download_overlay_link").click(function() {
		button = $('#album_download_container');
		overlay = button.find('.album_download_overlay');
		box = button.find('.album_download_overlay_box');

		w = document.body.clientWidth;

		overlay.css('opacity', 0.75);
		overlay.fadeIn('slow');

		box.css('left', Math.round((w - box.width())/2) + "px");
		box.fadeIn('slow');
	});

	$(".album_download_overlay_close_link").click(function() {
		button = $('#album_download_container');
		button.find('.album_download_overlay').hide();		
		button.find('.album_download_overlay_box').hide();

	});


	
	$(".ivyv_state_select").change(function() {
		$(".state_container").hide('slow');
		$("#state_" + $(this).val()).show('slow');
		$("#right").css('height', 'auto');
	});

	
	$("span.email").each(function() {
		email = $(this).text().replace('(at)', '@');
		$(this).empty().append($("<a href='mailto:" + email + "' />").text(email));
	});


	$(".body_hrc #survey-response-form").each(function() {

		$(this).find(".form-submit").hide();
		$(this).append($("<input type='image' src='sites/all/themes/ivoteyouvote/images/ivoteyouvote_button_submit.gif' />"));
	
	});

	$(".body_hrc #edit-field-8").click();

});


function doReplacement() {
	for (i in replace_items) {
		$(replace_items[i].selector).each(function() {
			var tokens = $(this).text().split(' ');
			$(this).empty();
			for (k in tokens) {
				var url = replace_items[i].url + "?text=" + escape(tokens[k]+' ');
				$("<img />").addClass('replacement').attr('alt', tokens[k]).attr('src', url).appendTo($(this));
			}
		});
	}

}

function stripeTable(table) {
	$(table).find("tr").removeClass('even');
	$(table).find("tr").removeClass('odd');	

	$(table).find("tr:even").addClass('even');
	$(table).find("tr:odd").addClass('odd');
}

function removeVoter(voter) {
	$(voter).parents('.getstarted_voter_fieldset').hide('slow', function() {
		$(this).remove();
	});
}
