﻿//
// (C) Copyright 2010, 2011 gemelo network solutions GmbH, Hamburg, Germany
//
// All rights reserved! Alle Rechte vorbehalten!
//

var popupElements = new Array();
var popupMenuElements = new Array();
var emptyPopupElementIDs = new Array();
var inPopupArea = false;
var popupCurrentTimeout = null;
var popupRestrictionElement = null;

var popupNewColumnIndicator = "||";
var popupIncludeSubsIndicator = "++";

var popupLevel1Header = "<div class=\"PopupNavigationTopBorder-[col]col\"></div><div class=\"PopupNavigationArea-[col]col\"><ul class=\"PopupNavigationLevel0\">";
var popupLevel1Element = "<li><a href=\"[url]\"><span>[title]</span></a>";
var popupLevel1Separator = "<div class=\"PopupNavigationLevel0Separator\"></div></li>";
var popupLevel1SeparatorWithNewColumn = "</li></ul><ul class=\"PopupNavigationLevel0\">";
var popupLevel1Footer = "</li></ul><div style=\"clear: both\"></div></div>";

var popupLevel2Header = "<ul class=\"PopupNavigationLevel1\">";
var popupLevel2Element = "<li><a href=\"[url]\"><span>[title]</span></a></li>";
var popupLevel2Separator = "";
var popupLevel2Footer = "</ul>";

var popupMaxColumns = 4;
var popupColumnWidth = 200;
var popupRightBorderWidth = 10;
var popupTotalBorderWidth = 14;
var popupLeftPositionCorrection = 3;
var popupTopPositionCorrection = -4;
var popupHidingTimeout = 800;

var popupIE6RightBoundCorrection = -4;

function popup (url) {
	fenster = window.open(url, '', 'width=760,height=540,resizable=yes');
	fenster.focus();
	return false;
}

function PopupEntry(title, description, url, subEntries) {
    var popupEntry = new Object();
    popupEntry.title = title;
    popupEntry.description = description;    
    if (url.substring(0, 1) == "~") popupEntry.url = GetUrlFromLinkElement(url.substring(1));
    else popupEntry.url = url;
    popupEntry.subEntries = subEntries;
    popupEntry.newColumn = function () { return description.indexOf(popupNewColumnIndicator) >= 0; };
    popupEntry.includeSubs = function () {
        return description.indexOf(popupIncludeSubsIndicator) >= 0;
    };
    return popupEntry;
}

function GetUrlFromLinkElement(elementId) {
    var linkElement = document.getElementById(elementId);
    if (linkElement && linkElement.href) return linkElement.href;
    else return "";
}

function RestrictPopupBounds(elementId) {
    popupRestrictionElement = $("#" + elementId);
}

function CreatePopupMenu(popupElementId, menuElementId, entries) {
    var menuElement = $("#" + menuElementId);
    var popupElement = $("#" + popupElementId);
    if (entries.length > 0) {
        var columnCount = GetNumberOfColumns(entries);
        var width = CalculateTotalWidth(columnCount);
        popupElement.width(width);
        CreateSubElements(popupElement, entries, columnCount);
    }
    else AddToEmptyPopupElementIDs(popupElementId);
    AddToPopupElements(popupElement, menuElement);
    AddEventsToMenuAndPopupElement(menuElement, popupElement);
}

function ShowPopup(popupElementId, menuElementId) {
    inPopupArea = true;
    var current = $("#" + popupElementId);
    var menuElement = $("#" + menuElementId);
    HideAllPopupsExceptFor(current);
    menuElement.css("background-position", "center bottom");
    menuElement.css("color", "#003366");
    var empty = false;
    for (var emptyElementId in emptyPopupElementIDs) {
        if (emptyElementId == popupElementId) {
            empty = true;
            break;
        }
    }
    if (!empty) {
        SetPopupPosition(menuElement, current);
        if ($.browser.msie) current.show();
        else current.fadeIn("fast");
    }
}

function StartPopupHiding() {
    inPopupArea = false;
    if (popupCurrentTimeout) window.clearTimeout(popupCurrentTimeout);
    popupCurrentTimeout = window.setTimeout("TryPopupHiding()", popupHidingTimeout);
}

function TryPopupHiding() {
    popupCurrentTimeout = null;
    if (!inPopupArea) HideAllPopupsExceptFor(null);
}

function GetNumberOfColumns(entries) {
    var result = 1;
    var first = true;
    for (var i in entries) {
        if (!first && entries[i].newColumn()) result++;
        else first = false;
    }
    return Math.min(result, popupMaxColumns);
}

function CalculateTotalWidth(columnCount) {
    return columnCount * popupColumnWidth + popupTotalBorderWidth;
}

function HideAllPopupsExceptFor(current) {
    for (var i in popupElements) {
        var popupElement = popupElements[i];
        if (!current || popupElement.attr("id") != current.attr("id")) {
            if ($.browser.msie) popupElement.hide();
            else popupElement.fadeOut("fast");
        }
    }
    for (var i in popupMenuElements) {
        popupMenuElements[i].css("background-position", "left center");
        popupMenuElements[i].css("color", "#227ebd");
    }
}

function SetPopupPosition(menuElement, popupElement) {
    var menuOffset = menuElement.offset();
    var left = menuOffset.left + menuElement.width() / 2 -
                popupElement.width() / 2 + popupLeftPositionCorrection;
    left = CalculateRestrictedBounds(left, popupElement.width());
    var top = menuOffset.top + menuElement.height() + popupTopPositionCorrection;
    popupElement.css("left", left.toString() + "px");
    popupElement.css("top", top.toString() + "px");
}

function CalculateRestrictedBounds(left, width) {
    if (popupRestrictionElement) {
        var right = left + width;
        var boundsLeft = popupRestrictionElement.offset().left;
        var boundsRight = boundsLeft + popupRestrictionElement.width() +
                    popupRightBorderWidth;
        // IE6-Korrektur
        try {
            if (jQuery.browser.msie) {
                var version = parseInt(jQuery.browser.version);
                if (version && version <= 6) {
                    boundsRight = boundsRight + popupIE6RightBoundCorrection;
                }
            }
        }
        catch (ex) {
        }
        if (left < boundsLeft) return boundsLeft;
        if (right > boundsRight) return boundsRight - width;
    }
    return left;
}

function CreateSubElements(rootElement, entries, columnCount) {
    var html = popupLevel1Header;
    html = html.replace(/\[col\]/gi, columnCount.toString());
    var first = true;
    for (var i in entries) {
        var currentEntry = entries[i];
        if (!first) {
            if (currentEntry.newColumn()) html = html + popupLevel1SeparatorWithNewColumn;
            else html = html + popupLevel1Separator;
        }
        else first = false;
        var entryHtml = popupLevel1Element;
        entryHtml = entryHtml.replace("[url]", currentEntry.url);
        entryHtml = entryHtml.replace("[title]", currentEntry.title);
        html = html + entryHtml;
        if (currentEntry.includeSubs()) html = html + CreateLevel2SubEntries(currentEntry.subEntries);
    }
    html = html + popupLevel1Footer;
    $(html).appendTo(rootElement);
}

function CreateLevel2SubEntries(subEntries) {
    var result = popupLevel2Header;
    var first = true;
    for (var i in subEntries) {
        var currentEntry = subEntries[i];
        if (!first) result = result + popupLevel2Separator;
        else first = false;
        var entryHtml = popupLevel2Element;
        entryHtml = entryHtml.replace("[url]", currentEntry.url);
        entryHtml = entryHtml.replace("[title]", currentEntry.title);
        result = result + entryHtml;
    }
    result = result + popupLevel2Footer;
    return result;
}

function AddToPopupElements(popupElement, menuElement) {
    popupElements[popupElements.length] = popupElement;
    popupMenuElements[popupMenuElements.length] = menuElement;
}

function AddToEmptyPopupElementIDs(popupElement) {
    emptyPopupElementIDs[emptyPopupElementIDs.length] = popupElement;
}

function AddEventsToMenuAndPopupElement(menuElement, popupElement) {
    menuElement.bind("mouseenter", function () {
        ShowPopup(popupElement.attr("id"), menuElement.attr("id"));
    });
    menuElement.bind("mouseleave", function () {
        StartPopupHiding();
    });
    popupElement.bind("mouseenter", function () {
        inPopupArea = true;
    });
    popupElement.bind("mouseleave", function () {
        StartPopupHiding();
    });
}

/* Installateur-Suche */

var installerSearchSource = "http://beta8.conergy.com/modules/dealerlocator_de/default.aspx";
var installerSearchPage = "installateursuche-result.htm?Postcode=";

function installerSearchChangeSrc(source) {
    var installerSearchFrame = document.getElementById("InstallerSearchFrame");
    if (installerSearchFrame) installerSearchFrame.src = source;
}

function installerSearchPrepareFrame() {
    var href = window.location.href;
    var index = href.indexOf("?");
    if (index && index > 0) {
        installerSearchChangeSrc(installerSearchSource + href.substr(index));
    }
    else installerSearchChangeSrc(installerSearchSource);
}

function trim (str) {
  return str.replace (/^\s+/, '').replace (/\s+$/, '');
}

function installerSearchStart() {
    var installerSearchEdit = document.getElementById("InstallerSearchEdit");
    var installerSearchError = $("#InstallerSearchError");
    if (installerSearchEdit && installerSearchError) {
        var zipCode = installerSearchEdit.value;
        if (zipCode) {
            zipCode = trim(zipCode.toString());
            var zipCodeAsInt = parseInt(zipCode);
            if (zipCode.length == 5 && zipCodeAsInt) {
                var newLocation = installerSearchPage + zipCode.toString();
                window.location = newLocation;
            }
            else installerSearchError.show();
        }
        else installerSearchError.show();
    }
}
