﻿var valbumWidget = popupFlash;

var closeWidget = function() {
    valbumWidget.close();
};

var lastTarget = "com.ScatterTunes.VAlbum";
var lastParams = {};
var lastWidgetLocation = '/Scripts/flash/';
var lastUpc = null;
var lastWidgetFile = 'popUpWidget.swf';
var lastWidth = 900;
var lastHeight = 458;

var openWidget = function (upc, target, params, widgetLocation, widgetFile, width, height, wmode) {
    jQuery.get("/API/GetBrowserType/GetBrowserType/", "", function (response) {
        if (response.BrowserType == "Desktop") {
            openWidgetInternal(upc, target, params, widgetLocation, widgetFile, width, height, wmode);
            return;
        }
        window.location.href = "/installer/download";
    });
};

var openWidgetInternal = function (upc, target, params, widgetLocation, widgetFile, width, height, wmode) {
    if (!widgetLocation) {
        widgetLocation = lastWidgetLocation;
    }

    if (!widgetFile) {
        widgetFile = lastWidgetFile;
    }

    if (!target) {
        target = lastTarget;
    }

    if (!upc) {
        upc = lastUpc;
    }

    if (!width) {
        width = lastWidth;
    }

    if (!height) {
        height = lastHeight;
    }

    lastWidgetLocation = widgetLocation;
    lastWidgetFile = widgetFile;
    lastTarget = target;
    lastUpc = upc;
    lastWidth = width;
    lastHeight = height;

    if (!params) {
        params = lastParams;
    }
    lastParams = params;

    params.target = target;

    if (upc) {
        params.upc = upc;
    }

    //params.env = 'dev';
    if (window.env !== undefined) {
        params.env = window.env;
    }
    params.installerMacUrl = 'http://scattertunes-software.s3.amazonaws.com/VAlbumSetupForWidget.dmg';
    params.installerWinUrl = 'http://scattertunes-software.s3.amazonaws.com/VAlbumSetupForWidget.exe';

    window.setTimeout(function () {
        // Add 1 to the intended width in order to fix a Mac FF bug.
        valbumWidget.show(widgetLocation + widgetFile, width + 1, height, params, wmode);
    }, 200);
};

var openGenericWidget = function(cmd, installNow) {
    openWidget(null, null, { cmd: cmd, installNow: installNow || false }, "/", "getLaunchScatterPopUp.swf", 500, 347, "transparent");
};

var redeemPurchase = function(id, purchaseId, vapPath, albumArtPath, albumName, albumArtist) {
    openWidget('', "com.ScatterTunes.VAlbum", {
        purchaseId: purchaseId,
        albumName: albumName,
        artistName: albumArtist,
        albumId: id,
        albumArtUrl: albumArtPath,
        fullVapUrl: vapPath,
        group: "VAlbum"
    });
};

var launchPreviewExample = function() {
    openWidget('', 'com.ScatterTunes.VAlbum', {
        albumName: "Speak Now (Deluxe Edition) V-Album Preview",
        artistName: "Taylor Swift",
        albumId: "ScatterTunes.843930004393",
        cmd: '{"upc":"813703010494","remote_install":"http://scattertunes.s3.amazonaws.com/vaps/preview_vaps/843930004393.vap"}',
        albumArtUrl: '/Content/images/taylorSwiftAlbumArt.jpg',
        fullVapUrl: 'http://scattertunes.s3.amazonaws.com/vaps/preview_vaps/843930004393.vap',
        group: "VAlbum"
    });
};
