$(document).ready(function()
{
	//-------------------------------
	// tweaks
	//-------------------------------
	
	$("div#main-navigation > ul#nav > li:first").addClass("first");
	$("div#main-navigation > ul#nav > li > a > span").wrap('<span class="right"></span>');
	$("div#main-navigation > ul#nav").append($("div#main-navigation li.search-form"));
	$("div#main-navigation > ul#search-form-ul").remove();
	$("div#main-navigation > ul#nav").attr("id", "");


	$("div#secondary-navigation ul li:first").addClass("first");
	$("div#secondary-navigation ul li ul").addClass("child").parent().addClass("parent");
	$("div#secondary-navigation ul li.parent-here, div#secondary-navigation ul li.here").addClass("active");
	$("div#secondary-navigation ul li a").each(function() {
		if ($(this).html() == "Confirmation") {$(this).parent().remove();}
	});

	$("div.pagination1 strong, div.pagination2 strong").each(function() {
		$(this).html("<a href='#' class='active' onclick='return false;'>"+$(this).html()+"</a>");
	});
	$("div.pagination1 a, div.pagination2 a").each(function() {
		if ($(this).html() == "&lt;") {
			$(this).html("&lsaquo; Previous");
		}
		if ($(this).html() == "&gt;") {
			$(this).html("Next &rsaquo;");
		}
		$(this).html("<span>"+$(this).html()+"</span>");
	});

	$("h3.requirement").next("ul").addClass("list12");

	var this_month = false;
	$("#calendar-view-tab-content td.day").each(function() {
		if (!this_month && $(this).attr("id") > 1) {$(this).addClass("other-month");}
		else if (!this_month && $(this).attr("id") == 1) {
			this_month = true;
		}
		else if (this_month && $(this).attr("id") == 1) {
			$(this).addClass("other-month");
			this_month = false;
		}
	});


	$(".pop-up").each(function() {
		$(this).hide();
		$("#container").after($(this));
	});

	if ($(".pop-up").length == 1) {
		if ($(".tooltip-trigger-right").length == 0) {
			$(".metadata-bottom .pagination").prepend('<a href="#" onclick="return false;"> <span class="previous" style="color: #C8C8C8;"> &lt; PREV</span></a>');
		}
		else if ($(".tooltip-trigger-left").length == 0) {
			$(".metadata-bottom .pagination").append('<a href="#" onclick="return false;"> <span class="next" style="color: #C8C8C8;"> NEXT &gt;</span></a>');
		}
	}


	//-------------------------------
	// form validation
	//-------------------------------

	$("form.validated").submit(function() {
		var errors = false;
		$(this).find("input.required, textarea.required").each(function() {
			if ($(this).val() == "") {
				errors = true;
			}
		});
		if (errors) {
			alert("Please enter all of the fields in order to submit this form");
			return false;
		}

		// add segments to retURL
		if ($(this).hasClass("salesforce")) {
			var retURL = $(this).find("input[name=retURL]").val();
			retURL += "/submitted";
			retURL += "/" + $(this).find("input[name=first_name]").val();
			retURL += "/" + $(this).find("input[name=last_name]").val();
			retURL += "/" + $(this).find("input[name=company]").val();
			$(this).find("input[name=retURL]").val(retURL);
		}
	});


	//-------------------------------
	// careers filter
	//-------------------------------

	$("div#filter a").click(function() {
		$("div#filter li").removeClass("active");
		$(this).parent().addClass("active");

		var filter = $(this).attr("href").replace("#", "");
		if (filter) {
			$("#careers li").hide();
			$("#careers li."+filter).show();
		}
		else {
			$("#careers li").show();
		}

		return false;
	});


	//-------------------------------
	// entry image frames
	//-------------------------------

	$("div.post div.entry p img").each(function() {
		if (!$(this).parent().hasClass("frame")) {$(this).addClass("framed");}
	});


	//-------------------------------
	// management image classes
	//-------------------------------

	$("div.management-team-page img.no").addClass("off");
	$("div.management-team-page img.yes").addClass("on");


	//-------------------------------
	// videos
	//-------------------------------

	var video_count = 1;
	$("object").each(function() {
		var video = $(this).find("embed").attr("src");
		if (video && video.substr(-3) != "swf") {
			var height = $(this).find("embed").attr("height") ? $(this).find("embed").attr("height") : 350;
			var width = $(this).find("embed").attr("width") ? $(this).find("embed").attr("width") : 500;
			$(this).replaceWith('<div id="video'+video_count+'"></div>');
			jwplayer("video"+video_count).setup({
				skin: "http://www.cdgnow.com/themes/mediaplayer/skins/glow.zip",
				stretching: "uniform",
		        	file: video,
		        	height: height,
		        	width: width,
       				modes: [
        			    { type: "flash", src: "http://www.cdgnow.com/themes/mediaplayer/player.swf" },
        			    { type: "html5" },
        			    { type: "download" }
        			]
		    	});
			video_count++;
		}
	});


	//-------------------------------
	// draft feedback form
	//-------------------------------

	$("div#draft_feedback form").submit(function() {
		var draft_history = $(this).find("input#draft_history").val()+"\n";
		draft_history += $(this).find("select#decision").val()+" by "+$(this).find("input#screen_name").val()+" on "+$(this).find("input#current_date").val();
		if ($(this).find("textarea#comment").val()) {
			draft_history += " ["+$(this).find("textarea#comment").val().replace("\n", " ")+"]";
		}
		$(this).find("input#draft_history").val(draft_history);
	});


	//-------------------------------
	// events list
	//-------------------------------

	$("#list-view-tab-content .more-link1").click(function() {
		$("#list-view-tab-content li").show();
		$(this).hide();
		return false;
	});
});
