(function() {
try {
if(!ubk.exists) { throw "ogncObjUndefined"}

//Commonly used functions/helpers
ubk.mission =  {
	
	//This is for logged in user aggragate info
	showStatus: function() {
		// hold off on animation until the whole page is loaded
		$(".fake_loader").fadeOut("slow", function () {
			$(".make_half").fadeIn();
			$(".show_as_bar").progress_to_date();
		});
		
	},
	
	getSurveyPDF: function(){
			var options = new Object();
			options.url = '/downloads/pdf/U_by_KOTEX_Real_Talk_A_Study_On_Vaginal_Health.pdf';
			options.width = 1024;
			options.height = 768;
			ognc.utils.new_window(options);
			ubk.updateMissionFooter('download','pdf','survey_download');
			ognc.tracking.linkTrack('content','survey_pdf_download');
			ognc.tracking.pageTrack('survey_pdf_download');
			
	},
	
	getKitPDF: function(){
			var options = new Object();
			options.url = '/downloads/pdf/kit.pdf';
			options.width = 1024;
			options.height = 768;
			ognc.utils.new_window(options);
			ubk.updateMissionFooter('download','pdf','kit_download');
			ognc.tracking.linkTrack('content','kit_pdf_download');
			ognc.tracking.pageTrack('kit_pdf_download');
	},
	
	initWhos: function() {
		$thumb_strip = $("<div></div>").insertAfter("#whos_doing_what_list").attr("id", "thumb_strip");
		
		$("#whos_doing_what_list").children().each(function (item) {
			$thumb = $(".thumbnail", this).clone().appendTo($thumb_strip).click(function () {
				var offsetHeight = $("#whos_doing_what_wrap").height();
				$(".selected").removeClass("selected");
				$(this).addClass("selected");
				$("#whos_doing_what_list").stop(true).animate({marginTop: -(offsetHeight * item)}, 1500, "easeOutCirc");
				ognc.tracking.linkTrack('content',$(this).attr('alt') + " thumb");
			});
			
			if( item == 0 ) {
				$thumb.addClass("selected");
			}
		});
		$("#whos_doing_what_list").wrap('<div id="whos_doing_what_wrap"></div>')
	}
	
}

} catch(e) { console.log("error: " + e);}
})();

$(document).ready(function () {
//Set Title Colors for addThis
addthis_config.ui_header_background = "#ffda00";
addthis_config.ui_header_color = "#000000";
});

(function ($) {
	$.fn.progress_to_date = function () {
		return this.each(function () {
			var container = this;
			var total = parseInt($(".progress_total", this).text().replace(/,/g, ""));
			var parts =  $(".progress_container", this);
			
			var threshold = 15; // pixels
			var out_of_range = 0;
			var scale_factor = 0; // total amount of extra stuff
			var final_width = $(container).parent().outerWidth()
			
			/*$(container).empty().css({width: "0"});*/
			var tmp_ttl = total;
			$(parts).each( function() {
				var label = parseInt($(".progress_part",this).text().replace(",",""));
				var part_width = (label / total) * final_width;
				if(part_width <= threshold) {
					out_of_range++;
					tmp_ttl -= label;
				}
			});
			
			total = tmp_ttl;
			
			scale_factor = (out_of_range * threshold) / final_width;
			
			parts = $.map(parts, function (item) {
				var label = $(".progress_part",item).text();
				var percent = (label.replace(",","") / total);
				
				var size = ((percent * final_width) > threshold)
					? ( percent - (scale_factor * percent) )
					: (threshold / final_width);
				var id = $(".progress_id",item).text();
				
				return $("<div></div>")
				.css({"width": (size * 100) - (($.browser.msie && ($.browser.version == "7.0" || $.browser.version == "6.0"))?0.16:0) + "%"})// subtract a small amount to account for rounding errors in ie7
				.addClass("progBar_container")
				.append(
					$("<div></div>")
					.addClass("progress_part_bar")
					.addClass(id)
					.data("size", size)
					.append(
						$("<span></span>")
							.addClass("progress_label fix_png")
							.text(label)
					)
				)[0];
			});
		
			$(container).prepend($("<div>").addClass("progress_all").append(parts));
			
			// center labels
			$(".progress_label", container).each(function() {
				var final = ($(this).parent().data("size") * final_width);
				
				var left = .5 - (($(this).outerWidth() / 2 ) / final) 
				
				$(this).css("left", (left * 100) + "%");
			});
			
			$(".progress_all",container).animate({width : final_width}, 3000, "easeOutElastic");
			
		});
	}
})(jQuery);