﻿var ie6 = false;
var ie7 = false;
var ff = false;

/**
 *   ?
**/
if(navigator.appName.indexOf("Microsoft") > -1 ){         
	// ?÷η?  6 ?
	if( navigator.appVersion.indexOf("MSIE 6") > -1){
		ie6 = true;
	// ? ÷η?  7 ?   
	}else if(navigator.appVersion.indexOf("MSIE 7") > -1){
		ie7 = true;
	}
} else {
	ff = true;
}

function findPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function findPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function _closeLecturePop() {
	var container = document.getElementById('popupContainer');
	container.innerHTML = '';
	container.style.display = 'none';
	container.style.visibility = 'hidden';
}

function _highlight(index, isFocus) {
	var el = document.getElementById('lecture' + index);
	if (el) {
		if (isFocus) {
			el.style.backgroundColor = '#006AAE';
			el.style.color = '#FFFFFF';
		}		
		else {
			el.style.backgroundColor = '#FFFFFF';
			el.style.color = '#666666';
		}
	}
}

function _endsWith(str, s) {
	var reg = new RegExp (s + "$");
	return reg.test(str);
}

function _getFileType(location) {
	if (_endsWith(location, 'hwp'))
	{
		return '[HWP]&nbsp;';
	}
	else if (_endsWith(location, 'doc'))
	{
		return '[DOC]&nbsp;';
	}
	else if (_endsWith(location, 'pdf'))
	{
		return '[PDF]&nbsp;';
	}
	else if (_endsWith(location, 'ppt'))
	{
		return '[PPT]&nbsp;';
	}
	else {
		return "";
	}
}
	
function _viewLectures(obj, lectureObj, courseUrl, redirectUrl) {
	var container = document.getElementById('popupContainer');
	if (container.style.display == 'block') {
		return true;
	}

	var verticalPositionValue = findPosY(obj) + obj.offsetHeight-1;
	var verticalPosition = 'top: ' + verticalPositionValue + 'px;';
	var horizontalPosition = 'left: '+ findPosX(obj)+ 'px;';

	var numLectures = lectureObj[0]["lectures"].length;
	var html = [];
	html.push('<div id="popup-wrapper" class="popup-wrapper">');
	html.push('  <p class="tag-popup-close">');
	html.push('    <a id="tag-popup-close" onclick="_closeLecturePop();return false;" href="javascript:void(0);"></a>');
	html.push('  </p>');
	html.push('  <div style="display: none;" id="categoryPopupFeedback">');
	html.push('    <img src="/images/miniglobe.gif" id="categoryPopupImg"/>');
	html.push('    <small id="categoryPopupMsg" class="red"> Please wait...</small>');
	html.push('  </div>');
	html.push('  <b><a href="');
	html.push(courseUrl);
	html.push('" style="color:#006AAE;" title="">Lecture List');
	if (numLectures > 0) {
		html.push(' (');
		html.push(numLectures);
		html.push(')');
	}
	html.push('</a></b><div style="border-bottom:1px solid #DADADA; margin:2px 7px 0px 0px;"></div>');
	html.push('<br/><div id="lectures_container" class="lectures_container"><ol class="pop_lectures">');
	if (numLectures > 0) {
		for (var i = 0; i < numLectures; i++) {
			var lecture = lectureObj[0]["lectures"][i];
			var index = i + 1;
			html.push('<li><span id="lecture');
			html.push(index);
			html.push('">');
			html.push(index);
			html.push('</span>');
			html.push('<a href="');
			//html.push(redirectUrl + '?id=' + lecture['location']);
			html.push(redirectUrl + '?id=' + lecture['id']);
			html.push('" target="_new" onmouseover="_highlight(');
			html.push(index);
			html.push(', true)" onmouseout="_highlight(');
			html.push(index);
			html.push(', false)" title="\'');
			html.push(lecture['title']);
			html.push('\' ">');
			//html.push(_getFileType(lecture['location']) + lecture['title']);
			var title = lecture['title'];
			if (title.length > 30) {
				title = title.substring(0, 30) + '...';
			}
			html.push(title);
			//html.push('</a> <img src="/home/images/lecture/external.png" alt=" a " title=" a "/></li>');
			html.push('</a></li>');
		}
	}
	else {
		html.push('<li><p style="font-size:11px;">? ?. <a href="" target="_new" style="color:#006AAE; text-decoration:underline;">  ??</a>');
		//html.push(' <img src="/home/images/lecture/external.png" alt=" a " title=" a "/></li>');
		html.push('</li>');
	}
	html.push('  </ol></div><br/></div>');
	
	container.innerHTML = html.join('');
	
	container.style.display = 'block';
	container.style.top = verticalPositionValue + 'px';
	container.style.left = (findPosX(obj) - container.offsetWidth + obj.offsetWidth) + 'px';

	if (numLectures > 16) {
		var lecturesContainer = document.getElementById('lectures_container');
		lecturesContainer.style.height = '317px';
		lecturesContainer.style.overflowY = 'auto';

		container.style.height = '376px';
	}
	else {
		container.style.height = 'auto';
	}

	if (ie6)
		container.style.right = '-'+(container.offsetWidth+37)+'px';

	var popupCloseImg = document.getElementById('tag-popup-close');

	if (ie7)
		popupCloseImg.style.left = (container.offsetWidth-35)+ 'px';
	else
		popupCloseImg.style.left = (container.offsetWidth-35)+ 'px';

	container.style.visibility = 'visible';		
	getLectures(kemId);
}

function getLectures(obj, kemId, courseUrl, redirectUrl){
	var pageUrl = '/en/async/lectures.do';
	new Ajax.Request(pageUrl,
 {
			method: 'GET',
			parameters: {
'kemId':kemId, 'l':'en'},
			onFailure: function(transport) { 
				alert("Cannot get lectures.");
			},
			onSuccess: function(transport) {
				var lectureObj = eval("(" + transport.responseText + ")");				
				_viewLectures(obj, lectureObj, courseUrl, redirectUrl);				
			}
		});
}
