$.preloadImages([
	'assets/images/spacer.gif'
]);
$(document).ready(function(){
	// Clickable Logo
	$('.logo').click(function() {document.location.href='/';});
	
	// Rollover Images
	/*$('img').hover(
		function() {src = $(this).attr('src'); $(this).attr('src',src.replace(/off\./,'on.'));},
		function() {src = $(this).attr('src'); $(this).attr('src',src.replace(/on\./,'off.'));}
	);*/
	
	// Contact Form Validation
	$("#contactForm").validate({
		rules: {
			zip: {
				digits: true,
				rangelength: [5, 5]
			}
		},
		messages: {
			zip: {
				rangelength: "Zip must be 5 digits"
			}
		}
	});
	
	$(".hidden").hide();
	$(".show").click(function () {
		$(".hidden").slideToggle("slow");
	});
	
/**
  * Meeting Profile Form
  */
	
	// Company Details	
	$('input[name=ist_tabulate]').change(function(){
		if ($(this).val() == "No") {
			$('.servicesneeded').fadeIn();
			$('.servicesneeded input:checkbox').prop('disabled',false);
		}
		else {
			$('.servicesneeded').fadeOut();
			$('.servicesneeded input:checkbox').prop('disabled',true);
		}
	});
	
	$('input[name=proxy_solicitor]').change(function(){
		if ($(this).val() == "Yes") {
			$('.proxy_solicitor_details').fadeIn();
			$('.proxy_solicitor_details input').prop('disabled',false);
		}
		else {
			$('.proxy_solicitor_details').fadeOut();
			$('.proxy_solicitor_details input').prop('disabled',true);
		}
	});
	
	$('input[name=ist_inspector]').change(function(){
		if ($(this).val() == "Yes") {
			$('.ist_inspector_details').fadeIn();
		}
		else {
			$('.ist_inspector_details').fadeOut();
		}
	});
	
	
	/*$(':radio').change(function () {
        $(':radio[name=' + this.name + ']').parent().removeClass('active');
        $(this).parent().addClass('active');
    });*/
	
	$('input[name=material_distibution_option]').change(function(){
		if ($(this).val() == "Electronic Hosting of Proxy Materials") {
			$('.material_distibution_option1').fadeIn();
			$('.material_distibution_option2, .material_distibution_option3, .material_distibution_option4').hide();
			$('.material_distibution_option1 input').prop('disabled',false);
			$('.material_distibution_option2 input').prop('disabled',true);
		}
		else if ($(this).val() == "Notice & Access [SEC reportable companies]") {
			$('.material_distibution_option2').fadeIn();
			$('.material_distibution_option1, .material_distibution_option3, .material_distibution_option4').hide();
			$('.material_distibution_option2 input').prop('disabled',false);
			$('.material_distibution_option1 input').prop('disabled',true);
		}
		else if ($(this).val() == "Limited Materials Mailing Option") {
			$('.material_distibution_option3').fadeIn();
			$('.material_distibution_option1, .material_distibution_option2, .material_distibution_option4').hide();
			$('.material_distibution_option1 input, .material_distibution_option2 input').prop('disabled',true);
		}
		else if ($(this).val() == "Full Set Delivery Mailing Option") {
			$('.material_distibution_option4').fadeIn();
			$('.material_distibution_option1, .material_distibution_option2, .material_distibution_option3').hide();
			$('.material_distibution_option1 input, .material_distibution_option2 input').prop('disabled',true);
		}
	});
	
	// Voting
	$('input[name=CEDE_CO_position]').change(function(){
		if ($(this).val() == "Yes") {
			$('.CEDE_CO_position_details').fadeIn();
		}
		else {
			$('.CEDE_CO_position_details').fadeOut();
		}
	});
	
	$('input[name=SEC_reportable]').change(function(){
		if ($(this).val() == "Yes") {
			$('.what_term_vote').fadeIn();
			$('.what_term_vote input').prop('disabled',false);
		}
		else {
			$('.what_term_vote').fadeOut();
			$('.what_term_vote input').prop('disabled',true);
		}
	});
	
	$('input[name=cumulative_voting_allowed]').change(function(){
		if ($(this).val() == "Yes") {
			$('.cumulative_voting_allowed_details').fadeIn();
			$('.cumulative_voting_allowed_details input').prop('disabled',false);
		}
		else {
			$('.cumulative_voting_allowed_details').fadeOut();
			$('.cumulative_voting_allowed_details input').prop('disabled',true);
		}
	});
	
	// Mailing Materials Checkboxes
	/*$('input[name=enclosure_Notice_Meeting_Proxy_Statement_Letter]').attr("disabled", "disabled");
	$('#patrick').change(function(){
		if (this.checked) {
			$('input[name=enclosure_Notice_Meeting_Proxy_Statement_Letter]').removeAttr("disabled");
		}
		else {
			$('input[name=enclosure_Notice_Meeting_Proxy_Statement_Letter]').attr('disabled', '');
		}
	})*/
	
	/*$('.karen').attr("disabled", "disabled");
	$('input[name=enclosures]').change(function(){
		if (this.checked) {
			$('input.karen').removeAttr("disabled");
			$('.mailingmaterials').css('color', '#0099ff');
		}
		else {
			$('input.karen').attr('disabled', '');
		}
	})*/
	
	//$("input.cole + input").attr('disabled', 'disabled');
	
	$('.cole + label > input').attr("checked", "checked");
	
	
	$(".patrick").change(function() {
		$("input[name=enclosure_Annual_Report]").attr("disabled", !($(".patrick").is(":checked"))).removeAttr('checked');
		//$("input[name=enclosure_Annual_Report]").removeAttr("disabled")
	});
	
	/*$('.patrick').change(function(){
		if (this.checked) {
			$("input[name=enclosure_Annual_Report]").removeAttr("disabled");
		}
		else {
			$('input[name=enclosure_Annual_Report]').attr('disabled', 'disabled');
		}
	});*/
	
	// Mailing and Tabulation of External Plan(s) - checkboxes
	$(".otherplan").change(function(){
		if (this.checked) {
			$('input[name=included_plans_other]').removeAttr("disabled");
		}
		else {
			$('input[name=included_plans_other]').attr("disabled", "disabled");
		}
	})
	
	// Mailing and Tabulation of External Plan(s) - add'l blocks
	$("a.addblock").click(function() {
		$("div.block2").fadeIn(function() { //Show add'l block 2
			$("a.addblock").click(function() {
				$("div.block3").fadeIn(function() { //Show add'l block 2
					$("a.addblock").click(function() {
						$("div.block4").fadeIn(); //Show add'l block 4
						$("a.addblock").hide(); //Hide link to display add'l blocks
					});
				});
			});
		});
		return false;
	});
	
	// jQuery TimePicker - http://labs.perifer.se/timedatepicker/
	$(".timepicker").timePicker({
		//startTime: "07:00",  // Using string. Can take string or Date object.
		//endTime: new Date(0, 0, 0, 15, 30, 0),  // Using Date object.
		show24Hours: false
	});
	
	// jQuery UI DatePicker - http://jqueryui.com/demos/datepicker
	$(".datepicker").datepicker({
		showOtherMonths: true,
		selectOtherMonths: true
	});
	
	// jQuery Tooltips - http://www.sohtanaka.com/web-design/simple-tooltip-w-jquery-css/
	var tip;
	$(".tip_trigger").hover(function(){

		//Caching the tooltip and removing it from container; then appending it to the body
		tip = $(this).find('.tip').remove();
		$('body').append(tip);

		tip.fadeIn(100);

	}, function() {

		tip.hide().remove(); //Hide and remove tooltip appended to the body
		$(this).append(tip); //Return the tooltip to its original position

	}).mousemove(function(e) {
	//console.log(e.pageX)
		  var mousex = e.pageX + 20; //Get X coodrinates
		  var mousey = e.pageY + 20; //Get Y coordinates
		  var tipWidth = tip.width(); //Find width of tooltip
		  var tipHeight = tip.height(); //Find height of tooltip

		 //Distance of element from the right edge of viewport
		  var tipVisX = $(window).width() - (mousex + tipWidth);
		  var tipVisY = $(window).height() - (mousey + tipHeight);

		if ( tipVisX < 20 ) { //If tooltip exceeds the X coordinate of viewport
			mousex = e.pageX - tipWidth - 20;
			$(this).find('.tip').css({  top: mousey, left: mousex });
		} if ( tipVisY < 20 ) { //If tooltip exceeds the Y coordinate of viewport
			mousey = e.pageY - tipHeight - 20;
			tip.css({  top: mousey, left: mousex });
		} else {
			tip.css({  top: mousey, left: mousex });
		}
	});
	
});

// characters remaining code
function limitChars(textid, limit, infodiv)
{
	var text = $('#'+textid).val();	
	var textlength = text.length;
	if(textlength > limit)
	{
		$('#' + infodiv).html('You cannot write more then '+limit+' characters!');
		$('#'+textid).val(text.substr(0,limit));
		return false;
	}
	else
	{
		$('#' + infodiv).html('You have '+ (limit - textlength) +' characters left.');
		return true;
	}
}

$(function(){
 	$('#comments').keyup(function(){
 		limitChars('comments', 250, 'charlimitinfo');
 	})
});

