var previousSubMenu;

function showPossibleSubMenu(e) {
	return true; //stop the script
	
	if(!e) {
		//to stay friends with IE
		var e = window.event;
	}

	var node = e.target;

	if(node == undefined) {
		node = window.event ? window.event.srcElement : e ? e.target : null;
	}
	
	while(node.nodeType != node.ELEMENT_NODE) {
		node = node.parentNode;
	}

	if(node.parentNode.nextSibling.nextSibling != null && node.parentNode.nextSibling.nextSibling.tagName == "UL") {
		if(previousSubMenu != null) {
			previousSubMenu.style.display = "none";
		}

		previousSubMenu = node.parentNode.nextSibling.nextSibling;
		previousSubMenu.style.display = "block";
	}

	return true;
}

    
var mainMenuWithSubmenu;
var subEnabledSubmenu;

function enableMainSubmenu(sender, id) {
	var submenu = document.getElementById(id);
	
	if(submenu.style.display != "block") {
		var senderCoor = findPos(sender);
		var xPos = yPos = 0;
		
		xPos = senderCoor[0];
		yPos = senderCoor[1];
	
		submenu.style.display = "block";
	    
		xPos = xPos + sender.offsetWidth - submenu.offsetWidth;
		yPos = yPos + sender.offsetHeight;
	
	    submenu.style.position = "fixed";
		if(document.all) {
		    submenu.style.position = "absolute";
		}
		submenu.style.top = yPos + 2 + "px";
		submenu.style.left = xPos + 11 + "px";
	
		opacity(id, 0, 70, 350);
		
		focusLastTextbox(submenu);
		
		subEnabledSubmenu = submenu;
		mainMenuWithSubmenu = sender;
		
		if (window.captureEvents) {
		    window.captureEvents(Event.CLICK);
		    window.onclick = disableMainSubmenu;
        }
        else {
            document.onclick = disableMainSubmenu;
        }
	}
}

function disableMainSubmenu(e) {
	if (!e) var e = window.event;
	var tg = (window.event) ? e.srcElement : e.target;
	
	if(tg != mainMenuWithSubmenu) {
		if(tg != subEnabledSubmenu) {	
			if(!isChildClicked(subEnabledSubmenu, tg)) {
				subEnabledSubmenu.style.display = "none";
				
				subEnabledSubmenu = null;
				mainMenuWithSubmenu = null;
				
				if (window.releaseEvents) {
				    window.releaseEvents(Event.CLICK);
                }
                else {
                    document.onclick = null;
                }
			}
		}
	}
	
	return true;
}

function isChildClicked(parent, target) {
    var blnClicked = false;
    
    if(parent.hasChildNodes()) {
        for(var i = 0; i<parent.childNodes.length; i++) {
            if(isChildClicked(parent.childNodes[i], target))
            {
                blnClicked = true;
            }
                
            if(parent.childNodes[i] == target) {
                blnClicked = true;
            }
        }
    }

    return blnClicked;
}

function focusLastTextbox(parent) {
    if(parent.hasChildNodes()) {
        for(var i = 0; i<parent.childNodes.length; i++) {    
            if(parent.childNodes[i].nodeName == "INPUT" && parent.childNodes[i].style.display != "none") {
                parent.childNodes[i].focus();
            }
            
            focusLastTextbox(parent.childNodes[i]);
        }
    }
}

function findPos(obj) {
	var coorContainer;
				
	if(obj.id != 'container') {
		coorContainer = findPos(document.getElementById('container'));
	}
	
	var curleft = curtop = 0;
	
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
			
		if((document.all || navigator.userAgent.indexOf("Safari")!=-1) && coorContainer != undefined) {
			curleft -= coorContainer[0];
		}
	}

	return [curleft,curtop];
}

var prodCurrentFocus = null;
var prodCurrentBasketIcon = null;

function showIconToBasket(prodCont) {
	if(prodCont != prodCurrentFocus) {
		var childs = prodCont.childNodes;

		for(var i = 0; i<childs.length; i++) {
			if(childs[i].className == "producttobasket") {
				prodCurrentBasketIcon = childs[i];

				//setTimeout("showIcon()", 1000);
				
				//showIcon();
			}
			else {
				showIconToBasket(childs[i]);
			}
		}
	}

	prodCurrentFocus = prodCont;
}

function showIcon() {
	if(prodCurrentBasketIcon != null) {
		prodCurrentBasketIcon.style.display = "block";
	}
}

function hideIconToBasket(prodCont) {
	//find the fucking icon and hide it.

	prodCurrentFocus = null;

	if(prodCurrentBasketIcon != null) {
		prodCurrentBasketIcon.style.display = "none";
	}

	prodCurrentBasketIcon = null;
}

function showAddressPanel()
{
    document.getElementById("applicationDisabler").style.display = "block";    
    document.getElementById("addressPanel").style.display = "block";    
}

function hideAddressPanel()
{
    opacity("applicationDisabler", 80, 0, 150);
    setTimeout("document.getElementById(\"addressPanel\").style.display = \"none\";document.getElementById(\"applicationDisabler\").style.display = \"none\";",250);
    
}
function hideProductImage(productID)
{
    opacity("applicationDisabler", 80, 0, 150);
    setTimeout("document.getElementById(\"productImage" + productID + "\").style.display = \"none\";document.getElementById(\"applicationDisabler\").style.display = \"none\";",250);
    
}

function showProductImage(imageID)
{
    opacity("applicationDisabler", 0, 80, 150);
    document.getElementById("applicationDisabler").style.display = "block";    
    document.getElementById("productImage" + imageID).style.display = "block";
}

function showLogin() 
{
    //Check if external frame is loaded, when external frame loaded 
    //show message that design will be lost after loggin in...
    if(document.getElementById("ctl00_pageContentPlaceHolder_frmExternalPage")=="[object]" || document.getElementById("ctl00_pageContentPlaceHolder_frmExternalPage")=="[object HTMLIFrameElement]")
    {   
        document.getElementById("ExternalFrameLoginMessage").style.display = "block";
    }
    document.getElementById("loggin").style.display = "block";
    focusLoginTextbox(document.getElementById("loggin"));
}
function hideLogin() {
    document.getElementById("loggin").style.display = "none";
}


function focusLoginTextbox(parent) {
    if(parent.hasChildNodes()) {
        for(var i = 0; i<parent.childNodes.length; i++) {    
            //alert("" + parent.childNodes[i].nodeName);
            if(parent.childNodes[i].nodeName == 'INPUT' && parent.childNodes[i].id.indexOf('txtLoginUser') > 0) {
                parent.childNodes[i].focus();
            }
            focusLoginTextbox(parent.childNodes[i]);
        }
    }
}
function showCategory(targetId)	{
	if (document.getElementById) {
		target = document.getElementById(targetId);
		if (target.style.display == "none") {
			  target.style.display = "";
		}
		else {
			target.style.display = "none";
		}
	}
}



function showFullProductImage(imagePath) {
    alert('showIt');
}

function autoCropTextElements(strParentID, strTagName, intMaxHeight) {
    var links = document.getElementById(strParentID).getElementsByTagName(strTagName);
    
    for(var i = 0; i<links.length; i++) {
        var current = links[i];
        
        while(current.offsetHeight > intMaxHeight){
            var newString = current.innerHTML;
            newString = newString.substring(0, newString.length - 4) + "...";
            current.innerHTML = newString;
        }
    }
}
