/* -----------------------------------------------
Title: Global script
Author: Jarid Love, Connor Boyack

Modified: 19 Jan 2011
Modified-by: Chris Coray
Change: Fixing Connor's "check for fn.corner" function
----------------------------------------------- */ 

// Moved into a function for easy use in ajax callbacks
function styleTables() {
	if($(".table_styled").length > 0){
		$(".table_styled").each(function(){
			if($.fn.corner.defaults !== 'undefined'){
				$(this).find("tbody tr:odd").addClass("odd").find("td:first").corner("3px left").parent().find("td:last").corner("3px right");
				$(this).find("th:first").corner("3px left");
				$(this).find("th:last").corner("3px right").addClass("noborder");
			}
			$(this).find("tbody tr:even").addClass("even");
		});
		$(".table_styled tbody tr").each(function(){ 
			$(this).find("td:last").addClass("noborder");
		});
	}
}


$(document).ready(function(){
	//if we're on a thank you page, open the thank you notice
	$("#thankyou").slideDown("slow");
	
	//set up active field for form
	$("#form_wrap input[type='text']").focus(function(){
		$("#form_wrap .active").removeClass("active");
		$(this).addClass("active");
		if($(this).next("span.instructions").length > 0)
			$(this).next("span.instructions").fadeIn();
	});
	$("#form_wrap input[type='text']").blur(function(){
		if($(this).next("span.instructions").length > 0)
			$(this).next("span.instructions").fadeOut();
	});
	
	// $("#form_wrap").corner("5px");
	
	$(".form_submit_hide").addClass("form_submit_hidden");
	$(".form_submit").show(); 
	
	$(".form_submit a").click(function(){
		if($(this).parent().parent().prev().find("form").length > 0){ //we use these buttons on thank you pages where we want them to link to a file/asset, and not a submit a form.
			var submit = true;
			// $(this).parent().parent().prev().find("form").submit();
			if($(this).parent().parent().prev().find("form").attr("action") == "http://now.eloqua.com/e/f2.aspx"){
				if(typeof formCheck != "undefined") //only call the validator function if it's available
					submit = formCheck();
			}
			if(submit){
				createCookie("novell_scr", $("input[name='email']").val(), 365);		
				$(this).parent().parent().prev().find("form").find('input[type="submit"]').click();
			}
			return false;
		} else {
			//default action - prettyPhoto, simple anchor, etc.
		}
	});
	
	$("input[type='checkbox']").parent().addClass("checkbox"); //set up class to override checkbox style
	$(".checkbox input:checked").parent().find("label").addClass("selected"); //any previously selected checkboxes?

	$(".checkboxlist .checkboxlist").each(function(){
		if(($(this).find("input:checked").length == $(this).find("input[type='checkbox']").length) && $(this).find("input:checked").length > 0){ //all children are selected, select parent
			$(this).siblings("label").addClass("selected").siblings("input").attr("checked", "checked");
		}
		else if(($(this).find("input:checked").length != $(this).find("input[type='checkbox']").length) && $(this).find("input:checked").length > 0){ //partial
			$(this).siblings("label").addClass("partial").siblings("input").attr("checked","");
		}
		else{
			$(this).siblings("label").removeClass().siblings("input").attr("checked","");
		}
	});

	$(".pseudo_checkboxlist .pseudo_checkboxlist").each(function(){
		if(($(this).find("a.selected").length == $(this).find("a").length) && $(this).find("a.selected").length > 0){ //all children are selected, select parent
			$(this).prev().addClass("selected");
		}
		else if(($(this).find("a.selected").length != $(this).find("a").length) && $(this).find("a.selected").length > 0){ //partial
			$(this).prev().addClass("partial");
		}
		else{
			$(this).prev().removeClass();
		}
	});

	$(".checkbox label").click(function(event) { //override user action
		event.preventDefault();
		//handle the children first before changing the actual label/input they clicked
		if($(this).siblings("ul").find("input[type='checkbox']").length > 0){
			if($(this).hasClass("partial")){
				$(this).removeClass("partial").siblings("ul").find("input[type='checkbox']").each(function(){
					$(this).attr("checked", "checked").siblings("label").addClass("selected");
				});
			}
			else if($(this).hasClass("selected")){
				$(this).siblings("ul").find("input[type='checkbox']").each(function(){
					$(this).attr("checked", "").siblings("label").removeClass("selected");
				});
			}
			else{
				$(this).siblings("ul").find("input[type='checkbox']").each(function(){
					$(this).attr("checked", "checked").siblings("label").addClass("selected");
				});
			}
		}

		//handle the actual label/input they clicked
		if($(this).hasClass("partial")){ //unselect
			$(this).removeClass("partial").addClass("selected").siblings("input").attr("checked","checked");
		}
		else if($(this).hasClass("selected")){ //unselect
			$(this).removeClass("selected").siblings("input").attr("checked","");
		}
		else{
			$(this).addClass("selected").siblings("input").attr("checked","checked");
		}
		
		//if a child was clicked, handle its parent
		if($(this).closest("ul.checkboxlist").find(":checked").length < 1){
			//no selected children
			$(this).closest("ul.checkboxlist").siblings("label").removeClass().siblings("input").attr("checked","");
		}
		else if($(this).closest("ul.checkboxlist").find(":checked").length == $(this).closest("ul.checkboxlist").find("input[type='checkbox']").length){
			//all children are selected
			$(this).closest("ul.checkboxlist").siblings("label").removeClass("partial").addClass("selected").siblings("input").attr("checked","checked");
		}
		else{
			//it's a partial
			$(this).closest("ul.checkboxlist").siblings("label").removeClass("selected").addClass("partial").siblings("input").attr("checked","");
		}
	});
	
	//collapse any subgroups, add the toggle
	$(".checkboxlist .checkboxlist").each(function(){
		$(this).parent().parent().addClass("checkboxlist_toggle");
	    $(this).parent().prepend("<a href='' class='checkboxtoggle'></a>").find("a.checkboxtoggle").click(function(){
	        $(this).siblings("ul.checkboxlist").toggle();
	        $(this).toggleClass("checkboxtoggle_open");
	        return false;
	    });
	});
	$(".pseudo_checkboxlist .pseudo_checkboxlist").each(function(){
		$(this).parent().parent().addClass("checkboxlist_toggle");
	    $(this).parent().prepend("<a href='' class='checkboxtoggle'></a>").find("a.checkboxtoggle").click(function(){
	        $(this).siblings("ul.pseudo_checkboxlist").toggle();
	        $(this).toggleClass("checkboxtoggle_open");
	        return false;
	    });
	});

	$("form[action='http://now.eloqua.com/e/f2.aspx']").submit(function(){ //currently have this working for Eloqua forms only. may later abstract to all forms (including RAC?)
		createCookie("novell_scr", $("input[name='email']").val(), 365);		
		if(typeof formCheck != "undefined") //only call the validator function if it's available
			return formCheck();
		else
			return true;
	});
	
	if(window.location.protocol == 'https:')
		$("body").addClass("secure");

	// $("#form_wrap input[type='text']:first").focus(); //auto-focus first form field element
	
	if(getQueryParm("color") != null)
		$("body").addClass(getQueryParm("color"));

		if($.fn.corner.defaults !== 'undefined'){
			$("#modules h3, .modules h3").corner("3px top");
			$(".module").corner("3px bottom");
		}
	
	$("#modules h3 a").each(function(){
		if(($(this).attr("href") != "#") && $(this).attr("href") != ""){
			$(this).addClass("plus");
		}
		else{
			$(this).click(function(){ return false; }); //don't go to the URL since there isn't one
		}
	});
	
	$(".module").each(function(){ 
		var hidden = $(this).find(".story:hidden").length;
		if(hidden > 0){ //activate the add button
			$(this).find(".story:last").after('<div class="add"><a href="">&nbsp;</a></div>');
			$(this).find(".add a").click(function(){
				if(hidden > 1){
					$(this).parent().parent().find(".story:hidden").eq(0).fadeIn();
					hidden--;
				}
				else{
					//add latest, remove border, then destroy .add
					$(this).parent().parent().find(".story:hidden").eq(0).fadeIn().css("background", "none");
					$(this).parent().remove();
				}
				return false;
			});
		}
	});
	$("#awards li:last").addClass("last"); 
	$("#news .story:last").addClass("last"); 
	$("#community tr:last td").css("border","none");
	
	if($("#p4").length > 0){
		$(".requestcall_button").remove();
	}
	
	styleTables();
	
	if($("a[rel^='prettyPhoto']").length > 0) {
		$("a[rel^='prettyPhoto']").prettyPhoto();
	}
	
	$(".rac").each(function(){
		$(this).click(function(){
			requestCall();
			return false;
		});
	});
	
	
	/*
	NOTE: The qtip() functionality breaks with jquery 1.4.2 (works on 1.4.1 AFAIK), so we have to comment it out until they release an updated plugin. We need to be on 1.4.2 because of the Jquery UI Core depending on the 1.4.2 library
	*/
	
	//community tooltips
	// $("#community img").each(function(){
	// 	$(this).qtip({
	// 		content: $(this).attr("alt"),
	// 		show: "mouseover",
	// 		hide: "mouseout",
	// 		position: {
	// 			corner: {
	// 				target: "topMiddle",
	// 				tooltip: "bottomMiddle"
	// 			}
	// 		},
	// 		style: {
	// 			"font-size": "12px",
	// 			background:"#2d2d2d",
	// 			border: {
	// 				color: "#2d2d2d",
	// 				radius: 4
	// 			},
	// 			color: "#fff",
	// 			tip: "bottomMiddle",
	// 			name: "dark"
	// 		}
	// 	});
	// });

	//social link tooltips
		// $("#social a").each(function(){
		// 	$(this).qtip({
		// 		content: $(this).text(),
		// 		show: "mouseover",
		// 		hide: "mouseout",
		// 		position: {
		// 			corner: {
		// 				target: "topMiddle",
		// 				tooltip: "bottomMiddle"
		// 			}
		// 		},
		// 		style: {
		// 			"font-size": "12px",
		// 			background:"#2d2d2d",
		// 			border: {
		// 				color: "#2d2d2d",
		// 				radius: 4
		// 			},
		// 			color: "#fff",
		// 			tip: "bottomMiddle",
		// 			name: "dark"
		// 		}
		// 	});
		// });

		//https://bugzilla.novell.com/show_bug.cgi?id=668364
		$(".promo").each(function(){
			var promo_height = $(this).find(".promo_text").height(); //get the height of the actual content
			var padding = parseInt($(this).find(".promo_text").css("top").replace("px",""))*2; //get the top position, double it to add padding to the bottom
			
			var current_height = parseInt($(this).height());
			
			if(promo_height + padding > current_height)
				$(this).css("height",promo_height+padding+"px"); //manually adjust the size of the box
		});
	
});

