﻿function popup(url, bredde, hojde) 
{
    var window_width = bredde;
    var window_height = hojde;
    var window_top = (screen.height-window_height)/2;
    var window_left = (screen.width-window_width)/2;
    
    var win = window.open(url,null,'width=' + window_width + ',height=' + window_height + ',top=' + window_top + ',left=' + window_left);
    win.focus();
}
    

function visStortBillede(vareNr, sprog, hovedKategori) {
    if (hovedKategori == '3') {
        popup("/ProduktPopup.aspx?sprog="+sprog+"&n="+encodeURIComponent(vareNr), 440, 600);
    } else {
        popup("/ProduktPopup.aspx?sprog="+sprog+"&n="+encodeURIComponent(vareNr), 760, 600);
    }
}

function visProdukt(vareNr, underKategori, sprog) {
    window.location.href = "?k=" + encodeURIComponent(underKategori) + "&n=" + encodeURIComponent(vareNr);
}




/// <summary>
/// NiceForms automatically finds all <select> elements to find
/// whether each has the attribute "enableStyling" set to "true".
/// In that case the <select> element is styled.
/// </summary>
var NiceForms = {
    init : function() {
        NiceForms.replaceSelects();
    },
    
    replaceSelects : function() {
	    // get all the select fields on the page
        var selects = document.getElementsByTagName('select');
    	
	    // cycle trough the select fields
        for (var i = 0; i < selects.length; i++) {
    		var select = selects[i];
    		
    		if (select.getAttribute("enableStyling") != "true") {
    		    continue;
    		}
    		
    		
    		var id = "";
    		if (select.id.length > 0) {
                id = select.id;
    		} else if (select.name.length > 0) {
    		    id = select.name;
    		} else {
    		    id = "nice-select-" + i;
    		}
    		
    		select.id = id;
    		
		    //create and build div structure
		    var selectBox = document.createElement("div");
		    selectBox.className = "select-container";
		    selectBox.id = "select-for-" + id;
		    
		    var selectedBox = document.createElement("div");
		    selectedBox.className = "selected";
		    
		    var optionsBox = document.createElement("div");
		    optionsBox.className = "options";
		    optionsBox.style.display = "none";
		    
		    
		    var clearBox = document.createElement("div");
		    clearBox.style.clear = "both";
		    
		    var optionsList = document.createElement("ul");
		    
		    selectBox.appendChild(selectedBox);
		    selectBox.appendChild(optionsBox);
		    selectBox.appendChild(clearBox);
		    
		    optionsBox.style.position = "absolute";
		    optionsBox.selectObj = select;
            optionsBox.style.left = NiceForms._findPosX(select) + 'px';
		    optionsBox.style.top = NiceForms._findPosY(select) + 17 + 'px';

		    optionsBox.appendChild(optionsList);
		    
		    var selectedText = document.createElement("a");
		    selectedText.className = "selected-text";
		    selectedText.href = "#l";
		    NiceForms._addEvent(selectedText, "click", NiceForms._onSelectedTextClick);
		    
		    selectedText.appendChild(document.createTextNode(NiceForms._getSelectedText(select)));
		    selectedBox.appendChild(selectedText);
		    
		    //hide the select field
            select.style.display = "none"; 
    		
		    //insert select div
		    select.parentNode.insertBefore(selectBox, select);
		        		
		    //get select's options and add to options div
		    for (var j = 0; j < select.options.length; j++) {
		        var option = select.options[j];
		        
		        var optionLinkId = id + "-option-" + j;
		        
		        var optionLink = document.createElement("a");
		        optionLink.href = "#";
		        //optionLink.addEvent("click", NiceForms._onListItemClick);
		        NiceForms._addEvent(optionLink, "click", NiceForms._onListItemClick);
		        optionLink.appendChild(document.createTextNode(option.text));
		        optionLink.optionObj = option;
		        optionLink.id = optionLinkId;
		        
		        var optionListItem = document.createElement("li");
		        optionListItem.appendChild(optionLink);
		        
		        if (option.selected) {
		            optionListItem.className = "selected";
		        }
		        
			    optionsList.appendChild(optionListItem);
		    }
	    }
    },
    
    _addEvent : function(obj, type, callbackFunction) {
        if (obj.addEvent) {
            obj.addEvent(type, callbackFunction);
        } else if (obj.attachEvent) {
            obj.attachEvent("on" + type, callbackFunction);
        } else {
            alert("NiceForms._addEvent failed.");
        }
    },

    _getSelectedText : function(select) {
        
        return select.options[select.selectedIndex].text;
    },
    
    _findPosY : function(obj) {
	    var posTop = 0;
	    while (obj.offsetParent) {
		    posTop += obj.offsetTop;
		    obj = obj.offsetParent;
	    }
	    return posTop;
    },
    
    _findPosX : function(obj) {
	    var posLeft = 0;
	    while (obj.offsetParent) {
		    posLeft += obj.offsetLeft;
		    obj = obj.offsetParent;
	    }
	    return posLeft;
    },
    
    _onSelectedTextClick : function(e) {
        var link = e.target || e.srcElement;
        var selectBox = link.parentNode.parentNode;
        
        NiceForms._toggleOptions(selectBox);
        
        if (e.preventDefault) e.preventDefault();
        e.returnValue = false;
    },
    
    _onShowOptionsButtonClick : function(e) {
        NiceForms._onSelectedTextClick(e);
    },
    
    _onListItemClick : function(e) {
        var link = e.target || e.srcElement;
        var option = link.optionObj;
        var li = link.parentNode;
        var ul = li.parentNode;
        var optionsBox = ul.parentNode;
        var select = option.parentNode;
        var selectContainer = optionsBox.parentNode;
        var selectedTextLink = selectContainer.childNodes[0].childNodes[0];
        
        for (var i = 0; i < ul.childNodes.length; i++) {
            ul.childNodes[i].className = "";
        }
        
        li.className = "selected";
        optionsBox.style.display = "none";
        select.selectedIndex = option.index;
        if (select.onchange) {
            select.onchange(e);
        }
        selectedTextLink.innerHTML = option.text;
        
        if (e.preventDefault) e.preventDefault();
        e.returnValue = false;
    },
    
    _toggleOptions : function(selectBox) {
        var optionsBox = selectBox.childNodes[1];
        if (optionsBox.style.display == "none") {
            optionsBox.style.display = "block";
            optionsBox.style.left = NiceForms._findPosX(optionsBox.parentNode) + 'px';
		    optionsBox.style.top = NiceForms._findPosY(optionsBox.parentNode) + 17 + 'px';
        } else {
            optionsBox.style.display = "none";
        }
    },
    
    clickEventHandler : function(e) {
        var evt = new Event(e);
        var target = evt.target;
        var selectContainer = NiceForms._findSelectContainer(target);
        
        var selects = document.getElementsByTagName('select');
        for (var i = 0; i < selects.length; i++) {
            var div = $("select-for-" + selects[i].id);
            if (div != null && selectContainer != div) {
                div.childNodes[1].style.display = "none";
            }
        }
    },
    
    _findSelectContainer : function(child) {
        if (child != null) {
            if (child.className == "select-container") {
                return child;
            } else {
                return NiceForms._findSelectContainer(child.parentNode);
            }
        }
        
        return null;
    }
}



window.addEvent("domready", NiceForms.init);
document.addEvent("click", NiceForms.clickEventHandler);