/*
	Kick Butts Day scripts (utilizing jQuery 1.2.6)
	Developed by Saforian
*/


/* --- Initialize page --- */
$(document).ready(function(){

	// Insert document icons
	$("a[href$=rtf]:not(:has(img))").append('<img class="icon" src="/images/icon_rtf.gif" width="14" height="14" alt=" (RTF)">');
	$("a[href$=pdf]:not(:has(img))").append('<img class="icon" src="/images/icon_pdf.gif" width="14" height="14" alt=" (PDF)">');
	$("a[href$=doc]:not(:has(img)), a[href$=docx]:not(:has(img))").append('<img class="icon" src="/images/icon_word.gif" width="14" height="14" alt=" (Word Document)">');
	$("a[href$=xls]:not(:has(img)), a[href$=xlsx]:not(:has(img))").append('<img class="icon" src="/images/icon_excel.gif" width="14" height="14" alt=" (Excel Spreadsheet)">');
	$("a[href$=ppt]:not(:has(img)), a[href$=pptx]:not(:has(img))").append('<img class="icon" src="/images/icon_powerpoint.gif" width="14" height="14" alt=" (Powerpoint Presentation)">');

	// Stripe row colors
	$("table.data tr:nth-child(even)").not(".even").not(".odd").addClass("even");

	// CSS3 selector replacement
	$(".columns>.col:last-child").addClass("last-child");
	$(".columns>.col:first-child").addClass("first-child");
	$(".columns.three>.col:eq(1)").addClass("middle-child");


	// Right-column grey box
	$(".right_grey_box").wrap('<div class="right_grey_box_wrap"></div>');

	// Delete link confirmation (use rel attribute to optionally display the object type)
	$("a:contains('Delete')").click(function(){
		var type = $(this).attr("rel") || 'item';
		if(type == "event"){
			var message = "Are you sure you want to continue? Doing so will cancel any grant application associated with this event. You will no longer be able to access information you have saved or submitted for this event or grant.";
		}else{
			var message = "Are you sure you want to delete this " + type + "?";
		}

		return confirm(message);
	});


	// Post-login pre-register checkbox
	$("input#pre-register").click(function(){
		$("#preregister-hide").slideToggle();
	});

	$("a#pre-register").click(function(){
		$("#preregister-hide").slideDown();

		$(this).fadeTo("normal", 0.3);
		return false;
	});
	
	
	/******* MINI-GRANT SHOW MODE *********/
	$('#mini_hear.just_text').replaceWith($('#mini_hear.just_text :selected').text());
	$('#mini_adult.just_text').replaceWith($('#mini_adult.just_text :selected').text());
	$('#mini_recruit.just_text').replaceWith($('#mini_recruit.just_text').text());
	$('#mini_partners.just_text').replaceWith($('#mini_partners.just_text').text());
	$('#mini_market.just_text').replaceWith($('#mini_market.just_text').text());
	$('#mini_plan.just_text').replaceWith($('#mini_plan.just_text').text());
	$('#mini_empowerment.just_text').replaceWith($('#mini_empowerment.just_text').text());
	$('#mini_impact.just_text').replaceWith($('#mini_impact.just_text').text());
	$('#mini_funds.just_text').replaceWith($('#mini_funds.just_text').text());
	$('input[name=mini_amount].just_text').parent().replaceWith('<td colspan="2">'+$('input[name=mini_amount].just_text:checked').val()+"</td>");
	$('#mini_adult_Fname.just_text').replaceWith($('#mini_adult_Fname.just_text').val());
	$('#mini_adult_Lname.just_text').replaceWith($('#mini_adult_Lname.just_text').val());
	$('#mini_adult_phone.just_text').replaceWith($('#mini_adult_phone.just_text').val());
	$('#mini_adult_email.just_text').replaceWith($('#mini_adult_email.just_text').val());
	$('#mini_money_printed.just_text').replaceWith($('#mini_money_printed.just_text').val());
	$('#mini_money_transportation.just_text').replaceWith($('#mini_money_transportation.just_text').val());
	$('#mini_money_admin.just_text').replaceWith($('#mini_money_admin.just_text').val());
	$('#mini_money_art.just_text').replaceWith($('#mini_money_art.just_text').val());
	$('#mini_money_ads.just_text').replaceWith($('#mini_money_ads.just_text').val());
	$('#mini_money_refreshments.just_text').replaceWith($('#mini_money_refreshments.just_text').val());
	$('#mini_money_recognition.just_text').replaceWith($('#mini_money_recognition.just_text').val());
	$('#mini_money_printed_description.just_text').replaceWith($('#mini_money_printed_description.just_text').val());
	$('#mini_money_transportation_description.just_text').replaceWith($('#mini_money_transportation_description.just_text').val());
	$('#mini_money_admin_description.just_text').replaceWith($('#mini_money_admin_description.just_text').val());
	$('#mini_money_art_description.just_text').replaceWith($('#mini_money_art_description.just_text').val());
	$('#mini_money_ads_description.just_text').replaceWith($('#mini_money_ads_description.just_text').val());
	$('#mini_money_refreshments_description.just_text').replaceWith($('#mini_money_refreshments_description.just_text').val());
	$('#mini_money_recognition_description.just_text').replaceWith($('#mini_money_recognition_description.just_text').val());
	
	/******* TOGGLE SPECIAL GUESTS INPUT ********/
	if($('input[name=any_special_guests]:checked').val() == "Y"){
		$('#special_guests').show();
	}else{
		$('#special_guests').hide();
		$('#special_guests').val("N/A");
	}
	
	$('input[name=any_special_guests]').change(function(){
		if (this.value == 'Y') {
			$('#special_guests').val("");
			$('#special_guests').show();
		}else{
			$('#special_guests').hide();
			$('#special_guests').val("N/A");
		}
	});
	
	/******* VALIDATE TOTAL EXPENSES ********/
	var total_expenses_ids = ["printed", "transportation", "admin", "art", "ads", "refreshments", "recognition"];
	
	$('input[name=mini_amount]').change(function(){
		selected_amount = $(this).val();
	});
	
	$.each(total_expenses_ids, function() {
		var amount_id = $("#mini_money_" + this);
		var amount_description = $("#mini_money_" + this + "_description");
		if(amount_id.val() != 0 && $(amount_description).val() == "N/A"){
			$(amount_description).val("");
		}
		
		$(amount_id).change(function(){
			if(this.value != 0 && $(amount_description).val() == "N/A"){
				$(amount_description).val("");
			}else if(this.value == 0){
				$(amount_description).val("N/A");
			}
		});
		
	});
	
	if($("input#mini_understand:checked").length == 1){
		$("input#submit_minigrant").removeAttr('disabled');
	}else if ($("input#mini_understand:checked").length != 1) {
		$("input#submit_minigrant").attr('disabled', 'disabled');
	}
	
	$("input#mini_understand").click(function() {
		if($("input#mini_understand:checked").length != 1){
			$("input#submit_minigrant").attr('disabled', 'disabled');
		}else if($("input#mini_understand:checked").length == 1){
			$("input#submit_minigrant").removeAttr('disabled');
		}
	});
	
	// Address field country switcher
	if (document.getElementById("country")) {
		$("select#country").change(function(){

			if (this.value == 'USA') {
				$("div#address-other:visible").slideUp();
				$("div#address-canada:visible").slideUp();
				$("div#address-usa:hidden").slideDown();

			} else if (this.value == 'Canada') {
				$("div#address-other:visible").slideUp();
				$("div#address-canada:hidden").slideDown();
				$("div#address-usa:visible").slideUp();

			} else {
				$("div#address-other:hidden").slideDown();
				$("div#address-canada:visible").slideUp();
				$("div#address-usa:visible").slideUp();
			}
		});

		$("select#country").click(function(){ $(this).change(); }).click();
	}


	// Start/end time fields
	if (document.getElementById("start_time_fields")) {

		$("input#start_time_known").change(function(){
			start_time_enable();
		}).click(function(){ $(this).change(); });

		$("input#end_time_known").change(function(){
			end_time_enable();
		}).click(function(){ $(this).change(); });


		start_time_enable();
		end_time_enable();
	}


	// Login overlay
	$("a[rel=login]").click(function(){
		$("body").append('<div id="overlay-cover"></div><div id="overlay-body"></div>');

		// Set overlay type and destination
		var dest = '';

		if ($(this).attr("href").indexOf('/register/guide/') >= 0) {
			dest = '?dest=guide';
		} else if ($(this).attr("href").indexOf('/register/events/') >= 0) {
			dest = '?dest=event';
		}

		$("#overlay-body").load("/register/login-overlay.php" + dest, null, function(){

			// Close overlay link
			$("#overlay #close").click(function(){
				$("#overlay-cover, #overlay-body, #overlay").remove();
				return false;
			});

		});
		return false;
	});


	// General overlay
	$("a[rel=overlay]").click(function(){
		$("body").append('<div id="overlay-cover"></div><div id="overlay-body"></div>');

		$("#overlay-body").load(this.href);
		return false;
	});


	// Clear placeholder default values of Twitter and Facebook URL
	if (document.getElementById("facebook_url")) {
		var $field = $("#facebook_url");

		if ($field.val() == "http://facebook.com/") {
			$field.addClass("placeholder");
		}

		$field.focus(function(){
			if ($(this).val() == "http://facebook.com/") {
				$(this).removeClass("placeholder").val("");
			}
		}).blur(function(){
			if ($(this).val() == "") {
				$(this).addClass("placeholder").val("http://facebook.com/");
			}
		});
	}

	if (document.getElementById("twitter_url")) {
		$field = $("#twitter_url");

		if ($field.val() == "http://twitter.com/") {
			$field.addClass("placeholder");
		}

		$field.focus(function(){
			if ($(this).val() == "http://twitter.com/") {
				$(this).removeClass("placeholder").val("");
			}
		}).blur(function(){
			if ($(this).val() == "") {
				$(this).addClass("placeholder").val("http://twitter.com/");
			}
		});
	}


	// Collapsible event descriptions
	$("#events").find("a.collapsible").click(function(){
		$(this).siblings("span.description").slideToggle("fast");
		$(this).toggleClass("expanded");
		return false;
	});

	$("#expandevents").toggle(function(){
		$("#events").find("a.collapsible")
			.siblings("span.description").slideDown("fast")
			.end().addClass("expanded");

		$(this).html("Hide All");
		return false;

	}, function(){
		$("#events").find("a.collapsible")
			.siblings("span.description").slideUp("fast")
			.end().removeClass("expanded");

		$(this).html("Expand All");
		return false;

	});


	// Country maps select redirect
	$("select.redirect").bind("change", function(){
		if (this.value != "") {
			location.href = this.value;
		}
	});
	
	if (document.getElementById("mini_adult") && document.getElementById("mini_adult").value == '1') {
		$('#adult_info').show();
		$('#adult_info td.label').addClass('required');
	}
	
	if (document.getElementById("mini_adult")) {
		$("select#mini_adult").change(function(){
			if (this.value == '1') {
				$('#adult_info input').attr('value','');
				$('#adult_info').show();
				$('#adult_info td.label').addClass('required');
			}
			
			if (this.value != '1') {
				$('#adult_info').hide();
				$('#adult_info input').attr('value','N/A');
				$('#adult_info td.label').removeClass('required');
			}
		})}

/*
	// Country map tabs
	$("#countryswitcher").find("ul.tabs>li>a").each(function(){

		var newmap = this;

		// Hide maps onload
		if ($(this).is(":not(.active)")) {
			$(this.hash).hide();
		}

		// Switch sections
		$(this).click(function(){
			if ($(this).is(":not(.active)")) {

				// Switch tab's content
				var $oldmap = $(this).parent("li").parent("ul").find("a.active");

				$($oldmap.attr("href")).fadeOut("fast", function(){
					$(newmap.hash).fadeIn("fast");
				});

				// Switch active tab
				$oldmap.removeClass("active");
				$(this).addClass("active");
			}

			return false;
		});

	});
*/


	// IE6 helper functions
	if ($.browser.msie && $.browser.version < 7) {

		// PNG fix
		iepngfix();
	}
});



function start_time_enable(){
	if ($("input#start_time_known").is(":checked")) {
		$("#start_time_fields")
			.removeClass("disabled")
			.find("select, input").removeAttr("disabled");
	} else {
		$("#start_time_fields")
			.addClass("disabled")
			.find("select, input").attr("disabled", "disabled");
	}
}

function end_time_enable(){
	if ($("input#end_time_known").is(":checked")) {
		$("#end_time_fields")
			.removeClass("disabled")
			.find("select, input").removeAttr("disabled");
	} else {
		$("#end_time_fields")
			.addClass("disabled")
			.find("select, input").attr("disabled", "disabled");
	}
}


/* --- IE6 foreground PNG fix --- */
function iepngfix() {

	// IE 5.5 and 6.0 PNG filter support (derived from youngpup.net)
	$("img[src$=png]").each(function(){
		var src = this.src;
		var div = document.createElement("div");

		// Set replacement div properties
		div.id = this.id;
		div.className = this.className;
		div.title = this.title || this.alt;
		div.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + this.src + "', sizing='scale')";
		div.style.width = this.width + "px";
		div.style.height = this.height + "px";

		// Replace image with transparent div
		this.replaceNode(div);
	});
}
