﻿var isApproved = false;
var isAdmin = false;
var tempdesigncontent;
var roomName;
var naturalSurroundings;
var tags;
var designGoals;
var roomTypeId;
var templateId;
var predesigned;
var lastPageURI;

$(document).ready(function () {
    isUserApproved(function (value) { isApproved = value; }, false);
    isUserAdmin(function (value) { isAdmin = value; }, false);

    var attributes = {
        id: "tempdesigncontent",
        style: "position:absolute"
    };
    swfobject.embedSWF(getTempDesignFlashUrl(), "tempdesigncontent", 1, 1, "10.1.102.64", null, null, null, attributes);
});

function onTempDesignFlashIsLoaded() {
    tempdesigncontent = getFlashObject('tempdesigncontent');
    if ($("#selectProductSet").length > 0)
        fillProductSetSelector(false);
}

function getFlashObject(movieName) {
    var isIE = navigator.appName.indexOf("Microsoft") != -1;
    var flash = (isIE) ? window[movieName] : document[movieName];
    if (flash == null)
        flash = $('#' + movieName)[0];
    return flash;
}

function createTempDesign(_roomName, _naturalSurroundings, _tags, _designGoals, _roomTypeId, _templateId, _lastPageURI, _predesigned) {
    roomName = _roomName;
    naturalSurroundings = _naturalSurroundings;
    tags = _tags;
    designGoals = _designGoals;
    roomTypeId = _roomTypeId;
    templateId = _templateId;
    predesigned = _predesigned;
    lastPageURI = _lastPageURI;

    tryCreateTempDesign();
}

function tryCreateTempDesign() {
    if (!isApproved) {
        //TODO: rewrite using jquery.externalInterface.js
        try {
            tempdesigncontent.createTempDesign(roomName, naturalSurroundings, tags, designGoals, roomTypeId, templateId);
            waitTempDesignCreated();
        } catch (e) {
            // if tempdesigncontent is not ready yet
            setTimeout("tryCreateTempDesign()", 100);
        }
    }
    else {

        var data = JSON.stringify({ "name": roomName, "naturalSurroundings": naturalSurroundings, "tags": tags, "designGoals": designGoals, "roomTypeId": roomTypeId, "templateId": templateId });

        $.ajax({
            type: "POST",
            url: "/WebServices/Room.asmx/CreateRoomIdea",
            contentType: "application/json; charset=utf-8",
            data: data,
            dataType: "json",
            success: function (json) {
                onTempDesignCreated(json.d);
            }
        });
    }
}

function waitTempDesignCreated() {
    if (tempdesigncontent == null || tempdesigncontent.getNewTempDesignId() == 0) {
        setTimeout("waitTempDesignCreated()", 200);
    } else {
        onTempDesignCreated(tempdesigncontent.getNewTempDesignId());
    }
}

function onTempDesignCreated(id) {
    $.cookie("selectedProductSet", id, { expires: 30, path: "/" });
    $.cookie("selectedRoomType", roomTypeId, { expires: 30, path: "/" });
    $.cookie("isMyRoomRolloverOpened", true, { expires: 30, path: "/" });
    $.cookie("isActionActive", true, { expires: 1, path: "/" });

    if (lastPageURI == '3D') {
        window.location.href = getDesignaRoomUrl(id, predesigned);
    } else {
        window.location.href = lastPageURI;
    }
}
