/**
 * @author pixelmonkey
 */
// define COGTREE namespace
COGTREE = {
	// active widgets
	widgets: {},
	// debug utilities
	debug: {},
	util: {}
};

/* This is a little bit of a hack; we store the user_id in a DOM element on index.html,
 * so this code retrieves it.  I guess in practice we should be using the cookie/session,
 * or something?
 */
COGTREE.get_user_id_from_page = function() {
	var user_id_jq = $("#userid");
	var user_id_str = user_id_jq.html();
	return user_id_str;	
}

COGTREE.relativize_media = function(url) {
	return COGTREE.MEDIA_ROOT + "/" + url;	
};

COGTREE.relativize_url = function(url) {
	return COGTREE.APP_ROOT + "/" + url;
}

COGTREE.interest_req_url = function() {
	return 	COGTREE.APP_ROOT + 
			"/user/" + 
			COGTREE.get_user_id_from_page() + 
			"/interests"; 
}

COGTREE.score_explanation_req_url = function(userid, itemid) {
	var req_url = COGTREE.relativize_url( 
			"user/" + 
			userid + 
			"/item/" + itemid +
            "/score_explanations");
    // console.log(req_url);
    return req_url;
}

COGTREE.plink_anchor = function(unique_id, anchor_text, color) {
	var link = "<a href='" + COGTREE.plink(unique_id) + "' target='_plink'";
	if (color) {
		link += " style='color: " + color + ";'";
	}
	link += ">";
	link += anchor_text + "</a>";
	return link;
}

COGTREE.plink = function(unique_id) {
	return COGTREE.relativize_url("plink/" + unique_id);
}

COGTREE.debug.OFFLINE_DATA = false;

COGTREE.debug.lastWidth = null;

COGTREE.debug.lastResizeTime = new Date();

COGTREE.debug.relayout = function() {
		var lastWidth = COGTREE.debug.lastWidth;
		var currentWidth = window.innerWidth;
		if (lastWidth == currentWidth) {
			return;
		}
		COGTREE.debug.lastWidth = currentWidth;
		
		COGTREE.debug.forceResize("win");
}

COGTREE.debug.forceResize = function (type, delay) {
    //
    // THIS CODE IS SO BROKEN; but it's better than the Feed 
    // 
	var now = new Date();
	var millis_since_last = COGTREE.debug.lastResizeTime.getElapsed(now);
	var happened_recently = millis_since_last < 500;
	
	if (!happened_recently) {
		Ext.utilz.msg('Resizing Page', '(this might take a few seconds)');
	}

	var task1 = new Ext.util.DelayedTask(function(){
		// add grid to layout and re-render
		var centerRegion = Ext.getCmp("centerRegion");

		centerRegion.doLayout();
		centerRegion.ownerCt.doLayout();
		
		var eastRegion = Ext.getCmp("eastRegion");
        // only resize the eastRegion when the window resize
        // event happens; during collapse/expand, keep it the same
        if (type !== undefined && type == "win") {
            eastRegion.setWidth("40%");
            eastRegion.syncSize();
        }
		eastRegion.doLayout();
		eastRegion.ownerCt.doLayout();
		
		COGTREE.widgets.grid.setWidth(centerRegion.getWidth()-2);
		COGTREE.widgets.grid.syncSize();
		COGTREE.debug.lastResizeTime = new Date();
	});
	task1.delay(delay === undefined ? 500 : delay);	
}

COGTREE.openUrl = function(url) {
    if (url === undefined) {
        Ext.Msg.alert("No link for article", "There is no link for the selected article.");
        return;
    }
    if (url.toString() == "NO_SELECTION") {
        Ext.Msg.alert("No article selected", "You must select an article in the grid before opening.");
        return;
    }
    window.open(url);
};

COGTREE.util.truncate = function truncate(text, length, ellipsis) {    

    // Set length and ellipsis to defaults if not defined
    if (typeof length == 'undefined') var length = 100;
    if (typeof ellipsis == 'undefined') var ellipsis = '...';

    // Return if the text is already lower than the cutoff
    if (text.length < length) return text;

    // Otherwise, check if the last character is a space.
    // If not, keep counting down from the last character
    // until we find a character that is a space
    for (var i = length-1; text.charAt(i) != ' '; i--) {
        length--;
    }

    // The for() loop ends when it finds a space, and the length var
    // has been updated so it doesn't cut in the middle of a word.
    return text.substr(0, length) + ellipsis;
};
