/*
	Roku.Util
*/

if(typeof(Roku) == "undefined")
    Roku = { classes : [] };

if(typeof(Roku.Util) == "undefined")
	Roku.Util = {};

Roku.Util.Point = function()
{
	this.x = 0;
	this.y = 0;
};

Roku.Util.Rect = function()
{
	this.left = 0;
	this.top = 0;
	this.width = 0;
	this.height = 0;
};

Roku.Util.isParent = function(parent, child)
{
	var el = child;
	while(el && (el != parent))
		el = el.parentNode;
	return el;
}

Roku.Util.parentClassElement = function(child, className)
{
	var el = child;
	while(el && (el.className != className))
		el = el.parentNode;
	return el;
};

Roku.Util.parentIdElement = function(child, id)
{
	if(!id)
		return null;
	var el = child;
	while(el && (el.id != id))
		el = el.parentNode;
	return el;
};

Roku.Util.parentAttribElement = function(child, attrib_name)
{
	if(!attrib_name)
		return null;
	var el = child;
	while(el && el.getAttribute && (el.getAttribute(attrib_name) == null))
		el = el.parentNode;
	return el;
};

Roku.Util.parentTagElement = function(child, tagName)
{
	var el = child;
	while(el && !this.CompareNoCase(el.tagName, tagName))
		el = el.parentNode;
	return el;
};

Roku.Util.childClassElement = function(parent, className)
{
	if(!parent)
		return null;
		
	var elements = parent.childNodes;
	var length = elements.length;
	for(var index = 0; index < length; index++)
	{
		var element = elements[index];
		if(element && element.className == className)
			return element;
			
		var child = this.childClassElement(element, className)
		if(child)
			return child;   
	}
		   
	return null;
}

Roku.Util.childIdElement = function(parent, id)
{
	if(!parent || !id)
		return null;
		
	var elements = parent.childNodes;
	var length = elements ? elements.length : 0;
	for(var index = 0; index < length; index++)
	{
		var element = elements[index]; 
		if(element && element.id == id)
			return element;
			
		var child = this.childIdElement(element, id)
		if(child)
			return child;   
	}
		   
	return null;
}

Roku.Util.childrenClassElements = function(element, className, result)
{
	if(!result)
	{
		result = [];
		this.childrenClassElements(element, className, result);
		return result;
	}
	else if(element)
	{
		if(element.className == className)
			result.push(element);
		else
		{
			var children = element.childNodes;
			var length = children ? children.length : 0;
			for(var index = 0; index < length; index++)
				this.childrenClassElements(children[index], className, result);
		}
	}
}

Roku.Util.childrenIdElements = function(element, id, result)
{
	if(!result)
	{
		result = [];
		this.childrenIdElements(element, id, result);
		return result;
	}
	else if(element)
	{
		if(element.id == id)
			result.push(element);
		else
		{
			var children = element.childNodes;
			var length = children ? children.length : 0;
			for(var index = 0; index < length; index++)
				this.childrenIdElements(children[index], id, result);
		}
	}
}

Roku.Util.childrenAttribElements = function(element, attrib_name, result)
{
	if(!result)
	{
		result = [];
		this.childrenAttribElements(element, attrib_name, result);
		return result;
	}
	else if(element)
	{
		if(element.getAttribute && (element.getAttribute(attrib_name) != null))
			result.push(element);
		else
		{
			var children = element.childNodes;
			var length = children ? children.length : 0;
			for(var index = 0; index < length; index++)
				this.childrenAttribElements(children[index], attrib_name, result);
		}
	}
}

Roku.Util.indexTagElement = function(children, tagName, index)
{
	if(!children)
		return null;
		 
	if(!index)
		index = 0;
	
	var length = children.length; 
	for(var i = 0; i < length; i++)
	{
		var element = children[i];
		if(element && Roku.Util.CompareNoCase(element.tagName, tagName))
		{
			if(index == 0)
				return element;
			else
				index--;
		}
	}
	
	return null;
}

Roku.Util.childTagElement = function()
{
	try
	{
		var parent = arguments[0];
		var arg_count = arguments.length; 
		for(var iArg = 1; iArg < arg_count; iArg++)
		{
			var tagName = arguments[iArg]; 
			var children = parent.childNodes;
			var tagChild = null;
			var children_count = children.length; 
			for(var iChild = 0; iChild < children_count; iChild++)
			{
				var child = children[iChild];
				if(child && Roku.Util.CompareNoCase(child.tagName, tagName))
				{
					tagChild = child;
					break;
				}
			}
			if(tagChild)
				parent = tagChild;
			else
				return null;
		}
		return parent;
	}
	catch (e) { }
	return null;
}

Roku.Util.childrenTagElements = function(element, tagName, result)
{
	if(!result)
	{
		result = [];
		this.childrenTagElements(element, tagName, result);
		return result;
	}
	else if(element)
	{
		if(Roku.Util.CompareNoCase(element.tagName, tagName))
			result.push(element);
		else
		{
			var children = element.childNodes;
			var length = children ? children.length : 0;
			for(var index = 0; index < length; index++)
				this.childrenTagElements(children[index], tagName, result);
		}
	}
}

Roku.Util.elementRect = function(el)
{
	var rect = new Roku.Util.Rect();
	rect.left = 0;
	rect.top = 0;
	rect.width = (el) ? el.offsetWidth : 0;
	rect.height = (el) ? el.offsetHeight : 0;
	while(el != null)
	{
		rect.left += el.offsetLeft - el.scrollLeft; 
		rect.top += el.offsetTop - el.scrollTop;
		var parent = el.offsetParent;
		el = parent;
		
	}
	rect.left += document.body.scrollLeft; 
	rect.top += document.body.scrollTop;
	return rect;  
}

Roku.Util.clientWidth = function()
{
	var width = 0;
	if(window.innerWidth)
		width = window.innerWidth;
	else if(0 < document.documentElement.clientWidth)
		 width = document.documentElement.clientWidth;
	else  
		 width = document.body.clientWidth;
	return width;
}

Roku.Util.clientHeight = function()
{
	var height = 0;
	if(window.innerHeight)
		height =  window.innerHeight;
	else if(0 < document.documentElement.clientHeight)
		height = document.documentElement.clientHeight;
	else
		height = document.body.clientHeight;
	return height;
}

Roku.Util.CompareNoCase = function(text1, text2)
{
	return text1 && (typeof(text1.toUpperCase) == "function") && text2 && (typeof(text2.toUpperCase) == "function") && (text1.toUpperCase() == text2.toUpperCase());
}

Roku.Util.TruncateText = function(text, maxLength)
{
	if(!text)
		return "";
		
	if(text.length <= maxLength)
		return text;
		
	var last = maxLength;
	while((0 <= last) && (text.charAt(last) != ' '))
		 last--;
	if(last <= 0)
		last = maxLength;
	return text.substring(0, last) + " ...";
}

Roku.Util.Trim = function(text)
{
	if(!text)
		return "";
	
	var first = 0, last = text.length - 1;
	while((first <= last) && (text.charCodeAt(first) < 33))
		first++;
	while((first <= last) && (text.charCodeAt(last) < 33))
		last--;
	return (first <= last) ? text.substr(first, last - first + 1) : "";
}

Roku.Util.eventHandler = function(element, event_name, event_handler)
{
	if(!element)
		return;
		
	if(element.attachEvent)
	{
		element.attachEvent("on" + event_name, event_handler);	// IE
	}
	else if(element.addEventListener)
	{
		element.addEventListener(event_name, event_handler, true);	//Mozilla
	}
}

Roku.Util.eventHandlerDetach = function(element, event_name, event_handler)
{
	if(!element)
		return;
		
	if(element.detachEvent)
	{
		element.detachEvent("on" + event_name, event_handler);	// IE
	}
	else if(element.removeEventListener)
	{
		element.removeEventListener(event_name, event_handler, true);	//Mozilla
	}
}

Roku.Util.eventCancel = function(ev)
{
	if(ev)
	{
		if(ev.preventDefault)
			ev.preventDefault();	//Mozilla
		ev.returnValue = false;		//IE
	}
}

Roku.Util.eventCancelBuble = function(ev)
{
	if(ev)
	{
		if(ev.stopPropagation)
			ev.stopPropagation();	//Mozilla
		ev.cancelBubble = true;		//IE
	}
}

Roku.Util.srcEventElement = function(ev)
{
	var element = null;
	if(ev)
	{
		element = ev.srcElement;
		if(!element)
			element = ev.target;
	}		
	return element;
}

Roku.Util.getCookie = function(name, file_name)
{
	if(!file_name)
	{
		var split = Roku.Util.splitUrl(window.location.href);
		file_name = split['file'];
	}
	
	var cookie_name = name + file_name;

	var cookies = (document.cookie != null) ? document.cookie.split(";") : [];
	var cookies_count = cookies.length;  
	for(var iCookie = 0; iCookie < cookies_count; iCookie++)  
	{ 
		var cookie = cookies[iCookie].split("=");
		if((cookie.length == 2) && (0 <= cookie[0].indexOf(cookie_name)))
		{
			return unescape(cookie[1]);
		}   
	}
	return "";
}

Roku.Util.setCookie = function(name, value, file_name, exp)
{
	if(!file_name)
	{
		var split = Roku.Util.splitUrl(window.location.href);
		file_name = split['file'];
	}
	var cookie_name = name + file_name;

	if(!exp)
		exp = 6;
    var expdate = new Date(); 
    expdate.setMonth(expdate.getMonth() + exp);
		
	var sCookie = cookie_name + "=" + escape(value) + "; expires=" + expdate.toGMTString();

	document.cookie = sCookie;
}

Roku.Util.parseCookie = function(cookie, entry_delimiter, value_delimiter)
{
	if(!entry_delimiter)
		entry_delimiter = ";";
	
	if(!value_delimiter)
		value_delimiter = "="; 
	
	var result = []; 
	var entries = cookie.split(entry_delimiter);
	var entries_count = entries.length; 
	for(var iEntry = 0; iEntry < entries_count; iEntry++)
	{
		var entry = entries[iEntry].split(value_delimiter);
		if(entry && (entry.length == 2))
			result[entry[0]] = entry[1];
	}
	
	return result;
}

Roku.Util.Encode = function(s)
{
	var result = "";
	
	var zero_str = "0";
	var zero_code = zero_str.charCodeAt(0);
	
	var A_str = "A";
	var A_code = A_str.charCodeAt(0);
	
	var length = s.length;
	for(var index = 0; index < length; index++)
	{
		var ch_code = s.charCodeAt(index);
		
		var high = ch_code / 16; 
		result += String.fromCharCode(((high < 10) ? zero_code : A_code) + high);

		var low = ch_code % 16;
		result += String.fromCharCode(((low < 10) ? zero_code : A_code) + low);
	}
	return result;
}

Roku.Util.Decode = function(s)
{
	var result = "";
	
	var zero_str = "0";
	var zero_code = zero_str.charCodeAt(0);
	
	var A_str = "A";
	var A_code = A_str.charCodeAt(0);
	
	var length = s.length;
	for(var index = 0; index < length; index++)
	{
		var ch_code = 0;
		
		var high_code = s.charCodeAt(index++);
		ch_code += (high_code - ((A_code <= high_code) ? A_code : zero_code)) * 16 
		
		var low_code = (index < length) ? s.charCodeAt(index) : zero_code;
		ch_code += (low_code - ((A_code <= low_code) ? A_code : zero_code));
		
		result += String.fromCharCode(ch_code);
	}
	return result;
}

Roku.Util.splitUrl = function(url)
{
	var result = { path : "", file : "", extention : "", extra : ""};
	if(!url)
		return result;
		
	var length = url.length;
	var posExtra = url.indexOf("?");
	if(0 <= posExtra)
	{
		result.extra = url.substr(posExtra + 1, length - posExtra - 1);
		length = posExtra;
	}
	

	for(var index = length - 1; 0 <= index; index--)
	{
		var ch = url.charAt(index);
		if(!result.extention && (ch == '.'))
		{
			result.extention = url.substr(index, length - index);
			length = index;
		} 
		if((ch == '\\') || (ch == '/'))
		{
			result.file = url.substr(index + 1, length - index - 1);
			result.path = url.substr(0, index + 1);
			break;
		}
	}
	
	if(!result.file)
	{
		if(result.extention)
			result.file = url.substr(0, length);
		else
			result.path = url.substr(0, length);
	}
	return result;
}


Roku.Util.Trace = function()
{
	var statusEl, statusText;
	var append = false; 
	if(1 == arguments.length)
	{
		statusEl = document.getElementById("status");
		statusText = arguments[0];
		window.status = statusText; 
	}
	else if(2 <= arguments.length)
	{
		statusEl = document.getElementById(arguments[0]);
		statusText = arguments[1];
		append = (2 < arguments.length) && arguments[2];  
	}

	if(statusEl)
		if(append)
			statusEl.innerHTML = statusEl.innerHTML + statusText + "<br>";
		else
			statusEl.innerHTML = statusText;
}


