
/* Function to display product names starting 
with 0-9 on load of Product listings page */
function onLoad() {

	var selected_products = [];
	var prod_list = $('product-list');
	var list = $('azlist-container');
	var selector = '1';
	var numericRegEx=/^[0-9]*$/;
	selected_products.empty();

	var links = list.getElements('li');

	if(links[1]) {
		links[1].addClass('selected');	
	}
	
	prod_list.getElements('a').each(function(product){
	
		var letter = product.innerHTML.substr(0,1).toLowerCase();
		if (selector.match(numericRegEx)&&letter.match(numericRegEx)){	
			selected_products.push(product.getParent().innerHTML)				
		}
		else if (letter==selector){
			selected_products.push(product.getParent().innerHTML);
		};
		
	});
	
	
	//Create display
	if ($('product-display')){$('product-display').dispose()};
	var div = new Element('div',{'id':'product-display'});
	if (selected_products.length==0){
		var h3 = new Element('h3');
			var productFlow = $('FlowStaus').value;
			if(productFlow != 'LEFTNAVFILTER') {
				h3.innerHTML = 'There are no products beginning with this letter.';
			} else {
				h3.innerHTML = 'There are no documents for products beginning with this letter.';
			}
		h3.inject(div);
	} else {
		var ul = new Element('ul');
		for (var i=0;i<selected_products.length;i++){
			var li = new Element('li');
			li.innerHTML = selected_products[i];
			li.inject(ul);
		}				
		ul.inject(div);
	}
	div.inject(list,'after');
}


//global variable to store responseXML 
 gResponseXML = null;
 var isInProcess = 'false';
var searchSuggestion = new Class();
searchSuggestion.prototype = {
	initialize: function(params){
		var self = this;
		this.element = $(params.name);
		this.name = params.name;
		this.source = params.source;
		this.maxheight = params.maxheight || 150;
		this.method = params.method || 'post';
		this.open=0;
		this.empty=1;
		this.hasFocus=0;
		
		this.link_array = [];
		this.selected_link = 0;
		
		this._create();
		//this.element.addEvent('click',this._update.bindWithEvent(this));
		
		this.element.addEvent('keydown',function(event){
			self._whichKey(event);
		});
		
		this.element.addEvent('blur',function(event){
			self.hasFocus=0;
		})
		document.addEvent('click',function(e){	
			if (self.open){
				if (e.target!=(self.element||self.holder)){			
					if (!self.holder.hasChild(e.target)){
						self._close();
					};	
				};
			};		
		});
	},
	_open: function(){
	//alert('_open');
		
		if (this.element.value!=''){
			this.holder.style.left = '10px';
			this.open=this.hasFocus=1;
			this.holder.style.borderBottom = 'none';
		} else {
			this._close();
		}
	},
	_close: function(){
	//alert('_close');
		this.holder.style.left = '-1000em';
		this._reset();
		this.open=this.hasFocus=0;	
	},
	_reset: function(){
	//alert('_reset');
		while (this.list.lastChild){
			this.list.removeChild(this.list.lastChild);
		};	
		try {
			this.holder.getElements('div.custom-scrollbar')[0].destroy();
		} catch (e){};
		this.link_array.empty();
		this.selected_link=0;
		this.list.setStyles({
			top: '0px'	
		});
	},
	_update: function(){
		var self = this;
		this.empty=1;
		clearTimeout(this.keyPress);
		this.keyPress = setTimeout(function(){
			self._open();
			self._reset();
			self._populate();			
		},200)
	},
	_create: function(){
		var self = this;
		this.holder = new Element('div', {id:self.name + '-holder', 'class':'suggestion-holder'});
		this.list = new Element('ul');
		this.list.inject(this.holder);
		this.holder.inject(this.element,'after');
		this.holder.addEvents({
			'click': function(e){
						if (e.target.nodeName.toLowerCase() == 'a') {
							self._leavePage.bind(self)
						};
					},
			'keydown':function(e){
						
						self._whichKey(e);
					}
		});
	},  
	_populate: function(){
		var self = this;
		
		//alert("gResponseXML "+gResponseXML);
		
		
		this.val = this.element.value;
		var clicked_link = $('clicked_link').value;
		var status_list = "";
		//if()
		//alert("in process :"+isInProcess);
		if (this.val.length>0){
		
			if(gResponseXML==null && isInProcess == 'false'){
				isInProcess = 'true';
				this.req = new Request({
					//url: self.source,
					url:"/css/portlets/product/getResponsePRoduct.do?type="+clicked_link,
					link: 'cancel',
					method: self.method,
					onComplete:function(response, responseXML){},
					onSuccess: function(response, responseXML){
							
						this.products = responseXML.getElementsByTagName("product");
						gResponseXML = responseXML.getElementsByTagName("product");
						
						for (var i=0;i<this.products.length;i++){
							this._strmatch = this.products[i].firstChild.nodeValue.substr(0,self.val.length);
							if (this._strmatch.toLowerCase()==self.val.toLowerCase()){
								this.li = new Element('li');
								this.a = new Element('a',{href:this.products[i].getAttribute('href'),'tabIndex':'-1'});
								this.a.innerHTML = this.products[i].firstChild.nodeValue;
								this.a.inject(this.li);
								this.li.inject(self.list);	
								self.empty = 0;
								self.link_array.push(this.a);	
								
								status_list = "List";
							};
							if(i==this.products.length-1){

							};
						};
						
						if (this.empty){
							
							this.li = new Element('li');
							this.a = new Element('a',{href:'#','tabIndex':'-1'});
							this.a.innerHTML = 'There are no products beginning with this letters';
							this.a.inject(this.li);
							this.li.inject(self.list);	
							self.empty = 0;
							self.link_array.push(this.a);
							self._scrollbars();
							self._focusLink();
							status_list = "None";
							
						} else {
							if(status_list == ""){
								this.li = new Element('li');
								this.a = new Element('a',{href:'#','tabIndex':'-1'});
								this.a.innerHTML = 'There are no products beginning with this letters';
								this.a.inject(this.li);
								this.li.inject(self.list);	
								self.empty = 0;
								self.link_array.push(this.a);
							}
							self._scrollbars();
							self._focusLink();
							isInProcess = 'false';
						}
						
						
					}
					
				}).send();
			}
			else if(gResponseXML!=null && isInProcess == 'false'){
				//alert("gResponseXML length"+gResponseXML.length);
				
				for (var i=0;i<gResponseXML.length;i++){
					this._strmatch = gResponseXML[i].firstChild.nodeValue.substr(0,self.val.length);
					if (this._strmatch.toLowerCase()==self.val.toLowerCase()){
						this.li = new Element('li');
						this.a = new Element('a',{href:gResponseXML[i].getAttribute('href'),'tabIndex':'-1'});
						this.a.innerHTML = gResponseXML[i].firstChild.nodeValue;
						this.a.inject(this.li);
						this.li.inject(self.list);	
						self.empty = 0;
						self.link_array.push(this.a);	
						status_list = "List";		
					};
					if(i==gResponseXML.length-1){
					
						//self._scrollbars();
						//self._focusLink();
					};
				};	
				if (this.empty){
					
					this.li = new Element('li');
					this.a = new Element('a',{href:'#','tabIndex':'-1'});
					this.a.innerHTML = 'There are no products beginning with this letters';
					this.a.inject(this.li);
					this.li.inject(self.list);	
					self.empty = 0;
					self.link_array.push(this.a);
					self._scrollbars();
					self._focusLink();
					status_list = "None";
					
				} else {
					if(status_list == ""){
						this.li = new Element('li');
						this.a = new Element('a',{href:'#','tabIndex':'-1'});
						this.a.innerHTML = 'There are no products beginning with this letters';
						this.a.inject(this.li);
						this.li.inject(self.list);	
						self.empty = 0;
						self.link_array.push(this.a);
					}
					self._scrollbars();
					self._focusLink();
					//alert("in else picking from cache");
				}
			
			};
		};	
	},
	_scrollbars: function(){	
		
		if (!this.empty){
			this.holder.style.borderBottom = '1px solid #d6d6d6';
		} else {
			this.holder.style.borderBottom = 'none';
			this._close();
		};
		
		if (this.list.getSize().y>this.maxheight){
			this.holder.setStyles({
				height: this.maxheight + 'px',
				borderRight: 'none'
			});
			setGapScrollBars(this.list,this.holder,this.maxheight,this.list.getSize().y);
		} else {		
			this.holder.setStyles({
				height: 'auto',
				borderRight: '1px solid #d6d6d6'
			});
		};
	},
	_leavePage: function(){
		this._reset();
		this.element.value = "Enter Product Name";
	},
	_whichKey: function(event){
		this.code = event.key;
		if ((this.code.length==1&&this.code.match(/\w/))||this.code=='space'||this.code=='backspace'||this.code=='delete'){
			this._update();
		}
		else if (this.code=='down') {
			event.stop();			
			this._arrowNav('down');											
		} 
		else if (this.code=='up'){
			event.stop();
			this._arrowNav('up');						
		} 
	},
	_arrowNav: function(direction){
		this.direction=direction=='down'?1:-1;
		if (!this.hasFocus){
			try {			
				this.element.value = this.link_array[this.selected_link+this.direction].innerHTML;				
				this.link_array[this.selected_link+this.direction].focus();				
			} catch(e) {};
		} else {
			this.element.value = this.link_array[0].innerHTML;
			this.link_array[0].focus();
		};	
	},
	_focusLink: function(){
		var self = this;
		var links = this.holder.getElements('a');
		links.each(function(link,index){
			link.addEvents({
				'focus': function(e){
					link.addClass('focus');
					self.selected_link = index;
				},
				'blur': function(e){
					link.removeClass('focus');
				}
			});
		});			
	}
};

function fnTOggleNav(toggleName) {

	if($('productStatus')) {
		var pageStatus = $('productStatus').value;
	}
	if($('toggleNavigation')) {
		var toggleActive = $('toggleNavigation').value;
	}
	if($('toggleNavigationFilter')) {
		var toggleNavigationFilter = $('toggleNavigationFilter').value;
	}
	
	if(pageStatus == 'OVERVIEW') {
		if($('OverviewLink')){
			$('OverviewLink').addClass('red');
		}
	} else if(pageStatus == 'LEFTNAVFILTER') {		
	
		if($('OverviewLink')){
			$('OverviewLink').removeClass('red');
		}		
		if($(toggleActive)) {
			
			if(toggleActive == 'Declarations of Conformity' || toggleActive == 'Service Support Notices' || toggleActive == 'Security Advisories' || toggleActive == 'End of Sale Notices' || toggleActive == 'Product Correction Notices' || toggleActive == 'Product Support Notices' || toggleActive == 'Product Upgrade Notices' || toggleActive == 'Security Alerts'){
				
				if(navigator.appName == 'Netscape') {
					simulateClick($("Notices"));
				} else {
					$("Notices").click();
				}
			} else {
				if(navigator.appName == 'Netscape') {
					simulateClick($("Documentation"));
				} else {
					$("Documentation").click();
				}
			}
			
			$(toggleActive).style.color = '#cc0000';
		}
		
	} else if(pageStatus == 'PRODUCTDOC') {
	
		if($('OverviewLink')){
			$('OverviewLink').removeClass('red');
		}		
		if($(toggleNavigationFilter)) {
			
			if(toggleNavigationFilter == 'Declarations of Conformity' || toggleNavigationFilter == 'Service Support Notices' || toggleNavigationFilter == 'Security Advisories' || toggleNavigationFilter == 'End of Sale Notices' || toggleNavigationFilter == 'Product Correction Notices' || toggleNavigationFilter == 'Product Support Notices' || toggleNavigationFilter == 'Product Upgrade Notices' || toggleNavigationFilter == 'Security Alerts'){
				if(navigator.appName == 'Netscape') {
					simulateClick($("Notices"));
				} else {
					$("Notices").click();
				}
				
			} else {
				if(navigator.appName == 'Netscape') {
					simulateClick($("Documentation"));
				} else {
					$("Documentation").click();
				}
							
			}
			
			$(toggleNavigationFilter).style.color = '#cc0000';
		}
	}
	
}


function simulateClick(cb) {

  var evt = document.createEvent("MouseEvents");
  evt.initMouseEvent("click", true, true, window,
    0, 0, 0, 0, 0, false, false, false, false, 0, null);

  var canceled = !cb.dispatchEvent(evt);
 
}
