﻿// JScript File

var ProductMenu=function () {

	function init() {
		var SelectGo = $('product-menu-select-go');
		var SearchGo = $('product-menu-search-go');
		var SearchBox = $('SearchForProduct');
		var ProductContainer = $('product-cat-container');
		var ProductSubContainer = $('product-subcat-container');
		var ProductSubSubContainer = $('product-subsubcat-container');
		var ProductCategory = $('product-cat-select');
		var ProductSubCategory = $('product-subcat-select');
		var ProductSubSubCategory = $('product-subsubcat-select');
		var ProductCategoryDiv = new elSelect({container:ProductContainer});
		var ProductSubCategoryDiv = new elSelect({container:ProductSubContainer});
		var ProductSubSubCategoryDiv = new elSelect({container:ProductSubSubContainer});
		var ProductSearchLink = $('product-search-link');
		
		SearchGo.addEvent('click',function(e){
			if(SearchBox.value)
				location.href= ProductSearchLink.get('href')+"/"+SearchBox.value;
		});
		
		SelectGo.addEvent('click',function(e){
			if(ProductSubCategory.options.length <= 1)
			{
				if(ProductCategory.value != 0)
				{
					location.href=ProductCategory.value;
				}
			}
			else if(ProductSubSubCategory.options.length <= 1)
			{
				if(ProductSubCategory.value != 0)
				{
					location.href=ProductSubCategory.value;
				}
			}
			else if(ProductSubSubCategory.value != 0)
			{
				location.href=ProductSubSubCategory.value;
			}
		});
		
		ProductCategory.addEvent('change',function(e){
			if (!this.value) return;

			var url = this.value;
			url=url.substr(Config.rootUrl.length);

			var urlName=url.split("/").pop();
			
			if (!urlName) return;

			Mantis.Netafim.UI.Services.HeaderMenus.GetProductSubcategories(urlName,function (source) {
				var base = ProductSubCategory.getElement('option.first').dispose();
				ProductSubCategoryDiv.clear();
				ProductSubCategoryDiv.addOption(base);
				if(source.length < 1)
				{
					ProductSubContainer.hide();
				}
				else
				{
					ProductSubContainer.show();
					source.each(function(link){
						ProductSubCategoryDiv.addOption(new Element('option',{value: link.link, text: link.text}));
					});
				}
			});
			
			if(ProductSubSubCategoryDiv)
			{
				var subbase = ProductSubSubCategory.getElement('option.first').dispose();
				ProductSubSubCategoryDiv.clear();
				ProductSubSubCategoryDiv.addOption(subbase);
				ProductSubSubContainer.hide();
			}
		});
		
		ProductSubCategory.addEvent('change',function(e){
			if (!this.value) return;

			var url = this.value;
			url=url.substr(Config.rootUrl.length);

			var urlName=url.split("/").pop();
			
			if (!urlName) return;

			Mantis.Netafim.UI.Services.HeaderMenus.GetProductSubcategories(urlName,function (source) {
				var base = ProductSubSubCategory.getElement('option.first').dispose();
				ProductSubSubCategoryDiv.clear();
				ProductSubSubCategoryDiv.addOption(base);
				
				if(source.length < 1){
					ProductSubSubContainer.hide();
				}
				else
				{
					ProductSubSubContainer.show();
					source.each(function(link){
						ProductSubSubCategoryDiv.addOption(new Element('option',{value: link.link, text: link.text}));
					});
				}
			});
		});
		
		ProductSubContainer.hide();
		ProductSubSubContainer.hide();
	}
	
	$domready(init);	
}();