(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;
			options.name	= options.name || "ubk_"+(new Date()).getMilliseconds();
			options.replace	= options.replace || false;
			options.width	= options.width || 1024;
			options.height	= options.height || 768;
			options.left	= options.left || (screen.availWidth - options.width) / 2;
			options.top		= options.top || ((screen.availHeight - options.height) / 2) - 40;
			
			var window_features = [];

			window_features.push("width=" + options.width);
			window_features.push("height=" + options.height);
			window_features.push("left=" + options.left);
			window_features.push("top=" + options.top);

			if (options.directories)	{ window_features.push("directories=" + options.directories); }
			if (options.location)		{ window_features.push("location=" + options.location); }
			if (options.menubar)		{ window_features.push("menubar=" + options.menubar); }
			if (options.resizable)		{ window_features.push("resizable=" + options.resizable); }
			if (options.scrollbars)		{ window_features.push("scrollbars=" + options.scrollbars); }
			if (options.status)			{ window_features.push("status=" + options.status); }
			if (options.toolbar)		{ window_features.push("toolbar=" + options.toolbar); }
			
			window.open('/downloads/pdf/u_by_kotex_real_talk_a_study_on_vaginal_health.pdf', options.name, window_features.join(','), options.replace);
			
			if (options.close) window.close();
//			if (options.url.indexOf(':8591')>0)
//			{
//			options.url=options.url.replace(':8591','');
//			window.open(options.url);
//			}
			//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(",","").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);
