﻿function contains(List,value)
{
	if(List!=null)
	{
		for(ii=0;ii<List.length;ii++)
		{
			if(List[ii] == value)
				return true;
		}
	}
	return false;
}

function selectClickedItem(id)
{	
	var item = document.getElementById(id);
	if(item != null)
		item.className = 'active';
}

function unSelectClickedItem(id)
{	
	var item = document.getElementById(id);
	if(item != null)
		item.className = '';
}

function selectItems(firstList,firstRelations,productsInItem,firstId)
{
	k=0;
	
	var activeInFirstPositions = new Array();
		
	for(i=0;i<firstRelations.length;i++)
	{		
		for(j=0;j<productsInItem.length;j++)
		{
			if(contains(firstRelations[i],productsInItem[j]))
			{
				activeInFirstPositions[k] = i;
				k++;
				break;
			}
		}
	}
	
	for (t=0; t < firstList.length; t++)
	{
		id = firstId+firstList[t];
		var firstLink = document.getElementById(id);
		
		if(firstLink != null)
		{	
			if(!contains(activeInFirstPositions, t))
				firstLink.className = 'inactive';
			else
				firstLink.className = '';
		}
	}
}

function firstChangeStyles(mainList,mainRelations,firstList,firstRelations,secondList,secondRelations,itemid,firstId,secondId)
{
	var productsInItem = new Array(0);
	var position = 0;
	
	var activeInSecondPositions = new Array();

		for(i=0;i< mainList.length; i++)
		{
			if(mainList[i] == itemid)
			{
				position = i;
				break;
			}
		}	
		
		productsInItem = mainRelations[position];
		
		selectItems(firstList,firstRelations,productsInItem,firstId);
		
		selectItems(secondList,secondRelations,productsInItem,secondId);
}

function unClickItem(firstList,firstId)
{
	for (t=0; t < firstList.length; t++)
	{
		id = firstId+firstList[t];
		var firstLink = document.getElementById(id);
		
		if(firstLink != null)
		{					
			firstLink.className = '';
		}
	}
}

function UnClick(itemid,firstList,secondList,firstId,secondId)
{			
	 unClickItem(firstList,firstId);
	 unClickItem(secondList,secondId);
}

function getIndexOf(list,item)
{
	for(i=0;i< list.length; i++)
	{
		if(list[i] == item)
			return i;
	}
	
	return -1;
}

function doubleSelect(itemFirst,itemSecond, firstList,secondList,firstRelations,secondRelations, source,sourceRelations, sourceId)
{
	var productsFirst = new Array();
	var productsSecond = new Array();	
	var productsFinal = new Array();
	var position;
	
	position = getIndexOf(firstList,itemFirst);
	
	if(position < 0) return;
	
	productsFirst = firstRelations[position];
	
	position = getIndexOf(secondList,itemSecond);
	
	if(position < 0) return;
	
	productsSecond = secondRelations[position];
	
	counter = 0;
	
	for(i=0;i<productsFirst.length;i++)
	{
		if(contains(productsSecond,productsFirst[i]))
		{
			productsFinal[counter]=productsFirst[i];
			counter++;
		}
	}
	
	selectItems(source,sourceRelations,productsFinal,sourceId);
}

var clickedBrand;
var clickedCategory;
var clickedShop;

var brandLink;
var categoryLink;
var shopLink;

function selectText()
{
    if(document.getElementById("btc") != null)
    {
        //debugger;
        tooltip = document.getElementById("btc");
        spanTag = tooltip.getElementsByTagName("span");
        
                                
        if((clickedShop!= null) + (clickedCategory!= null) + (clickedBrand!= null) == 0)
            tetxtemp = firstText;
        if((clickedShop!= null) + (clickedCategory!= null) + (clickedBrand!= null) == 1)
            tetxtemp = secondText;
        if((clickedShop!= null) + (clickedCategory!= null) + (clickedBrand!= null) == 2)
            tetxtemp = thirdText;

        spanTag[1].textContent = tetxtemp;
        spanTag[1].innerText = tetxtemp;
        spanTag[1].innerHtml = tetxtemp;
    }
}

function changeBrandsMenu(itemid,link)
{
    if(document.getElementById('brands'+itemid).className == 'inactive')
        return;
    
	if(clickedShop != null && clickedCategory != null)
		document.location.href ='/home/showcase/' +  's-'+shopLink+'/c-'+categoryLink+'/b-'+link +'/';
	
	if(clickedBrand == itemid)
	{
		clickedBrand = null;
		brandLink=null;
		unSelectClickedItem('brands'+itemid);
		
		if(clickedShop == null && clickedCategory == null)
			UnClick(itemid,shops,categories,'shops','categories');
		else if(clickedShop == null && clickedCategory != null)
			firstChangeStyles(categories,categoriesProducts,brands,brandsProducts,shops,shopsProducts,clickedCategory,'brands','shops');
		else if(clickedShop != null && clickedCategory == null)
			firstChangeStyles(shops,shopsProducts,brands,brandsProducts,categories,categoriesProducts,clickedCategory,'brands','categories');
	}
	else
	{	
		if(clickedBrand != null)
			unSelectClickedItem('brands'+clickedBrand);
				
		clickedBrand = itemid;
		brandLink=link;				
		//unClickItem(brands,'brands');
		selectClickedItem('brands'+itemid);	
		
		if(clickedShop == null && clickedCategory == null)
			firstChangeStyles(brands,brandsProducts,shops,shopsProducts,categories,categoriesProducts,itemid,'shops','categories');
		else if(clickedShop == null && clickedCategory != null)
				doubleSelect(itemid,clickedCategory,brands,categories,brandsProducts,categoriesProducts,shops,shopsProducts,'shops');
		else if(clickedShop != null && clickedCategory == null)
			doubleSelect(itemid,clickedShop,brands,shops,brandsProducts,shopsProducts,categories,categoriesProducts,'categories');
	}
	
	selectText();
	
	if(clickedCategory == null && clickedBrand == null && clickedShop == null)
	    document.getElementById('show-btn').style.visibility = 'hidden';
	else
	    document.getElementById('show-btn').style.visibility = 'visible';
}

function changeShopsMenu(itemid,link)
{
    if(document.getElementById('shops'+itemid).className == 'inactive')
        return;

    if(clickedBrand != null && clickedCategory != null)
	{		
		document.location.href ='/home/showcase/' +  's-'+link+'/c-'+categoryLink+'/b-'+brandLink + '/';
	}

	if(clickedShop == itemid)
	{
		clickedShop = null;
		shopLink = null;
		unSelectClickedItem('shops'+itemid);
		
		if(clickedBrand == null && clickedCategory == null)
			UnClick(itemid,brands,categories,'brands','categories');
		else if(clickedBrand == null && clickedCategory != null)
			firstChangeStyles(categories,categoriesProducts,brands,brandsProducts,shops,shopsProducts,clickedCategory,'brands','shops');
		else if(clickedBrand != null && clickedCategory == null)
			firstChangeStyles(shops,shopsProducts,categories,categoriesProducts,brands,brandsProducts,clickedBrand,'shops','categories');
	}
	else
	{
		if(clickedShop != null)
			unSelectClickedItem('shops'+clickedShop);
			
		clickedShop = itemid;
		shopLink = link;
		//unClickItem(shops,'shops');
		selectClickedItem('shops'+itemid);	

		if(clickedBrand == null && clickedCategory == null)
			firstChangeStyles(shops,shopsProducts,brands,brandsProducts,categories,categoriesProducts,itemid,'brands','categories');
		else if(clickedBrand == null && clickedCategory != null)
			doubleSelect(itemid,clickedCategory,shops,categories,shopsProducts,categoriesProducts,brands,brandsProducts,'brands');
		else if(clickedBrand != null && clickedCategory == null)
			doubleSelect(itemid,clickedBrand,shops,brands,shopsProducts,brandsProducts,categories,categoriesProducts,'categories');
	}
	
	selectText();
	
	if(clickedCategory == null && clickedBrand == null && clickedShop == null)
	    document.getElementById('show-btn').style.visibility = 'hidden';
	else
	    document.getElementById('show-btn').style.visibility = 'visible';
}

function changeCategoriesMenu(itemid,link)
{
    if(document.getElementById('categories'+itemid).className == 'inactive')
        return;

	if(clickedBrand != null && clickedShop != null)
	{		
		document.location.href ='/home/showcase/' + 's-'+shopLink+'/c-'+link+'/b-'+brandLink+'/';
	}

	if(clickedCategory == itemid)
	{
		clickedCategory = null;
		categoryLink=null;
		unSelectClickedItem('categories'+itemid);
		
		if(clickedBrand == null && clickedShop == null)
			UnClick(itemid,shops,brands,'shops','brands');	
		else if(clickedBrand == null && clickedShop != null)
			firstChangeStyles(shops,shopsProducts,brands,brandsProducts,categories,categoriesProducts,clickedShop,'brands','categories');
		else if(clickedBrand != null && clickedShop == null)
			firstChangeStyles(brands,brandsProducts,shops,shopsProducts,categories,categoriesProducts,clickedBrand,'shops','categories');			
	}
	else
	{
		if(clickedCategory != null)
			unSelectClickedItem('categories'+clickedCategory);
			
		clickedCategory = itemid;
		categoryLink=link;
		//unClickItem(categories,'categories');
		selectClickedItem('categories'+itemid);	
		
		if(clickedBrand == null && clickedShop == null)
			firstChangeStyles(categories,categoriesProducts,brands,brandsProducts,shops,shopsProducts,itemid,'brands','shops');
		else if(clickedBrand == null && clickedShop != null)
			doubleSelect(itemid,clickedShop,categories,shops,categoriesProducts,shopsProducts,brands,brandsProducts,'brands');
		else if(clickedBrand != null && clickedShop == null)
			doubleSelect(itemid,clickedBrand,categories,brands,categoriesProducts,brandsProducts,shops,shopsProducts,'shops');			
	}
	
	selectText();
	
	if(clickedCategory == null && clickedBrand == null && clickedShop == null)
	    document.getElementById('show-btn').style.visibility = 'hidden';
	else
	    document.getElementById('show-btn').style.visibility = 'visible';
}

function SearchClick()
{
	var link = '';
	
	if(shopLink != null)
		link = link+ '/s-'+shopLink ;
		
	if(categoryLink != null)
		link = link + '/c-'+ categoryLink;
	
	if(brandLink != null)
		link = link + '/b-'+ brandLink;
		
	if(link != null)
		link = 'showcase' + link + '/';
	else
		link = 'showcase/';
		
	document.location.href ='/home/' + link;
}