/* Vars related to main category menu */
	var hasSubCategories = new Array();
	var currentSubMenu = "";
	var lastMenu = "";
	var currentMenu = "";
	var menuHover = false;
	var hideMenu = false;
	var currentRootCategory = "";
	var leftMenu = "";
	var rightMenu = "";
	var isLeftMenu = false;
	var isRightMenu = false;
	var rd = false;
	var menuLoaded = new Object();
	var goLinks = { 'links' : [ 
		{ 'link': 'frnc/blog', 'name': 'Get Organized Blog' },
		{ 'link': 'frnc/board', 'name': 'GO User Forum' },
		{ 'link': 'partners', 'name': 'Meet Our Partners' }
	]};


	var pauseDrop = 250;
      	var prepend = '';
  	if ( contextPath == '/au' || contextPath == '/nz' ) { prepend = "AUNZ_"; }
/* Vars related to main category menu */

function dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;
	
	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";
	
	if(typeof(arr) == 'object') { //Array/Hashes/Objects 
		for(var item in arr) {
			var value = arr[item];
			
			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}

function searchFormValidate(boxid){
	var searchValue = (document.searchform.q.value=='Search');
	if (searchValue == false){
		document.searchform.action="http://search.franklinplanner.com/";
		document.searchform.submit();
	} 
			
}
		
function showSearchBubble(boxid){
	var pos = $('#search_box').position();;
        var posX = pos.left - 120;
        var posY = pos.top - 75;
	document.searchform.action="http://search.franklinplanner.com/";
        if (document.searchform.q.value.length < 1) {
		document.getElementById(boxid).style.left=posX + "px";
		document.getElementById(boxid).style.top=posY + "px";
                $('#searchErrorBubble').fadeIn(200);
		document.searchform.q.focus();
		return false;
	} else {   
	   document.searchform.submit();
	   return true;	 
	}
}

function hideSearchBubble(boxid){
   document.getElementById(boxid).style.display="none";
}	

function subMenu(catId,store,on) {

  if ( document.getElementById('sub_'+catId) ) {
    var el = document.getElementById('sub_' + catId);
    el.style.width = "300px";
    if ( on == true ) {
      $("#sub_"+catId).load("/shopping/common/ajx_submenucategories.jsp?currentRootCategory="+catId);
      el.style.display = "block";
      el.style.height = "300px";
    } else {
      el.style.display = "none";
    }
  }
}

function whatsThis(which,state,parent)
{
	//close all other open windows
	closeWhatsThis();
	
	var whatsThisDiv = document.getElementById("sub_menu_2_whats_this_"+which);
	if (whatsThisDiv) 
	{
		if (state=="block")
		{
			var whatsThisParent = document.getElementById("sub_menu_2_"+which);
			var menuParent = document.getElementById("subcat_"+parent);
			var parentCoords = getObjCoords(whatsThisParent);
			var menuCoords = getObjCoords(menuParent);
			var newLeft=parentCoords.x - menuCoords.x + 110;
			var newTop = parentCoords.y - menuCoords.y + 5;
			whatsThisDiv.style.top=newTop+"px";
			whatsThisDiv.style.left=newLeft+"px";
		}
		whatsThisDiv.style.display=state;
	}
}

function closeWhatsThis()
{
	var these = getElementsByClass("sub_menu_2_whats_this",null,"div");
	if (these!=null)
	{
		for (i=0; i<these.length; i++)
		{
			these[i].style.display="none";
		}
	}	
	
}

var thisCat      = '';
var lastCat      = '';
var hoverCat     = false;
var hoverGo      = false;
var hoverGoComm  = false;
var catLength    = 0;
var catDropped   = false;
var goDropped    = false;
var goLinksDropped  = false;
var theGoDropped = false;

function showSubCats(cat,hasSubs) {

         $('#goDayDrop').hide();
         $('#goSpaceDrop').hide();
         $('#goLifeDrop').hide();

  lastCat = thisCat; 

  // Hide the div and load up the selected content

  $(".subCat").hide();
  $("#catDrop").hide();
  //$(".clearImg").show();

  if ( !isNaN(lastCat) ) {
  	$("#subCat_" + lastCat).hide();
  }


  if ( lastCat == ( catLength - 1 ) ) {
      $("#catDiv_" + lastCat).removeClass().addClass('catDivLast');
  } else {
      $("#catDiv_" + lastCat).removeClass().addClass('catDiv');
  }

  $(".catSepDiv").show();
  $("#sep_" + cat).hide();
  if ( cat > 0 ) {
    var i = cat - 1;
    $("#sep_" + i).hide();
  }
  if ( lastCat > 0 ) {
    var j = lastCat - 1;
    $("#catDiv_" + j).removeClass().addClass('catDiv');
  }

  //if ( hasSubs ) {
  if ( hasSubCategories[cat] != '' && hasSubCategories[cat] > 0 ) {
    $("#subCat_" + cat).show();
    var vers = null;
    vers = $.browser.version;
    //$(".clearImg").hide();
    if ( catDropped ) {
    	$("#catDrop").show();
  	$("#catDiv_" + cat).removeClass().addClass('catDivOn');
    } else {
  		$("#catDiv_" + cat).removeClass().addClass('catDivOn');
		setTimeout(function() {
			if ( hoverCat ) {
				if ( vers == null || ($.browser.msie && $.browser.version <= 7) ) {
    					$("#catDrop").show();
        			} else {
    					$("#catDrop").slideDown(200);
        			}
				catDropped = true;
			}
		},pauseDrop);
    }
  } else {
  	$("#catDiv_" + cat).removeClass().addClass('catDivOn');
  }
  thisCat = cat;
}

function showSubCatsOP(cat,hasSubs) {

  lastCat = thisCat; 
  // Hide the div and load up the selected content
  $(".subCat").hide();
  $("#catDrop").hide();

  if ( !isNaN(lastCat) ) {
  	$("#subCat_" + lastCat).hide();
  }
  $("#catDiv_" + lastCat).removeClass().addClass('navOff');
  if ( lastCat > 0 ) {
    var j = lastCat - 1;
    $("#catDiv_" + j).removeClass().addClass('navOff');
  }

  if ( hasSubCategories[cat] != '' && hasSubCategories[cat] > 0 ) {
    $("#subCat_" + cat).show();
    var vers = null;
    vers = $.browser.version;
    if ( catDropped ) {
    	$("#catDrop").show();
  		$("#catDiv_" + cat).removeClass().addClass('navOn');
    } else {
    	$("#catDiv_" + cat).removeClass().addClass('navOn');
		setTimeout(function() {
			if ( hoverCat ) {
				if ( vers == null || ($.browser.msie && $.browser.version <= 7) ) {
    					$("#catDrop").show();
        			} else {
    					$("#catDrop").slideDown(200);
        			}
				catDropped = true;
			}
		},pauseDrop);
    }
  } else {
  	$("#catDiv_" + cat).removeClass().addClass('navOn');
  }
  thisCat = cat;
}

function showGoCats(which) {

        theGoDropped = which;
	if ( hoverCat ) { 
		hoverCat=false;
		hideSubCats(false);
	}

	$('#goDayDrop').hide();
        $('#goSpaceDrop').hide();
        $('#goLifeDrop').hide();
        $('#goDayDiv').removeClass().addClass(prepend + 'goDay');
        $('#goSpaceDiv').removeClass().addClass(prepend + 'goSpace');
        $('#goLifeDiv').removeClass().addClass(prepend + 'goLife');
        $('#goCommDiv').removeClass().addClass(prepend + 'goComm');

        var vers = null;
        vers = $.browser.version;

        if ( $('#' + which + 'Drop').html() != 'EMPTY' ) {
        	//$(".clearImg").hide();
		if ( goDropped ) {
       		 	$('#' + which + 'Drop').show();
        		$('#' + which + 'Div').removeClass().addClass(prepend + which + 'On');
       		} else {
        		$('#' + which + 'Div').removeClass().addClass(prepend + which + 'On');
			setTimeout(function() {
				if ( hoverGo ) {
					if ( vers == null || ($.browser.msie && $.browser.version <= 7) ) {
        					$('#' + which + 'Drop').show();
					} else {
        					$('#' + which + 'Drop').slideDown(200);
					}
					goDropped = true;
				}
			}, pauseDrop );
        	}
	}

}

function hideSubCats(hasSubs) {

  // Wait a bit before checking
  var wait = 0;
  if ( catDropped ) { wait = 500; }
  	setTimeout(function() {
    		if ( !hoverCat ) { 
      			catHeaderOff();
      			$("#catDrop").hide();
			catDropped = false;
  			$(".catSepDiv").show();
    		}
  	},wait);
}

function hideSubCatsOP(hasSubs) {

  // Wait a bit before checking
  var wait = 0;
  if ( catDropped ) { wait = 500; }
  	setTimeout(function() {
    		if ( !hoverCat ) { 
      			catHeaderOffOP();
      			$("#catDrop").hide();
			catDropped = false;
  			$(".catSepDiv").show();
    		}
  	},wait);
}



function catHeaderOff() {
	if ( !isNaN(thisCat) ) { 
		if ( thisCat > 0 && thisCat == (catLength - 1) ) {
    			$("#catDiv_" + thisCat).css('border-bottom','1px solid #dedede');
			$("#catDiv_" + thisCat).removeClass().addClass('catDivLast');
      		} else {
          		$("#catDiv_" + thisCat).removeClass().addClass('catDiv');
		}
		if ( thisCat > 0 ) {
			var i = thisCat - 1;
        		$("#catDiv_" + i).removeClass().addClass('catDiv');
      		}
	}
	thisCat = '';
	lastCat = '';
}

function catHeaderOffOP() {
	if ( !isNaN(thisCat) ) { 
     	$("#catDiv_" + thisCat).removeClass().addClass('navOff');
		if ( thisCat > 0 ) {
			var i = thisCat - 1;
	    	$("#catDiv_" + i).removeClass().addClass('navOff');
   		}
	}
	thisCat = '';
	lastCat = '';
}

function hideGoCats() {

  wait = 500;
  setTimeout(function() {
    if ( hoverGo == false ) { 
	 //$('#' + theGoDropped + 'Drop').slideUp(100);
	 $('#' + theGoDropped + 'Drop').hide();
/*
         $('#goDayDrop').hide();
         $('#goSpaceDrop').hide();
         $('#goLifeDrop').hide();
*/
        //$(".clearImg").show();
         $('#goCommDrop').hide();
         $('#goDayDiv').removeClass().addClass(prepend + 'goDay');
         $('#goSpaceDiv').removeClass().addClass(prepend + 'goSpace');
         $('#goLifeDiv').removeClass().addClass(prepend + 'goLife');
         $('#goCommDiv').removeClass().addClass(prepend + 'goComm');
	 goDropped = false;
    }
  },wait);

}

function showGoLinks() {
  
  $('#goCommDiv').removeClass().addClass(prepend + 'goCommOn');

  if ( contextPath == "/store" ) {
	var goUrl = "http://getorganized.franklinplanner.com/";

	if ( hoverCat ) { 
		hoverCat=false;
		hideSubCats(false);
	}
	if ( hoverGo ) { 
		hoverGo=false;
		hideGoCats();
	}
        // Get the GO Links
	// Only load it up if nothing is there yet.
	if ( $('#goCommLinks').is(':empty') ) {
		$.each(goLinks.links, function(i,item){
			var goDiv = document.createElement('div');
			var goLink = document.createElement('a');
			$(goDiv).addClass('thirdCatDiv');
			goLink.setAttribute('href',goUrl+item.link);
			$(goLink).html(item.name);
			$(goLink).appendTo($(goDiv));
			$(goDiv).appendTo($('#goCommLinks'));
		});
	}

    var vers = null;
    vers = $.browser.version;
    if ( vers == null || ($.browser.msie && $.browser.version <= 7) ) {
        $('#goCommDrop').show();
	goLinksDropped = true;
    } else {
	setTimeout(function() {
       		if ( hoverGoComm ) {
       	 		$('#goCommDrop').slideDown(200);
			goLinksDropped = true;
		}
	},pauseDrop);
    }
  }
}

function hideGoLinks() {
  if ( contextPath == "/store" ) {
  	wait = 3000;
	if ( !goLinksDropped ) { wait = 0; }
  	setTimeout(function() {
  		if ( hoverGoComm == false ) {
    			$('#goCommDiv').removeClass().addClass(prepend + 'goComm');
    			//$('#goCommDrop').slideUp(100);
    			$('#goCommDrop').hide();
			goLinksDropped = false;
  		}
 	},wait);
  } else {
 	 $('#goCommDiv').removeClass().addClass(prepend + 'goComm');
  }
}


function buildTopMenu(url) {

     // Define the get organized categories
    var goCats = new Array();
    if ( contextPath == "/store" ) {
   	 goCats['cat370002']   = 'goDay';
    	 goCats['cat370004']   = 'goSpace';
    	 goCats['cat370006']   = 'goLife';
    } else if ( contextPath == "/ca" ) {
         goCats['cat470029']   = 'goDay';
    	 goCats['cat470078']   = 'goSpace';
    	 goCats['cat470044']   = 'goLife';
    } else if ( contextPath == "/au" ) {
         goCats['cat470102']   = 'goDay';
    	 goCats['cat470106']   = 'goSpace';
    	 goCats['cat470104']   = 'goLife';
    } else if ( contextPath == "/nz" ) {
         goCats['cat470102']   = 'goDay';
    	 goCats['cat470106']   = 'goSpace';
    	 goCats['cat470104']   = 'goLife';
    } else {  
   	 goCats['cat370002']   = 'goDay';
    	 goCats['cat370004']   = 'goSpace';
    	 goCats['cat370006']   = 'goLife';
    }

    // Define categories to ignore.
    var ignoreCats = new Array();
    ignoreCats['cat330023'] = 1;

  	var specialOffersSelected = false;
  	if (-1!=document.location.href.indexOf("promotions.jsp")) specialOffersSelected = true;
	
	// Get the categories with a JSON call
	menu = document.getElementById("catMenu");	
	var tbl = document.createElement("table");
	tbl.style.width = "900px";
	tbl.setAttribute("id","topNavTbl");
	
        // Test only!!!
        //url = "http://store.franklinplanner.com/store/common/json_categories.jsp?local=n";

	var tbdy = document.createElement("tbody");
	var trw = document.createElement("tr");
  		//contentType: "application/json",	
	$.ajax({ type:"GET", cache:true, contentType:'application/json',global:true,dataType:'jsonp',jsonp:'jsoncallback',processData:false, url:url,
    		success:function(jsonp) {
			var len = jsonp.categories.length;
			var isLast = false;
			var isFirst = false;
          
            if ( jsonp.status && jsonp.status == "success" && jsonp.categories && jsonp.categories.length > 0  ) {
            
	        var catTable = document.createElement("table");
		$(catTable).attr('cellpadding','0');
		$(catTable).attr('cellspacing','0');
		catTable.className='catTable';
		var catTBody = document.createElement("tbody");
		var catTr = document.createElement("tr");
		var lastCat = jsonp.categories.length - 1;

                catLength = jsonp.categories.length;
		
		if ( lastCat > 0 ) {
			var leftTd = document.createElement("td");
			leftTd.className = 'catLeft';
			catTr.appendChild(leftTd);
     			// Build the main menu of categories.
			$.each(jsonp.categories, function(i,item){

				if ( goCats[item.id] ) {
					buildSubMenu($('#' + goCats[item.id] + 'Drop'),item,false);
					$('#' + goCats[item.id] + 'Div').click(function() { window.location=item.templateUrl; } );
					$('#' + goCats[item.id]).css('cursor','pointer');
                                }  else {
				  if ( !ignoreCats[item.id] && item.name ) {
					var hasSubs = false;
					if ( item.subcategories && item.subcategories.length > 0 ) { hasSubs = true; }
					var catTd = document.createElement("td");
					catTd.className='catTd';
					$(catTd).attr('id','catTd_' + i);

					var catDiv = document.createElement("div");
					$(catDiv).attr('id','catDiv_' + i);

					if ( i == (catLength - 1) ) {
					    catDiv.className='catDivLast';
       		                        } else {
					    catDiv.className='catDiv';
					}
					$(catDiv).html(item.name);
					$(catDiv).click(function() { window.location=item.templateUrl; } );
					$(catDiv).hover(function() {hoverCat=true;showSubCats(i,hasSubs);},function(){hoverCat=false;hideSubCats(hasSubs);});
//$(catDiv).html('<div class="catName" onclick="window.location=\'' + item.templateUrl + '\';" onmouseover="showSubCats(' + i + ',' + hasSubs + ');" onmouseout="hoverCat=false;hideSubCats(' + hasSubs + ');">' + item.name + '</div>');
					catTd.appendChild(catDiv);
					catTr.appendChild(catTd);
					if ( i < catLength - 1 ) {
						var sepTd = document.createElement("td");
						var sepDiv = document.createElement("div");
						sepTd.className='catSepTd';
						sepDiv.className='catSepDiv';
						$(sepDiv).attr('id','sep_' + i);
						sepTd.appendChild(sepDiv);
					  	catTr.appendChild(sepTd);
					}
	
       		                        // Now build up the sub categories 
					// Write the content to the holder div in the page
					var subCatDiv = document.createElement('div');
					subCatDiv.style.display = 'none';
					subCatDiv.className='subCat';
					$(subCatDiv).attr('id','subCat_' + i);

					if ( hasSubs ) {
                                        	buildSubMenu(subCatDiv,item,true);
					}
				    }
				}
       	     		});
			var rightTd = document.createElement("td");
			rightTd.className = 'catRight';
			catTr.appendChild(rightTd);
		}
		catTBody.appendChild(catTr);
		catTable.appendChild(catTBody);
		menu.innerHTML="";
		menu.appendChild(catTable);

								//looks for what's this:
/*
								if (subitem2.whatsThis)
								{
									subDiv.innerHTML+="<a href=\"#\" onclick=\"whatsThis('"+subitem2.id+"', 'block', '"+item.id+"'); return false;\" class=\"whatsThis\">what's this?</a>";
									var subDivWhatsThis = document.createElement("div");
			   						$(subDivWhatsThis).hover(
			   						  function () {
			   						    menuHover = true;
			   						  },
			   					  	function () {
			   					  		menuHover = false;
			   					  	}
			   						);
									
									subDivWhatsThis.className="sub_menu_2_whats_this";
									subDivWhatsThis.id="sub_menu_2_whats_this_"+subitem2.id;
									subDivWhatsThis.innerHTML = subitem2.whatsThis;
									menu.appendChild(subDivWhatsThis);
								}
*/
            	tbdy.appendChild(trw);
		    tbl.appendChild(tbdy);
		    if (document.getElementById('defaultTopNav')) document.getElementById('defaultTopNav').style.display="none";
            } else {             
            	// The menu items are unavailable for some reason.
            	// Just show an empty bar with no options.
            }
		    //menu.innerHTML = "";
   		    //menu.appendChild(tbl);
        },
        error:function() {
        	// The menu items are unavailable for some reason.
            	// Just show an empty bar with no options.
            	var tdl = document.createElement("td");
     			tdl.style.height = "32px";
     			tdl.style.width = "6px";
   				tdl.innerHTML = "<img name='menuLeft' src='http://www.franklinplanner.com/images/shop_template/lft_nav_corn_off.gif' border='0'/>";  
   					
   				var tda = document.createElement("td");  				
   				tda.innerHTML = "&nbsp;";
                                tda.className = 'menu_bg_off';
   				var tdr = document.createElement("td");
     			tdr.style.height = "32px";
     			tdr.style.width = "6px";
   				tdr.innerHTML = "<img name='menuRight' src='http://www.franklinplanner.com/images/shop_template/rt_nav_corn_off.gif' border='0'/>";
   				trw.appendChild(tdl);
   				trw.appendChild(tda);
   				trw.appendChild(tdr);	
   				tbdy.appendChild(trw);
		    	tbl.appendChild(tbdy);
			    if (document.getElementById('defaultTopNav')) document.getElementById('defaultTopNav').style.display="none";
   		   		menu.appendChild(tbl);	
        }
    });


	$('.goDropDiv').hover( function () { hoverGo=true; }, function () { hoverGo=false;hideGoCats();});

	$('#goLifeDiv').hover( 
	  function () { hoverGo=true;showGoCats('goLife'); },
	  function () { hoverGo=false;hideGoCats('goLife'); }
        );
	$('#goSpaceDiv').hover( 
	  function () { hoverGo=true;showGoCats('goSpace'); },
	  function () { hoverGo=false;hideGoCats('goSpace'); }
        );
	$('#goDayDiv').hover( 
	  function () { hoverGo=true;showGoCats('goDay'); },
	  function () { hoverGo=false;hideGoCats('goDay'); }
        );
	$('#catDrop').hover( function() { hoverCat=true; }, function() { hoverCat=false; hideSubCats(true); });
	$('#goCommDrop').hover( function() { hoverGoComm=true; }, function() { hoverGoComm=false; hideGoLinks(); });
	$('#goCommDiv').hover( function() { hoverGoComm=true;showGoLinks(); }, function() { hoverGoComm=false;hideGoLinks(); });

}


function buildTopMenuOP(url) {

    // Define the get organized categories
    var goCats = new Array();

    // Define categories to ignore.
    var ignoreCats = new Array();

  	var specialOffersSelected = false;
  	if (-1!=document.location.href.indexOf("promotions.jsp")) specialOffersSelected = true;
	
	// Get the categories with a JSON call
	menu = document.getElementById("catMenu");	
	var tbl = document.createElement("table");
	tbl.style.width = "900px";
	tbl.setAttribute("id","topNavTbl");
	
	var tbdy = document.createElement("tbody");
	var trw = document.createElement("tr");
	$.ajax({ type:"GET", cache:true, contentType:'application/json',global:true,dataType:'jsonp',jsonp:'jsoncallback',processData:false, url:url,
    		success:function(jsonp) 
    		{
				var len = jsonp.categories.length;
				var isLast = false;
				var isFirst = false;
	            if ( jsonp.status && jsonp.status == "success" && jsonp.categories && jsonp.categories.length > 0  ) 
	            {
            
			        var catTable = document.createElement("table");
					$(catTable).attr('cellpadding','0');
					$(catTable).attr('cellspacing','0');
					catTable.className='navTbl';
					var catTBody = document.createElement("tbody");
					var catTr = document.createElement("tr");
					var lastCat = jsonp.categories.length - 1;
                	catLength = jsonp.categories.length;
					if ( lastCat > 0 ) 
					{
						var leftTd = document.createElement("td");
						leftTd.className = 'catLeft';
						catTr.appendChild(leftTd);
			     		// Build the main menu of categories.
						$.each(jsonp.categories, function(i,item)
						{
							if ( goCats[item.id] ) 
							{
								buildSubMenu($('#' + goCats[item.id] + 'Drop'),item,false);
								$('#' + goCats[item.id] + 'Div').click(function() { window.location=item.templateUrl; } );
								$('#' + goCats[item.id]).css('cursor','pointer');
			                }  
			                else 
			                {
							  	if ( !ignoreCats[item.id] && item.name ) 
							  	{
									var hasSubs = false;
									if ( item.subcategories && item.subcategories.length > 0 ) 
									{ 
										hasSubs = true;
										hasSubCategories[i]=item.subcategories.length; 
									}
									
									var catTd = document.createElement("td");
									catTd.className='catTdOP';
									$(catTd).attr('id','catTd_' + i);
									$(catTd).attr('nowrap','nowrap');
									var catDiv = document.createElement("div");
									$(catDiv).attr('id','catDiv_' + i);
								    catDiv.className='navOff';

									$(catDiv).click(function() { window.location=item.templateUrl; } );
									$(catDiv).hover(function() {hoverCat=true;showSubCatsOP(i,hasSubs);tabOn(i)},function(){hoverCat=false;hideSubCatsOP(hasSubs);tabOff(i);});
									
									var navLeftOP = document.createElement("div");
									navLeftOP.className="navLeftOP";

									var navMiddle = document.createElement("div");
									navMiddle.className="navMiddle";
									$(navMiddle).html(item.name);

									var navRightOP = document.createElement("div");
									navRightOP.className="navRightOP";
									
									catDiv.appendChild(navLeftOP);
									catDiv.appendChild(navMiddle);
									catDiv.appendChild(navRightOP);
									
									catTd.appendChild(catDiv);
									catTr.appendChild(catTd);
									if ( i < catLength - 1 ) 
									{
										var sepTd = document.createElement("td");
										var sepDiv = document.createElement("div");
										sepTd.className='catSepTd';
										sepDiv.className='catSepDiv';
										$(sepDiv).attr('id','sep_' + i);
										sepTd.appendChild(sepDiv);
									  	catTr.appendChild(sepTd);
									}
				
				       		        // Now build up the sub categories 
									// Write the content to the holder div in the page
									var subCatDiv = document.createElement('div');
									subCatDiv.style.display = 'none';
									subCatDiv.className='subCat';
									$(subCatDiv).attr('id','subCat_' + i);
				
									if ( hasSubs ) 
									{
			                        	buildSubMenu(subCatDiv,item,true,"subCatTitleOP");
									}
							    }
							}
			       	   	});
						var rightTd = document.createElement("td");
						rightTd.className = 'catRight';
						catTr.appendChild(rightTd);
					}
					catTBody.appendChild(catTr);
					catTable.appendChild(catTBody);
					menu.innerHTML="";
					menu.appendChild(catTable);
            		tbdy.appendChild(trw);
				    tbl.appendChild(tbdy);
		    		if (document.getElementById('defaultTopNav')) document.getElementById('defaultTopNav').style.display="none";
            } 
            else 
            {             
            	// The menu items are unavailable for some reason.
            	// Just show an empty bar with no options.
            }
            if (jsonp.cart_count && jsonp.cart_total)
            {
             	var content = jsonp.cart_count+"&nbsp;Item";
             	if (jsonp.cart_count==1) {}
             	else { content+="s"; }
             	content+="&nbsp;|&nbsp;"+jsonp.cart_total;
            	$("#cartTotals").html(content);
            }
            else
            {
            	$("#cartTotal").html("0&nbsp;Items&nbsp;|&nbsp;$0.00");
            }
            
            if (jsonp.first_name!="")
            {
            
            	var loggedInContent = '<div id="myAccount" style="float: left">';
				loggedInContent += '<table cellpadding="0" cellspacing="0" style="float:right;"><tbody><tr>';
				loggedInContent += '<td style="width:3px;"><img src="http://store.theorganizedparent.com/static/site/common/fcp/account-box-left.jpg"/></td>';
				loggedInContent += '<td id="myAccount"><a href="http://store.theorganizedparent.com/organized/user/profile.jsp?sc=09-myaccounthdr&c=09-myaccounthdr">';
				loggedInContent += jsonp.first_name;
				loggedInContent += '\'s Account</a></td><td style="width:3px;"><img src="http://store.theorganizedparent.com/static/site/common/fcp/account-box-right.jpg"/></td>';
				loggedInContent += '</tr></tbody></table>';
				loggedInContent += '</div>';
				loggedInContent += '<div style="float:left"><a href="http://store.theorganizedparent.com/organized/user/uLogout.jsp"><img src="http://store.theorganizedparent.com/static/site/common/fcp/utl-nav-sign-out.gif"></a></div>';
            	$("#signInMsg").html(loggedInContent);
            }
        },
        error:function() 
        {
        	// The menu items are unavailable for some reason.

        }
    });
	$('#catDrop').hover( function() { hoverCat=true; }, function() { hoverCat=false; hideSubCatsOP(true); });
}



function buildSubMenu(container,item,isCatDrop,className) {
	var subTable = document.createElement('table');
	var subTBody = document.createElement('tbody');
	var subTr    = document.createElement('tr');
	var lastLine = false;
	var subCount = 0;
	var allCount = 0;
	$(container).html('');
	$.each(item.subcategories, function(j,subItem){
		var allSubCount = 0;
		$.each(subItem, function(k,sub){
		lastLine = false;
		if ( subCount == 0 ) { subTr = document.createElement('tr'); }
			subCount++;
			allCount++;
			var subTd = document.createElement('td');
			if ( subCount == 5 ) {
				subTd.className = 'subCatTdFive';
			} else {
				subTd.className = 'subCatTd';
			}
			var titleDiv = document.createElement('div');
			if (!className)
			{
				className = 'subCatTitle';
			}
			titleDiv.className = className;
			$(titleDiv).html(sub.name.toUpperCase());
			$(titleDiv).appendTo($(subTd));
	
			// Now get the subcat links 
   			if (sub.thirdcategories) {
				$.each(sub.thirdcategories, function(k,sub3){
					var linkDiv = document.createElement("div");
					linkDiv.className = "thirdCatDiv";
					var catLink = document.createElement('a');
					catLink.className = 'thirdCatLink';
					$(catLink).attr('href',sub3.templateUrl);
					$(catLink).html(sub3.name);
					$(catLink).appendTo($(linkDiv));
					$(linkDiv).appendTo($(subTd));
				});
			}

			$(subTd).appendTo(subTr);

			if ( subCount == 5 ) {
				lastLine = true;
				$(subTr).appendTo(subTBody);
				var spaceTr = document.createElement('tr');
				var spaceTd = document.createElement('td');
				spaceTd.colSpan = '5';
				spaceTd.style.height = '23px';
				$(spaceTd).html('&nbsp;');
				$(spaceTd).appendTo(spaceTr);
				$(spaceTr).appendTo(subTBody);
				subCount = 0;
			}
		});
	});

	if ( subCount < 5 && subCount > 0 ) { // Append the last TR
		var subTd = document.createElement('td');
		subTd.colSpan = (5 - subCount);
		$(subTd).html('&nbsp;');
		$(subTd).appendTo(subTr);
		$(subTr).appendTo(subTBody);
	}
	/* Last Spacer */
	var spaceTr = document.createElement('tr');
	var spaceTd = document.createElement('td');
	spaceTd.colSpan = '5';
	spaceTd.style.height = '23px';
	$(spaceTd).html('&nbsp;');
	$(spaceTd).appendTo(spaceTr);
	$(spaceTr).appendTo(subTBody);

	$(subTBody).appendTo(subTable);
	$(subTable).appendTo(container);
	if ( isCatDrop ) {
		$(container).appendTo('#catDrop');
	}
}

function setPos(itemId)
{
	//DON't reset the position if it has already been set
	var isLoaded = eval("menuLoaded."+itemId);
	if (!isLoaded) 
	{
		//determine proper position for subNav elements
		var cell = document.getElementById("td_"+itemId);
		var submenu = document.getElementById("subcat_"+itemId);
		var topNavTbl = document.getElementById("topNavTbl");
		var menuDiv = document.getElementById("menuDiv");
		var cellLeft = getObjCoords(submenu);
		var menuLeft = getObjCoords(menuDiv);
		
		//get left position of highlighted menu option, relative to nav table.
		var pos = cellLeft.x - menuLeft.x;
		var navTableWidth=getDivWidth(topNavTbl);
		var topNavWidth=getDivWidth(cell);
		var subMenuWidth=getDivWidth(submenu);
		var relPos = pos+subMenuWidth;
		var relPos2 = relPos+topNavWidth;
		//if the submenu is wider than space available, then we'll need to adjust its x-coord
	
		/*//center the submenus under the top tops
		var center = subMenuWidth/2;
		var centerPx = (cellLeft.x+(topNavWidth/2))-center;
		//alert("cellPos: "+cellLeft.x+"\nsubMenuWidth: "+subMenuWidth+"\ncenteredPx: "+centerPx);
		if (centerPx < menuLeft.x+6) { centerPx = menuLeft.x+6; }
		else if (centerPx+subMenuWidth > menuLeft.x+navTableWidth) { 
			centerPx = menuLeft.x+navTableWidth-subMenuWidth-6; 
		}
		submenu.style.left= centerPx+"px";
		*/
		
		var leftAlign=cellLeft.x+topNavWidth-subMenuWidth;		
		//right-align elements that are past center section of topNavTbl
		if (pos > navTableWidth/2)
		{
			if (leftAlign>menuLeft.x)
			{
				//align with right side of topNav button
				submenu.style.left=leftAlign+"px";
			}
			else
			{
				submenu.style.left=menuLeft.x+6+"px";
			}
		}
		//alternate option, left-align submenu with top tab where possible
		else if (pos+subMenuWidth>navTableWidth)
		{
			//first see if we can just left-align the sub-menu, without it going past the leftCoord
			if (subMenuWidth<pos+subMenuWidth)
			{
				
				if (leftAlign>menuLeft.x)
				{
					//align with right side of topNav button
					//submenu.style.left=leftAlign+"px";
		
					//or rightAlign with right-most boundary of table
					submenu.style.left=(menuLeft.x+navTableWidth-subMenuWidth-6)+"px";
				}
				else
				{
					submenu.style.left=menuLeft.x+6+"px";
				}
				
			}
			//otherwise, just left-align it
			else
			{
				submenu.style.left=(cellLeft.x)+"px";
			}
			
		}
		eval("menuLoaded."+itemId+"=true");

	}
}

function Point(x, y) {
    this.x = x;
    this.y = y;
}

function getObjCoords(o) {
    var oX = 0;
    var oY = 0;
    if (o.offsetParent) {
        while (1) {
            oX+=o.offsetLeft;
            oY+=o.offsetTop;
                if (!o.offsetParent) {
                    break;
                }
            o=o.offsetParent;
        }
    } else if (o.x) {
        oX+=o.x;
        oY+=o.y;
    }
    //alert(oX + ":" + oY);
    return new Point(oX, oY);
}

function hideAllSubs()
{
	// Turn off all other menus - if any are on
  	var menuNode= document.getElementById('topNavTbl');
  	var subMenus = getElementsByClass("sub_menu_on", menuNode, "div");
  	if (null!=subMenus && ""!=subMenus)
  	{
  		for (i=0; i<subMenus.length; i++)
  		{
  			if ( menuHover == true && currentSubMenu != "" && currentSubMenu==subMenus[i].id )
  			{ /*do not hide this submenu*/ }
  			else
  			{
			  	$(subMenus[i]).hide();
			}
  		}
  	}
}

function isGC()
{
  	var gcSelected = false;
  	if (-1!=window.location.search.indexOf("prod290002")) gcSelected = true;
	return gcSelected;
}

function showSubMenu(catId,lr) {
  //alert(currentRootCategory);
  var catTd  = document.getElementById('td_' + catId);
  var currTd  = document.getElementById('td_' + currentMenu);
	
  	var specialOffersSelected = false;
  	if (-1!=document.location.href.indexOf("promotions.jsp")) specialOffersSelected = true;


  if ( currentSubMenu != "" ) {
  	$(currentSubMenu).hide();
  	//document.getElementById(currentSubMenu).style.display = "none";
  }
  if ( currentMenu != "") {
  	if (
  		(currentMenu == currentRootCategory && !isGC()) ||  
  		(specialOffersSelected && currentMenu=="special_offers") ||
  		(isGC() && currentMenu=="gift_cards")
  	) 
  	{
  		//do nothing
		currTd.className = 'menu_bg_off';
  	}
  	else
	{
		currTd.className = 'menu_bg_off';
	}
  }
  
  if ( lr != "" ) {
  	if ( lr == "l" ) 
  		document['menuLeft'].src = "http://www.franklinplanner.com/images/shop_template/lft_nav_corn_on.gif";
  	if ( lr == "r" ) 
  		document['menuRight'].src = "http://www.franklinplanner.com/images/shop_template/rt_nav_corn_on.gif";    
  } else {
    if ( currentMenu != "" && currentMenu != currentRootCategory && !specialOffersSelected) {
  		document['menuRight'].src = "http://www.franklinplanner.com/images/shop_template/rt_nav_corn_off.gif";
  		document['menuLeft'].src = "http://www.franklinplanner.com/images/shop_template/lft_nav_corn_off.gif";
  	}
  }
  if ( currentRootCategory != "" && leftMenu == currentRootCategory ) {
  	document['menuLeft'].src = "http://www.franklinplanner.com/images/shop_template/lft_nav_corn_on.gif";
  }
  if ( currentRootCategory != "" && rightMenu == currentRootCategory ) {
  	document['menuLeft'].src = "http://www.franklinplanner.com/images/shop_template/rt_nav_corn_on.gif";
  }
  catTd.className = 'menu_bg_on';
  if (   document.getElementById('subcat_' + catId) ) {
  	var sub = document.getElementById('subcat_' + catId);

	//div must be set to 0 opacity & display = block in order to get the values for the submenu width and position
	changeOpac(0, 'subcat_'+catId);
	sub.style.textAlign = "left";
	sub.style.zIndex = '9999';
  	sub.style.display = "block";
  	//set proper pos of submenu
  	setPos(catId);
  	sub.style.display="none";
	changeOpac(100, 'subcat_'+catId);
	$(sub).slideDown(200);
	//sub.style.display="block";
  	currentSubMenu = "subcat_" + catId;
	hideAllSubs();
  	lastMenu = currentMenu;
  	currentMenu = catId;
  }
  currentMenu = catId;
}

function hideSubMenu() {
	var currTd = document.getElementById("td_" + currentMenu);
  	var specialOffersSelected = false;
  	if (-1!=document.location.href.indexOf("promotions.jsp")) specialOffersSelected = true;

	hideAllSubs();

  if ( document.getElementById(currentSubMenu) ) 
  {
    //document.getElementById(currentSubMenu).style.display = "none";   
    $(currentSubMenu).hide();
    closeWhatsThis();
  }

  if ( document.getElementById("td_" + currentMenu))
  {
  	if (
  		(currentMenu == currentRootCategory && !isGC()) ||  
  		(specialOffersSelected && currentMenu=="special_offers") ||
  		(isGC() && currentMenu=="gift_cards")
  	) 
  	{
  		//do nothing
		// Just for non-store stuff.
		currTd.className = 'menu_bg_off';
  	}
  	else
  	{
		currTd.className = 'menu_bg_off';
  	}
  }
  if ( document['menuLeft'] ) {
  	if ( currentRootCategory != "" && leftMenu == currentRootCategory ) {
  		document['menuLeft'].src = "http://www.franklinplanner.com/images/shop_template/lft_nav_corn_on.gif";
 	 } else {
 	   	if ( isLeftMenu ) {
  			document['menuLeft'].src = "http://www.franklinplanner.com/images/shop_template/lft_nav_corn_off.gif";
  			isLeftMenu = false;
  		}
  	}
  }
  
  if ( document['menuRight'] ) {
  	if (( currentRootCategory != "" && rightMenu == currentRootCategory) || specialOffersSelected ) {
  		document['menuRight'].src = "http://www.franklinplanner.com/images/shop_template/rt_nav_corn_on.gif";
  	} else {
  	    if ( isRightMenu ) {
  		document['menuRight'].src = "http://www.franklinplanner.com/images/shop_template/rt_nav_corn_off.gif";
  			rightMenu = false;
  		}
  	}
  }
 
}



 // Generates a go nav menu
 function goNav() {
	    if ( document.getElementById('go_nav') ) {
	    	var url = "http://www.franklinplanner.com/fc/common/json_leftnav.jsp";
		$.ajaxSetup({
        		global  : true,
       			dataType: 'jsonp',
        		jsonp   : 'jsoncallback',
        		cache   : true
		}); 
		    
		$.ajax({ type:"GET", processData:false, url:url,
    			success:function(jsonp) {
				if ( jsonp.items && jsonp.items.length > 0 ) {
					// Build up a dom element with the menu items
					var goDiv = document.getElementById('go_nav');
					var goTable = document.createElement('table');
					goTable.setAttribute('style','width:170px;');
					var goTbody = document.createElement('tbody');
					// The Title
					var titleTr = document.createElement('tr');
					var titleTd = document.createElement('td');
					titleTd.style.paddingLeft = "5px";
					titleTd.innerHTML = "<b>" + jsonp.title + "</b>";
					titleTr.appendChild(titleTd);
					goTbody.appendChild(titleTr);

					$.each(jsonp.items, function(i,item) {
						var aTr = document.createElement('tr');
						var aTd = document.createElement('td');
						aTd.style.paddingLeft = "7px";
						aTd.style.paddingRight = "5px";
						aTd.style.paddingTop = "2px";
						aTd.innerHTML = "<a href='" + item.url + "' class='gonav_link'>" + item.name + "</a>";
						aTr.appendChild(aTd);
						goTbody.appendChild(aTr);
					});
					goTable.appendChild(goTbody);
					goDiv.appendChild(goTable);
				}
			}
		});
	    }
    }
function findPositionX(obj) {
    var curleft = 0;
    if (obj.offsetParent) {
        while (1) {
            curleft+=obj.offsetLeft;
            if (!obj.offsetParent) {
                break;
            }
            obj=obj.offsetParent;
        }
    } else if (obj.x) {
        curleft+=obj.x;
    }
    return curleft;
}
function findPositionY(obj) {
    var curtop = 0;
    if (obj.offsetParent) {
        while (1) {
            curtop+=obj.offsetTop;
            if (!obj.offsetParent) {
                break;
            }
            obj=obj.offsetParent;
        }
    } else if (obj.y) {
        curtop+=obj.y;
    }
    return curtop;
}

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if (els[i].className && els[i].className.match(pattern)) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function getNodeElementsByName(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if (els[i].name && els[i].name.match(pattern)) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
} 

function getDivHeight(pNode)
{ 
	return pNode.innerHeight?pNode.innerHeight:pNode.offsetHeight+document.body.offsetTop; 
}
function getDivWidth(pNode)
{ 
	return pNode.innerWidth?pNode.innerWidth:pNode.offsetWidth; 
}

