var enableAjaxLoading = false;

var initMenu = function(thisMenu)
{
	if (!enableAjaxLoading) return;
	thisMenu.onmouseover = null;
	if ($("#products").length && location.href.indexOf('https') == -1)
	{
		$(thisMenu).parent().find('li.menuPricelist a').each(function(){
			var thisObj = $(this);
			var href = ""+thisObj.attr("href");
			if (href.indexOf("#pg=") == -1)
			{
				if (href.indexOf(gRootFolder) == 0) href = href.substr(gRootFolder.length);
				var pos = href.indexOf("?");
				if (pos != -1) thisObj.attr("href", "#pg=" + href.replace("?", "&"));
				else thisObj.attr("href", "#pg=" + href);		
				thisObj.focus(function() {
					if (this.blur) this.blur();
				});
			}
		});
	}
}

var initProductgroups = function(thisPG)
{
	if (!enableAjaxLoading) return;
	thisPG.onmouseover = null;
	if ($("#products").length && location.href.indexOf('https') == -1)
	{
		$(thisPG).find('a').each(function(){
			var thisObj = $(this);
			var href = ""+thisObj.attr("href");
			if (href.indexOf("#pg=") == -1)
			{
				if (href.indexOf(gRootFolder) == 0) href = href.substr(gRootFolder.length);
				var pos = href.indexOf("?");
				if (pos != -1) thisObj.attr("href", "#pg=" + href.replace("?", "&"));
				else thisObj.attr("href", "#pg=" + href);		
				thisObj.focus(function() {
					if (this.blur) this.blur();
				});
			}
		});
	}
}

var initPGTree = function(thisPGTree)
{
	if (!enableAjaxLoading) return;
	initProductgroups(thisPGTree);
	if ($("#products").length && location.href.indexOf('https') == -1)
	{
		$(thisPGTree).find('a').each(function(){
			var thisObj = $(this);
			var href = ""+thisObj.attr("href");
			if (href.indexOf(gRootFolder) == 0) href = href.substr(gRootFolder.length);
			var url = href.substr(href.indexOf("#")+3);
			var id = url.substr(url.indexOf('=')+1).replace(/[\/&=\.]/g, '_');
			id = 'pg_' + id.replace(/^[_]*/, '');
			thisObj.attr("id", id);
		});
	}
}

var initProduct = function(thisProduct)
{
	if (!enableAjaxLoading) return;
	thisProduct.onmouseover = null;
	if ($("#products").length && location.href.indexOf('https') == -1)
	{
		$(thisProduct).find('a').each(function(){
			var thisObj = $(this);
			var href = ""+thisObj.attr("href");
			if (href.indexOf(gRootFolder) == 0) href = href.substr(gRootFolder.length);
			var pos = href.indexOf("?");
			if (pos != -1) 
			{
				if (href.indexOf("keep=1") == -1) thisObj.attr("href", "#path=" + href.replace("?", "&"));
			}
			thisObj.focus(function() {
				if (this.blur) this.blur();
			});
		});
	}
};

var initPage = function(thisPage)
{
	if (!enableAjaxLoading) return;
	thisPage.onmouseover = null;
	if ($("#products").length && location.href.indexOf('https') == -1)
	{
		var fragment = $.param.fragment();
		fragment = fragment.replace(/page=[0-9]+/g, '').replace(/pg=/g, '').replace(/path=/g, '');
		
		$(thisPage).find('a').each(function(){
			var thisObj = $(this);
			var href = ""+thisObj.attr("href");
			if (href.indexOf('#path=') == -1)
			{
				var pos = href.indexOf("?");
				var page = (pos != -1)? "&" + href.substr(pos+1).replace("?", "&") : '';
				href = "#path="+fragment + page;
				href = href.replace(/&&/g, '&');
				thisObj.attr("href", href);
			}
		});
	}
}


$(function() {
		   
var currentSelected = [];
var currentVisible = [];
var container = [];
if (enableAjaxLoading && $("#products").length)
{
	var getContainer = function(selector) 
	{
		if (!container[selector]) 
		{
			var cont = $div.find(selector);
			if (cont.length) container[selector] = cont;
			else return false;
		}
		return container[selector];
	}
	var $div = container['#shopInner'] = $('#shopInner');
}

		   
var setPGID = function(id)
{
	if (!enableAjaxLoading) return;
	var url = $.param.fragment();
	if (url.indexOf("pg=") != -1 || url.indexOf("path=") != -1)
	{
		if ($cont = getContainer('.bbq-default-pg')) $cont.hide();
		if ($cont = getContainer('.bbq-default-path')) $cont.hide();
	}
	if (currentSelected.length == 0)
	{
		$('#productGroupTree .selected').each(function(){
			var thisObj = $(this);
			if (!thisObj.data('oldClass'))
			{
				var nodeName = this.nodeName.toLowerCase();
				if (nodeName == 'li')
				{
					if (thisObj.attr('oldClass')) thisObj.data('oldClass', thisObj.attr('oldClass'));
				}
				else if (nodeName == 'ul') thisObj.data('oldClass', '');
			}
			currentSelected.push(thisObj);
		});
	}
	for (var i = 0; i < currentSelected.length; i++)
	{
		currentSelected[i].removeClass('selected closed').addClass(currentSelected[i].data('oldClass'));
		currentSelected[i].children().each(function(){
			var thisObj = $(this);
			if (this.nodeName.toLowerCase() == 'ul') 
			{
				thisObj.removeClass('selected');
			}
		});
	}
	currentSelected = [];

	var $menu = $('#productGroupTree #'+id);
	if ($menu.length == 0)
	{
		initPGTree('#productGroupTree');
		$menu = $('#productGroupTree #'+id);
	}
	$menu.siblings().each(function(){
		var thisObj = $(this);
		if (!thisObj.data('oldClass')) thisObj.data('oldClass', thisObj.attr('className').split(',').join(' '));
		if (!thisObj.hasClass('root')) 
		{
			if (this.nodeName.toLowerCase() == 'li') thisObj.addClass('subroot').removeClass('closed root');
			else if (this.nodeName.toLowerCase() == 'ul') 
			{
				thisObj.addClass('selected');
				thisObj.children().each(function(){
					var thisObj = $(this);
					if (!thisObj.data('oldClass')) thisObj.data('oldClass', thisObj.attr('className').split(',').join(' '));
					if (!thisObj.hasClass('root')) 
					{
						if (this.nodeName.toLowerCase() == 'li') thisObj.addClass('subroot').removeClass('closed root');
					}
					currentSelected.push(thisObj);
				});
			}
		}
		currentSelected.push(thisObj);
	});

	$menu.parentsUntil('#productGroupTree ul.dropdown').each(function(){
		var thisObj = $(this);
		if (!thisObj.data('oldClass')) thisObj.data('oldClass', thisObj.attr('className').split(',').join(' '));
		thisObj.addClass('selected').removeClass('closed dir root subroot');
		currentSelected.push(thisObj);
		if (thisObj.data('oldClass') == 'dir root')
		{
			thisObj.children().each(function(){
				var thisObj = $(this);
				if (this.nodeName.toLowerCase() == 'ul') 
				{
					thisObj.addClass('selected');
					thisObj.children().each(function(){
						var thisObj = $(this);
						if (!thisObj.data('oldClass')) thisObj.data('oldClass', thisObj.attr('className').split(',').join(' '));
						if (this.nodeName.toLowerCase() == 'li') 
						{
							if (!thisObj.hasClass('selected')) thisObj.addClass('subroot').removeClass('closed');								 
						}
					});
				}
			});
		}

		thisObj.siblings().each(function(){
			var thisObj = $(this);
			if (!thisObj.data('oldClass')) thisObj.data('oldClass', thisObj.attr('className').split(',').join(' '));
			if (!thisObj.hasClass('root')) 
			{
				if (this.nodeName.toLowerCase() == 'li') thisObj.addClass('subroot').removeClass('closed root');
				else if (this.nodeName.toLowerCase() == 'ul') thisObj.addClass('selected');
			}
			currentSelected.push(thisObj);
		});

	});
}

if (enableAjaxLoading && $("#products").length && location.href.indexOf('https') == -1)
{
	var cache = {'': $('.bbq-default-path')};
	var whatPG = '';	
	
	$(window).bind('hashchange', function(e) {

		var url = $.param.fragment();
		var id = url.substr(url.indexOf('=')+1);
		id = id.replace(/[\/\.]*/, '');
		var a = id.split('/');
		if (a.length == 1 || a.length == 2 && a[1] == '') 
		{
			url = '';
			id = '';
		}

		id = id.replace(/index\.php/, '');
		id = id.replace(/[\/&=\.]/g, '_');
		id = id.replace(/^[_]*/, '');
		id = id.replace(/_page_1/, '');
		var what = (url == '')? '-path' : '-'+url.substr(0, url.indexOf('='));
		
		var reloadID = false;
		var reoladURL = false;
		if (url.indexOf('path=') != -1)
		{
			var tempID = url.substr(url.indexOf('=')+1);
			tempID = tempID.substr(0, tempID.indexOf('&'));
			tempURL = tempID;
			tempID = tempID.replace(/index\.php/, '');
			tempID = tempID.replace(/[\/\.]*/, '');
			tempID = tempID.replace(/[\/&=\.]/g, '_');
			tempID = tempID.replace(/^[_]*/, '');
			tempID = tempID.replace(/_page_1/, '');
			if (getContainer('.'+tempID  + ' .bbq-default-path') === false) 
			{
				what = '-pg';
				reloadID = tempID;
				reoladURL = tempURL;
				id = tempID;
				url = 'pg=' +tempURL;
			}
		}
		var reloadID = false;
		var reoladURL = false;

		if (what == '-pg') 
		{
			whatPG = id;
			setPGID('pg_' + id);
		}
	
		var ajaxPage = (what == '-pg')? 'shopOverviewPGAjax.php' : 'shopOverviewAjax.php';
		var div = (what == '-pg')? '#shopInner' : '#products';
		var whatDIV = (what == '-pg')? div : ((whatPG != '')? "." + whatPG + ' ' + div : div);
	
		var currentPG = [];
		for (var i in currentVisible)
		{
			if (i == null || ''+i == 'undefined') continue;
			if (what == '-pg' || id.substr(0, i.length) != i) currentVisible[i].hide();
			else if (i != '') currentPG[i] = currentVisible[i];
		}
		currentVisible = [];
		for (var i in currentPG)
		{
			currentVisible[i] = currentPG[i];
		}
		
		if (cache[id]) 
		{
			currentVisible[id] = cache[id];
			if (url != '')
			{
//				if (what == '-pg') 
				{
//				currentVisible['.bbq-default-pg'] = getContainer('.bbq-default-pg').hide();
//				currentVisible['.bbq-default-path'] = getContainer('.bbq-default-path').hide();

					if ($cont = getContainer('.'+id  + ' .bbq-default-path')) currentVisible['.'+id  + ' .bbq-default-path'] = $cont.show();
				}
				currentVisible[id].show();
				currentVisible[id].parents().show();
			}
			else
			{
				if ($cont = getContainer('.bbq-default-pg')) currentVisible['.bbq-default-pg'] = $cont.show();
				if ($cont = getContainer('.bbq-default-path')) currentVisible['.bbq-default-path'] = $cont.show();
			}
		}
		else 
		{
			var $whatDIV = getContainer(whatDIV);
			if ($whatDIV === false) return;
			var $loading = (whatDIV == '#shopInner')? getContainer('.bbq-loading'+what) : getContainer(whatDIV + ' .bbq-loading'+what);
			if ($loading === false) return;
			$loading.show();
			container[id] = currentVisible[id] = cache[id] = $('<div />')
			.addClass(id)
			.appendTo($whatDIV)
			.load(gRootPath + 'includes/'+ajaxPage+'?'+url, function(){
				if (what == '-pg') container[id + ' .bbq-default-path'] = currentVisible[id + ' .bbq-default-path'] = $whatDIV.find('.' + id + ' .bbq-default-path');
				$div.intoViewport();
				$loading.hide();
				if (reloadID !== false && ""+reloadID != "undefined") 
				{
					$(window).trigger('hashchange', e);
				}
			});
		}
	})
	  
	$(window).trigger('hashchange');

}

}); 
