//This library contains a number of useful tools related to enriching E4 UI

var hover_active = 0;

function showHoverMessage(e) {
	if (hover_active == 0) {
		var posx = 0;
		var posy = 0;
		if (!e)
			var e = window.event;
		if (e.pageX || e.pageY) {
			posx = e.pageX;
			posy = e.pageY;
		} else if (e.clientX || e.clientY) {
			posx = e.clientX + document.body.scrollLeft
					+ document.documentElement.scrollLeft;
			posy = e.clientY + document.body.scrollTop
					+ document.documentElement.scrollTop;
		}
		width = 308;
		height = 25;

		if (parseInt(navigator.appVersion) > 3) {
			if (navigator.appName == "Netscape") {
				var winW = window.innerWidth;
				var winH = window.innerHeight;
			}
			if (navigator.appName.indexOf("Microsoft") != -1) {
				var winW = document.body.offsetWidth;
				var winH = document.body.offsetHeight;
			}
		}
		if (winW-30 < posx + width + 5) {
			posx = posx - width - 5;
		}
		if (winH-30 < posy + height + 5) {
			posy = posy - height - 26;
		}

		showHover(
				'An issue with this question has already been reported and is currently under review by the WileyPLUS Team',
				height, width, posx, posy);
	}
}

function showHover() {
	var text_copy = arguments[0];
	var height = arguments[1];
	var width = arguments[2];
	var top = arguments[4] + 5;
	var left = arguments[3] + 5;
	var the_hover = "<div id='hover_popup' style='width: "
			+ width
			+ "px; height: "
			+ height
			+ "px; position: absolute; top: "
			+ top
			+ "px; left: "
			+ left
			+ "px; border: 1px solid black; background: #ffff99; padding: 3px;'>"
			+ text_copy + "</div>";
	var body_elt = document.getElementsByTagName("body");
	jQuery("body").append(the_hover);
	// body_elt.innerHtml = body_elt.innerHtml + the_hover;
	hover_active = 1;
}

function hideHover() {
	hover_active = 0;
	var body_elt = document.getElementsByTagName("body");
	var hover = document.getElementById("hover_popup");
	// body_elt.removeChild(hover);
	jQuery("#hover_popup").remove();
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return [ curleft, curtop ];
}
var msg_count = 0;
var file_count = 0;
var link_count = 0;

function process_array(ar) {
	msg_count = ar.length;
	if (ar.length != 0) {
		for (var i=0;i<ar.length;i++) {
			file_count = ar[i].file_count;
			link_count = ar[i].link_count;
			shorten(ar[i].message,ar[i].id);
		}
	}
}

function shorten(msg, id){
	var full_heading = stripXmlHTML($('#'+id+'-full-announcement-heading').html());
	$('#'+id+'-full-announcement-heading').html(full_heading)
	var short_heading = stripXmlHTML($('#'+id+'-short-announcement-heading').html());
	$('#'+id+'-short-announcement-heading').html(short_heading);
	
	$('#'+id+'-full-announcement-heading').hide();
	$('#'+id+'-short-announcement-heading').show();
	$('.'+id+' #'+id+'-full-body-container #lessener').remove();
	$('.'+id+' #'+id+'-short-body-container').remove();
	$('.'+id+' #'+id+'-full-body-container').hide();
	$('.'+id+' #'+id+'-file-container').hide();
	$('.'+id+' #'+id+'-link-container').hide();
	msg = stripHTML(msg);
	if (msg_count == 1) {
		if (msg.length > 80) {
			var shorter_str = msg.substr(0,80)+"..."+' <a href="javascript:void(0);" onclick="javascript:show_full(\''+id+'\');">More&#160;&#187;</a>';
				$('.'+id).html("<div id='"+id+"-short-body-container' class='short-body-container'>"+shorter_str+"</div>"+$('.'+id).html());	
				$('.'+id+' #'+id+'-full-body-container').hide();
				$('.'+id+' #'+id+'-file-container').hide();
				$('.'+id+' #'+id+'-link-container').hide();
		} else {
		   if(msg.length <= 80 && (file_count>0 || link_count>0) ) {
				var shorter_str = msg+' <a href="javascript:void(0);" onclick="javascript:show_full(\''+id+'\');">More&#160;&#187;</a>';
					$('.'+id).html("<div id='"+id+"-short-body-container' class='short-body-container'>"+shorter_str+"</div>"+$('.'+id).html());	
					$('.'+id+' #'+id+'-file-container').hide();
					$('.'+id+' #'+id+'-link-container').hide();
		   } else {
				var full_unescaped = tagify($('.'+id+' #'+id+'-full-body-container').html());
				$('.'+id+' #'+id+'-full-body-container').html(full_unescaped);			   
		   		$('.'+id+' #'+id+'-full-body-container').show();
		   }
		}
	}
	
	if (msg_count == 2) {
		if (msg.length > 35) {
			var shorter_str = msg.substr(0,32)+"..."+' <a href="javascript:void(0);" onclick="javascript:show_full(\''+id+'\');">More&#160;&#187;</a>';
				$('.'+id).html("<div id='"+id+"-short-body-container' class='short-body-container'>"+shorter_str+"</div>"+$('.'+id).html());				
				$('.'+id+' #'+id+'-full-body-container').hide();
				$('.'+id+' #'+id+'-file-container').hide();
				$('.'+id+' #'+id+'-link-container').hide();
		} else {
		   if(msg.length <= 35 && (file_count>0 || link_count>0) ) {
				var shorter_str = msg+' <a href="javascript:void(0);" onclick="javascript:show_full(\''+id+'\');">More&#160;&#187;</a>';	
				$('.'+id).html("<div id='"+id+"-short-body-container' class='short-body-container'>"+shorter_str+"</div>"+$('.'+id).html());				
				$('.'+id+' #'+id+'-file-container').hide();
				$('.'+id+' #'+id+'-link-container').hide();
		   } else {
				var full_unescaped = tagify($('.'+id+' #'+id+'-full-body-container').html());
				$('.'+id+' #'+id+'-full-body-container').html(full_unescaped);			   
		   		$('.'+id+' #'+id+'-full-body-container').show();
		   }
		}		
	}
}

function show_full(id) {
	$('#'+id+'-short-body-container').hide();
	$('#'+id+'-short-announcement-heading').hide();
	var full_msg = fixquotes($("."+id+" #"+id+"-escaped-body-container").html());
	var lessener = ' <a id="lessener" href="javascript:void(0)" onclick=\"javascript:shorten(\''+full_msg+'\',\''+id+'\')\">Less&#160;&#171;</a>';
	var full_unescaped = tagify($('.'+id+' #'+id+'-full-body-container').html());
	$('.'+id+' #'+id+'-full-body-container').html(full_unescaped);
	$('.'+id+' #'+id+'-full-body-container').append(lessener);
	$('.'+id+' #'+id+'-full-body-container').show();
	$('#'+id+'-full-announcement-heading').show();
	$('.'+id+' #'+id+'-file-container').show();
	$('.'+id+' #'+id+'-link-container').show();		
}

function tagify(inp) {
	var ary = inp.split("&lt;");
	var out = ary.join("<");
	ary = out.split("&gt;");
	out = ary.join(">");
	return out;
}

function filterTagBrackets(input) {
	var filter_tmp = input.split(">");
	var filter_str = filter_tmp.join(")");
	filter_tmp = filter_str.split("<");
	filter_str = filter_tmp.join("(");
	return filter_str;
}

function fixquotes(inp) {
	var ary = inp.split('"');
	var out = ary.join("'");
	return out;
}

function stripHTML(inp){
	var re= /<\S[^><]*>/g
	var out=inp.replace(re, "");
	return out;
}

function stripXmlHTML(inp){
	var ary = inp.split('&lt;');
	var out = ary.join("<");
	ary = out.split("&gt;");
	out = ary.join(">");	
	return stripHTML(out);
}

function announcement_obj(msg,id, fcount, lcount) {
	this.id = id;
	this.message = msg;
	this.file_count = fcount;
	this.link_count = lcount;
}

function fix_announcement_height() {
	if($(".middle-homepage-content .main-content .top-row #right-div .gradient-box #bottom-left").height()<198) {
		$(".middle-homepage-content .main-content .top-row #right-div .gradient-box #bottom-left").height(199);
	}
}

function injectCss(browser_string, web_root) {
	var ind = browser_string.lastIndexOf("MSIE");
	if (ind != -1) { 
		var the_browser = browser_string.substr(ind, 8);
		if (the_browser == "MSIE 6.0") {
			$.ajax({
			url: web_root+'/css/ie6_styles.css',
			type: "GET",
			async: false,
			success: function (msg) {
				$('head').append('<style type="text/css">'+msg+'</style>');
			}
			});
		} else {
			$.ajax({
				url: web_root+'/css/ie7_styles.css',
				type: "GET",
				async: false,
				success: function (msg) {
					$('head').append('<style type="text/css">'+msg+'</style>');
				}
				});			
		}
//IE version/version emulation check. Currently disabled.		
/*		if (the_browser == "MSIE 7.0") {
			if (browser_string.lastIndexOf("Trident")==-1) {
				$.ajax({
				url: web_root+'/css/ie7_styles.css',
				type: "GET",
				async: false,
				success: function (msg) {
					$('head').append('<style type="text/css">'+msg+'</style>');
				}
				});
			}
		}
		if (the_browser == "MSIE 8.0") {
				$.ajax({
				url: web_root+'/css/ie8_styles.css',
				type: "GET",
				async: false,
				success: function (msg) {
					$('head').append('<style type="text/css">'+msg+'</style>');
				}
				});
		}*/		
		
	}
}

function injectLoadingWindow(root) {
	//Below is the html with inline css for the appearing popup window
	if (jQuery.browser.msie) {
			if (arguments[1] != null) {		
				var loader_div = "<div id='loader-div' style='background: none repeat scroll 0% 0% black; position: absolute; left: 50%; top: "+arguments[1]+"px; margin-left: -100px; margin-top: -50px; width: 200px; height: 100px; float: right; z-index:10;' >&nbsp;</div><div id='loader-div-ie' style='background: none repeat scroll 0% 0% rgb(153, 153, 153); position: absolute; left: 50%; top: "+arguments[1]+"px; margin-left: -103px; margin-top: -53px; width: 200px; height: 100px; float: right; z-index:1000;'><div style='background: none repeat scroll 0% 0% rgb(255, 255, 153); margin: 5px; width: 190px; height: 90px;'><div style='background: none repeat scroll 0% 0% rgb(255, 255, 225); margin: 8px 5px 5px 5px; width: 180px; height: 75px; border: 1px solid #999999; text-align: center; padding-top: 2px; float: right;'><b>"+arguments[2]+"</b><br /><img src='"+root+"/art2/graphics/ajax-loader.gif' alt='Please, wait... Loading' style='margin: 5px 0;' /><br />Please, wait...</div></div>";
			} else {
				var loader_div = "<div id='loader-div' style='background: none repeat scroll 0% 0% black; position: absolute; left: 50%; top: 615px; margin-left: -100px; margin-top: -50px; width: 200px; height: 100px; float: right; z-index:10;' >&nbsp;</div><div id='loader-div-ie' style='background: none repeat scroll 0% 0% rgb(153, 153, 153); position: absolute; left: 50%; top: 615px; margin-left: -103px; margin-top: -53px; width: 200px; height: 100px; float: right; z-index:1000;'><div style='background: none repeat scroll 0% 0% rgb(255, 255, 153); margin: 5px; width: 190px; height: 90px;'><div style='background: none repeat scroll 0% 0% rgb(255, 255, 225); margin: 8px 5px 5px 5px; width: 180px; height: 75px; border: 1px solid #999999; text-align: center; padding-top: 2px; float: right;'><b>Exporting grades</b><br /><img src='"+root+"/art2/graphics/ajax-loader.gif' alt='Please, wait... Loading' style='margin: 5px 0;' /><br />Please, wait...</div></div>";				
			}
		} else {
			if (arguments[1] != null) {
				var loader_div = "<div id='loader-div' style='background: none repeat scroll 0% 0% black; position: absolute; left: 50%; top: "+arguments[1]+"px; margin-left: -100px; margin-top: -50px; width: 200px; height: 100px; float: right;' ><div style='background: none repeat scroll 0% 0% rgb(153, 153, 153); margin-right: 3px; margin-top: -3px; width: 200px; height: 100px; float: right;'><div style='background: none repeat scroll 0% 0% rgb(255, 255, 153); margin: 5px; width: 190px; height: 90px;'><div style='background: none repeat scroll 0% 0% rgb(255, 255, 225); margin: 4px; width: 180px; height: 75px; text-align: center; border: 1px solid #999999; padding-top: 5px; float: right;'><b>"+arguments[2]+"</b><br /><img src='"+root+"/art2/graphics/ajax-loader.gif' alt='Please, wait... Loading' style='margin: 5px 0;' /><br />Please, wait...</div></div></div>";
			} else {
				var loader_div = "<div id='loader-div' style='background: none repeat scroll 0% 0% black; position: absolute; left: 50%; top: 615px; margin-left: -100px; margin-top: -50px; width: 200px; height: 100px; float: right;' ><div style='background: none repeat scroll 0% 0% rgb(153, 153, 153); margin-right: 3px; margin-top: -3px; width: 200px; height: 100px; float: right;'><div style='background: none repeat scroll 0% 0% rgb(255, 255, 153); margin: 5px; width: 190px; height: 90px;'><div style='background: none repeat scroll 0% 0% rgb(255, 255, 225); margin: 4px; width: 180px; height: 75px; text-align: center; border: 1px solid #999999; padding-top: 5px; float: right;'><b>Exporting grades</b><br /><img src='"+root+"/art2/graphics/ajax-loader.gif' alt='Please, wait... Loading' style='margin: 5px 0;' /><br />Please, wait...</div></div></div>";
			}
		}
	var dct = this.parent.parent;
	if (dct == null) {
		dct = this;
	} 
	
	//alert("Top level: "+this.name);
	//alert("Top name: "+top.name);
	//while (dct.opener != null && self!=dct.opener) {
	//	alert("Lower level: "+this.name);
	//	dct = dct.opener;
	//}	
	//var dct = top;
	//while (dct.opener != null && self!=dct.opener) {
	//	dct = dct.opener;
	//}
	jQuery('body',dct.document).append(loader_div);
	jQuery('#view-graph-btn').html('<img border="0" alt="View Graph" width="76" height="16" hspace="4" name="viewgraph" src="'+root+'/art2/btn/type1/viewgraph-grey.gif"/>');
	jQuery('#export-btn').html('<img border="0" alt="Export Grades" width="90" height="16" hspace="4" name="export" src="'+root+'/art2/btn/type1/exportgrades-grey.gif"/>');
	
}

function exportRequest(root, dmn, cls, usr, ast) {
	injectLoadingWindow(root);
	var dct = this.parent.parent;
	//var dct = top;
	//while (dct.opener != null && self!=dct.opener) {
	//	dct = dct.opener;
	//}
	try {
		jQuery.ajax({ url: root+"/roster/export/export_gradebook.csv", 
				 type: "POST",
				 data: "test=test&operation=export-assignment&domain="+dmn+"&class="+cls+"&user="+usr+"&assignment="+ast+"&mode=edugen&download=false", 
				 success: function(m){
					 jQuery("#view-graph-btn").html('<a href="javascript:void();" onclick="javascript:viewGraph(); return false;" onmouseout="javascript:top.status=\'\'; return true;" onmouseover="javascript:top.status=\'View Graph\'; return true;"><img border="0" alt="View Graph" width="76" height="16" hspace="4" name="viewgraph" src="'+root+'/art2/btn/type1/viewgraph.gif"/></a>');
					 jQuery("#export-btn").html('<a href="javascript:void();" onclick="javascript:exportAGResults(); return false;" onmouseout="javascript:top.status=\'\'; return true;"	onmouseover="javascript:top.status=\'Export\'; return true;"><img border="0" alt="Export Grades" width="90" height="16" hspace="4" name="export" src="'+root+'/art2/btn/type1/exportgrades.gif"/></a>');
					 jQuery("#loader-div",dct.document).fadeOut(500);
					 if (jQuery("#loader-div-ie",dct.document).length > 0){
						 jQuery("#loader-div-ie",dct.document).fadeOut(500);
					 }
					 var s_form = document.forms.agExportForm;
					 s_form.submit();
					 jQuery("#loader-div",dct.document).remove();
					 if (jQuery("#loader-div-ie",dct.document).length > 0){
						 jQuery("#loader-div-ie",dct.document).remove();
					 }
		      	 },
		      	 error: function(e) {
		      	 	alert("Error: "+e);
		      	 }
		       });
	} catch  (err) {
		// nothing here, really	
	}
}

var popupActive = 0;
function showFullName(name, id) {
	if (jQuery("#so-name-span-"+id).offset() != null) {
		var x = jQuery("#so-name-span-"+id).offset().left;
		var y = jQuery("#so-name-span-"+id).offset().top;
		var name_tmp = name.split(" ");
		name = name_tmp.join("&nbsp;");
		if (popupActive == 0) {
			popupActive = 1;
			var fullNamePopup = '<div id="fname" onmouseover="javascript:showFullName(\''+name+'\',\''+id+'\')" onmousemove="javascript:showFullName(\''+name+'\',\''+id+'\')" onmouseout="javascript:removeFullName()" style="border:1px solid #000000; background:#FFFFE1; height: 20px; display: block; position: absolute; left:'+x+'px; top:'+y+'px; z-index: 10;"><nowrap>'+name+'</nowrap></div>';
			var clickoverlay = '<div id="fname-overlay" onmouseover="javascript:showFullName(\''+name+'\',\''+id+'\')" onmousemove="javascript:showFullName(\''+name+'\',\''+id+'\')" onmouseout="javascript:removeFullName()" style="float: left; position: absolute; margin-top: -14px; display: block; height: 25px; z-index: 1000;">&nbsp;</div>';
			jQuery("#so-name-span-"+id).append(fullNamePopup);
			jQuery("#fname").append(clickoverlay);
			jQuery("#fname-overlay").css("width",jQuery("#fname").width()+"px");
			if (jQuery.browser.msie) {
				jQuery("#fname-overlay").css("margin-left","-"+jQuery("#fname").width()+"px");
				jQuery("#fname-overlay").css("margin-top","-3px");
			}			
			var centering_offset = (jQuery("#fname").width() - jQuery("#so-name-span-"+id).width()); // /2
			jQuery("#fname").css("left",(x-centering_offset)+"px");
		}
	}
}

function removeFullName() {	
	popupActive = 0;
	jQuery("#fname").remove();	
	jQuery("#fname-overlay").remove();
}

function getParameter(parameterName) {
	   var queryString = window.top.location.search.substring(1);
	   // Add "=" to the parameter name (i.e. parameterName=value)
	   var parameterName = parameterName + "=";
	   if ( queryString.length > 0 ) {
	      // Find the beginning of the string
	      begin = queryString.indexOf ( parameterName );
	      // If the parameter name is not found, skip it, otherwise return the value
	      if ( begin != -1 ) {
	         // Add the length (integer) to the beginning
	         begin += parameterName.length;
	         // Multiple parameters are separated by the "&" sign
	         end = queryString.indexOf ( "&" , begin );
	      if ( end == -1 ) {
	         end = queryString.length
	      }
	      // Return the string
	      return unescape ( queryString.substring ( begin, end ) );
	   }
	   // Return "null" if no parameter has been found
	   return "null";
	   }
}

