var categoryCounter = 0;
var categoryStore;
var storeArray = new Array();
function loadCategories()
{
	deleteAllCategories();
	dojo.xhrGet({
		url: '/cat?a=v',
		handleAs: 'json',
		preventCache: true,
		load: function(response, ioArgs) {
			if (dojo.byId('categoryLoading'))
			{
				dojo.byId('categoryLoading').parentNode.removeChild(dojo.byId('categoryLoading'));
			}

			var cData = response;
			if (!cData || cData == '')
			{
				return;
			}

			categoryArray = new Array();
			var accordian = dijit.byId('leftAccordion');
			if (!accordian)
			{
				accordian = new dijit.layout.AccordionContainer({id:'leftAccordion', duration:200, minSize:20}, dojo.byId('leftAccordDiv'));
			}

			storeArray = new Array();
			for (var i = 0; i < cData.length; i++)
			{
				var obj = cData[i];
				var category = new Category(obj.id, obj.title);
				var storeObj = new Object();
				storeObj.id = obj.id;
				storeObj.title = obj.title;
				storeArray[storeArray.length] = storeObj;
				category.paint();
			}
			
			var storeData = {'identifier': 'id', items:storeArray};
			categoryStore = new dojo.data.ItemFileWriteStore({jsId:"categoryStore", data: storeData});
			updateCenterPaneCategory();
			accordian.startup();
			accordian.resize();
			dijit.byId('left1').resize();
			dijit.byId('left1').startup();
			accordian.selectChild(categoryArray[0].uid);
			return response;
		},
		error: function(response, ioArgs) {
			if (dojo.byId('categoryLoading'))
			{
				dojo.byId('categoryLoading').parentNode.removeChild(dojo.byId('categoryLoading'));
			}

			return response;
		}
	});
}

function updateCenterPaneCategory()
{
	var sc = dijit.byId('searchCategory');
	if (sc)
	{
		sc.store = categoryStore;
	}
	else
	{
		sc = new dijit.form.ComboBox({
											id:"searchCategory",
											name:"searchCategory",
											autoComplete:true,
											store: categoryStore,
											searchAttr:"title",
											style:"width:140px;font-size:11pt;vertical-align:middle;"
									}, dojo.byId("searchCategoryDiv"));
	}

	initializeSearchCombo();
}

function updateCategoryStore(id, title)
{
	var storeObj = new Object();
	storeObj.id = id;
	storeObj.title = title;
	storeArray[storeArray.length] = storeObj;
	var storeData = {'identifier': 'id', items:storeArray};
	categoryStore = new dojo.data.ItemFileWriteStore({jsId:"categoryStore", data: storeData});
	updateCenterPaneCategory();
}

function deleteAllCategories()
{
	for (var i =0; i < categoryArray.length; i++)
	{
		categoryArray[i].destroy();
	}
}

function addAnotherCategory(response)
{
	var category = new Category(response.id, response.title);
	categoryArray[categoryArray.length] = category;
	categoryStore.items[categoryStore.items.length] = category;
	category.paint();
}

function Category(id, title)
{
	this.id = id;
	this.uid = 'category_' + categoryCounter;
	categoryCounter++;
	this.title = title;
	this.tabs = new Array();

	this.addTab = function (tabObj){
		this.tabs[this.tabs.length] = tabObj;
	}

	this.getTabs = function (){
		var catObj = this;
		var url = '/cat?a=t&catId=' + this.id;
		dojo.xhrGet({
			url: url,
			handleAs: 'json',
			preventCache: true,
			load: function(response, ioArgs) {
				var tData = response;
				if (!tData || tData == '')
				{
					return;
				}

				catObj.tabs = new Array();
				for (var i = 0; i < tData.length; i++)
				{
					var obj = tData[i];
					if (obj.id && obj.id != '')
					{
						var tabObj = new CategoryChildObj(obj.id, obj.title, obj.url, obj.searchUrl, catObj, obj.hullabuParams, obj.searchParams);
						catObj.addTab(tabObj);
					}
				}

				catObj.paintAllChild();
				return response;
			},
			error: function(response, ioArgs) {
				return response;
			}
		}, catObj);
	}

	this.paint = function (){
		this.dijitObj = new dijit.layout.AccordionPane({id:this.uid, title:this.title});
		var container = dijit.byId('leftAccordion');
		container.addChild(this.dijitObj);
		this.getTabs();
		container.resize();
		categoryArray[categoryArray.length] = this;
	}

	this.paintAllChild = function(){
		this.table = dojo.doc.createElement('table');
		this.thead = dojo.doc.createElement('thead');
		this.table.appendChild(this.thead);
		this.table.style.width = "100%";
		this.dijitObj = dijit.byId(this.uid);
		this.dijitObj.setContent(this.table);
		for (var i = 0; i < this.tabs.length ; i++)
		{
			this.tabs[i].paint();
		}

		this.paintAddLink();
	}

	this.paintAddLink = function (){
		this.addTR = dojo.doc.createElement('tr');
		this.addTR.style.height = "100%";
		this.addTD = dojo.doc.createElement('td');
		this.addTD.style.height = "100%";

		this.span2 = dojo.doc.createElement('span');
		dojo.addClass(this.span2, 'spanningLeft');
		dojo.addClass(this.span2, 'link');
		dojo.addClass(this.span2, 'topSpacer');
		this.span2.innerHTML = "Add More";
		this.addTD.appendChild(this.span2);

		this.addTR.appendChild(this.addTD);
		this.thead.appendChild(this.addTR);
		dojo.connect(this.span2, 'onclick', 'showTabForm');
	}

	this.destroy = function (){
		this.dijitObj.destroy();
	}
	
	this.reload = function (){
		this.getTabs();
	}
}

function CategoryChildObj(id, title, url, searchUrl, parentObj, hullabuParams, searchParams)
{
	this.id = id;
	this.title = title;
	this.url = url;
	this.searchUrl = searchUrl;
	this.parentCategoryObj = parentObj;
	this.hullabuParams = hullabuParams;
	this.searchParams = searchParams;

	this.paint = function (){
		this.tr = dojo.doc.createElement('tr');
		this.tr.style.height = "100%";
		this.td = dojo.doc.createElement('td');
		this.td.style.height = "100%";
		if (dojo.isIE)
		{
			this.tr.className = "ClassOut";
			this.tr.onmouseover = function (){this.className='ClassOver';};
			this.tr.onmouseout = function (){this.className='ClassOut';};
		}
		else
		{
			this.td.setAttribute("class", "ClassOut");
			this.td.setAttribute("onmouseover", "this.className='ClassOver';");
			this.td.setAttribute("onmouseout", "this.className='ClassOut';");
			//this.td.setAttribute("onClick", "openTab('" + this.id + "', '" + this.title + "', '" + this.url + "', '" + this.searchUrl + "')");
		}

		this.span1 = dojo.doc.createElement('span');
		dojo.addClass(this.span1, 'spanningLeft');
		this.span1.innerHTML = this.title;
		this.td.appendChild(this.span1);

		this.span2 = dojo.doc.createElement('span');
		dojo.addClass(this.span2, 'spanningRight');
		this.span2.innerHTML = "<img src='http://ajax.googleapis.com/ajax/libs/dojo/1.1.1/dijit/themes/tundra/images/tabClose.png'>";
		this.span2.style.display = 'none';
		this.td.appendChild(this.span2);

		this.tr.appendChild(this.td);
		this.parentCategoryObj.thead.appendChild(this.tr);
		dojo.connect(this.span2, 'onclick', this, 'deleteChild');
		dojo.connect(this.tr, 'onclick', this, 'open');
		dojo.connect(this.td, 'onmouseover', this, 'showImg');
		dojo.connect(this.td, 'onmouseout', this, 'hideImg');
	}

	this.showImg = function (){
		this.span2.style.display = "block";
	}

	this.hideImg = function (){
		this.span2.style.display = "none";
	}

	this.deleteChild = function (e){
		var catObj = this.parentCategoryObj;
		if (confirm('Are you sure you want to delete "' + this.title + '" tab?'))
		{
			var urlData = "/tab?s=104&a=d&id=" + this.id + "&catId=" + catObj.id;
			dojo.xhrGet({
				url: urlData,
				handleAs: 'json',
				preventCache: true,
				load: function(response, ioArgs) {
					catObj.reload();
					return response
				},
				error: function(response, ioArgs) {
					alert(response.responseText);
					return response;
				}
			}, catObj);
		}
		
		dojo.stopEvent(e);
	}

	this.open = function (){
		openTab( this.id, this.title, this.url, this.searchUrl);
	}

}

function getCategoryObjById(id)
{
	var dumy = categoryArray;
	for (var i = 0; i < categoryArray.length; i++)
	{
		var id1 = categoryArray[i].id;
		var catId = id1.toString();
		if (catId == id)
		{
			return categoryArray[i];
		}
	}
}

function getCategoryObjByName(name)
{
	for (var i = 0; i < categoryArray.length; i++)
	{
		var obj = categoryArray[i];
		if (obj.title == name)
		{
			return obj;
		}
	}

	return null;
}

