// JScript File

// Implements the CatNav object
function CatNav(catalogId){

    // Constructor.
    var categoriesObj = null;
    var currCategoryId = null;
    var NavDivEl = document.getElementById('NavDiv');
    var NavPathDiv = document.getElementById('NavPathDiv');
    var theCatalogId = catalogId;
    var theLastCategories = null;
	var theNavPath = new Array();
	var theRootCategory = new Category(catalogId,"Full Collection");
	theNavPath.push(theRootCategory);


	this.drawNav = function(catId){	
		currCategoryId = catId;
		deleteNav();
		// Check if we need to add the catId to theNavPath or we nedd to
		// chop the array because we are going up.
		var matched = false;
		var label = null;
		for(var i=0;i< theNavPath.length;i++){
			if(theNavPath[i].catId==catId){
				label = theNavPath[i].label;
				theNavPath.splice(i+1, theNavPath.length - i+1);
				matched = true;
				break;
			}
		}
		if(matched){
			addText(NavDivEl,label);
		}else{
			// Draw and Add from theLastCategories
			for(var i=0;i<theLastCategories.length;i++){
				if(theLastCategories[i].catId==catId){
					addText(NavDivEl,theLastCategories[i].label);
					var aCategory = new Category(theLastCategories[i].catId,theLastCategories[i].label);
					theNavPath.push(aCategory);
					break;
				}
			}
		}
		// Draw the path.
		for(var i=0;i<theNavPath.length;i++){
			if(i==theNavPath.length-1){
				addText(NavPathDiv,theNavPath[i].label);
			}else{
				var linkEl = addLink(NavPathDiv,"javascript:theCatNav.drawNav('"+ theNavPath[i].catId +"')");
				addText(linkEl,theNavPath[i].label);
				addText(NavPathDiv," > ");
			}
		}
		// Get the categories under the current one.
		var parameters = "parentId=" + catId;
		theRequest.post("/getCategories", this.drawNavOnComplete, parameters);
	}
	this.drawNavOnComplete = function(jsonObj){  
		theLastCategories = jsonObj
		for(var i=0;i<jsonObj.length;i++){
			addText(NavDivEl," | ");
			var linkEl = addLink(NavDivEl,"javascript:theCatNav.drawNav('"+ jsonObj[i].catId +"')");
			addText(linkEl,jsonObj[i].label);
		}
		// Now update the images.
		theImgRibbon.initCategory(currCategoryId);
  	}    
	//	
	// ================================================================================
	// START DOM Support.
	//
	function addText(parentEl,txt){
	    var txtNode = document.createTextNode(txt);
        parentEl.appendChild(txtNode);
        return txtNode;
	}
	//
	function addLink(parentEl,href){
	    var linkEl = document.createElement('a')
        linkEl.href = href;
        parentEl.appendChild(linkEl);
        return linkEl;
	}
    //
    function deleteNav() {
		// As we delete the nodes the lenth of oNodeList will vary so we
		// set now how many iterations we want to have.
		var cnt = NavDivEl.childNodes.length
		for (var i=0; i<cnt; i++) {
			NavDivEl.removeChild(NavDivEl.firstChild);
		}
		//
		cnt = NavPathDiv.childNodes.length
		for (var i=0; i<cnt; i++) {
			NavPathDiv.removeChild(NavPathDiv.firstChild);
		}		
	}// deleteNav	
	//
	// END DOM Support.
	// ================================================================================
}


// Implements the Category object
function Category(c,l){
	this.catId = c;
	this.label = l;
	
}	



