﻿var SpawnDirectiveTargetType = { "NewPath": 1, "NonPath": 2, "LinkOut": 3 };
var SpawnDirectiveNavigateType = { "Popup": 1, "Inline": 2 };

// Builds the Url
//---------------
function BuildUrl(sdSpawnDirectiveTargetType, sdPathName, pathId, pathCampaignWithDetailId, pathCampaignWithDetailThirdPartyId, sdAspxPageName, sdOfferSetTemplateName, sdOfferId) {
    var mUrl = "";
    var url1 = "";

    if (sdSpawnDirectiveTargetType == SpawnDirectiveTargetType.NewPath)
        url1 = "SpawnPage.aspx?path=" + sdPathName + "&parent=" + pathId + "&c=" + pathCampaignWithDetailId + "&s=" + pathCampaignWithDetailThirdPartyId;
    else // it has to be "NonPath"
        url1 = sdAspxPageName + "?p=" + pathId;

    if ((sdOfferSetTemplateName != null) && (sdOfferSetTemplateName != ""))
        mUrl = url1.concat("&OfferSetTypeName=", sdOfferSetTemplateName);
    else
        mUrl = url1.concat("&OfferId=", sdOfferId);

    return mUrl;
}

// Main function for SpawnDirective script
//----------------------------------------
function RunSpawnDirective(sdSpawnDirectiveTargetType, sdSpawnDirectiveNavigateType, sdSpawnDirectiveTriggerTypeName,
                           sdPathName, sdJavaScript, sdAspxPageName, sdOfferSetTemplateName, sdOfferId, sdWindowStyle, sdPopover, 
                           pathId, pathCampaignWithDetailId, pathCampaignWithDetailThirdPartyId, pageIdentifier) {
    var onLoadSpawnWindowIndex = 1;
    var winUrl = "";

    if ((sdSpawnDirectiveTargetType == SpawnDirectiveTargetType.NewPath) || (sdSpawnDirectiveTargetType == SpawnDirectiveTargetType.NonPath)) {
        winUrl = BuildUrl(sdSpawnDirectiveTargetType, sdPathName, pathId, pathCampaignWithDetailId, pathCampaignWithDetailThirdPartyId, sdAspxPageName, sdOfferSetTemplateName, sdOfferId);
    }
    else if (sdSpawnDirectiveTargetType == SpawnDirectiveTargetType.LinkOut) {
        if (sdSpawnDirectiveNavigateType == SpawnDirectiveNavigateType.Popup) {
            if ((sdJavaScript != null) && (sdJavaScript != ""))
                winUrl = sdJavaScript;
            else if ((sdAspxPageName != null) && (sdAspxPageName != ""))
                winUrl = sdAspxPageName;
            else
                return false;
        }
        else
            winUrl = sdAspxPageName;
    }
    else
        return false;

    if (sdSpawnDirectiveNavigateType == SpawnDirectiveNavigateType.Popup) {
        popup = window.open(winUrl, sdSpawnDirectiveTriggerTypeName + "Spawn" + pageIdentifier + "_" + onLoadSpawnWindowIndex, sdWindowStyle);
        if (!sdPopover) {
            popup.blur();
            if (navigator.userAgent.indexOf('Chrome/') > 0) { window.blur(); }
            setTimeout("window.focus();", 2000);
        }
    }
    else if (sdSpawnDirectiveNavigateType == SpawnDirectiveNavigateType.Inline) {
        if ((sdSpawnDirectiveTargetType == SpawnDirectiveTargetType.NewPath) || (sdSpawnDirectiveTargetType == SpawnDirectiveTargetType.LinkOut)) {
            window.location = winUrl;
        }
        else
            return false;
    }
    else
        return false;

    return true;
}

