/*

	Series of methods that should have been classes together with objects created,
	but was done through programmatic functionality instead.  These scripts observe
	and provide the fuctionality for the left hand navigation panel, "Refresh Your Map"

*/


var panes = null;
var titles = null;

function observeRefreshYourMap() {
	observeDevice();
	observeDevices();
	
	observeManufacturer();
	observeManus();
	
	observeModel();
	observeModels();
	
	observeYear();
	observeYears();
	
	observeSystem();
	observeSystems();
	
	observeHovering();

	insertBreaks();


	titles = $$('#map-select dt');
	panes = $$('#map-select dd');
	
	[$('device-select'), $('manu-select'), $('model-select'), $('year-select'), $('system-select')].invoke('addClassName', 'unselected');

}

function hideAllBut(title) {

	panes.without(title.next()).invoke('addClassName', 'hidden');
	titles.without(title).invoke('removeClassName', 'on');
}



function insertBreaks() {
	var text;
	$$('#map-select li').each(function(e) {
									   
									   //&#8203;
									   
									   
									   text = e.innerHTML;
									   
									   
									   
									   
									   });
	
}



function cleanListItem(listItem) {
	if (listItem.length > 25 ) {
	
		listItem = listItem.truncate(25);
	}
	return listItem;
}

function observeHovering() {

	$$('#map-select li').each(function(e){
									Event.observe(e, 'mouseover', function(event){
																	Event.element(event).addClassName('hover');		   
															       });
							  });
	$$('#map-select li').each(function(e){
									Event.observe(e, 'mouseout', function(event){
																	Event.element(event).removeClassName('hover');		   
															     });
							  });
}




///////// DEVICES ///////////////////////////
function observeDevice() {
	Event.observe('device-select', 'click', showDevices);
}
function showDevices() {
	
	hideAllBut($('device-select'));
	
	if ( $('device-select').hasClassName('on') ) {
		
		$('device-select').next().addClassName('hidden');
		$('device-select').removeClassName('on')
	}
	
	else {
		
		$('device-select').next().removeClassName('hidden');
		$('device-select').addClassName('on')
	}
	// Fix for Safari refresh issue
    document.getElementById("device-select").innerHTML = document.getElementById("device-select").innerHTML + " "; 
}
function observeDevices() {
	$$('.device li').invoke('observe', 'click', function (event){
														$('device-select').innerHTML = cleanListItem(Event.element(event).innerHTML);
														
														$('manu-select').innerHTML = $('manu-value').innerHTML;	
														
														[$('model-select'), 
														 $('year-select'), $('system-select')].invoke('addClassName', 'hidden');
														
														[$('manu-select'), $('model-select'), 
														 $('year-select'), $('system-select')].invoke('addClassName', 'unselected');



														pickDevice();
														  });
}
function pickDevice() {

	$$('.device')[0].addClassName('hidden');
	$('device-select').removeClassName('on');
	$('device-select').removeClassName('unselected');
	$('manu-select').removeClassName('hidden');
	// Set the menu state to open. This prevents Flash from re-opening the menu.
	menuIsOpen = true; 
}

// Hide the left drop down menu
function hideDevice()
 {
            $$('.device')[0].addClassName('hidden');
            $('device-select').removeClassName('on');
           

            // Fix for Safari refresh issue
            document.getElementById("device-select").innerHTML = document.getElementById("device-select").innerHTML + " ";
} 

///////// MANUFACTURERS ///////////////////////////////

function observeManufacturer() {
	Event.observe('manu-select', 'click', showManus);
}
function showManus() {
	
	hideAllBut($('manu-select'));
	
	if ( $('manu-select').hasClassName('on') ) {
		$('manu-select').next().addClassName('hidden');
		$('manu-select').removeClassName('on')
	}
	
	else {
		$('manu-select').next().removeClassName('hidden');
		$('manu-select').addClassName('on')
	}
}
function observeManus() {
	$$('.manu li').invoke('observe', 'click', function (event){
														$('manu-select').innerHTML = cleanListItem(Event.element(event).innerHTML);
														$('model-select').innerHTML = $('model-value').innerHTML;
														[$('year-select'), $('system-select')].invoke('addClassName', 'hidden');
														[$('model-select'), 
														 $('year-select'), $('system-select')].invoke('addClassName', 'unselected');

														pickManu();
														  });
}
function pickManu() {

	$$('.manu')[0].addClassName('hidden');
	$('manu-select').removeClassName('on');
	$('manu-select').removeClassName('unselected');
	//$('model-select').removeClassName('hidden');
}

////////  MODELS  //////////////////////////////////

function observeModel() {
	Event.observe('model-select', 'click', showModels);
}
function showModels() {
	hideAllBut($('model-select'));

	if ( $('model-select').hasClassName('on') ) {
		$('model-select').next().addClassName('hidden');
		$('model-select').removeClassName('on')
	}
	
	else {
		$('model-select').next().removeClassName('hidden');
		$('model-select').addClassName('on')
	}
}
function observeModels() {
	$$('.model li').invoke('observe', 'click', function (event){
														$('model-select').innerHTML = cleanListItem(Event.element(event).innerHTML);
														$('year-select').innerHTML = $('year-value').innerHTML;
														[$('system-select')].invoke('addClassName', 'hidden');
														
														[$('year-select'), $('system-select')].invoke('addClassName', 'unselected');

														pickModel();
														  });
}
function pickModel() {

	$$('.model')[0].addClassName('hidden');
	$('model-select').removeClassName('on');
	$('model-select').removeClassName('unselected');
	//$('year-select').removeClassName('hidden');
}

///////// YEARS  /////////////////////////////////

function observeYear() {
	Event.observe('year-select', 'click', showYear);
}
function showYear() {
	hideAllBut($('year-select'));

	if ( $('year-select').hasClassName('on') ) {
		$('year-select').next().addClassName('hidden');
		$('year-select').removeClassName('on')
	}
	
	else {
		$('year-select').next().removeClassName('hidden');
		$('year-select').addClassName('on')
	}
}
function observeYears() {
	$$('.year li').invoke('observe', 'click', function (event){
														
														$('year-select').innerHTML = cleanListItem(Event.element(event).innerHTML);
														$('system-select').innerHTML = $('system-value').innerHTML;
														$('system-select').addClassName('unselected');
														pickYear();  
														  });
}
function pickYear() {
	
	$$('.year')[0].addClassName('hidden');
	$('year-select').removeClassName('on');
	$('year-select').removeClassName('unselected');
	//$('system-select').removeClassName('hidden');
}

///////// SYSTEMS  /////////////////////////////////

function observeSystem() {
	Event.observe('system-select', 'click', showSystem);
}
function showSystem() {
	hideAllBut($('system-select'));

	if ( $('system-select').hasClassName('on') ) {
		$('system-select').next().addClassName('hidden');
		$('system-select').removeClassName('on')
	}
	
	else {
		$('system-select').next().removeClassName('hidden');
		$('system-select').addClassName('on')
	}
}
function observeSystems() {
	$$('.system li').invoke('observe', 'click', function (event){
														$('system-select').innerHTML = cleanListItem(Event.element(event).innerHTML);
														pickSystem();
														  });
}
function pickSystem() {
	$('system-select').removeClassName('on');
	$('system-select').removeClassName('unselected');
	$$('.system')[0].addClassName('hidden');

}

function notListed(){
	var prodlisturl = document.getElementById('prodlisturl').value;												
	var notlistedid = document.getElementById('notlistedid').value;
	var noprodlisturl = document.getElementById('noprodlisturl').value;
	
	if(notlistedid==""){
	  prodlisturl = noprodlisturl + '?Page=ManufacturerContactInfo';
	}else{
	  prodlisturl = prodlisturl + '?CatalogCategoryID=' + notlistedid;
	}
		
	document.location.href = prodlisturl;
}


function removeElements(container){
	if ( container.hasChildNodes() )
		{
		   while ( container.childNodes.length >= 1 )
			{
				container.removeChild( container.firstChild );       
			} 
	}
}

function populateMenu(container,id,name)
{		
	var new_element = document.createElement('li');
	new_element.innerHTML = name;			
	new_element.id = id;
	new_element.onclick= function(){category_div(this.id)};	
	document.getElementById(container).appendChild(new_element);				
}

function XML2div(id,xmlobj,prevId) {
	/* This function takes the ID of the next level and the XML it will insert the categories in the next Select-Box*/	
	var cats = xmlobj.getElementsByTagName("category");
	var container = document.getElementById(id);
	var container2 = document.getElementById(id + "2");
	var promo = document.getElementById('promo').value;	
	/* Remove existing li elements */
	
	if(container != null)	
	{
		removeElements(container);
		removeElements(container2);
	}
	
	numRows=15;	
		
	var level = id=='manu-list'?'Level2':id=='model-list'?'Level3':id=='year-list'?'Level4':id=='system-list'?'Level5':'Level1';
	
	/* Remove existing li elements */	
	if (container2 != null && container2.hasChildNodes() )
	{
	   while ( container2.childNodes.length >= 1 )
		{
			container2.removeChild( container2.firstChild );       
		} 
	}	
	if (cats.length != 0) {		
		/* Parse xml and get category ID and name  */		
		for (var i = 0; i < cats.length; i++) {
			var catname, catid;
			var category = cats[i];
			for (var j = 0; j < category.childNodes.length; j++) {
			    with (category.childNodes[j]) {
					if (nodeName == "categoryname") {
						catname = firstChild.nodeValue;
					}
					else if (nodeName == "categoryid") {
						catid = firstChild.nodeValue;
					}
					else if (nodeName == "categoryidname") {
						catidname = firstChild.nodeValue;
					}
				}
			}
			
									
			/* Get childlist of current level */
			if(id=='manu-list'){
			  childList = 'model-list';
			  numRows=16;			  
			}
			if(id=='model-list'){
			  if(!(promo=='true')){
			    $('model-select').removeClassName('hidden');
			  }
			  numRows=14;
			  childList = 'year-list';			  
			}
			if(id=='year-list'){		
			  if(!(promo=='true')){
			  	$('year-select').removeClassName('hidden');
			  }
			  numRows=13;
			  childList = 'system-list';			  
			}
			if(id=='system-list'){
			  if(!(promo=='true')){
			  	$('system-select').removeClassName('hidden');
			  }	
			  childList = 'end-list';			 		  			  			  
			}
			
			/* check catidname for Not listed value.  Get value from model level. */
			pos = catidname.toLowerCase().indexOf('notlisted'); 
			if(pos>=0){										
				document.getElementById('notlistedid').value = catid;	
			}else{																			
				// Create a new <li> element for to insert inside <ul>
				var new_element = document.createElement('li');
				new_element.innerHTML = catname;				
				new_element.id = childList + "*" + catid;				
				new_element.onclick = function(){category_div(this.id)};
				
				if(i<numRows){
					/* Set ID and onclick for new li element. Insert into UL.*/
					container.appendChild(new_element);					
				}else{
					/* Set ID and onclick for new li element. Insert into UL.*/
					container2.appendChild(new_element);
				}				
			}
		}
						
		/* Set hover and click properties for new elements.  */		
		if(promo=='true'){
		   observePromoMap();
		}else{
		   observeRefreshYourMap();
		}
	}
	else {		
		/* else - if no subcategories have been found */
		/* display view product button.	      */
		/* build URL for product-list		*/
		var prodlisturl = document.getElementById('prodlisturl').value;												
		/* get the prodlist-Url from hidden Input */
		prodlisturl = prodlisturl + '?CatalogCategoryID=' + prevId + '&ShowAllProducts=YES';
		if(promo=='true'){
		  var dealerCode = document.getElementById('dealer-access-code').value;	
		  if(!(dealerCode=='')){
		   	prodlisturl = prodlisturl + '&DealerCode=' + dealerCode;
		  }
		  document.location.href = prodlisturl;	
		  return;
		}		
								
		prodlisturl = prodlisturl + '&PersistNavigationMenu=YES';								
		
		//Hide empty lists		
		if(id=='manu-list')
		{			
			if($('manu-list').empty() && !$('manu-select').hasClassName('hidden'))
			{
				$('manu-select').addClassName('hidden');	
			}
		}
		if(id=='model-list')		
		{
			if($('model-list').empty() && !$('model-select').hasClassName('hidden'))
			{				
				$('model-select').addClassName('hidden');	
			}
		}
		if(id=='year-list')
		{
			if($('year-list').empty() && !$('year-select').hasClassName('hidden'))
			{
				$('year-select').addClassName('hidden');	
			}
		}
		if(id=='system-list')
		{			
			if($('system-list').empty() && !$('system-select').hasClassName('hidden'))
			{
				$('system-select').addClassName('hidden');					
			}
		}
		
		document.location.href = prodlisturl;									
	}		
}


function category_div(divNameWithId) {	
	/* this Function is called from the template */	
	/* only of a category is selected - first option is something like "please select" */		
	pos=divNameWithId.indexOf('*');
	
	//alert(divNameWithId.substring(0,pos));
	divName = divNameWithId.substring(0,pos);
	pos = pos+1;
	selected =  divNameWithId.substring(pos,divNameWithId.length);	
	var callurl = document.getElementById('callurl').value;	
	/* get pipelinecall from hidden Input  */	
			
	callurl = callurl + '?CatalogCategoryID='+ selected;	
	//alert(callurl); /* add parameter for next category */
	callAJAX(callurl,divName, selected); /* call the AJAX function */
}

/*************************************************************************************/
/*  now 2 standard - AJAX Functions | In most cases there is no need to change these */
/*************************************************************************************/

function callAJAX(url,id, prevId) {
     try {
     req = new XMLHttpRequest();
     /* e.g. Firefox */
     } catch(e) {
       try {
       req = new ActiveXObject("Msxml2.XMLHTTP");
       /* some versions IE */
       } catch (e) {
         try {
         req = new ActiveXObject("Microsoft.XMLHTTP");
         /* some versions IE */
         } catch (E) {
          req = false;
         }
       }
     }
     req.onreadystatechange = function() {responseAHAH(id, prevId);};
     req.open("GET",url,true);
     req.send(null);
  }

function responseAHAH(id, prevId) {
   if(req.readyState == 4) {
      if(req.status == 200) {
      										/**************************************************/
      										/*	alert(req.responseText); uncomment for Debug  */
      										/**************************************************/
         //XML2select(id,req.responseXML);
         XML2div(id,req.responseXML, prevId);
         }
      }
  }
  
// Tracks the menu state
var menuIsOpen = false;
function isMenuOpen()
{
            // Return the menu state to Flash
return menuIsOpen;
}
   