
/* unobtrusive js for links */
var linkcount = 0;

function initKSB() {
    // check to see if browser supports the getElementsByTagName method. if not exit the loop
    if (!document.getElementsByTagName) {
        return false;
    }
    // create an array of objects of each link in the document
    var links = document.getElementsByTagName("a");
    // loop trough each of these links
    for (var i=0;i < links.length;i++) {
        // loop each link named preventClicks and give them an generic number
        // see more for class function preventMoreClicks
        if(links[i].parentNode.id == "preventClicks") {
            links[i].parentNode.id = links[i].parentNode.id + linkcount;
            linkcount++;
        }
        // if the link has a class popup
        // entfernt da diese aufgabe nun popup.js und modalWindow.js machen
        if (links[i].className.indexOf("popup") != -1) {
            // add an onclick event on the fly to pass the href attribute of the link to open a default Popup
            links[i].onclick = function() {
                openPopup(this.getAttribute("href"));
                return false;
            }
        }
        // if the link has a class popup
        if (links[i].className.indexOf("popupBig") != -1) {
            links[i].onclick = function() {
                openPopupBig(this.getAttribute("href"));
                return false;
            }
        }
        // if the link has a class popup
        if (links[i].className.indexOf("popupLP") != -1) {
            links[i].onclick = function() {
                openPopupLP(this.getAttribute("href"));
                return false;
            }
        }
        // if the link has a class external-popup
        if (links[i].className.indexOf("external-link") != -1) {
            // add an onclick event on the fly to pass the href attribute
            // open link in external window or tab
			/*
            @depracated
            var ziel=links[i].href;
			links[i].setAttribute('href', '#');
            links[i].onclick = function() {
                window.open(ziel,"fenster");
                return false; 
            }*/
            links[i].onclick = function() {
                window.open(this.getAttribute("href"));
                return false;
            }
        }
        // if the link has a class preventMoreClicks
        if (links[i].className.indexOf("preventMoreClicks") != -1) {
            // add an onclick event on the fly to pass klick action
            // prevent user by clicking same link several times
            links[i].onclick = function() {
                // open function with 2 parameters
                preventClicks(this.parentNode.id,this.firstChild.data);
                // return true for linkaction but remove href for prevent clicking twice
                return true;
            }
        }
    }
    // end of link function

	// tableRow colorRuler
	if (document.getElementById && document.createTextNode) {
		var tables=document.getElementsByTagName('table');
		for (i=0;i<tables.length;i++) {
			if (tables[i].className=='tableView') {
				var tablerows=tables[i].getElementsByTagName('tr');
				for (j=0;j<tablerows.length;j++) {
					if((tablerows[j].className=='topDomain') && (i%2==0)) {
						tablerows[j].onmouseover=function() {this.className='topDomain_ruled';return false}
						tablerows[j].onmouseout=function() {this.className='topDomain';return false}
					}
					if((tablerows[j].className=='topDomain') && (i%2==1)) {
						tablerows[j].className='topDomain2';
						tablerows[j].onmouseover=function() {this.className='topDomain_ruled';return false}
						tablerows[j].onmouseout=function() {this.className='topDomain2';return false}
					}
					if((tablerows[j].className=='subDomain') && (j%2==0)) {
						tablerows[j].onmouseover=function() {this.className='subDomain_ruled';return false}
						tablerows[j].onmouseout=function() {this.className='subDomain';return false}
					}
					if((tablerows[j].className=='subDomain') && (j%2==1)) {
						tablerows[j].className='subDomain2';
						tablerows[j].onmouseover=function() {this.className='subDomain_ruled';return false}
						tablerows[j].onmouseout=function() {this.className='subDomain2';return false}
					}
				}
			}
		}
	}
	// end of tableRow colorRuler

    var resizeAndButton = (function () {
        var initKsbParam = pushVarsFrom('initKSB.js'),
        divHeight = 0;

        function getDivHeight() {
            return divHeight;
        }

        function setDivHeight(value) {
            divHeight = value;
        }

        function getInitKsbParam() {
            return initKsbParam;
        }

        return {
            getHighestElement: function (elem) {
                var divHeight = getDivHeight();

                if (elem.offsetHeight > divHeight) {
                    setDivHeight(elem.offsetHeight);
                }
            },

            init: function (elem) {
                var divHeight = getDivHeight(),
                    initKsbParam = getInitKsbParam(),
                    moreButtonHeight = elem.style.lineHeight * 2,
                    moreButtonName,
                    elemSum = divHeight + moreButtonHeight;                    
                    
                if (initKsbParam['initKSB.js']['cc']) {
                    elem.style.height = ((elemSum / 10) + "em");

                    if (elem.className !== 'featureBox-textlink isUpgrade'){
                        switch (initKsbParam['initKSB.js']['cc']) {
                        case "de": moreButtonName = "Weiter&nbsp;&raquo;";
                            break;
                        case "nl": moreButtonName = "Meer&nbsp;&raquo;";
                            break;
                        case "es": moreButtonName = "M&aacute;s&nbsp;&raquo;";
                            break;
                        case "gb": moreButtonName = "More&nbsp;&raquo;";
                            break;
                        case "it": moreButtonName = "pi&ugrave;&nbsp;&raquo;";
                            break;
                        case "fr": moreButtonName = "Plus&nbsp;&raquo;";
                            break;
                        default: moreButtonName   = "not defined";
                        }
                    }
                    if (elem.className === 'featureBox-textlink isUpgrade') {
                        switch (initKsbParam['initKSB.js']['cc']) {
                        case "de": moreButtonName = "Upgrade&nbsp;&raquo;";
                            break;
                        case "nl": moreButtonName = "Pakketupgrade&nbsp;&raquo;";
                            break;
                        case "es": moreButtonName = "Upgrade&nbsp;&raquo;";
                            break;
                        case "gb": moreButtonName = "Upgrade&nbsp;&raquo;";
                            break;
                        case "it": moreButtonName = "Upgrade applicazione&nbsp;&raquo;";
                            break;
                        case "fr": moreButtonName = "Modification de votre pack&nbsp;&raquo;";
                            break;
                        default: moreButtonName   = "not defined";
                        }
                    }
                    elem.innerHTML += "<span class=\"featureBox-textlink-more\">" + moreButtonName + "</span>";
                }
            }
        };
    }());

    var arrOfDynBoxes = getElementsBy(['featureBox-textlink', 'featureBoxLong-textlink'], 'className', 'a', resizeAndButton.getHighestElement);
    for (var i = 0; i < arrOfDynBoxes.searchReturn.length; i += 1) {        
        resizeAndButton.init(arrOfDynBoxes.searchReturn[i]);
    }

   	// resize box headlines
    var resizeHeadline = (function () {
        divHeight = 0;
        function getDivHeight() {
            return divHeight;
        }

        function setDivHeight(value) {
            divHeight = value;
        }

        return {
            getHighestElement: function (elem) {
                var divHeight = getDivHeight();
                if (elem.childNodes[0].offsetHeight > divHeight) {
                    setDivHeight(elem.childNodes[0].offsetHeight);
                }
            },

            init: function (elem) {
                var divHeight = getDivHeight(),
				elemSum = divHeight;
				if (elemSum > 22) {
					elem.style.height = ((elemSum / 12.6) + "em");
					elem.childNodes[0].style.height = ((elemSum / 10.8) + "em");
				}
            }
        };
    }());

    var arrOfDynBoxHeadlines = getElementsBy('headline', 'className', 'h3', resizeHeadline.getHighestElement);
    for (var i = 0; i < arrOfDynBoxHeadlines.searchReturn.length; i += 1) {        
        resizeHeadline.init(arrOfDynBoxHeadlines.searchReturn[i]);
    }
    // end of resize box headlines
    
    if(typeof getElementsBy === 'function'){
        dropdown.setClickCancel();
        dropdown.isLayer = true;

        // attach events for layer
        var aLayer = getElementsBy('a-Layer-.*','id', 'a', dropdown.attachEvents);
        dropdown.isLayer = false;
    	dropdown.configImg('/assets/ksb/strato/shared/images/dropdown/', 'bg_button_packageSelect.png', 'bg_button_packageSelect_hover.png');

    	// attach events for dropdown
    	var aDropdown = getElementsBy('a-Dropdown-.*','id', 'a', dropdown.attachEvents);
    	var divDropdown = getElementsBy('div-Dropdown-.*','id', 'div',dropdown.attachEvents);
    	var imgDropdown = getElementsBy('img-Dropdown.*','id', 'img', dropdown.attachEvents);
    }   
    
    /* show hide debug information */
    if ($('#debug')) {
        var debug_show = 'show debug';
        var debug_hide = 'hide debug';
        var flip = 0;
        $('#debug').hide();
        $("div#debug").before('<br><div class="show_hide_debug">' + debug_show + '</div>');
        $('.show_hide_debug').click(function(e) {
            $('#debug').toggle(flip++ %2 == 0);
            if (flip %2 == 1) {
                $('.show_hide_debug').text(debug_hide);
            } else {
                $('.show_hide_debug').text(debug_show);
            }
        });
    }
    
// eof initKSB();
}