﻿function CreatePP(settings) {
    if (!settings.parent || !settings.child)
        return false;

    var newPPSettings = new Object();
    newPPSettings.parent = settings.parent;
    newPPSettings.child = settings.child;
    newPPSettings.disappearDelay = settings.disappearDelay;
    newPPSettings.disappearInterval = settings.disappearInterval;
    newPPSettings.appearInterval = settings.appearInterval;
    //set default intervals
    if (!newPPSettings.disappearInterval || newPPSettings.disappearInterval == null)
        newPPSettings.disappearInterval = 400;
    if (!newPPSettings.appearInterval || newPPSettings.appearInterval == null)
        newPPSettings.appearInterval = 400;
    if (!newPPSettings.disappearDelay || newPPSettings.disappearDelay == null)
        newPPSettings.disappearDelay = 600;
    //************
    newPPSettings.showMethod = settings.showMethod;
    newPPSettings.hideMethod = settings.hideMethod;

    newPPSettings.allowHorizontalOffset = settings.allowHorizontalOffset;
    newPPSettings.allowVerticalOffset = settings.allowVerticalOffset;
    newPPSettings.alignHorizontal = settings.alignHorizontal;
    newPPSettings.alignVertical = settings.alignVertical;
    newPPSettings.alignedHorizontalBorder = settings.alignedHorizontalBorder;
    newPPSettings.alignedVerticalBorder = settings.alignedVerticalBorder;
    newPPSettings.offsetVertical = settings.offsetVertical;
    newPPSettings.offsetHorizontal = settings.offsetHorizontal;
    newPPSettings.slideDirection = settings.slideDirection;
    newPPSettings.cornerRound = settings.cornerRound;
    if (!newPPSettings.cornerRound || newPPSettings.cornerRound == null)
        newPPSettings.cornerRound = 0;
    newPPSettings.preShow = settings.preShow;
    newPPSettings.afterShow = settings.afterShow;
    newPPSettings.preHide = settings.preHide;
    newPPSettings.afterHide = settings.afterHide;
    newPPSettings.zIndex = settings.zIndex;

    settings.parent.ppParentData = newPPSettings;
    settings.child.ppChildData = newPPSettings;

    $(settings.parent).hover(ShowChildPP, HideChildPP);
    $(settings.child).hover(ShowThisPP, HideThisPP);

    if (newPPSettings.cornerRound && newPPSettings.cornerRound > 0) {
        var cornerSettings = {
            tl: { radius: newPPSettings.cornerRound },
            tr: { radius: newPPSettings.cornerRound },
            bl: { radius: newPPSettings.cornerRound },
            br: { radius: newPPSettings.cornerRound },
            antiAlias: true,
            autoPad: true
        }

        if ($.browser.msie)//Bug exists in IE with curvycorners
            settings.child.style.border = 0;
        $(settings.child).corner(cornerSettings);
    }

    //set coordinates
    $().ready(function() {
        newPPSettings.parent.coordinates = new Object();
        newPPSettings.parent.coordinates.width = $(newPPSettings.parent).outerWidth();
        newPPSettings.parent.coordinates.height = $(newPPSettings.parent).outerHeight();

        newPPSettings.child.style.display = 'inline';
        newPPSettings.child.style.visibility = 'visible';
        newPPSettings.child.coordinates = new Object();
        newPPSettings.child.coordinates.width = $(newPPSettings.child).outerWidth();
        newPPSettings.child.coordinates.height = $(newPPSettings.child).outerHeight();
        newPPSettings.child.coordinates.offsetParent = newPPSettings.child.offsetParent;
        newPPSettings.child.style.visibility = 'hidden';
        newPPSettings.child.style.display = 'none';

        var xCorrection = 0;
        var yCorrection = 0;
        if (newPPSettings.alignHorizontal) {
            if (newPPSettings.alignHorizontal == 'right')
                xCorrection = xCorrection + newPPSettings.parent.coordinates.width;
            else if (newPPSettings.alignHorizontal == 'center')
                xCorrection = xCorrection + newPPSettings.parent.coordinates.width / 2;
        }
        if (newPPSettings.alignVertical) {
            if (newPPSettings.alignVertical == 'bottom')
                yCorrection = yCorrection + newPPSettings.parent.coordinates.height;
            else if (newPPSettings.alignVertical == 'center')
                yCorrection = yCorrection + newPPSettings.parent.coordinates.height / 2;
        }

        if (newPPSettings.alignedHorizontalBorder) {
            if (newPPSettings.alignedHorizontalBorder == 'right')
                xCorrection = xCorrection - newPPSettings.child.coordinates.width;
            else if (newPPSettings.alignedHorizontalBorder == 'center')
                xCorrection = xCorrection - newPPSettings.child.coordinates.width / 2;
        }
        if (newPPSettings.alignedVerticalBorder) {
            if (newPPSettings.alignedVerticalBorder == 'bottom')
                yCorrection = yCorrection - newPPSettings.child.coordinates.height;
            else if (newPPSettings.alignedVerticalBorder == 'center')
                yCorrection = yCorrection - newPPSettings.child.coordinates.height / 2;
        }

        if (newPPSettings.offsetHorizontal)
            xCorrection = xCorrection + newPPSettings.offsetHorizontal;
        if (newPPSettings.offsetVertical)
            yCorrection = yCorrection + newPPSettings.offsetVertical;

        if (newPPSettings.cornerRound && newPPSettings.cornerRound > 0) {//this code should to bee executed before checking by windowVerticalOffset
            if (!newPPSettings.alignedVerticalBorder || newPPSettings.alignedVerticalBorder == 'top')
                y = y + newPPSettings.cornerRound;
            else if (newPPSettings.alignedVerticalBorder == 'bottom')
                yCorrection = yCorrection - newPPSettings.cornerRound;
        }

        newPPSettings.child.coordinates.xCorrection = xCorrection;
        newPPSettings.child.coordinates.yCorrection = yCorrection;
    });
    //***********
}

function ShowChildPP(event) {
    var ppContainer = this.ppParentData.child;
    ShowPP(ppContainer);
}

function ShowThisPP(event) {
    var ppContainer = this;
    ShowPP(ppContainer);
}

function ShowPP(ppContainer) {
    if (!ppContainer)
        return;

    window.clearInterval(ppContainer.ppChildData.disappearTimeoutId);
    if ($(ppContainer).css('visibility') == 'visible' && $(ppContainer).css('display') != 'none')
        return;
    DoShowPP(ppContainer);
}

function DoShowPP(ppContainer) {
    ppContainer.style.position = 'absolute';
    var ppContainerData = ppContainer.ppChildData;
    SetPPCoordinates(ppContainer);
    ppContainer.style.zIndex = ppContainerData.zIndex;
    ppContainer.style.visibility = 'visible';
    switch (ppContainerData.showMethod) {
        case 'show':
            $(ppContainer).show(ppContainerData.appearInterval);
            break;
        case 'slideDown':
            $(ppContainer).slideDown(ppContainerData.appearInterval);
            break;
        case 'fadeIn':
            $(ppContainer).fadeIn(ppContainerData.appearInterval);
            break;
        default:
            $(ppContainer).fadeIn(ppContainerData.appearInterval);
            break;
    }
}

function SetPPCoordinates(ppContainer) {
    var ppContainerData = ppContainer.ppChildData;
    var parentElement = ppContainerData.parent;
    var parentElementOffset = $(parentElement).offset();
    var x = parentElementOffset.left + ppContainer.coordinates.xCorrection;
    var y = parentElementOffset.top + ppContainer.coordinates.yCorrection;

    if (ppContainerData.allowHorizontalOffset == true) {
        var windScrollLeft = $(window).scrollLeft();
        var ppContainerLeftBorderWindowCoordinate = x - windScrollLeft;
        if (ppContainerLeftBorderWindowCoordinate < 0)
            x = windScrollLeft;
        else {
            var windWidth = $(window).width();
            var widthDifference = windWidth - (ppContainerLeftBorderWindowCoordinate + ppContainer.coordinates.width);
            if (widthDifference < 0)
                x = x - Math.min(-widthDifference, ppContainerLeftBorderWindowCoordinate);
        }
    }

    if (ppContainerData.allowVerticalOffset == true) {
        var windScrollTop = $(window).scrollTop();
        var ppContainerTopBorderWindowCoordinate = y - ppContainerData.cornerRound - windScrollTop;
        if (ppContainerTopBorderWindowCoordinate < 0)
            y = windScrollTop + ppContainerData.cornerRound;
        else {
            var windHeight = $(window).height();
            var heightDifference = windHeight -
          (ppContainerTopBorderWindowCoordinate + ppContainer.coordinates.height + 2 * ppContainerData.cornerRound);
            if (heightDifference < 0)
                y = y - Math.min(-heightDifference, ppContainerTopBorderWindowCoordinate);
        }
    }

    if (ppContainer.coordinates.offsetParent && ppContainer.coordinates.offsetParent != null) {
        var offsetParentOffset = $(ppContainer.coordinates.offsetParent).offset();
        var x = x - offsetParentOffset.left;
        var y = y - offsetParentOffset.top;
        if ($.browser.msie && ppContainer.coordinates.offsetParent == document.body) {
            x = x + $(window).scrollLeft();
            y = y + $(window).scrollTop();
        }
    }
    ppContainer.style.left = x + "px";
    ppContainer.style.top = y + "px";
}

function HideChildPP(event) {
    var ppParentElement = this.ppParentData.parent;
    if (CheckPPRelation2(event, ppParentElement))
        return;
    HidePP(this.ppParentData.child);
}

function HideThisPP(event) {
    var ppContainer = this;
    if (CheckPPRelation2(event, null, ppContainer))
        return;
    HidePP(ppContainer);
}

function HidePP(ppContainer) {
    if (!ppContainer)
        return;

    if (ppContainer != null && ppContainer.style.display != "none") {
        var ppContainerData = ppContainer.ppChildData;
        if (ppContainerData.disappearDelay && ppContainerData.disappearDelay > 0) {
            ppContainerData.disappearTimeoutId =
          window.setInterval(function() { DoHidePP(ppContainer); }, ppContainerData.disappearDelay);
        }
        else
            DoHidePP(ppContainer);
    }
}

function DoHidePP(ppContainer) {
    var ppContainerData = ppContainer.ppChildData;
    window.clearInterval(ppContainerData.disappearTimeoutId);
    switch (ppContainerData.hideMethod) {
        case 'show':
            $(ppContainer).hide(ppContainerData.disappearInterval);
            break;
        case 'slideDown':
            $(ppContainer).slideUp(ppContainerData.disappearInterval);
            break;
        case 'fadeIn':
            $(ppContainer).fadeOut(ppContainerData.disappearInterval);
            break;
        default:
            $(ppContainer).fadeOut(ppContainerData.disappearInterval);
            break;
    }
}

function CheckPPRelation2(event, parent, child) {
    if (parent && child) {
        if (child.ppChildData == parent.ppParentData)
            return true
    }
    else if (event) {
        var toElement;
        if (event.toElement)
            toElement = event.toElement; //for IE
        else
            toElement = event.relatedTarget; //for Firefox and other
        var toElementParents = $(toElement).parents();
        if (parent)
            var parentForChecking = parent.ppParentData.child;
        else if (child)
            var parentForChecking = child.ppChildData.parent;

        if (parentForChecking == toElement)
            return true;
        else if (toElementParents && toElementParents.length > 0)
            if (toElementParents.index(parentForChecking) >= 0)
            return true;
    }

    return false;
}

function GetTarget(event) {
    if (event && event != null && event.target)
        return event.target;
    return null;
} 