contextPath="http://mbit.tv";



var closeCate;
var openCate; 

var openList;
var closeList;
var closeMoreList;
var closeAllList;

var bigList;
var mediumList;
var smallList;

var openDetail;
var closeDetail;
var openOther;
var closeOther;

var func;
var username;

function init() {

	
	
   var attributes = {
      height: { to: 2 }
   }
   closeCate = new YAHOO.util.Anim('categoryBoxes', attributes, 0.5, YAHOO.util.Easing.easeOut);
   attributes = {
      height: { to: 200 }
   }
   
   openCate = new YAHOO.util.Anim('categoryBoxes', attributes, 0.5, YAHOO.util.Easing.easeOut);

   attributes = {
      height: { to: 450 }
   }
   
   openList = new YAHOO.util.Anim('channelBox', attributes, 0.5, YAHOO.util.Easing.easeOut);

   attributes = {
      height: { to: 400 }
   }
   
   bigList = new YAHOO.util.Anim('listbox', attributes, 0.5, YAHOO.util.Easing.easeOut);

   attributes = {
      height: { to: 250 }
   }
   
   smallList = new YAHOO.util.Anim('listbox', attributes, 0.5, YAHOO.util.Easing.easeOut);
   
   attributes = {
      height: { to: 250 }
   }
   
   mediumList = new YAHOO.util.Anim('listbox', attributes, 0.5, YAHOO.util.Easing.easeOut);
   
   
   attributes = {
      height: { to: 300 }
   }
   
   closeList = new YAHOO.util.Anim('channelBox', attributes, 0.5, YAHOO.util.Easing.easeOut);
   
   attributes = {
      height: { to: 300 }
   }
   
   closeMoreList = new YAHOO.util.Anim('channelBox', attributes, 0.5, YAHOO.util.Easing.easeOut);

   attributes = {
      height: { to: 2 }
   }
   
   closeAllList = new YAHOO.util.Anim('channelBox', attributes, 0.5, YAHOO.util.Easing.easeOut);


   attributes = {
      height: { to: 2 }
   }
   
   closeDetail = new YAHOO.util.Anim('details', attributes, 0.5, YAHOO.util.Easing.easeOut);
   
   attributes = {
      height: { to: 250 }
   }
   
   openDetail = new YAHOO.util.Anim('details', attributes, 0.5, YAHOO.util.Easing.easeOut);

   attributes = {
      height: { to: 2 }
   }
   
   closeOther = new YAHOO.util.Anim('otherBox', attributes, 0.5, YAHOO.util.Easing.easeOut);
   
   attributes = {
      height: { to: 500 }
   }
   
   openOther = new YAHOO.util.Anim('otherBox', attributes, 0.5, YAHOO.util.Easing.easeOut);

	 var dd, dd2;
	 
}



/*
 * A List contains all the display objects
 */
var displayObjects = new dojo.collections.SortedList();
displayObjects.minimizeAll = function() {
	var val = displayObjects.getValueList();
	var i;
	
	
	var n=val.length;
	for (i=0;i<n;i++) {
		if (val[i].minimize)
			val[i].minimize();
	}
}


/********************************************************************************************/

/*
 * Sign Up Box
 */
var signUpBox = new n2n.FormBox("signUpBox", contextPath+"/JSON",  "jsonManager.signUp",document);
//var signUpBox = new n2n.FormBox("signUpBox");
signUpBox.title = "Sign Up"
signUpBox.form.submitLabel = "Sign Up";
signUpBox.titleBarBackground=contextPath+"/images/menu_header_center.jpg";
signUpBox.titleBarCloseButton=contextPath+"/images/menu_header_close_button.jpg";

signUpBox.field = signUpBox.addField("text", "username","Username",170, true, "required,duplicate"); 

signUpBox.field.validateDuplicate = function() {
	
	var fieldname = signUpBox.field.id + 'Progress';
	
	var jsonrpcjava = signUpBox.field.form.jsonrpcjava;
	
	var procedure = new n2n.JsonProcedure("jsonManager.validateUsername");
	procedure.addParam(signUpBox.field.getValue());
	
	procedure.loadingMethod = function() {
		Element.show(fieldname);
	}
	
	procedure.successMethod = function(data) {
		var result = data;
    		
    	Element.hide(fieldname);
    	    
    	if (Undefined(result)) {
	    	
	    } else if (result != "OK")  {
    		signUpBox.field.rules.replace("duplicate",false);
			signUpBox.field.showError();
		}
	}
	
	procedure.errorMethod = function(data) {
		alert("Error| code["+data.code+"]|msg["+data.msg+"]|trace["+data.trace+"]");  
		Element.hide(fieldname);
	}
	
	jsonrpcjava.run(procedure);
	
	return true;
}

signUpBox.field2 = signUpBox.addField("password", "password","Password",170, true); 
signUpBox.field2.label2 = "Re-enter Password";

signUpBox.field3 = signUpBox.addField("text", "firstName","First Name",170, true, "required"); 
signUpBox.field4 = signUpBox.addField("text", "lastName","Last Name",170, true, "required"); 
signUpBox.field5 = signUpBox.addField("text", "email","Email Address",170, true, "required"); 

signUpBox.hide = function() {
	Effect.Puff(signUpBox.id, {duration:1});
};
signUpBox.show = function() {
	Effect.Appear(signUpBox.id, {duration:1});
};

signUpBox.paint(true);

signUpBox.form.loadingMethod = function() {
	var progressBar = contextPath+"/images/pleasewait.gif";
	var s= "Your request is being processed... Please wait!";
	s= s+ "<br/><br/><br/><br/>";
	s= s+ "<img src=\""+progressBar+"\">";
	dialogBox.setContent(s);
	dialogBox.show();
}

signUpBox.form.successMethod = function(data) {
	
	if (data.returnCode == 1) {
		//Successful
		var s="";
        s=s+    "Congratulations! Your channel is created successfully.<br>";
        s=s+	"Now you can start to add media to your channel";
        confirmationBox.setContent(s);
        confirmationBox.show();
		
		
	} else if (data.returnCode == 2) {
		//Username exist
		alert("The username you have chosen is not available. Please choose another username");
		dialogBox.hide();
	} else {
		alert("Unexpected Error");
		dialogBox.hide();
	}
}

displayObjects.add("signUpBox",signUpBox);


/********************************************************************************************/

/*
 * Profile Box
 */
var profileBox = new n2n.FormBox("profileBox", contextPath+"/JSON", "jsonManager.updateProfileUser",document);
profileBox.title = "Edit Profile"
profileBox.form.submitLabel = "Update";
profileBox.titleBarBackground= "images/menu_header_center.jpg";
profileBox.titleBarCloseButton= "images/menu_header_close_button.jpg";

profileBox.field1 = profileBox.addField("hidden", "username"); 
profileBox.field2 = profileBox.addField("password", "password","New Password",170, true); 
profileBox.field2.label2 = "Re-enter New Password";
profileBox.field3 = profileBox.addField("text", "email","Email Address",170, true, "required"); 
profileBox.field4 = profileBox.addField("text", "firstName","First Name",170, true, "required"); 
profileBox.field5 = profileBox.addField("text", "lastName","Last Name",170, true, "required"); 
profileBox.field6 = profileBox.addField("text", "msisdn", "Mobile No", 170, true, "required");


profileBox.hide = function() {
	Effect.Puff(profileBox.id, {duration:1});
};
profileBox.show = function() {
	Effect.Appear(profileBox.id, {duration:1});
	
	profileBox.setValue("username", profile_username);
	//profileBox.setValue("password", profile_password);
	
	var email = document.getElementById("profileBox_email");
	email.value = profile_email;
	
	var firstName = document.getElementById("profileBox_firstName");
	firstName.value = profile_firstName;
	
	var lastName = document.getElementById("profileBox_lastName");
	lastName.value = profile_lastName;
	
	var msisdn = document.getElementById("profileBox_msisdn");
	msisdn.value = profile_msisdn;
	
};

profileBox.paint(true);

profileBox.form.loadingMethod = function() {
}

profileBox.form.successMethod = function(data) {	
	//Successful
	alert("Update successful");
	profileBox.hide();
	window.location.href = contextPath + "/profilepage.do?username=" + profile_username;
}

displayObjects.add("profileBox",profileBox);



/********************************************************************************************/
/*
 * Picture Box
 */
var pictureBox = new n2n.FormBox("pictureBox",contextPath+ "/JSON", "jsonManager.updateItem", document);
pictureBox.title = "Edit Photo"
pictureBox.form.submitLabel = "Update";
pictureBox.titleBarBackground= "images/menu_header_center.jpg";
pictureBox.titleBarCloseButton= "images/menu_header_close_button.jpg";

pictureBox.field1 = pictureBox.addField("hidden", "pictureId"); 
pictureBox.field2 = pictureBox.addField("text", "picturetitle", "Title",170, true, "required"); 
pictureBox.field3 = pictureBox.addField("textarea", "description","Description",2, true, "required"); 


pictureBox.hide = function() {
	Effect.Puff(pictureBox.id, {duration:1});
};
pictureBox.show = function() {	
	Effect.Appear(pictureBox.id, {duration:1});
	
	var pictureId = document.getElementById('itemId').value;
	pictureBox.setValue("pictureId", pictureId);
	
	loadJSON();
	try {
		data = jsonrpc.jsonManager.getItem(pictureId);
		var title = document.getElementById("pictureBox_picturetitle");
		title.value = data.title;
		
		var description = document.getElementById("pictureBox_description");
		description.value = data.description;
		
	} catch (ex) {
		alert (ex);
	}
};

pictureBox.paint(true);

pictureBox.form.loadingMethod = function() {
}

pictureBox.form.successMethod = function(data) {	
	//Successful
	alert("Update successful");
	pictureBox.hide();
	//reload
	var title = document.getElementById("pictureBox_picturetitle");
	loadTitle(title.value);
	var description = document.getElementById("pictureBox_description");
	loadDescription(description.value);
	window.location.reload(true);
}

displayObjects.add("pictureBox",pictureBox);

/********************************************************************************************/




/*
 * Video Box
 */
var videoBox = new n2n.FormBox("videoBox",contextPath+ "/JSON", "jsonManager.updateItem", document);
videoBox.title = "Edit Video"
videoBox.form.submitLabel = "Update";
videoBox.titleBarBackground= "images/menu_header_center.jpg";
videoBox.titleBarCloseButton= "images/menu_header_close_button.jpg";

videoBox.field1 = videoBox.addField("hidden", "videoId"); 
videoBox.field2 = videoBox.addField("text", "videotitle", "Title",170, true, "required"); 
videoBox.field3 = videoBox.addField("textarea", "description","Description",2, true, "required"); 


videoBox.hide = function() {
	Effect.Puff(videoBox.id, {duration:1});
};
videoBox.show = function() {	
	Effect.Appear(videoBox.id, {duration:1});
	
	var videoId = document.getElementById('itemId').value;
	videoBox.setValue("videoId", videoId);
	
	loadJSON();
	try {
		dataArray = jsonrpc.jsonManager.getItem(videoId);
		var title = document.getElementById("videoBox_videotitle");
		title.value = dataArray.title;
		
		var description = document.getElementById("videoBox_description");
		description.value = dataArray.description;
		
	} catch (ex) {
		alert (ex);
	}
};

videoBox.paint(true);

videoBox.form.loadingMethod = function() {
}

videoBox.form.successMethod = function(data) {	
	//Successful
	alert("Update successful");
	videoBox.hide();
	//window.location.reload(true);
	var username = document.getElementById("login_username");
	var media_type = document.getElementById("media_type");
	reloadMedia(media_type.value, username.value, showCom);
}

displayObjects.add("videoBox",videoBox);

/********************************************************************************************/



/********************************************************************************************/

/*
 * Dialog Box
 */
var dialogBox = new n2n.Box("dialogBox");

dialogBox.hasTitleBar = false;

dialogBox.hide = function() {
	Effect.Puff(dialogBox.id, {duration:1});
};
dialogBox.show = function() {
	Effect.Appear(dialogBox.id, {duration:1});
};


dialogBox.paint(true);

displayObjects.add("dialogBox", dialogBox);

/********************************************************************************************/


/********************************************************************************************/

/*
 * Confirmation Box
 */
var confirmationBox = new n2n.Box("confirmationBox");

confirmationBox.titleBarBackground=contextPath+"/images/menu_header_center.jpg";
confirmationBox.titleBarCloseButton=contextPath+"/images/menu_header_close_button.jpg";

confirmationBox.hide = function() {
	this.box.getDiv().style.display='none';
	dialogBox.hide();
	signUpBox.hide();
	
};
confirmationBox.show = function() {
	Effect.Appear(confirmationBox.id, {duration:1});
};


confirmationBox.paint(true);

displayObjects.add("confirmationBox", confirmationBox);

/********************************************************************************************/


/*
 * Category Box
 */
 
var content = document.getElementById("content");
var categoryLabel = new n2n.Div("categoryLabel", content);
categoryLabel.paint();
categoryLabel.write("Categories");

var categoryBox = new n2n.Grid("categoryBox", content);

var col = categoryBox.addColumn("Id");

col.isVisible = false;

categoryBox.addColumn("Name");


categoryBox.updateData = function(data) {
	
	var tb = this.getTable();
	for (i=0;i<data.length;i++) {
		var result = categoryBox.addData([data[i].id,"&nbsp;"+data[i].name]);
		var cell = result[1];
		
		cell.categoryId = result[0].data;
		if (data[i].leaf) {
			cell.onClickFunction = function() {
				if (this.cell) {
					//channelListBox.getData(this.cell.categoryId);
					subCategoryBox.clear();
				}
			}
		} else {
			cell.onClickFunction = function() {
				var temp = document.getElementById("content");
				
				if (this.cell) {
					subCategoryBox.getData(this.cell.categoryId);
					//channelListBox.clear();
				}
			}
		}
	}
	
}

categoryBox.getData = function(loaded) {
	
	var jsonrpcjava = signUpBox.field.form.jsonrpcjava;
	
	var procedure = new n2n.JsonProcedure("jsonManager.getCategories");
		
	procedure.loadingMethod = function() {
		if (!loaded)
			categoryBox.getDiv().innerHTML = "&nbsp;Loading Categories";
	}
	
	procedure.successMethod = function(data) {
		categoryBox.reset();
		var result = data;
    	categoryBox.updateData(data);    
 		categoryBox.paint();   
 		categoryBox.getTable().width='100%';
 		categoryBox.getTable().cellPadding=0;
 		categoryBox.getTable().cellSpacing=0;
 		tigra_tables('categoryBox_table', 0, 0, '#ffffff', '#d6f2ff', '#e6e6e6', '#7cb4ff');
 		subCategoryBox.clear();
 		//channelListBox.clear();
	}
	
	procedure.errorMethod = function(data) {
		alert("Error| code["+data.code+"]|msg["+data.msg+"]|trace["+data.trace+"]");  
		categoryBox.getDiv().innerHTML = "&nbsp;Error Loading Categories";
	}
	
	jsonrpcjava.run(procedure);
	
	return true;
}

categoryBox.paint();
categoryBox.getData();

categoryBox.minimize = function() {
	categoryLabel.hide();
	categoryBox.hide();
}
categoryBox.maximize = function() {
	categoryBox.getData(true);
	categoryLabel.show();
	categoryBox.show();
}
categoryBox.minimize();
displayObjects.add("categoryBox", categoryBox);

/********************************************************************************************/

/*
 * Sub-Category Box
 */
var subCategoryLabel = new n2n.Div("subCategoryLabel", content);
subCategoryLabel.paint();
subCategoryLabel.write("Sub-categories");
 
var subCategoryBox = new n2n.Grid("subCategoryBox", content);
var col = subCategoryBox.addColumn("Id");
col.isVisible = false;
subCategoryBox.addColumn("Name");
subCategoryBox.updateData = function(data) {
	
	var tb = this.getTable();
	for (i=0;i<data.length;i++) {
		var result = subCategoryBox.addData([data[i].id,"&nbsp;"+data[i].name]);
		var cell = result[1];
		
		cell.subCategoryId = result[0].data;
		
		cell.onClickFunction = function() {
			if (this.cell) {
				//channelListBox.getData(this.cell.subCategoryId);
			}
		}
	}
	
}

subCategoryBox.clear = function() {
	subCategoryBox.getDiv().innerHTML = "&nbsp;Please choose one of the categories";
}

subCategoryBox.getData = function(id) {
	
	var jsonrpcjava = signUpBox.field.form.jsonrpcjava;
	
	var procedure = new n2n.JsonProcedure("jsonManager.getSubCategories");
	
	procedure.addParam(id);
		
	procedure.loadingMethod = function() {
		subCategoryBox.getDiv().innerHTML = "&nbsp;Loading Sub-Categories";
	}
	
	procedure.successMethod = function(data) {
		
		subCategoryBox.reset();
		if (data.length==0) {
			subCategoryBox.getDiv().innerHTML = "&nbsp;No Sub-Categories";
			return;
		}
		var result = data;
    	subCategoryBox.updateData(data);    
 		subCategoryBox.paint();   
 		subCategoryBox.getTable().width='100%';
 		subCategoryBox.getTable().cellPadding=0;
 		subCategoryBox.getTable().cellSpacing=0;
 		tigra_tables('subCategoryBox_table', 0, 0, '#ffffff', '#d6f2ff', '#e6e6e6', '#7cb4ff');
	}
	
	procedure.errorMethod = function(data) {
		alert("Error| code["+data.code+"]|msg["+data.msg+"]|trace["+data.trace+"]");  
		subCategoryBox.getDiv().innerHTML = "&nbsp;Error Loading Categories";
	}
	
	jsonrpcjava.run(procedure);
	
	return true;
}


subCategoryBox.paint();


subCategoryBox.minimize = function() {
	subCategoryLabel.hide();
	subCategoryBox.hide();
}
subCategoryBox.maximize = function() {
	subCategoryLabel.show();
	subCategoryBox.show();
}
subCategoryBox.minimize();
displayObjects.add("subCategoryBox", subCategoryBox);

/********************************************************************************************/


/*
 * Create Channel Box 2
 */
var createChannelBox2 = new n2n.Box("createChannelBox2", document);
createChannelBox2.title = "Create Channel Step 2"
createChannelBox2.titleBarBackground=contextPath+"/images/menu_header_center.jpg";
createChannelBox2.titleBarCloseButton=contextPath+"/images/menu_header_close_button.jpg";

createChannelBox2.content = "Please wait while we are retrieving some information from the server.";

createChannelBox2.hide = function() {
	Effect.Puff(createChannelBox2.id, {duration:1});
};
createChannelBox2.show = function() {
	Effect.Appear(createChannelBox2.id, {duration:1});
};

createChannelBox2.paint(true);

displayObjects.add("createChannelBox2",createChannelBox2);

/********************************************************************************************/

