/*
 * Cellblock general-purpose management console, a component of
 * the Gloto Platform.  See www.gloto.com for more information,
 * terms, and licensing.
 *
 * Copyright (C) 2005-2008, Gloto Corp.
 * http://www.cellblock.com/terms.htm
 */
 
function Util(){
	var util = this;

	this.getUrlParameter = function( name )
	{
		name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
		var regexS = "[\\?&]"+name+"=([^&#]*)";
		var regex = new RegExp( regexS );
		var results = regex.exec( window.location.href );
		if( results === null )
			return null;
		else
			return results[1];
	};

	this.getElement = function( id )
	{
		if( typeof( id ) == "string" ){
			return document.getElementById( id );
		}else{
			return id;
		}
	};
	
	this.removeElement = function( element )
	{
		element.parentNode.removeChild(element);
	};
	
	this.valid = function( data, error ){
		if( data !== null && data.status == "ok" ){
			return true;
		}
	
		return false;
	};
	
	this.capitalize = function( str )
	{ 
		str = str.toLowerCase();
		var firLet = str.substr(0,1); 
		var rest = str.substr(1, str.length -1);
		return firLet.toUpperCase() + rest;
	};
	
	this.error = function( error )
	{
		var name = error.errorName;
		var words = name.split("_"); 
		 for (var i=0 ; i < words.length ; i++){ 
				words[i] = util.capitalize( words[i] );
		 } 
		 var message = "Error: " + words.join(" ") + "\n" + error.errorMsg;
		 if( util.console != null && util.console.installed()){
			 util.console.errorln( message );
		 }else{
			alert( message ); 
		 }
	};
	
	this.include = function( filename )
	{
		document.write('<script type="text/javascript" src="' + filename + '"></scr' + 'ipt>'); 
	};
	
	this.isPrivateAttribute = function( key )
	{
		/*for( var attribute in _privateAttributes ){
			if(key.toLowerCase() == attribute.toLowerCase()){
				return true;
			}
		}*/
		
		return false;
	};
	
	this.hide = function( element )
	{
		if( typeof( element ) == "string" ){
			element = util.getElement( element );
		}
		
		if( element == null ){
			return;
		}
		
		element.style.display = "none";
	};
	
	this.show = function( element )
	{		
		if( typeof( element ) == "string" ){
			element = util.getElement( element );
		}
		
		if( element == null ){
			return;
		}
		
		element.style.display = "block";
	};
	
	this.toggleVisibility = function( element )
	{		
		if( typeof( element ) == "string" ){
			element = util.getElement( element );
		}
		
		if( element == null ){
			return;
		}
		
		var display = element.style.display;
		if( display !== null && display.toLowerCase() == "none" ){
				util.show( element );
		}else{
				util.hide( element );
		}
	};
	
	this.constructAttributeMap = function( data )
	{
		var attributes = {};
		for( var i in data.attributes ){
			var attribute = data.attributes[i];
			attributes[attribute.name] = attribute.value;
		}
		
		return attributes;
	};
	
	this.createElement = function( props )
	{
		var type = (props.type != null) ? props.type : "span";
		var element = document.createElement( type );
		
		if( props.text != null ){
			element.innerHTML = props.text;
			//element.appendChild( document.createTextNode( props.text ) );
		}
		
		if( props.id != null ){
			element.setAttribute("id", props.id);
		}
		
		if( props.className != null ){
			element.setAttribute("class", props.className);
			element.setAttribute("className", props.className);
		}
		
		if( props.onclick != null ){
			element.onclick = props.onclick;
		}

		if( props.onmouseover != null ){
			element.onmouseover = props.onmouseover;
		}
		
		if( props.onmouseout != null ){
			element.onmouseout = props.onmouseout;
		}		

		if( props.onchange != null ){
			element.onchange = props.onchange;
		}
		
		if( props.name != null ){
			element.setAttribute( "name", props.name );
		}
		
		if( props.value != null ){
			element.setAttribute( "value", props.value );
		}
		
		if( props.childNode != null ){
			element.appendChild( props.childNode );
		}
		
		if( props.href != null ){
			element.setAttribute( "href", props.href );
		}
		
		if( props.src != null ){
			element.setAttribute( "src", props.src );
		}

		if( props.style != null )
		{
			element.setAttribute( "style", props.style );
		}

		if( props.parentNode != null )
		{
			util.getElement( props.parentNode ).appendChild( element );
		}
		
		return element;
	};
	
	this.clear = function( element )
	{
		var node = util.getElement( element );
		node.innerHTML = "";
	};
	
	this.closure = function( params, func )
	{
		return function(){
			func( params );
		};
	};
	
	this.isCellblock = function( context )
	{
		if( context == "cellblock" )
		{
			return true;
		}
		return false;
	};
	
	this.isFrame = function( context )
	{
		if( context == "frame" ){
			return true;
		}
		return false;
	};
	
	this.isUser = function( context )
	{
		if( context == "user" ){
			return true;
		}
		return false;
	};

	this.createPaging = function(target, currentPage, pageCount, label, callback)
	{
		function createPageNumber( pageNumber )
		{
			var text = pageNumber;
			var className = "default";			
			var onclick = function(){};
			
			if( pageNumber != ( currentPage + 1 ) ){
				className = "pointer";
				onclick = util.closure( pageNumber - 1, callback );
			}
			
			util.createElement({
				type: "span",
				className: className,
				text: text,
				onclick: onclick,
				parentNode: target
			});
		}
		
		function createElipse(target)
		{
			util.createElement({
				type: "span",
				text: " ... ",
				parentNode: target
			});
		}
		
		util.clear(target);
		
		if( label != null ){
			util.createElement({
				type: "span",
				text: label,
				style: "font-weight: bold; 	text-transform: capitalize;",
				parentNode: target
			});
		}
		
		if (pageCount > 1) {
			createPageNumber( 1 );
			
			// show start elipse?
			if(currentPage > 3 && pageCount > 7){
				createElipse( target );
			}
			
			var delta = 1;
			var count = pageCount - 2;
			if (pageCount > 6) {
				count = 5;
				if (currentPage >= 3) {
					delta = currentPage - 2;
				}
				delta = Math.min(delta, pageCount - count);
				if( pageCount - currentPage <= 3){
					--delta;
				}
				
			}
			for (var i = 0; i < count; ++i) {
				var ii = i + delta;
				createPageNumber( ii + 1 );
			}
			
			// show end elipse?
			if(pageCount - currentPage > 4 && pageCount > 7){
				createElipse( target );
			}
			
			createPageNumber( pageCount );
		}else{
			createPageNumber( 1 );
		}
	};

}

Object.prototype.toString = function( )
{
	var buf = "";
	if( this instanceof Object )
	{
		buf += "[";
		for( key in this )
		{
			buf += key + " = " + this[key].toString() + ", ";
		}
		buf += "]";
	}
	else
	{
		buf += obj;
	}
	return buf;
};

Array.prototype.remove = function(s)
{
	var index = this.indexOf(s);
	if(index != -1)this.splice(index, 1);
};

String.prototype.noCache = function()
{
	if( this.indexOf("?") == -1 ){
		return this + "?" + Math.ceil(Math.random()*1000);
	}
	
	return this + "&" + Math.ceil(Math.random()*1000);
};

Array.prototype.indexOf = function(s)
{
	var length = this.length;
	
	for( var i = 0; i < length; i++ ){
		if( this[i] == s ){
			return i;
		}
	}
	
	return -1;
};

var util = new Util();
