/****************************
Global variables
****************************/
var safe = {}

/****************************
Startup script
****************************/
safe.startUp = function() {
	// Initialize CoursePortalPage tabview
	var tabView = new safe.TabView('TabView');
	//Wait 100 milliseconds, then call safe.initAccordian().
	//This fixes the problem loading accordion in EPiServer Edit mode
	safe.initAccordian.delay(100);
	safe.initReference();
	safe.initCallOuts();
	safe.initEducationDropDown();
	safe.initTeacherList();
}

//Write style for JavaScript enabled browsers -
//this script has to be executed before DOM is ready
safe.writeStyleForJavaScriptEnabledBrowsers = function() {
	var str = "";
	str = "<style type=\"text/css\">";
	str += "div.NoJavaScript,";
	str += "input.NoJavaScript,";
	str += "a.NoJavaScript {";
	str += "display: none";
	str += "}";
	str += ".CallOut,";
	str += ".Reference {";
	str += "cursor: pointer";
	str += "}";
	str += ".Hide {";
	str += "display: none";
	str += "}";
	str += "</style>";
	document.write(str);
}
safe.writeStyleForJavaScriptEnabledBrowsers();

//Add events on elements in the reference container
safe.initReference = function() {
    $$(".Reference").each(function(item) {
		var linkUrl = item.getElements('a')[0];
		item.addEvent('click', function() {
			location.href = linkUrl;
		});
		item.addEvent('mouseenter', function() {
			item.addClass('Over');
		});
		item.addEvent('mouseleave', function() {
			item.removeClass('Over');
		});
	});
}
//Add events on elements in the callout container
safe.initCallOuts = function() {
    $$(".CallOuts .CallOut").each(function(item) {
		var linkUrl = item.getElements('a')[0];
		item.addEvent('click', function() {
			location.href = linkUrl;
		});
		item.addEvent('mouseenter', function() {
			item.addClass('Over');
		});
		item.addEvent('mouseleave', function() {
			item.removeClass('Over');
		});
	});
}
//Create an Accordion instance if container has the class name Accordian
safe.initAccordian = function() {
	var accordionObject = $$('.Accordion')[0];
	if(accordionObject == null) return;
	accordionObject.removeClass('NoJavaScript');
	var accordion = new Accordion(accordionObject, 'h2.toggler', 'div.element', {
		opacity: true,
		alwaysHide: false,
		onActive: function(toggler, element){
			toggler.addClass('active');
		},
		onBackground: function(toggler, element){
			toggler.removeClass('active');
		}
	});
}
//Add onchange event on Education dropdown
safe.initEducationDropDown = function() {
	var educationSelect = $$(".Educations select")[0];
	if(educationSelect != null) {
		educationSelect.addEvent('change', function(item) {
			location.href = this[this.selectedIndex].value;
		});
	}
}
//Add events on elements in the Teacher List container
safe.initTeacherList = function() {
    $$(".TeacherList .TeacherListItem").each(function(item) {
		var linkUrl = item.getElements('a')[0];
		item.addEvent('click', function() {
			location.href = linkUrl;
		});
		item.addEvent('mouseenter', function() {
			item.addClass('Over');
		});
		item.addEvent('mouseleave', function() {
			item.removeClass('Over');
		});
	});
}
//Class TabView: make elements appear as tabs
safe.TabView = new Class({
	// Constructor
	initialize: function(tabViewClass) {
		this.className = tabViewClass;
		this.tabContentClassName = 'Content';
		this.id = $$('.'+tabViewClass);
		if(this.id.length > 0) {
			this.loadTabView();
			this.hideNameLinks();
			this.initTable();
		}
	},
	loadTabView: function() {
		var thisSelf = this;
		var el = this.id;
		var isActive = false;
		$$('.'+this.className+' ul.Tabs li a').each(function(item, index) {
			if(thisSelf.getStringFromLastIndexOf(item.href) == thisSelf.getQueryString()) {
				thisSelf.showTabContent(index);
				isActive = true;
			}
		});
		if(!isActive)
			thisSelf.showTabContent(0);
		$$('.'+this.className+' ul.Tabs li a').each(function(item, index) {
			item.addEvent('click', function(e) {
				thisSelf.showTabContent(index);
			});
		});
	},
	initTable: function() {
		var thisSelf = this;
		var el = this.id;
		var details;
		var detailsPreview;
		$$('.'+this.className+' table tr td.MoreInfo').each(function(item, index) {
			item.getElement('a').href = 'javascript:void(0);'
			item.addEvent('click', function(e) {
				details = $$('.'+thisSelf.className+' table tr.Details')[index];
				if(Browser.Engine.trident) {
					detailsPreview = details.previousSibling;
				} else {
					detailsPreview = details.previousSibling.previousSibling;
				}
				if(details.getStyle('display') == 'none') {
					details.removeClass('Hide');
					$$(detailsPreview).each(function(item, index) {
						item.getElements('td').each(function(item, index) {
							item.addClass('BorderBottomNone');
						});
					});
				} else {
					details.addClass('Hide');
					$$(detailsPreview).each(function(item, index) {
						item.getElements('td').each(function(item, index) {
							item.removeClass('BorderBottomNone');
						});
					});
				}
			});
		});
	},
	getQueryString: function(){
		var href = window.location.toString();
		return this.getStringFromLastIndexOf(href);
	},
	getStringFromLastIndexOf: function(str){
		return str.substring(str.lastIndexOf('/')+1, str.length);
	},
	
	hideTableTrDetails: function(){
		$$('.'+this.className+' table tr.Details').each(function(item, index) {
			item.addClass('Hide');
		});
	},
	hideTabContent: function(){
		$$('.' + this.className + ' ul li').each(function(item, index) {
			item.removeClass('Active');
		});
		$$('.' + this.className + ' .' + this.tabContentClassName).each(function(item, index) {
			item.addClass('Hide');
			item.removeClass('Show');
		});
	},
	hideNameLinks: function(){
		$$('.' + this.className + ' a.NoJavaScript').each(function(item, index) {
			item.setProperty('name', null);
		});
	},
	showTabContent: function(showID){
		this.hideTabContent();
		if($$('.' + this.className + ' ul.Tabs li').length == 0) return;
		$$('.' + this.className + ' ul.Tabs li')[showID].addClass('Active');
		$$('.' + this.className + ' .' + this.tabContentClassName)[showID].removeClass('Hide');
	}
});

//Executes when the DOM is ready
window.addEvent('domready', safe.startUp);

