/*
 * @(#)jQuery.CEA.SelectSite.js
 *       
 * 系统名称：东航电子商务国内B2C系统
 * 版本号：1.0
 * 
 * Copyright (c)  TravelSky
 * All Rights Reserved.
 * 
 * 作者：bjiang
 * 创建日期：Nov 4, 2009
 * 
 * 功能描述：子站点选择器
 *      需要DWR支持.
 *      需要Cookies支持.
 * 公用方法描述：
 *      $('#id').jSelectSite();
 *    
 * 修改人：
 * 修改日期：
 * 修改原因：
 * 
 * 
 */

; (function($)
{
    var SiteCity = null;
    $.fn.SelectSite = function(options)
    {
        var defaultop = {
            title: '选择子站点',
            callback: null,
            Adjacent: 'E',
            dataurl: '/mu/images/Sitecity.data'
        };
        var op = $.extend(defaultop, options);
        var $this = $(this);
        var windowid = 'selectsite';
        var getXY = function(obj)
        {
            var hight = 156;
            var width = 260;
            switch (op.Adjacent)
            {
            case 'T':
                return {
                    top: $(obj).offset().top - hight,
                    left: $(obj).offset().left
                };
            case 'E':
                return {
                    top: $(obj).offset().top + $(obj).height(),
                    left: $(obj).offset().left
                };
            case 'R':
                return {
                    top: $(obj).offset().top,
                    left: $(obj).offset().left + $(obj).width()
                };
            case 'L':
                return {
                    top: $(obj).offset().top,
                    left: $(obj).offset().left - width
                };
            case 'TL':
                return {
                    top: $(obj).offset().top - hight,
                    left: $(obj).offset().left - width
                };
            case 'EL':
                return {
                    top: $(obj).offset().top + $(obj).height(),
                    left: $(obj).offset().left - width
                };
            case 'TR':
                return {
                    top: $(obj).offset().top - hight,
                    left: $(obj).offset().left + $(obj).width()
                };
            case 'ER':
                return {
                    top: $(obj).offset().top + $(obj).height(),
                    left: $(obj).offset().left + $(obj).width()
                };
            default:
                return {
                    top: $(obj).offset().top + $(obj).height(),
                    left: $(obj).offset().left
                };
            }
        };

        var getSiteCity = function(async)
        {
            $.ajax({
                //取缓存数据
                cache: true,
                async: async,
                url: op.dataurl,
                success: function(date)
                {
                    SiteCity = date.match(/[^,]*,/ig);
                    for (var i = 0; i < SiteCity.length; i++)
                    {
                        SiteCity[i] = SiteCity[i].match(/[^,\|]+/ig);
                    }
                }
            });
        };
        var createTitle = function(current)
        {
            //生成对像
            var $title = $('<a id="sitecity" href="#" title="' + op.title + '">' + op.title + '</a>');

            //添加事件
            $title.click(function()
            {
                return false;
            });
            return $title;
        };

        var sitecityDate = function()
        {
            //检查数据
            if (SiteCity == null)
            {
                getSiteCity(false);
            }

            //构造对像
            var $sitecity = $('<ul id="sitecitys"></ul>');
            for (var i = 0; i < SiteCity.length; i++)
            {
                $sitecity.append('<li _3CODE="' + SiteCity[i][0] + '">' + SiteCity[i][1] + '</li>');
            }

            //添加事件
            $sitecity.find('li').click(function()
            {
                $this.find('#header_changecity_lable').text($(this).text() + '：');
                $this.attr('_show', '');
                $.msgbox.closemsgbox(windowid);
                if (op.callback != null)
                {
                    op.callback($(this).text());
                }
                $.cookie('city', $(this).text(), {
                    expires: 365,
                    path: '/'
                });
                $.cookie('_3CODE', $(this).attr('_3CODE'), {
                    expires: 365,
                    path: '/'
                });
                //TODO 添加页面跳转到首页
                window.location.href = '/mu';
                return false;
            });

            return $sitecity;
        };
        //显示列表
        var show = function(obj, $title, $content)
        {
            var position = getXY(obj);
            var $popwin = $.msgbox.msgbox(windowid, 260, 100, $title, $content).css({
                'top': position.top + 'px',
                'left': position.left + 'px'
            }).addClass('popwin_' + windowid);
            return $popwin;
        };

        $(this).click(function()
        {
            show(this, createTitle(), sitecityDate());
            $this.attr('_show', 'show');
        });
        var init = function()
        {
            var $thisDOM = $this;
            getSiteCity(true);
            $(document).mousedown(function(event)
            {
                var $target = $(event.target);
                if (($thisDOM.attr('_show') == 'show') && $('#' + windowid).length != 0 && $target.parents("#" + windowid).length == 0 && !($target.attr('_show') == 'show'))
                {
                    $('#resultcitys > a[sel="sel"]').click();
                    $this.attr('_show', '');
                    $.msgbox.closemsgbox(windowid);
                    return false;
                }
            });
        };
        return init();
    }
})(jQuery);