
AXIS.Util.getSignInUrl = function() {
  return AXIS._siteData.hosts.secure + 'secure/access.html#sign_up=if_new';
};

AXIS.Util.escapeEntities = function(text) {
	if(!text) {
		return "";
	}

	text = text.replace(/&/g, "&amp;");
	text = text.replace(/</g, "&lt;");
	text = text.replace(/>/g, "&gt;");
	text = text.replace(/"/g, "&quot;");

    return text;
};

/**
 * utility to get current root url
 */
AXIS.Util.getRootURL = function(url) {
    // if (url.match(/^\//)|| url.match(/http:\/\//)) 
    //     return url;
    return window.location.pathname.replace(/(\/$|$|\/[^\/]+$)/, "/") + url;
};

AXIS.Util.getAbsoluteUrl = function( url ) {
	if (/^http:/.test(url) || /^www\./.test(url) ) {
	       return url;
	     }

 	var returnUrl = "http://" + window.location.host;

	// append a backslash ?
	returnUrl = returnUrl + ((url.indexOf('/') == 0) ? "" : "/") + url;

	return returnUrl;
};

AXIS.Util.getBitNameFromRelativeUrl = function(url) {
    
    if(!url) {
        return "";
    }
    
    var lastSlash = url.lastIndexOf("/");

    if( lastSlash != -1 ) {
        url = url.substr(lastSlash+1);
    }
    
    return url;
};


/*
 * Convert : /home/user/bits/app/...  =>  app.user.hostname/suffix
 */

AXIS.Util.convertToSubdomainUrl = function( url ) {
    
    /* Get the suffix for the string */
    
    var suffix = url.replace(/^\/home\/(\w+)\/bits\/([\w-]+)\/?/, "");

    // RegExp.$1 contains the user name
    var username = RegExp.$1 || "";

    // RegExp.$2 contains the application name
    var appname = RegExp.$2 || "";

    /* Start the return URL from the hostname */
    var returnUrl = window.location.host.replace(/^www\./, "");

    returnUrl = (username ? (username + ".") : "") + returnUrl;
    returnUrl = (appname ? (appname + ".") : "") + returnUrl;
    returnUrl += suffix ? "/" + suffix : "";
    
    return returnUrl;
};

/* 
 * Return a date object parsed from a ISO8601 date string
 */
AXIS.Util.getDateFromISO8601String = function( string ) {
    
    if(!string) {
        return null;
    }

    var date = new Date();

    var regexp = /(\d\d\d\d)(-)?(\d\d)(-)?(\d\d)(T)?(\d\d)(:)?(\d\d)(:)?(\d\d)(\.\d+)?(Z|([+-])(\d\d)(:)?(\d\d))/;

    var d = string.match(regexp);

    if (d) {
        var offset = 0;

        date.setUTCFullYear( parseInt(d[1], 10) );
        date.setUTCMonth( parseInt(d[3], 10) - 1 );
        date.setUTCDate( parseInt(d[5], 10) );
        date.setUTCHours( parseInt(d[7], 10) );
        date.setUTCMinutes( parseInt(d[9], 10) );
        date.setUTCSeconds( parseInt(d[11], 10) );

        if (d[12])
            date.setUTCMilliseconds( parseFloat(d[12]) * 1000 );
        else
            date.setUTCMilliseconds(0);
            
        if (d[13] != 'Z') {
            offset = (d[15] * 60) + parseInt(d[17],10);
            offset *= ((d[14] == '-') ? -1 : 1);
            date.setTime(date.getTime() - offset * 60 * 1000);
        }
    }
    else {
        // return null ?
        date.setTime(Date.parse(string));
    }
    return date;   
}

/* Takes a date object as input and returns a pretty string */
AXIS.Util.prettifyDate = function(date){

    if(!date) {
        return "";
    }
    
    var	diff = (((new Date()).getTime() - date.getTime()) / 1000);
	var day_diff = Math.floor(diff / 86400);

    if ( isNaN(day_diff) )
    	return;
    	
    if( (day_diff < 0) || (day_diff > 31)) {
            return AXIS.Util.standardiseDate(date);
    }

    return day_diff == 0 && (
    		diff < 60 && "just now" ||
    		diff < 120 && "1 minute ago" ||
    		diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" ||
    		diff < 7200 && "1 hour ago" ||
    		diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") ||
    	day_diff == 1 && "Yesterday" ||
    	day_diff < 7 && day_diff + " days ago" ||
    	day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago";
}


AXIS.Util.standardiseDate = function(date) {
    
    if(!date) {
        return "";
    }
    
    var months = ["Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec"];
    
    var hours = date.getHours();
    var ampm = "AM";
    if(hours > 12) {
        ampm = "PM";
        hours -= 12;
    }
    
    var minutes = date.getMinutes();
    minutes = (minutes.length == 1 ) ? ("0" + minutes): minutes;
    
    var dateString = [months[date.getMonth()], date.getDate()].join(" ") + ", " + [date.getFullYear(), 
                    hours, ":", date.getMinutes(), ampm].join(" ");
    
    return dateString;
};


AXIS.evalJSON = function(text) {
      var json_object = !!text && !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(text.replace(/"(\\.|[^"\\])*"/g, ''))) && eval('(' + text + ')');
      /*")*/
      return json_object;
};


AXIS.Util.normalizeUUID = function( uuid ) {
    if(!uuid) {
        return "";
    }

    // TODO : moar error checking ?
    // an example href is "urn:uuid:bb3eb226-999a-11de-943c-99aa08b61ce1"
    // strip the urn:uuid:
    uuid = uuid.substr(uuid.lastIndexOf(":") + 1);
    
    // remove the hyphens
    uuid = uuid.replace(/-/g, "");
    
    return uuid;
}

// Truncate <text> to <maxLength>, and add ellipses at end
AXIS.Util.lang.truncate = function( text, maxLength )
{
    if( typeof(text) != 'string' )
        return text;
    
    if( text.length > maxLength )
        text = text.substr(0,maxLength-3) + '...';
    
    return text;
}

AXIS.Util.lang.inArray = function(needle, haystack){
    if (!AXIS.isArray(haystack)) 
        return false;
    
    for (var i = 0; i < haystack.length; i++) {
        if (haystack[i] == needle) 
            return true;
    }
    return false;
}
/*

http://webthumb.bluga.net/easythumb.php?user=4934&url=http%3A%2F%2Fbits.limebits.com%2Fbits%2Fbattleship%2F%3Fnobar&hash=42921d3c08f5838127ef9e16486b885c&size=medium&cache=1

*/

var thumbnailService = {
	
	uid : "4856"  
	
	, apiKey : "977982db725f4060c9506369bcb63fcb" 
				
	, serviceBaseUrl : "http://www.shrinktheweb.com/xino.php?embed=1&STWAccessKeyId=38628cb4c94ee1e&stwsize=lg&stwUrl=<!--url-->"

	, ourBaseUrl : "#"
				
	, getThumbUrlFromService: function( url, serviceName ) {
		
		// We're using ShrinkTheWeb now ..
		if(!url) {
			return "";
		}
		
		//var baseUrl = window.location.hostname.match(/\.limebits\.com$/) ? this.serviceBaseUrl : this.ourBaseUrl;

		var absUrl = AXIS.Util.getAbsoluteUrl(url);
		
		var finalUrl = this.serviceBaseUrl.replace(/<!--url-->/g, absUrl);
	
		return finalUrl;
	}
	
};

/*var thumbnailService = {
	
	count: 0,
	
	uid : "4856"  
	
	, apiKey : "977982db725f4060c9506369bcb63fcb" 
				
	, serviceBaseUrl : "http://webthumb.bluga.net/easythumb.php?user=<!--uid-->&url=<!--url-->&hash=<!--md5_hash-->&size=medium&cache=30"
	
	, ourBaseUrl : "#"
				
	, getThumbUrlFromService: function( url, serviceName ) {
		
		// just using webthumb for now .. if there are multiple options later
		// there will be a switch case here ?
		
		if(!url) {
			return "";
		}
		
		var baseUrl = window.location.hostname.match(/\.limebits\.com$/) ? this.serviceBaseUrl : this.ourBaseUrl;
		
		var encodedUrl = encodeURIComponent(AXIS.Util.getAbsoluteUrl(url));
		
		var finalUrl = baseUrl.replace( /<!--url-->/g, encodedUrl ).replace(/<!--uid-->/g, this.uid);
		
		var date = new Date();
		var dateString = date.getUTCFullYear().toString();
        var month = (date.getUTCMonth() + 1).toString();
        var day = date.getUTCDate().toString();

        if(month.length == 1) {
            month = "0" + month;
        }
        
        if(day.length == 1) {
            day = "0" + day;
        }

        dateString += month + day;
        
        var hashString = dateString + url + this.apiKey;
						
		var md5_hash = hex_md5( hashString );
		
		finalUrl = finalUrl.replace( /<!--md5_hash-->/g, md5_hash );
	
		return finalUrl;
	}
	
};*/
