﻿/// <reference path="jQuery.js" />

var cname = '', t = null, flag = false, data = null;
var Popwin = {
    //初始化Popwin
    init: function (title, name) {
        //if (document.getElementById('popwin')) { Popwin.remove(); }
        var html = '<div id="bgcover"><iframe id="framecover" frameborder="1" scrolling="no" width="100%" height="100%"></iframe></div>' +
                   '<div id="popwin"><div class="tbar"><div class="title">推荐' + title + '专辑<span>' + name + '</span></div><div title="关闭窗口" class="close" onclick="Popwin.close()">关闭窗口</div></div>' +
                        '<ul class="list">' +
                            '<li><a href=""><img src="Statics/Images/Icon_Loading2.gif"/></a></li>' +
                            '<li><a href=""><img src="Statics/Images/Icon_Loading2.gif"/></a></li>' +
                            '<li><a href=""><img src="Statics/Images/Icon_Loading2.gif"/></a></li>' +
                            '<li><a href=""><img src="Statics/Images/Icon_Loading2.gif"/></a></li>' +
                            '<li><a href=""><img src="Statics/Images/Icon_Loading2.gif"/></a></li>' +
                            '<li><a href=""><img src="Statics/Images/Icon_Loading2.gif"/></a></li>' +
                            '<li><a href=""><img src="Statics/Images/Icon_Loading2.gif"/></a></li>' +
                            '<li><a href=""><img src="Statics/Images/Icon_Loading2.gif"/></a></li>' +
                        '</ul>' +
                   '<div class="bbar"><div class="tag"></div><div class="more"><a href="L_AlbumPage.aspx?fatherName=' + name + '" title="更多' + title + '专辑...">更多' + title + '专辑</a></div></div>' +
                   '</div>';
        $(document.body).append(html);
        Popwin.slidedown();
        cname = name;
    },
    //显示Popwin
    show: function (title, name) {
        if ($.browser.msie && $.browser.version == 6.0) $('select').hide();
        if (document.getElementById('popwin')) {
            $('div.title').text('推荐' + title + '专辑').find('span').text(name);
            $('div.more>a').text('更多' + title + '专辑').attr({ 'href': 'L_AlbumPage.aspx?fatherName=' + name, 'title': '更多' + title + '专辑' });
            Popwin.slidedown();
        } else {
            Popwin.init(title, name);
        }
        Popwin.load(name);
        $(window).bind('resize scroll', function () { Popwin.resize(); });
    },
    //关闭Popwin
    close: function () {
        $(window).unbind('resize scroll');
        var pw = $('#popwin');
        pw.animate({ top: -pw.outerHeight() }, 500, function () {
            $(this).hide();
            $('#bgcover').hide();
            if ($.browser.msie && $.browser.version == 6.0) $('select').show();
        });
    },
    //加载数据
    load: function (name) {
        if (data == null) {
            $.get("Xml/AM.xml", function (d) {
                data = d;
                Popwin.display(data, name);
            });
        } else {
            Popwin.display(data, name);
        }
    },
    //展示真实数据
    display: function (d, name) {
        var list = $(d).find('am ul[en="' + name + '"]>li:gt(2)');
        $('#popwin>ul.list>li').each(function (i) {
            if (list[i]) {
                $(this).html('<a href="' + $(list[i]).attr('link') + '"><img src="' + $(list[i]).attr('src') + '"/></a></li>');
            } else {
                $(this).empty();
            }
        });
    },
    //窗口大小改变时重置Popwin的位置
    resize: function () {
        if (flag) {
            t = setTimeout(function () {
                $('#bgcover').height($(document).height()).show();
                $('#bgcover iframe').height($(document).height()).show();
                var pw = $('#popwin');
                pw.css({ top: (($(window).height() - pw.outerHeight()) / 2 + $(window).scrollTop()), left: (($(window).width() - pw.outerWidth()) / 2 + $(window).scrollLeft()) });
                flag = false;
            }, 50);
        } else {
            clearTimeout(t);
            flag = true;
        }
    },
    //Popwin从顶部滑下
    slidedown: function () {
        var FW = $(document).width();
        var FH = $(document).height();
        var SH = window.screen.height;
        FH = FH < SH ? SH : FH;
        $('#bgcover').height(FH).show();
        $('#bgcover iframe').height(FH).show();

        var pw = $('#popwin');
        pw.css({ top: -pw.outerHeight(), left: (($(window).width() - pw.outerWidth()) / 2 + $(window).scrollLeft()) });
        pw.animate({ 'top': ($(window).height() - pw.outerHeight()) / 2 + $(window).scrollTop() }, 500).show();
    }
};
