﻿var current_page = "toolbar";
var current_splash = "home";
var pageX = null;
var pageY = null;

function Page(page) {
    if (page == "toolbar") {
        splash = "home";
    } else if (page == "branch" || page == "panel" || page == "transformer" || page == "productrequest") {
        splash = "products";
    } else if (page == "experts" || page == "exhibitors") {
        splash = "services";
    } else if (page == "faq" || page == "power") {
        splash = "solutions";
    } else if (page == "offices" || page == "nsc" || page == "order") {
        splash = "company";
    } else {
        return;
    }

    if (splash != null && splash != current_splash) {
        $("#wireframe_overlay").hide().removeClass().addClass('splash overlay_' + splash);
        $("#wireframe_behind").hide().removeClass().addClass('splash behind_' + splash);
        var newpos = $("#menu_" + splash).offset().left - $("#wireframe").offset().left;
        $("#indicator").animate({ left: newpos });

        /*
        $(".menu_title").removeClass("menu_title_active");
        $("#menu_" + splash).addClass("menu_title_active");
        */
        $(".splash").fadeIn('slow');
        current_splash = splash;
    }
    if (page != current_page) {
        $.ajax({
            url: "/Page/" + page,
            dataType: "html",
            type: "get",
            success: function (data) {
                $('.menu_dropdown').hide();
                $("#page").html(data);
                current_page = page;
                if (is_mobile) {
                    $(".product_thumbnail").click(function (event, ui) {
                        try {
                            var iid = $(this)[0].alt;
                            var div = $("<div class='zoom' style='display:none;'></div>");
                            var img = new Image();
                            img.onload = function () {
                                $(".zoom,.zoom_3d").hide();
                                div.width(this.width).height(this.height + 28);
                                div.append(img);
                                var wf = $("#wireframe");
                                wf.append(div);
                                div.css("position", "absolute");
                                div.css("top", (wf.height() - this.height) / 2 + wf.scrollTop() + "px");
                                div.css("left", 50);
                                div.append("<div style='padding:2px;text-align:center'><input type='button' value='Close' onclick='$(\".zoom\").hide();' /></div>");
                                div.fadeIn("medium");
                            }
                            img.src = "/ProductImage/" + iid;
                        } catch (e) {
                            window.alert(e);
                        }
                    });
                } else {
                    $(".product_thumbnail").mouseenter(function (event, ui) {
                        try {
                            var iid = $(this)[0].alt;
                            var div = $("<div class='zoom' style='display:none;'></div>");
                            var img = new Image();
                            img.onload = function () {
                                $(".zoom,.zoom_3d").hide();
                                div.width(this.width).height(this.height);
                                div.append(img);
                                var wf = $("#wireframe");
                                wf.append(div);
                                div.css("position", "absolute");
                                div.css("top", (wf.height() - this.height) / 2 + wf.scrollTop() + "px");
                                div.css("left", 50);
                                div.fadeIn("medium");
                            }
                            img.src = "/ProductImage/" + iid;
                        } catch (e) {
                            window.alert(e);
                        }
                    })
                .mouseleave(function (event, ui) {
                    $(".zoom").fadeOut();
                });
                }

                $(".product_3d").click(function (event, ui) {
                    try {
                        $(".zoom,.zoom_3d").hide();
                        var pid = $(this)[0].alt;
                        var div = $("<div class='zoom_3d' style='display:none;'></div>");
                        div.width(480).height(390);
                        var embed = $("<embed src='/Product3dImage/" + pid + "' height='360' width='480'></embed>");
                        div.append(embed);
                        var wf = $("#wireframe");
                        wf.append(div);
                        div.css("position", "absolute");
                        div.css("top", (wf.height() - div.height()) / 2 + wf.scrollTop() + "px");
                        div.css("left", 50);
                        div.append("<div style='padding:2px;text-align:center'><input type='button' value='Close' onclick='$(\".zoom_3d\").hide();$(\".panel_right\").unblock();' /></div>");
                        $(".panel_right").block({ message: null });
                        div.fadeIn("medium");
                    } catch (e) {
                        window.alert(e);
                    }
                })

            },
            error: function (request, status, thrown) {
                $("#page").html(formatAjaxError(request, status, thrown));
            }
        });
    }
}

function formatAjaxError(request, status, thrown) {
    if (request != null) {
        if (request.responseText.length > 0) {
            return request.responseText;
        }
    }
    switch (status) {
        case 'timeout':
            return "The request timed out.";
        case 'notmodified':
            return "The request was not modified but was not retrieved from the cache.";
        case 'parseerror':
            return "XML/Json format is bad.";
        default:
            return "HTTP Error (" + request.status + " " + request.statusText + ").";
    }
}



