﻿/// <reference path="../../../Studio/resources/Scripts/mootools.js" />
/// <reference path="../../../Studio/resources/Scripts/MooStudio.js" />
$import("UI.Mask.js");
window.addEvent("domready", function () {
    $$("#Left a").each(function (item) {
        item.fade(0.01);
        item.addEvents({
            "mouseover": function () { this.fade(1); },
            "mouseout": function () { this.fade(0.01); }
        });
    });

    HeaderCurrent();

    (function () {
        var items = $$("#Header table.items").getLast();
        items.fade("hide"); items.fade("in");
        var width = items.getParent().getSize().x;
        items.setStyle("width", width * items.getElements("td").length);
        var fx = new Fx.Tween(items);
        var timer = 0;
        var max = width - items.getSize().x;
        $$("#Header a.arrow").addEvent("click", function () {
            timer = 0;
            var left = items.getStyle("margin-left").toInt();
            if (this.hasClass("right")) {
                if (max == left)
                    fx.set("margin-left", 0);
                else
                    fx.start("margin-left", left - width);
            } else if (this.hasClass("left")) {
                if (left == 0)
                    fx.set("margin-left", max);
                else
                    fx.start("margin-left", left + width);
            }
        });

        function autoClick() {
            timer++;
            if (timer % 10 == 0)
                $$("#Header a.right").getLast().fireEvent("click");
        }

        setInterval(function () {
            timer++;
            var left = items.getStyle("margin-left").toInt();
            if (timer % 10 == 0 && left != max) {
                var right = $$("#Header a.right").getLast();
                right.fireEvent("click");
            }
        }, 300);
    })();
});

// 头部的当前状态标识
function HeaderCurrent() {
    var url = location.href.toLowerCase();
    var page = url.substring(url.lastIndexOf('/'));
    if (page.indexOf("?") != -1) page = page.substring(0, page.indexOf("?"));

    if (page == "/") page = "/default.aspx";

    $$(".Header .menu .wrapper a").each(function (item) {
        if (item.get("href").indexOf(page) == 0) {
            item.addClass("current");
            var subMenu = item.get("for");
        }
    });

    $$(".Header .subMenu .list a").each(function (item) {
        if (item.get("href").indexOf(page) == 0) {
            item.addClass("current");
            var parent = item.getParent(".list");
            parent.setStyle("display", "block");
            $$(".Header .menu .wrapper a[for=" + parent.get("id") + "]").each(function (menu) {
                menu.addClass("current");
            });
        }
    });
}

function KoreaView(url) {
    var width = $(document.body).getSize().x;
    var height = $(document.body).getSize().y;

    var lnkLeft = null;
    var lnkRight = null;

    var mask = new UI.Mask({
        "opacity": 50,
        "zIndex": 1,
        "background": "#FFFFFF",
        "onClick": function (e) {
            mask.close();
            mask = null;
            container.dispose();
            if (lnkLeft != null) lnkLeft.dispose();
            if (lnkRight != null) lnkRight.dispose();
            titObj.dispose();
            closeObj.dispose();
        }
    });
    var container = new Element("div", {
        "styles": {
            "position" : "fixed",
            "z-index": "2",
            "width" : width,
            "height": height,
            "top" : "0px",
            "left" : "0px",
            "text-align" : "center",
            "color" : "#FFF"
        }
    });
    container.inject($(document.body));

    var titObj = new Element("div", {
        "styles": {
            "position": "fixed",
            "z-index": "3",
            "top": "0px",
            "left": "0px",
            "text-align": "center",
            "color": "#000000",
            "padding" : "12px"
        },
        "html": "正在加载..."
    });
    titObj.inject($(document.body));

    var closeObj = new Element("a", {
        "href": "javascript:",
        "styles": {
            "position": "fixed",
            "z-index": "3",
            "top": "0px",
            "right": "0px",
            "width": "42px",
            "height": "42px",
            "background": "url(images/close.png) no-repeat"
        },
        "events": {
            "click": function () {
                close();
            }
        }
    });
    closeObj.inject($(document.body));

    new Request.HTML({
        "url": url,
        "method": "get",
        "noCache": false,
        "evalScripts": false,
        "onSuccess": function (responseTree, responseElements, responseHTML, responseJavaScript) {

            var body = null;
            for (var i = 0; i < responseElements.length; i++) {
                if ($(responseElements[i]).hasClass("ContainerMain")) {
                    body = $(responseElements[i]);
                }
            }
            var title = body.getElement(".Title h1").get("text").trim();
            var pics = body.getElements(".Content img");
            container.setStyle("width", width * pics.length);

            pics.each(function (item, index) {
                new Element("div", {
                    "styles": {
                        "width": width,
                        "position": "absolute",
                        "left": index * width,
                        "background": "url(/image/loadbar.gif) no-repeat center 300px"
                    },
                    "html": "<img src=\"" + item.get("src") + "\" style=\"border:1px solid #CCCCCC;\" />"
                }).inject(container);
            });
            showTitle(title);
            titObj.setStyles({
                "font": "18px/1.5 Tahoma,Helvetica,Arial,'宋体b8b\4f53',sans-serif",
                "font-weight" : 700
            });
            var fx = new Fx.Tween(container, {
                "onStart": function () {
                    rePosition();
                },
                "onComplete": function () {
                    showTitle(title);
                }
            });

            lnkLeft = new Element("a", {
                "href": "javascript:",
                "styles": {
                    "position": "fixed",
                    "z-index": "3",
                    "width": 37,
                    "height": 78,
                    "top": height / 2 - 39,
                    "right": 0,
                    "background": "url(/Studio/resources/Images/image-marquee-btn.png) no-repeat -37px top"
                },
                "events": {
                    "click": function () {
                        var marginLeft = container.getStyle("left").toInt();
                        if (Math.abs(marginLeft) == (pics.length - 1) * width) {
                            fx.set("left", 0);
                            rePosition();
                            showTitle(title);
                        } else {
                            fx.start("left", marginLeft - width);
                        }
                    },
                    "mouseover": function () {
                        this.setStyle("background-position", "-37px -78px");
                    },
                    "mouseout": function () {
                        this.setStyle("background-position", "-37px top");
                    }
                }
            });
            lnkLeft.inject($(document.body));

            lnkRight = new Element("a", {
                "href": "javascript:",
                "styles": {
                    "position": "fixed",
                    "z-index": "3",
                    "width": 37,
                    "height": 78,
                    "top": height / 2 - 39,
                    "left": 0,
                    "background": "url(/Studio/resources/Images/image-marquee-btn.png) no-repeat left top"
                },
                "events": {
                    "click": function () {
                        var marginLeft = container.getStyle("left").toInt();
                        if (Math.abs(marginLeft) == 0) {
                            fx.set("left", (pics.length - 1) * width * -1);
                            rePosition();
                            showTitle(title);
                        } else {
                            fx.start("left", marginLeft + width);
                        }
                    },
                    "mouseover": function () {
                        this.setStyle("background-position", "left -78px");
                    },
                    "mouseout": function () {
                        this.setStyle("background-position", "left top");
                    }
                }
            });
            lnkRight.inject($(document.body));

            var down = false;
            var start = { x: 0, y: 0 };
            container.getElements("div").each(function (item) {
                new Drag(item);
            });
        }
    }).send();

    function showTitle(title) {
        var count = container.getElements("div").length;
        var current = Math.abs(container.getStyle("left").toInt() / width) + 1;
        titObj.set("html", "模板编号: " + title + " (" + current + "/" + count + ")");
    }

    // 重新定位
    function rePosition() {
        container.getElements("div").each(function (item, index) {
            item.setStyles({
                "left": index * width,
                "top": 0
            });
        });
    }

    //关闭
    function close() {
        mask.fireEvent("click");
    }
}
