// starbak js utility class
dojo.declare("SbCommon", null, {
        constructor: function(){
        }
});

SbCommon.search = function(typeIndex, field){
	typeIndex = typeIndex || SbCommon.searchTypeIndex;
	field = field || 'searchField';
	if(typeIndex==SbCommon.SEARCH_TYPE_DISABLED) return;
	
	var searchQuery = dojo.byId(field).value;
	var url = SbUtil.appendParamToUrl(SbCommon.searchTypes[typeIndex].url, {searchQuery:searchQuery});
	dojo.xhrGet({
        url : url,
        preventCache : true,
        //Run this function if the request is successful
        load : function(response, ioArgs) {
            SbUtil.innerHTML(SbCommon.searchTypes[typeIndex].responseElem, response);
            SbCommon.searchTypes[typeIndex].cleanup();
            return response;
        },

        //Run this function if the request is not successful
        error : function(response, ioArgs) {
        	dojo.byId(SbCommon.searchTypes[typeIndex].repsonseElem).innerHTML = "SbLinkList request failed: " + SbUtil.dojoErrorToString(response);
        	SbCommon.searchTypes[typeIndex].cleanup();
        	return response;
        }
    });
}

SbCommon.searchDisable = function(disabled){
	SbCommon.searchTypeIndex = SbCommon.SEARCH_TYPE_DISABLED;
	var searchElem = dojo.byId('searchField');
	//var searchBtnElem = dojo.byId('searchButton');
	if (searchElem != null)
		searchElem.disabled = disabled;
	/*if (searchBtnElem != null)
		searchBtnElem.disabled = disabled;*/
}

SbCommon.searchTypeIndex = 0;

SbCommon.nop = function() {}

SbCommon.usersCleanup = function()
{
	SbUtil.innerHTML("programListingsBreadCrumbs", "");
	SbUtil.innerHTML("manageUsersForm", "&nbsp;");
}

SbCommon.formatTime = function(time){
	// Function to convert time from milliseconds into a string format
	// convert time into seconds
	time = Math.floor(time / 1000);
	var seconds = time % 60;
	var minutes = ((time - seconds) % 3600) / 60;
	var hours   = Math.floor(time / 3600);
	
	if( seconds < 10 )
		seconds = "0" + seconds;
	if( minutes < 10 && hours != 0)
		minutes = "0" + minutes;
	
	return (hours == 0 ? "" : hours + ":" ) + 
		   minutes + ":" +
		   seconds;
}
	
SbCommon.SEARCH_TYPE_DISABLED = -1;
SbCommon.SEARCH_TYPE_PROGRAM = 0;
SbCommon.SEARCH_TYPE_USER = 1;
SbCommon.SEARCH_TYPE_CREATE_ALL_PROG = 2;
SbCommon.SEARCH_TYPE_CREATE_LIBRARY = 3;
SbCommon.SEARCH_TYPE_MANAGE_USERS = 4;
SbCommon.SEARCH_TYPE_MANAGE_PUBS = 5;
SbCommon.SEARCH_TYPE_USER_DIALOG = 6;

SbCommon.searchTypes = new Array( 
		{type:SbCommon.SEARCH_TYPE_PROGRAM,
			url:'view_programList.html',
			responseElem:'programListingsContent',
			cleanup: SbCommon.nop},
		{type:SbCommon.SEARCH_TYPE_USER,
			url:'',
			responseElem:'',
			cleanup: SbCommon.nop},
		{type:SbCommon.SEARCH_TYPE_CREATE_ALL_PROG,
			url:'create_allPrograms_programList.html',
			responseElem:'programListingsContent',
			cleanup: SbCommon.nop},
		{type:SbCommon.SEARCH_TYPE_CREATE_LIBRARY,
			url:'create_library_mediaList.html',
			responseElem:'programListingsContent',
			cleanup: SbCommon.nop},
		{type:SbCommon.SEARCH_TYPE_MANAGE_USERS,
			url:'manage_users_userList.html',
			responseElem:'programListingsContent',
			cleanup: SbCommon.usersCleanup},
		{type:SbCommon.SEARCH_TYPE_MANAGE_PUBS,
			url:'manage_publishers_publisherList.html',
			responseElem:'publisherResultsContent',
			cleanup: SbCommon.nop},
		{type:SbCommon.SEARCH_TYPE_USER_DIALOG,
			url:'dialog_addSecurityUserList.html',
			responseElem:'userSercurityDialogContent',
			cleanup: SbCommon.nop}
	);
