

//jumptoabus logic
function openSubNav()
{
	alert('hey');
	return false;
}
 $(document).ready(function() {
	 	InitializeMenu();
   });
 
 function InitializeMenu(){
	 	 LoadBusMenu();
 }
 
 function LoadBusMenu(){
	 $.ajax({
         type: "GET",
         url: "/iccorp/jumptoabus",
         dataType: "json",
         success: jumpToABusMenu.setJSON,
         async: false
     }); 
 
 }
 var jumpToABusMenu = new function jtabmObj(){
	 var _this = this;
	 this.jsonData = null;
	 this.menuBuilt = false;
	 this.isIE6 = function() { return !$.support.cssFloat && typeof document.body.style.maxHeight == "undefined"; };	
	 this.isIE7 = function(){return $.browser.version == "7.0";};
	 this.amountLeft;
	 this.heightMultiplier;
	 this.heightOffset;
	 this.rollOutActive = true;
	 
	 
	 
	 this.setJSON = function(obj){
		 _this.jsonData = obj.menu;
	 };
	 this.open = function(ctl, level, topLevel, count){
		
		

		if(level != 'application')
		{
			$('ul#ulBusMenu li ul').hide();
			$('ul#ulBusMenu li').css('height','16px');
			$('li a').removeClass('selectedDropUp');
			$('#'+topLevel+' a:not(.subItems a)').addClass('selectedDropUp');
			rollOutActive = false;
		}
		else
		{
		rollOutActive = true;
			_this.buildMenu();
			$("#busesMenu").customFadeIn(250);	
		}
		
		var h = count*_this.heightMultiplier;

		//alert(((count /1)+ 1)  * (_this.heightMultiplier/1));
		$("#" + topLevel).animate({height: (((count /1)+ 1)  * (_this.heightMultiplier/1)) + "px"}, 1000, function()
		{	
			$("#" + level).customFadeIn(250);
			rollOutActive = true;
		});

		//$("#" + level).css({display:"block"});
		//$("#" + topLevel).css({height: (((count /1)+ 1)  * (_this.heightMultiplier/1)) + "px"});		
		return false;
	 };
	 this.adjustHeight = function(topLevel, count){			
		 var newHeight = $("#" + topLevel).height();
		 newHeight += (((count /1))  * ((_this.heightMultiplier/1) + (_this.heightOffset/1)));
		if(_this.isIE7()){				
			newHeight -= 50;
		}
		// $("#" + topLevel).css({height: newHeight + "px"});		
			return false;
		 };
	 this.closeAll = function(){
		if(rollOutActive)
		{
			$("#busesMenu").customFadeOut(250);
		}
	 }
	 this.close = function(ctl, level){
		 
	 }
	 this.buildMenu = function(){
		if(!_this.menuBuilt){
			_this.menuBuilt = true; 
		//if(_this.isIE7())
			var menu = _this.jsonData;
			 var htmlString = "";
			  //have to figure out offset later
			 if(_this.isIE7()){				
				 _this.amountLeft = 415;
				 _this.heightMultiplier = 18;
				 _this.heightOffset = 2;
			 }
			 else
			 {
				 _this.amountLeft = 575;
				 _this.heightMultiplier = 18;
				 _this.heightOffset = 2;
			 }
			 
			 
			 htmlString = '<div id="lowMenus" style="position:absolute;">';
			  htmlString += '<div id="' + 'buses' + 'Menu" class="dropUpMenu"style="bottom: 0px; background-color: transparent; background-image: url(/images/lowMenuBg.png); background-position: right bottom; background-repeat: no-repeat; width: 190px; display: none;">';
			  htmlString += '<ul id="ulBusMenu" style="padding: 3px 10px 6px 5px;"></ul></div></div>';
			  $("body").append(htmlString);
			  htmlString = "";
			  
			  $("#busesMenu").hover(function() {}, jumpToABusMenu.closeAll);
			  
			  for(var appIndex = 0; appIndex < menu.length; appIndex++){
				  
				  var appName = menu[appIndex].application;
				  //var series = menu[appIndex].series;
				  var models = menu[appIndex].models;
				  var appId = "app" + removeSpaces(appName);				  				 
				  var funcString = "jumpToABusMenu.open(this,\"ul" + appId + "\",\"li" + appId + "\"," +  models.length + ");return false;"
				  var onClickString = " onclick='" + funcString + "'"
				  htmlString += '<li id="li' + appId + '" class="" style="height: 16px;"><a' + onClickString + ' id="a'+ appId + '" target="_self" href="#">' + appName + '</a>';
				  htmlString += '</li>';
				 
				  $("#ulBusMenu").append(htmlString);
				  htmlString = "";
				  
				  
				  htmlString += '<ul id="ul' + appId + '" class="subItems" style="display: none;"></ul>';
				  $("#li" + appId).append(htmlString);
				  htmlString = "";
				  
				  /*
				  for(var seriesIndex = 0; seriesIndex < series.length; seriesIndex++){
					 var seriesName = series[seriesIndex].seriesname;
					 var models = series[seriesIndex].models;
					 var seriesId = appId + removeSpaces(seriesName);
					 
					 funcString = "jumpToABusMenu.open(this,\"ul" + seriesId + "\",\"li" + seriesId + "\"," +  models.length + ");jumpToABusMenu.adjustHeight(" + "\"li" + appId + "\"," +  models.length + ");return false;";
					 onClickString = " onclick='" + funcString + "'"
					 
					 htmlString += '<li id="li' + seriesId + '"><a' + onClickString + ' id="a'+ seriesId + '" target="_self" href="#">' + seriesName + '</a>';
					 htmlString += '<ul id="ul' + seriesId + '" style="display:none"></li></ul>';
					 $("#ul" + appId).append(htmlString);
					 htmlString = "";
					 */
					 
					 
					 for(var modelIndex = 0; modelIndex < models.length; modelIndex++){
						
						var modelName = models[modelIndex].modelname;
						var modelLink = models[modelIndex].link;
						htmlString += '<li><a target="_self" href="'+ modelLink + '">' + modelName + '</a></li>';
						$("#ul" + appId).append(htmlString);
						htmlString = "";
					 }				
				 // }			
			  }			 
		}
		 
	 };
 }
 

 function removeSpaces(str){
	 return str.replace( /\s/g, "" );
 }
 function returnAJavaScriptObject(jsonText){
	 //return eval('(' + jsonText + ')');
	 //console.log(jsonText);
	 return jsonText;

 }

 
 
 
 
 
 
 
 
 
 
 
