/*
 * 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 API( )
{
	var api = this;
	var gloto = new Gloto.Api( config.getApiKey() );
 	this.getCellblockInfo = function( id, callback )
  	{
		var params = {
			id: id
		};
		
    	gloto.call( "CellblockInfo", params, callback );
  	};
  
	this.setPendingTitleIndicator = function( )
	{	
		var params = {
			id: config.getId()
		};
		
		gloto.call( "NumPendingFrames", params, function( data )
		{
			if( util.valid( data ) )
			{
				document.title = "(" + data.count + " Pending) " + cellblock.documentTitle;
			}else{
				util.error( data );
			}
		});
	};
	
  this.setFrameCaption = function( id, callback )
  {
		util.console.println( "Setting frame caption: " + util.getElement("caption").value );
		
		var params = {
			id: id,
			caption: util.getElement("caption").value
		};
		
		gloto.call( "SetFrameCaption", params, callback );
  };

  this.rotateFrameLeft = function(id, callback )
  {
		util.console.println( "Rotate frame left ..." );
		
		var params = {
			id: id
		};
		
		gloto.call( "RotateFrameLeft", params, callback );
  };

  this.rotateFrameRight = function(id, callback){
		util.console.println( "Rotate frame right ..." );
		
		var params = {
			id: id
		};
		
		gloto.call( "RotateFrameRight", params, callback );
  };

	this.setFrameAttributeValues = function( form ){
		util.console.println( "Updating frame attributes ..." );
		var id = form["id"].value;
		for( var i = 0; i < form.length; i++ ){
			if( form[i].name != "" && form[i].name != "id" ){
				var key = form[i].name;
				var value = form[i].value;
				var callback = function( data ){
					if( !util.valid( data ) ){
						util.error( data );
					}
				};
				api.setFrameAttributeValue( id, key, value, callback );
			}
		}
	};

  this.setFrameAttributeValue = function( id, name, value, callback )
  {
		var params = {
			id: id,
			attributeName: name,
			attributeValue: value
		};
		
    gloto.call( "SetFrameAttributeValue", params, callback );
  };

  this.attachFrameAttribute = function( id, name, callback )
  {
		var params = {
			id: id,
			attributeName: name,
			attributeValue: ""
		};
		
    gloto.call( "SetFrameAttributeValue", params, callback );
  };

  this.approveFrame = function( id, callback )
  {
		var params = {
			id: id
		};
		
		gloto.call( "ApprovePendingFrame", params, callback);
  };

  this.deleteFrame = function( id, callback )
  {
		var params = {
			id: id
		};
		
    gloto.call( "DeleteFrame", params, callback );
  };
  
  this.getSession = function( callback )
  {
		var params = {};
    gloto.call( "GetSessionInfo", params, callback );
  };
	
	this.createRealmSkin = function( name, attributes, callback )
	{
		var params = {
			name: name,
			realmId: config.getRealm(),
			attributeList: attributes
		};
		
		gloto.call( "CreateDisplayModel", params, callback );
	};
	
	this.deleteRealmSkin = function( id, callback )
	{
		var params = {
			id: id
		};
		
		gloto.call( "DeleteDisplayModel", params, callback );
	};
	
	this.getRealmSkins = function( callback )
	{
		var params = {
			realmId: config.getRealm(),
			offset: 0,
			count: 10
		};
		
		gloto.call( "GetDisplayModels", params, callback );
	};
	
	this.setRealmSkin = function( id, attributes, callback )
	{
		var params = {
			id: id,
			attributes: attributes
		};
		
		gloto.call( "SetDisplayModel", params, callback );
	};
	
	this.getFrameComments = function(id, offset, count, filter, callback )
	{
		var frameId = id || config.get("frame");
		var params = {
			frameId: frameId,
			offset: offset || 0,
			count: count || 10,
			filter: filter
		};
		
		gloto.call( "GetComments", params, callback );
	};	

	this.approveCellblockComment = function( id, index, callback ){
		var params = {
			id: id || config.getId(),
			index: index
		};
		gloto.call( "PostComment", params, callback );
	};

	this.deleteCellblockComment = function( id, index, callback ){
		var params = {
			id: id || config.getId(),
			index: index
		};
		gloto.call( "DeleteComment", params, callback );
	};

	this.approveFrameComment = function( frameId, index, callback ){
		var params = {
			frameId: frameId || config.get("frame"),
			index: index
		};
		gloto.call( "PostComment", params, callback );
	};

	this.deleteFrameComment = function( frameId, index, callback ){
		var params = {
			frameId: frameId || config.get("frame"),
			index: index
		};
		
		gloto.call( "DeleteComment", params, callback );
	};
	
	this.getCellblockComments = function(offset, count, filter, callback )
	{
		var params = {
			id: config.getId(),
			offset: offset,
			count: count,
			filter: filter
		};
		
		gloto.call( "GetComments", params, callback );
	};
	
	this.postComment = function( comment, target, callback )
	{
		var params = {
			comment: comment,
			source: "web"
		};
		
		if( target.cellblock ){ params.id = target.cellblock; }
		if( target.realm ){ params.realmName = target.realm; }
		if( target.user ){ params.userName = target.user; }
		if( target.frame ){ params.frameId = target.frame; }

		gloto.call( "PostComment", params, callback );
	};
	
	this.sendToPhone = function( number, carrier, callback )
	{
		var params = {
			id: config.getId(),
			phone: number,
			carrier: carrier
		};

		gloto.call( "SendCellblockToPhone", params, callback );
	};
	
	this.login = function( username, password, callback )
	{
		var params = {
			screenName: username,
			password: password,
			realmId: config.getRealm()
		};
		
		gloto.call( "Login", params, callback );
	};
	
	this.realmLogin = function( password, callback )
	{
		var params = {
			realmId: config.getRealm(),
			realmPassword: password
		};
		
		gloto.call( "RealmLogin", params, callback );
	};
	
	this.logout = function( callback )
	{
		var params = {};
		
		gloto.call( "Logout", params, callback );
	};
	
	this.getFrameInfo = function( callback )
	{
		var params = {
			frameId: config.get("frame")
		};
		gloto.call( "GetFrameInfo", params, callback );
	};
	
	this.getPreviousFrame = function( lastIndex, callback )
	{
		var params = {
			id: config.getId(),
			direction: -1,
			maxWIndex: -1,
			lastIndex: lastIndex
		};
		
		gloto.call( "More", params, callback );
	};
	
	this.getNextFrame = function( lastIndex, callback )
	{
		var params = {
			id: config.getId(),
			direction: 1,
			maxWIndex: -1,
			lastIndex: lastIndex
		};
		
		gloto.call( "More", params, callback );
	};

	this.getFrameRating = function( callback )
	{
		var params = {
			frameId: config.get("frame")
		};
		gloto.call( "GetRating", params, callback );
	};
	
	this.getCellblockRating = function( callback )
	{
		var params = {
			id: config.getId()
		};		
		
		gloto.call( "GetRating", params, callback );
	};	

	this.setFrameRating = function( rating, callback )
	{
		var params = {
			rating: rating,
			frameId: config.get("frame")
		};
	
		gloto.call( "SetRating", params, callback );
	};
	
	this.setCellblockRating = function( rating, callback )
	{
		var params = {
			rating: rating,
			id: config.getId()
		};
		
		gloto.call( "SetRating", params, callback );
	};	
	
	this.numRealmCellblocks = function( callback )
	{
		var params = {
			realmId: config.getRealm()
		};
		
		gloto.call( "NumRealmCellblocks", params, callback );
	};
	
	this.numRealmUsers = function( callback )
	{
		var params = {
			realmId: config.getRealm()
		};
		
		gloto.call( "NumRealmUsers", params, callback );
	};
	
	this.getRealmCellblocks = function( offset, count, callback )
	{
		var params = {
			offset: offset,
			count: count,
			realmId: config.getRealm()
		};
		
		gloto.call( "GetRealmCellblocks", params, callback );
	};

	this.getRealmUsers = function( offset, count, callback )
	{
		var params = {
			offset: offset,
			count: count,
			realmId: config.getRealm()
		};
		
		gloto.call( "GetUsersInRealm", params, callback );
	};
	
	this.getUserAttributeValue = function( user, attribute, callback ){
		var params = {
			realmId: config.getRealm(),
			screenName: user,
			attributeName: attribute
		};
		
		gloto.call( "GetUserAttributeValue", params, callback );
	};
	
	this.getUserAttributeValues = function( user, callback )
	{
		var params = {
			realmId: config.getRealm(),
			screenName: user
		};
		
		gloto.call( "GetUserAttributeValues", params, callback );
	};

	this.setCellblockAttributeValue = function( id, name, value, callback )
	{
		var params = {
			id: id,
			attributeName: name,
			attributeValue: value
		};
		
		gloto.call( "SetCellblockAttributeValue", params, callback );
	};

	this.getCellblockAttributeValues = function( id, callback )
	{
		var params = {
			realmId: config.getRealm(),
			id: id
		};
		
		gloto.call( "GetCellblockAttributeValues", params, callback);
	};

	this.setCellblockReceipt = function( id, enabled, subject, message, callback )
	{
		var params = {
			id: id,
			receiptEnabled: enabled,
			receiptSubject: subject,
			receiptBody: message
		};
		
		gloto.call( "SetCellblockInfo", params, callback );
	};

	this.setCellblockInfo = function( id, preview, callback )
	{
		var params = {
			id: id,
			preview: preview,
			receiptEnabled: config.get("receiptEnabled"),
			receiptSubject: config.get("receiptSubject"),
			receiptBody: config.get("receiptMessage")
		};
		
		gloto.call( "SetCellblockInfo", params, callback );
	};
	
	this.setCellblockInfoExtended = function( id, from, callback ){
		var params = {
			id: id,
			receiptFromAddress: from
		};
		
		gloto.call( "SetCellblockInfoExtended", id, from, callback );
	};
	
	this.setUserInfo = function( user, email, password, callback )
	{
		var params = {
			realmId: config.getRealm(),
			screenName: user,
			email: email,
			password: password
		};
		
		gloto.call( "SetUserInfo", params, callback );
	};
	
	this.setUserAttributeValue = function( user, attribute, value, callback )
	{
		var params = {
			realmId: config.getRealm(),
			screenName: user,
			attributeName: attribute,
			attributeValue: value
		};
		
		gloto.call( "SetUserAttributeValue", params, callback );
	};
	
	this.setPreviewStatus = function( id, preview, callback )
	{
		var params = {
			id: id,
			preview: preview
		};
		
	  gloto.call( "SetCellblockInfo", params, callback );
	};
	
	this.setPreviewCommentStatus = function( id, preview, callback )
	{
		var params = {
			id: id,
			previewComments: preview
		};
		
	  gloto.call( "SetCellblockInfo", params, callback );
	};	
	
	this.createCellblock = function( title, email, callback )
	{
		var params = {
			title: title,
			realmId: config.getRealm(),
			email: email,
			previewComments: true
		};
		
		gloto.call( "CreateRealmCellblock", params, callback );
	};
	
	this.createUser = function( name, password, email, callback )
	{
		var params = {
			realmId: config.getRealm(),
			screenName: name,
			password: password,
			email: email
		};
		
		gloto.call( "CreateUser", params, callback );
	};
	
	this.createPublicUser = function( name, password, email, number, callback )
	{
		var params = {
			realmId: config.getRealm(),
			screenName: name,
			password: password,
			email: email,
			mobileNumber: number
		};
		
		gloto.call( "CreateUser", params, callback );
	};
	
	this.deleteCellblock = function( id, callback )
	{
		var params = {
			id: id
		};
		
		gloto.call( "DeleteCellblock", params, callback );
	};
	
	this.deleteUser = function( user, callback )
	{
		var params = {
			realmId: config.getRealm(),
			screenName: user
		};
		
		gloto.call( "DeleteUser", params, callback );
	};
	
	this.numFrames = function( id, callback )
	{
		var params = {
			id: id
		};
		
		gloto.call( "NumFrames", params, callback );
	};
	
	this.numPendingFrames = function( id, callback )
	{
		var params = {
			id: id
		};
		
		gloto.call( "NumPendingFrames", params, callback );
	};
	
	this.numAcceptedFrames = function( id, callback )
	{
		var params = {
			id: id
		};
		
		gloto.call( "NumAcceptedFrames", params, callback );
	};
	
	this.frameInfo = function( id, offset, count, callback )
	{
		var params = {
			id: id,
			offset: offset,
			count: count
		};
		
		gloto.call( "FrameInfoForCellblock", params, callback );
	};
	
	this.pendingFrameInfo = function( id, offset, count, callback )
	{
		var params = {
			id: id,
			offset: offset,
			count: count
		};
		
		gloto.call( "PendingFrameInfoForCellblock", params, callback );
	};
	
	this.acceptedFrameInfo = function( id, offset, count, callback )
	{
		var params = {
			id: id,
			offset: offset,
			count: count
		};
		
		gloto.call( "AcceptedFrameInfoForCellblock", params, callback );
	};

	this.getFrameAttributes = function( id, callback )
	{
		var params = {
			id: id
		};
		
		gloto.call( "GetFrameAttributeValues", params, callback );
	};

	this.getPendingFrameComments = function( id, callback ){	};
	
	this.setAnnotation = function(props, save, callback)
	{
		var params = { 'frameId' : config.get("frame") };
		if( save != null ){
			params.save = save;
		}
	
		for( key in props )
		{
			params[key] = props[key];
		}
	
		gloto.call( "SetAnnotation", params, callback );
	};
}

var api = new API();
