﻿var anchors = document.getElementsByTagName('a');
var pageIsInEditMode = false;

function toggleDiv(div, img) {
    var a = document.getElementById(div);
    if (a.style.display == "none") {
        a.style.display = "block";
        document.getElementById(img).src = "/PublishingImages/minus.png";
    }
    else {
        a.style.display = "none";
        document.getElementById(img).src = "/PublishingImages/plus.png";
    }
}

function IsExternalSite(site)
{
		//if (site=="mycti.cti.depaul.edu" || site == "prerelease.cti.depaul.edu" || site == "clinton.cs.depaul.edu" || site == "securestore.cti.depaul.edu" || site == "authenticate.cti.depaul.edu")
		//	{return true;}
		return false;
}

function IsExternalSiteNoIcon(site)
{
		if (site == "blogs.cti.depaul.edu")
			{return true;}
}

//clean way of disabling the name.dll activex control
function ProcessImn() {}

function ExternalLink(event)
{
	var targ;
	if (!e)
	  {
	  var e=window.event;
	  }
	if (e.target)
	  {
	  targ=e.target;
	  }
	else if (e.srcElement)
	  {
	  targ=e.srcElement;
	  }
	if (targ.nodeType==3) // defeat Safari bug
	  {
	  targ = targ.parentNode;
	  }
	if (IsExternalSite(targ.hostname))
	{
		targ.target = '_blank';
	}
}

function CTIHacks()
{
	FixSearchBar();
	CheckRelatedLinks();
	CheckSidebar();
	DetectEditMode();
	getMaxDivHeight();
	removeStylesFromANames();
	AddPaddingToLeftNav();
	ChangeClassName();
	GetCDMExtendedCourseInfo();
	ToggleCourseList();
	ToggleSection();
	ToggleLSSection();
	GetLegendInfo();
	ToggleTechnicalReportsList();
	$('body').supersleight({shim: '/PublishingImages/x.gif'});
}

function FixSearchBar()
{
	var sb = document.getElementById('ctl00_SearchBox_S6F789EBA_InputKeywords');
	sb.value = 'Search...';
	sb.onfocus = function () {
		sb.value = "";
	};

}

function CheckRelatedLinks()
{
	var rl = document.getElementById('CTISidebarRelatedLinksContent');
	if (rl != null)
	{
		var tmp = rl.innerHTML;
		tmp.replace(/<[^>]*>/,'');
		if (rl.innerHTML == "" || rl.innerHTML == null || rl.innerHTML.length == 2 || tmp.length == 0)
		{
			document.getElementById('CTISidebarRelatedLinksContainer').style.display = 'none';
		}
	}
}
function CheckSidebar()
{
	var tmp = $("#CTISidebarContent").text();
	tmp.replace(/<[^>]*>/,'');
	//tmp.replace(/\s+/,'');
	var tmp2 = $("#CTISidebarTitle").text();
	tmp2.replace(/<[^>]*>/,'');
	//tmp2.replace(/\s+/,'');
	if ($("#CTISidebarTitle").text().length <= 3 || $("#SidebarContent").text().length <= 3 || tmp.length < 1 || tmp2.length < 1)
	{
		$("#CTISidebarTitle").hide();
		$("#SidebarContent").hide();
	}
}
function DetectEditMode()
{
	var x = document.getElementsByTagName('div');
	for (var i=0; i<x.length; i++)
	{
		if (x[i].className == "ms-input")
		{
			pageIsInEditMode = true;
			break;
		}
	}
}

function getMaxDivHeight()
{

	try
	{
		var rightHeight = document.getElementById('ctl00_PageRightColumContainer').offsetHeight;
		var mainHeight = document.getElementById('ctl00_PageBody').offsetHeight;
		if (rightHeight > mainHeight)
		{
			var diff = rightHeight-mainHeight + 120;
			document.getElementById('ctl00_PageFooter').style.paddingTop = diff + 'px';
			
		}
		else if ((mainHeight > rightHeight) && (pageIsInEditMode == false) && (mainHeight < 1200))
		{
			document.getElementById('ctl00_PageRightColumn').style.height = mainHeight - 10 + 'px';
		}
	}
	catch (err) {}
}
			
function removeStylesFromANames()
{
	var anchors = document.getElementsByTagName('a');
	var tmp = '';
	for (var i = 0; i < anchors.length; i++)
	{
		if (anchors[i].href == '')
		{
			anchors[i].style.color = 'black';
			anchors[i].style.textDecoration = 'none';
		}
	}
}

function AddPaddingToLeftNav()
{
	if (document.getElementById('page_left_nav_container').offsetHeight > 220)
	{
		//document.getElementById('page_left_nav').style.paddingBottom = '4px';
	}
}

function ChangeClassName()
{
	for (i = 0; i < anchors.length; i++)
	{
		if(anchors[i].href != 'https://mycti.cti.depaul.edu/cti/advising/login.asp')
		{
			if (IsExternalSite(anchors[i].hostname))
			{
				anchors[i].className = 'external';
				anchors[i].target = '_blank';
			}
			if (IsExternalSiteNoIcon(anchors[i].hostname))
			{
				anchors[i].target= '_blank';
			}
		}
	}
}

/*
function GetCDMExtendedCourseInfo()
{
	$(".CDMExtendedCourseInfo").each(
		function(i)
		{
			$(this).replaceWith(test(this));
		});
}

function test(i)
{
	var retval = this;

	$.get("/legacy/GetCDMCourseInfoProxy.aspx", function(data){
 		 return data;
		});

	//alert($(i).html());
	//return data;	
}
*/
function toggleDivNoImageHook() {
$(".CDMProgramLinks").parent().children('div').hide();
$(".CDMProgramLinks").prepend('<img src = "/PublishingImages/plus.png" border="0" /> ');
$(".CDMProgramLinks").each(
		function(i)
		{
 		 	$(this).click(function(event){
 		 	event.preventDefault();
 		 	//toggleDivNoImage($(this).text());
 		 	$(this).next('div').toggle();

 			 	}
 			);
			}
		);
}
function togglePlusMinus(icon)
{
	if (icon.src)
	{
		if (icon.src == "/PublishingImages/plus.png")
			return '<img src = "/PublishingImages/minus.png" /> ';
		else
			return '<img src = "/PublishingImages/plus.png" /> ';
	}
	else
		return '<img src = "/PublishingImages/plus.png" /> ';
}
function toggleDivNoImage(div)
{
    var a = document.getElementById(div);
    if (a.style.display == "none") {
        a.style.display = "block";
    }
    else {
        a.style.display = "none";
    }

}

function GetCDMExtendedCourseInfo()
{
	$(".CDMExtendedCourseInfo").each(
		function(i)
		{
			getExternalCRSInfo("/legacy/GetCDMCourseInfoProxy.aspx", this);			
		});
}


/*
	Code Sample Taken from 
        3.) Simply Centered Informational DIV Popup
    */

    /*
        The viewport object is mainly for the cross-browser mess
        that is unfortunately necessary to get a DIV to center.

        There is also an officially supported plugin called "Dimensions"
        that can manage some of this on your own projects. Check it out at:

        http://jquery.com/plugins/project/dimensions
    */

    var viewport = {
        o: function() {
            if (self.innerHeight) {
    			this.pageYOffset = self.pageYOffset;
    			this.pageXOffset = self.pageXOffset;
    			this.innerHeight = self.innerHeight;
    			this.innerWidth = self.innerWidth;
    		} else if (document.documentElement && document.documentElement.clientHeight) {
    			this.pageYOffset = document.documentElement.scrollTop;
    			this.pageXOffset = document.documentElement.scrollLeft;
    			this.innerHeight = document.documentElement.clientHeight;
    			this.innerWidth = document.documentElement.clientWidth;
    		} else if (document.body) {
    			this.pageYOffset = document.body.scrollTop;
    			this.pageXOffset = document.body.scrollLeft;
    			this.innerHeight = document.body.clientHeight;
    			this.innerWidth = document.body.clientWidth;
    		}
    		return this;
        },
        init: function(el) {
            $(el).css("left",Math.round(viewport.o().innerWidth/2) + viewport.o().pageXOffset - Math.round($(el).width()/2));
            $(el).css("top",Math.round(viewport.o().innerHeight/2) + viewport.o().pageYOffset - Math.round($(el).height()/2));
        }
    };
    

function getExternalCRSInfo(url,ref)
{
	var val = $(ref).text();
	var vals = val.split(" ");
	var str = "/legacy/GetCDMCourseInfoProxy.aspx?crs="+vals[0] +"&nbr="+vals[1];
	//alert(str);
	$.get(str, function(data){
 		 	//$(ref).replaceWith(data);
 		 	$(ref).empty();
 		 	$(ref).append(data);
 		 	var kids = $(ref).children();
 		 	//$(kids[0]).click(function(){alert("hello!");});
 		 	$(kids[1]).click(function(){
            $(".simple_popup_div").remove();
            var strSimple = "<div class='simple_popup_div'><div class='simple_popup_inner'>";
            strSimple += "<p id='simp_close_x' align='right' class='simple_close'><a href='#'>X</a></p>";
            strSimple += $(this).siblings(".simple_popup_info").html();
            strSimple += "<p align='center' class='simple_close'><a href='#'>CLOSE</a></p></div></div>";
            $("body").append(strSimple);
            viewport.init(".simple_popup_div");
            $(".simple_close").click(function(){
                $(".simple_popup_div").remove();
                return false;
            });
            return false;
        });
	});
}

function GetLegendInfo()
{
	$(".CourseLegend").each(
		function(i)
		{
			getCurrentTermForLegend("/legacy/GetCurrentProgramTerm.aspx", this);			
		});
}

function getCurrentTermForLegend(url,ref)
{
	var str = "/legacy/GetCurrentProgramTerm.aspx";
	$.get(str, function(data){
 		 	var kids = $(ref).children();
 		 	$(kids[0]).append("Displaying " + data);
 		 		});
}


function ToggleCourseList()
{
	$(".collapsedCourseList").children("li").each(
		function(i)
		{
			var a = $(this).children();
			for (var z = 0; z<a.length; z++)
			{
				$(a[z]).hide();
			}
			$(this).click(function()
			{
				
				var b = $(this).children();
				if ($(b[0]).css("display") == "none")
					$(this).css({"list-style-image" : "url('/PublishingImages/minus.png')"});
				else
					$(this).css({"list-style-image" : "url('/PublishingImages/plus.png')"});
				for (var z = 0; z<b.length; z++)
				{
					$(b[z]).toggle();
				}
				return false;
			});
		});
}
function ToggleSection()
{
	$(".collapsedSection").children("li").each(
		function(i)
		{
			var a = $(this).children();
			for (var z = 0; z<a.length; z++)
			{
				$(a[z]).hide();
			}
			$(this).click(function()
			{
				
				var b = $(this).children();
				if ($(b[0]).css("display") == "none")
					$(this).css({"list-style-image" : "url('/PublishingImages/minus.png')"});
				else
					$(this).css({"list-style-image" : "url('/PublishingImages/plus.png')"});
				for (var z = 0; z<b.length; z++)
				{
					$(b[z]).toggle();
				}
				return false;
			});
		});
}

function ToggleLSSection()
{
	$(".collapsedLSGroup").each(
		function(i)
		{
			var a = $(this).children();
			for (var z = 0; z<a.length; z++)
			{
				$(a[z]).hide();
			}
			$(this).click(function()
			{
				var b = $(this).children();
				if ($(b[0]).css("display") == "none")
					$(this).css({"list-style-image" : "url('/PublishingImages/minus.png')"});
				else
					$(this).css({"list-style-image" : "url('/PublishingImages/plus.png')"});
				for (var z = 0; z<b.length; z++)
				{
					$(b[z]).toggle();
				}
				return false;
			});
		});
}
//used for /SoC/research/Pages/TechnicalReports.aspx
function ToggleTechnicalReportsList()
{
	$(".collapsedTechReportList").children('li').each(
		function(i)
		{
			var a = $(this).children('table');
			for (var z = 0; z<a.length; z++)
			{
				$(a[z]).hide();
			}
			$(this).children('span').each(
			function()
			{
				$(this).click(function()
				{
				var b = $(this).siblings('table');
				if ($(b[0]).css("display") == "none")
					$(this).css({"background-image" : "url('/PublishingImages/minus.png')"});
				else
					$(this).css({"background-image" : "url('/PublishingImages/plus.png')"});
				for (var z = 0; z<b.length; z++)
				{
					$(b[z]).slideToggle();
				}

				});
			});
			$(this).click(function()
			{
			});

		});
}











