﻿// JScript source code
$(function() {
    //Turn off ajax cache in IE7-8
    $.ajaxSetup({
        cache: false
    });
    var html = document.documentElement.clientHeight;
    var container = document.getElementById("hgt100").offsetHeight;
    if (html > container)
        $(".pagebg-l").toggleClass("pagebg-l-height");

    $("a.homeManifsto").click(function() {
        if ($("div#contentLoadManifesto").is(":empty")) {
            $.ajax({
                url: "/ajaxcontents/manifesto.html",
                cache: false,
                success: function(html) { $("div#contentLoadManifesto").html(html) }
            });
        }
    })
});
$(function () {
    $("#openMgx, #openStpByStp, #openManifestoOvrlay, #openVatOverlay").overlay({ expose: '#555555', effect: 'apple', top: "center" });
    $("#blenderPlug, #rhinoPlug, #rhinoPlugBanner, #openDwnldPlugin, #googlePlug").overlay({ expose: '#555555', effect: 'default', top: 'center', onClose: function () {$("#imgUploaderInsert").show(); }, onLoad: function () {$("#imgUploaderInsert").hide();} });
    $("#TabLoadMaterial, #TabLoadSurface, #TabLoadColor").overlay({ expose: '#555555', effect: 'apple', top: '10%', onClose: function () { $("#imgUploaderInsert").show(); }, onLoad: function () { $("#imgUploaderInsert").hide(); } })


    $("a#openMgx").click(function () {
        $("div#contentLoad").load("/ajaxcontents/mgx.html");
    });

    $("a#openStpByStp").click(function () {
        $("div#contentLoadStpByStp").load("/ajaxcontents/step-by-step.html");
    });

    $("a#openDwnldPlugin, #blenderPlug, #rhinoPlug, #rhinoPlugBanner, #googlePlug").click(function () {
        var initIndex = $(this).attr("id")
        $("div#contentLoadDwnldPlugin").load("/ajaxcontents/download-plugin.html", function () {
            var a = $("#overlayDwnldPlugin a");
            for (var i = 0; i < a.length; i++) {//IE7 bug remove url before #
                if (a[i].href.indexOf('#') > 0)
                    a[i].href = a[i].href.substring(a[i].href.indexOf('#'));
            }
            if (initIndex == "openDwnldPlugin") {
                $("#tabPlugin").tabs("#panesPlugin > div", { initialIndex: 0 });
            } else if (initIndex == "blenderPlug") {
                $("#tabPlugin").tabs("#panesPlugin > div", { initialIndex: 1 });
            } else if (initIndex == "rhinoPlug" || initIndex == "rhinoPlugBanner") {
                $("#tabPlugin").tabs("#panesPlugin > div", { initialIndex: 2 });
            } else if (initIndex == "googlePlug") {
                $("#tabPlugin").tabs("#panesPlugin > div", { initialIndex: 3 });
            } else {
                $("#tabPlugin").tabs("#panesPlugin > div", { initialIndex: 2 });
            }
        });


    });
    $("#overlayDwnldPlugin div.close").click(function () {
        var url = location.href;
        if (url.indexOf("#") > 0) {
            var pos = url.indexOf("#");
            var newUrl = url.slice(0, pos);
            location.href = newUrl;

        }

    });

});

function dynamicModelProperties() {
    this.setTabIndex = setTabIndex;
    function setTabIndex(elmId) {
        var intIndx;
        if (_Model.GetState() == State.Succeed) {
            switch (elmId) {
                case "model":
                    intIndx = 0;
                    break;
                case "material":
                    intIndx = 1;
                    break;
                case "d3printer":
                    intIndx = 2;
                    break;
            }
            return intIndx;
        } else {
            switch (elmId) {
                case "model":
                    intIndx = 0;
                    break;
                case "material":
                    intIndx = 0;
                    break;
                case "d3printer":
                    intIndx = 1;
                    break;
            }
            return intIndx;
        }
    }
    this.showDynamicPropertiesPopup = function() {
        var setIndx = this.setTabIndex;
        $("img.activateTab").click(function(evt) {
            var tarGetId = $(this).attr("id");
            $("#tabHolder").empty();

            $("img.loaderStatusImg").bind("ajaxSend", function() {
                $(this).show();
            }).bind("ajaxComplete", function() {
                $(this).hide();
            });
            if (_Model.GetState() == State.Succeed || _Model.GetState() == State.CantCalculatePrice || _Model.GetState() == State.CantBeFixed) {
                $("div#tabHolder").load("/Helper/ModelDescription", function() {

                    $("ul#tabModel").tabs("div#panesModel > div", { initialIndex: setIndx(tarGetId) });
                    $("ul#tabModelPoperties").tabs("div#pannesModelProperties > div");
                    $("ul#modelMaterialPoperties").tabs("div#modelMaterialsPropertiesPannes > div");
                    $(".no-active a").click(function(evt) { evt.preventDefault(); });
                });
            }
            else {
                $("div#tabHolder").load("/Helper/ModelDescription",
                {
                    materialID: _View.SelectedMaterial
                }, function() {

                    $("ul#tabModel").tabs("div#panesModel > div", { initialIndex: setIndx(tarGetId) });
                    $("ul#tabModelPoperties").tabs("div#pannesModelProperties > div");
                    $("ul#modelMaterialPoperties").tabs("div#modelMaterialsPropertiesPannes > div");

                });
            }
        });
    }
}
$(document).ready(function() {
    var openModelDynamicPopup = new dynamicModelProperties();
    openModelDynamicPopup.showDynamicPropertiesPopup();
});



$(function () {

    function checkParentTabIndex(elmId) {
        switch (elmId) {
            case "TabLoadMaterial":
                return 0;
            case "TabLoadSurface":
                return 1;
            case "TabLoadColor":
                return 2;
        }
        return 0;
    };

    $("img[rel]").overlay(
    {
        expose: '#555555',
        effect: 'apple',
        onClose: function () { $("#imgUploaderInsert").show(); }, 
        onLoad: function () { $("#imgUploaderInsert").hide(); }

    });

    $("a#TabLoadMaterial, a#TabLoadSurface, a#TabLoadColor").click(function () {
        var trgId = $(this).attr("id");
        $.ajax({
            url: "/ajaxcontents/surface-properties.html",
            cache: false,
            success: function (html) {
                $("div#StaticTabsHolder").html(html);
                var a = $("#StaticTabsHolder a");
                for (var i = 0; i < a.length; i++) {//IE7 bug remove url before #
                    if (a[i].href.indexOf('#') > 0)
                        a[i].href = a[i].href.substring(a[i].href.indexOf('#'));
                }
                var tabApiMat = $("ul#tabMaterial").tabs("div#panesMaterial > div", { api: true });
                tabApiMat.click(checkParentTabIndex(trgId));
                loadMaterialsList();
                $("ul#SubTabsSurface").tabs("div#SubPanesSurface > div");
            }
        });

    });
    //default load model story
    //    $.ajax({
    //        url: "/ajaxcontents/modelStoryGargoyle.html",
    //        cache: false,
    //        success: function(html) { $("div.modelStoriesAjaxContHolder").html(html) }
    //    });
    $(function () {
        //        $(".items div img").click(function() {
        //            $("img#rllovredImg").attr("src", modelLargeImagesFolderUrl + "/" + getFileNameFromUrl(this.src) + "." + modelImagesFormat);
        //        })

        $(".modelRollOverImgs").scrollable({ vertical: true, size: 3, clickable: true });
    });
    $("div.modelImgs div.imgF img").bind("click", function (evt) {
        if ($(this).attr("title")) {
            $.ajax({
                url: $(this).attr("title"),
                cache: false,
                success: function (html) {
                    $("div.modelStoriesAjaxContHolder").html(html);
                }
            });
        }
    });

});

var onMaterialDetailsShow = function OnMaterialDetailsShow() {
    loadMaterialsList($(this).attr("id"))
}

function loadMaterialsList(matID) {
    $("img.loaderStatusImg").bind("ajaxSend", function() {
        $(this).show();
    }).bind("ajaxComplete", function() {
        $(this).hide();
    });
    if (typeof (matID) == "undefined") {
        $("#mtrlsDynamicDescHolder").load("/Helper/MaterialDescription", function() {
            var matTabApi = $("#modelMaterialPopertiesHlpr").tabs("#modelMaterialsPropertiesPannesHlpr > div", { api: true });
            matTabApi.click(1);
            $(".materialProp a, a.polyLink").click(onMaterialDetailsShow);
        });
    }
    else {
        $("#mtrlsDynamicDescHolder").load("/Helper/MaterialDescription", { materialId: matID }, function() {
            var matTabApiId = $("#modelMaterialPopertiesHlpr").tabs("#modelMaterialsPropertiesPannesHlpr > div", { api: true });
            matTabApiId.click(1);
            $(".materialProp a, a.polyLink").click(onMaterialDetailsShow);
        });
    }
}
