﻿var lastClicked;

function improveCategoryNavigation ()
{
	var shopCategories = document.getElementById ("shop-categories"),
		currentCategoryElement = document.getElementById ("current-category"),
		currentElement = currentCategoryElement;
		
	/*var tmp = shopCategories.getElementsByTagName ("ul");
	
	for (var i = 0; i < tmp.length; i++)
		tmp [i].style.display = "none";*/
		
		
	if (currentCategoryElement)
	{
		while (currentElement.id != "shop-categories")
		{
			if (currentElement.tagName == "UL")
				currentElement.style.display = "block";

			currentElement = currentElement.parentNode;
		}

		if (currentCategoryElement.lastChild && currentCategoryElement.lastChild.tagName == "UL")
			currentCategoryElement.lastChild.style.display = "block";
	}
	
	// attach funky side-category navigation
	
	var categoryLinks = shopCategories.getElementsByTagName ("A");
	
	for (var i = 0; i < categoryLinks.length; i++)
	{
		categoryLinks[i].onclick = function (e)
		{
			if (!e) e = window.event;

			if (!e.target) e.target = e.srcElement;
			
			var submenuElement = e.target.parentNode.lastChild;
			
			if (submenuElement.tagName == "UL")
			{
				if (submenuElement.style.display != "block")
					submenuElement.style.display = "block";
				else
					submenuElement.style.display = "none";
					
				return false;
			}
			else
			{
				return true;
			}
		}
	}
}

function notifySuccessfulVote (responseText)
{
	alert ("Вие гласувахте успешно");
}

function sendVote (e)
{
	if (!e) e = window.event;
	
	if (!e.target) e.target = e.srcElement;
	
	initiateAjaxRequest (e.target.href + "&ajaxrequest=1", "POST", "TEXT", true, notifySuccessfulVote);
	
	return false;
}

var _currentProductId = 0;

function initializeVotingButtons (inProduct)
{
	var currentProductLinks;
	
	if (!inProduct)
	{
		var currentProductContainer = document.getElementById ("product-list").getElementsByTagName ("LI")[_currentProductId];

		currentProductLinks = currentProductContainer.getElementsByTagName ("A");
	}
	else
	{
		currentProductLinks = document.getElementById ("content").getElementsByTagName ("A");
	}

	for (var i = 0; i < currentProductLinks.length; i++)
	{
		if (currentProductLinks[i].className.indexOf ("vote-") < 0)
			continue;

		currentProductLinks[i].onclick = sendVote;
	}

	++_currentProductId;
}

window.onload = function ()
{
	var links = document.links;
	
	for (var i = 0; i < links.length; i++)
	{
		if (links[i].className.indexOf ('newWindowLink') < 0)
			continue;
			
		var f = new Function ("window.open('" + links[i].href + "'); return false;");
			
		links[i].onclick = f;
		links[i].onkeypress = f;
	}
}

var searchFunctions = {
	'4fitness': {
		'action': "/shop/search.php",
		'backgroundUrl': "/_img/bg-srch.jpg",
		'fontColor': "#e1e1e1",
		'target': "_self"
	},
	'globdisk': {
		'action': "/shop/search-globdisc.php",
		'backgroundUrl': "/_img/bg-srch-2.jpg",
		'fontColor': "#000000",
		'target': "_blank"
	}
}

function swapSearchFunction (functionId)
{
	
	var searchForm = document.forms ['searchForm'],
		formOptions = searchFunctions [functionId];
	
	searchForm.action = formOptions['action'];
	searchForm.target = formOptions['target'];
}