/*
	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';
		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;
	});


	// 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;
		}
	});


/*
	// 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);
	});
}