﻿// JScript File

//
// Get key event code
//
function nkGetEventKeyCode(ev)
{
	ev = ev || window.event;
	if (ev == null)
		return null;
		
	return (document.all != null) ? ev.keyCode : ev.which;
}

//
// Get key event source object
//
function nkGetEventSrcElement(ev)
{
	ev = ev || window.event;
	if (ev == null)
		return null;
		
	return (document.all != null) ? ev.srcElement : ev.target;
}

//
// Returns the absolute position of an elemtn within the page
//
function nkGetAbsolutePos(obj) 
{
	var curleft = curtop = 0;
	if (obj.offsetParent) 
	{
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) 
		{
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

//
// Returns the absolute position of an elemtn within the page
//
function nkEnsureVisible(obj)
{
	var absPos = nkGetAbsolutePos(obj);
	
	var x1 = absPos[0];
	var x2 = x1 + obj.scrollWidth;
	
	var y1 = absPos[1];
	var y2 = y1 + obj.scrollHeight;
	
	var docX = document.body.scrollLeft;
	var docY = document.body.scrollTop;
	var docWidth = document.body.clientWidth;
	var docHeight = document.body.clientHeight;
	
	var scrollX = docX;
	var scrollY = docY;
	
	if (y2 > docY + docHeight)
		scrollY = y2-docHeight;
	else if (y1 < docY)
		scrollY = y1;

	if (x2 > docX + docWidth)
		scrollX = x2-docWidth;
	else if (y1 < docY)
	    scrollX = x1;
	    
	window.scrollTo(scrollX,scrollY);
}


//
// Get's the parent element that matches one of the tags in tagNames
//
function nkGetParentOfTagName(elem, tagNames)
{
	if (elem == null || tagNames == null || tagNames.length == 0)
		return null;

	for (elem = elem.parentNode; elem != null; elem = elem.parentNode)
	{
		var i = 0;
		
		for (; i < tagNames.length; ++i)
		{
			if (elem.tagName.toUpperCase() == tagNames[i].toUpperCase())
			{
				return elem;
			}
		}
	}
	
	return null;
}

//
// Insert a YouTube video
//
function nkCreateYouTubeObject(YouTubeVideoURL, DivID)
{
    var obHTML = '<object width="320" height="268">' +
                    '<param name="movie" value="' + YouTubeVideoURL + '">' + 
                    '<\/param>' + 
                    '<param name="wmode" value="transparent">' + 
                    '<\/param>' + 
                    '<embed src="' + YouTubeVideoURL + '" type="application\/x-shockwave-flash" wmode="transparent" width="320" height="268">' + 
                    '<\/embed>' + 
                  '<\/object>';
                  
    document.getElementById(DivID).innerHTML = obHTML;
}
