/*
	Function trunc()
	
	Trancates a string to fit a block element and adds 
    triple dot pattern '...' at the end of the string if 
	the string was truncated.
	
	Parameters:
	@elm - element (object) or elemen's ID; 
		   string is tracated based on the element width;

    @showldWrap - boolen; true - wrap, false - no-wrap; 
	@shortCut   - additional short-cut of the @elm width, if needed.
	
	return - original string if the string fits the element or
		     truncated string with "..." at the end otherwise.
*/
String.prototype.trunc = function(elm, shouldWrap, shortCut) {

	var boxWidth = 0;
	var ix = 0;
	var boxObj = null;
	var tempSpan = null;
	var truncStr = '';	
	var tempSpanHeight = 0;
	var adjust = 0.9;

	if (elm == null || this == null || this.length < 1 ) {
		return "";
	}

	if (typeof(elm) == "object" ) {
		boxObj = elm;
	} else if (typeof(elm) == "string" ) {
		boxObj = document.getElementById(elm);
	} else {
		return "";
	}

	boxWidth = boxObj.offsetWidth;
	if (shortCut) { 
		boxWidth -= shortCut;
	}
	boxWidth = Math.round(boxWidth*adjust); 
	boxHeight = Math.round(boxObj.offsetHeight*adjust); 

	tempSpan = document.createElement("span");
	tempSpan.setAttribute('style', 'visibility: hidden;');
	boxObj.appendChild(tempSpan);

	/* 
	 * Set a single char to check a single line height. 
	 */
	tempSpan.innerHTML = "m"; 
	tempSpanHeight = tempSpan.offsetHeight;
	if( boxWidth < tempSpan.offsetWidth) {
		// the box is too small; clean up and return.
		tempSpan.innerHTML = '';
		boxObj.removeChild(tempSpan);
		tempSpan = null;
		return "";	
	}
	
	tempSpan.innerHTML = this; //load an actual string

	if( !shouldWrap ) {//no-wrap
	
		truncStr = this + "...";
		ix = truncStr.length;
		 /* 
		  * the second condition is set to check 
		  * if the line is wrapped or not; 
		  */
		if ( tempSpan.offsetWidth < boxWidth && tempSpan.offsetHeight == tempSpanHeight) {
			truncStr = this; // string is shorter then box
		} else { // string is longer then box 
			while (tempSpan.offsetWidth > boxWidth || tempSpan.offsetHeight > tempSpanHeight ) {
				tempSpan.innerHTML = truncStr.substring(0,ix-5) + "...";
				ix--;
			}
		}
		truncStr = new String(tempSpan.innerHTML);
		
	} else { //wrap
	
		var linesInBox = Math.floor(boxHeight/tempSpanHeight);
		var iBegin = 0;
		var iEnd = 1;
		var buf = new String();
		truncStr = '';
		tempSpan.innerHTML = "";
		
		for (ix=0; ix<linesInBox; ix++ ) {
			while (tempSpan.offsetWidth < boxWidth && iEnd <= this.length ) {
				tempSpan.innerHTML = this.substring(iBegin,iEnd++);
				continue;
			}
			truncStr += (tempSpan.innerHTML+"");//insert white space
			tempSpan.innerHTML = "";
			iBegin = iEnd-1;
		}
		
		if(iEnd < this.length) { //line is truncated
			buf = truncStr;
			truncStr = buf.substring(0,buf.length - 3);
			truncStr += "...";
		}
	
	} 
	/* clean up */
	tempSpan.innerHTML = '';
	boxObj.removeChild(tempSpan);
	tempSpan = null;

	return truncStr;
};


/*
        Function truncate()
        
        Trancates a string to fit a block element and adds 
    triple dot pattern '...' at the end of the string if 
        the string was truncated.
        
        Parameters:
        @elm - element (object) or elemen's ID; 
                   string is tracated based on the element width;

    @showldWrap - boolen; true - wrap, false - no-wrap; 
        @shortCut   - additional short-cut of the @elm width, if needed.
        
        return - original string if the string fits the element or
                     truncated string with "..." at the end otherwise.
*/
String.prototype.truncate = function( inWidth, inHeight, shouldWrap ) {

        var boxWidth = 0;
        var ix = 0;
        var boxObj = null;
        var tempSpan = null;
        var truncStr = '';
        var tempSpanHeight = 0;
        var adjust = 0.9;

        if ( this == null || this.length < 1 ) {
                return "";
        }

        boxWidth = inWidth;
		boxHeight = inHeight;

        boxWidth = Math.round(boxWidth*adjust);
        boxHeight = Math.round(boxHeight*adjust);
		
        tempSpan = document.createElement("span");
        tempSpan.style.visibility =  "hidden";
        tempSpan.style.width =  boxWidth;
		
        document.body.appendChild(tempSpan);

        /* 
		 * Set a single char to check a single line height. 
		 */
        tempSpan.innerHTML = "m";
        tempSpanHeight = tempSpan.offsetHeight;
        if( boxWidth < tempSpan.offsetWidth) 
		{
                // the box is too small; clean up and return.
                tempSpan.innerHTML = '';
                document.body.removeChild(tempSpan);
                tempSpan = null;
                return "";
        }

        tempSpan.innerHTML = this; //load an actual string

        if( !shouldWrap ) {//no-wrap

                truncStr = this + "...";
                ix = truncStr.length;
                 /* 
			                  * the second condition is set to check 
			                  * if the line is wrapped or not; 
			                  */
                if ( tempSpan.offsetWidth < boxWidth && tempSpan.offsetHeight == tempSpanHeight) {
                        truncStr = this; // string is shorter then box
                } else { // string is longer then box 
                        while (tempSpan.offsetWidth > boxWidth || tempSpan.offsetHeight > tempSpanHeight ) {
                                tempSpan.innerHTML = truncStr.substring(0,ix-5) + "...";
                                ix--;
                        }
                }
                truncStr = new String(tempSpan.innerHTML);

        } else { //wrap

                var linesInBox = Math.floor(boxHeight/tempSpanHeight);
                var iBegin = 0;
                var iEnd = 1;
                var buf = new String();
                truncStr = '';
                tempSpan.innerHTML = "";

                for (ix=0; ix<linesInBox; ix++ ) {
                        while (tempSpan.offsetWidth < boxWidth && iEnd <= this.length ) 
			{
				if( this.substring(iEnd-1,iEnd) == " " ) //If whitespace, we should clear, because it'll already wrap
				{
					truncStr += tempSpan.innerHTML+" ";
					tempSpan.innerHTML = "";
					iBegin = iEnd;
				}
				
                                tempSpan.innerHTML = this.substring(iBegin,iEnd++);
								
                                continue;
                        }
						
			if( iEnd <= this.length )
					truncStr += (tempSpan.innerHTML+" ");//insert white space
			else
					truncStr += tempSpan.innerHTML;
					
			tempSpan.innerHTML = "";

                        iBegin = iEnd-1;
                }

                if(iEnd < this.length) { //line is truncated
                        buf = truncStr;
                        truncStr = buf.substring(0,buf.length - 3);
                        truncStr += "...";
                }

        }
		
        /* clean up */
        tempSpan.innerHTML = '';
        document.body.removeChild(tempSpan);
        tempSpan = null;

        return truncStr;
};
                               		


/*
 * Function sprintf()
 *
 * replaces tokens (%s) in a template string 
 * with substrings provided as arguments.
 * 
 * @param <String> ...
 * return <String> with replaced tokens;
 * (number tokens is not restricted.)
 *
 *  Example: 
 *
 * 	var templateStr = "My %s is %s.";
 *	var str = templateStr.sprintf("name","Vadim");
 *  ( now str is: My name is Vadim.)
 */

String.prototype.sprintf = function() {
  var i = 0, args = arguments;
  return this.replace(/%s/g, 
		function(x) {return (i in args) ? args[i++] : x;});
};

/*
 * @param - Unix Timestamp;
 * @return - String;
 */

Date.prototype.getSmartDate = function(unixTimeStamp)
{
	var timetext = 0;
	var foo = new Date; // Generic JS date object
	var unixtime_ms = foo.getTime(); // Returns milliseconds since the epoch
	var unixtime_now = parseInt(unixtime_ms / 1000);

	var timespan = unixtime_now - unixTimeStamp;
	/*
	if (timespan <= 3600) {
		timetext = Math.round(timespan / 60);
		timetext = ((timetext < 2) ? timetext += " minute ago" : timetext += " minutes ago");
		
	} else 
	if( (timespan > 3600) && (timespan <= 86400)){ // <1 day, ...60 seconds * 60 minutes * 
		timetext = Math.round(timespan/3600);
		timetext = ((timetext < 2) ? timetext += " hour ago" : timetext += " hours ago");
	} else 
	if( (timespan > 86400) && (timespan <= 172800 ) ){ // yesterday
		timetext = "1 day ago"; //was yesterday?
	} else {
	*/
		var dt = new Date(1000 * unixTimeStamp);
		timetext = dt.toLocaleDateString();	
	//}
	return timetext;
};

