﻿var MasterPage = {

    /*
     * Check url
     *
     * 检测页面被框架
     */
    checkUrl : function()
    {
        if (top.location != self.location)
        {
            top.location = self.location;
        }
    }
}

/*
 * Menu
 */
var Menu = {

    name : 'menuContext',

    timer : 10,

    timerValue : 3,

    container : '',

    pointer : '',

    initializeContext : function()
    {
        Menu.container = $(Menu.name);

        Menu.container.className = "alpha";

        Menu.container.style.position = "absolute";
        Menu.container.style.top = "22px";
        Menu.container.style.border ="1px solid #000";
        Menu.container.style.width ="160px";
        Menu.container.style.padding ="8px 8px 20px 8px";
        Menu.container.style.backgroundColor ="#fff";
        Menu.container.style.display = 'none';

        Event.observe(document, 'click',function(event)
        {
            var target = window.event ? window.event.srcElement : event ? event.target : null;

            if(target.name != Menu.name)
            {
                Menu.container.style.display = 'none';
            }
        });

        //
        // 设置城市信息.
        //

        var city = RuanYu.Cookies.find('city');

        if(city != '')
        {
            $("windowCityListener").innerHTML = "当前城市[" + city + "]";
        }
    },

    open : function(value)
    {
        if(Menu.container.style.display == 'none')
            Menu.container.style.display = '';

        Menu.container.style.width ="160px";
        
        Menu.paser(value);
    },

    paser : function(value)
    {
        var outString = '';
        
        switch(value)
        {
            case 0:
                Menu.close();
                break;
            case 1:
                Menu.container.innerHTML = $('corporationMenu').innerHTML;
                Menu.container.style.left ="638px";

                break;

            case 2:
                Menu.container.innerHTML = $('windowMenuWrapper2').innerHTML;
                Menu.container.style.left ="720px";
                break;

            case 3:
                Menu.container.innerHTML = $('windowMenuWrapper3').innerHTML;
                Menu.container.style.left ="808px";

                break;

            case 4:
                Menu.container.innerHTML = $('windowMenuWrapper4').innerHTML;
                Menu.container.style.left ="738px";
                break;

            case 5:
                Menu.close();
                break;

            case 6:
                Menu.close();
                break;

            case 7:
                outString += "<div class='header-nav-context-item'><a href='/question/tag/KeHuJianYiXiang/default.aspx'>客户建议箱</a></div>";
                outString += "<div class='header-nav-context-item'><a href='/question/tag/XuJiaXinXi/default.aspx'>假房源举报</a></div>";
                outString += "<div class='header-nav-context-item'><a href='/question/tag/HeiZhongJieQuBao/default.aspx'>黑中介举报</a></div>";
                outString += "<div class='header-nav-context-item'><a href='/question/tag/BuLiangXinXi/default.aspx'>不良信息</a></div>";
                outString += "<div class='header-nav-context-item'><a href='/question/tag/QiTaWenTi/default.aspx'>其他问题</a></div>";
                outString += "<div class='clear'></div>";

                Menu.container.innerHTML = outString;
                Menu.container.style.left ="807px";
                break;

            case 8:
                var list = $('cityGroup').innerHTML.split(' ');

                for(var i=0 ; i < list.length; i++)
                {
                    outString += "<div class='header-nav-context-item'><a href='javascript:Menu.setCity(\"" + list[i] +"\");'>" + list[i] +"</a></div>";
                }

                outString += "<div class='clear'></div>";

                Menu.container.innerHTML = outString;
                Menu.container.style.left ="10px";
                Menu.container.style.width ="320px";
                break;
                
            case 81:
                Menu.container.innerHTML = $('windowMenuWrapper81').innerHTML;
                Menu.container.style.left ="480px";
                break;
                
            case 82:
                Menu.container.innerHTML = $('windowMenuWrapper82').innerHTML;
                Menu.container.style.left ="542px";
                break;
                
            case 99:
                Menu.close();
                break;

            default:
                break;
        }
    },

    setCity : function(value)
    {
        $("windowCityListener").innerHTML = "当前城市[" + value + "]";

        var expire = new Date();

        expire.setFullYear(expire.getFullYear()+1);

        RuanYu.Cookies.add('city', value, expire, '/', 'kanf.cn');

        Menu.close();

        var memberToken = RuanYu.Web.memberToken

        if(typeof(memberToken.id) != 'undefined' && value != '全部')
        {
            var url = '/services/RuanYu/Membership/Ajax.MemberWrapper.aspx';

            var outString = '<?xml version="1.0" encoding="utf-8"?>';

            outString += '<ajaxStorage>';
            outString += '<memberId><![CDATA[' + memberToken.id +']]></memberId>';
            outString += '<city><![CDATA[' + value +']]></city>';
            outString += '</ajaxStorage>';

            var options = {
                asynchronous:false,
                method:'post',
                parameters:{
                    action:"set_city",
                    xml:outString
                },
                onSuccess: Menu.setCity_callback
            }

            new Ajax.Request(url, options);
        }
        else
        {
            location.href = '/city/redirect.aspx' ;
        }
    },

    setCity_callback : function(response)
    {
        RuanYu.Net.fetchException(response);

        var result = response.responseText.evalJSON().message;

        if(Number(result.returnCode) != 0)
        {
            alert(result.value);
        }
        else
        {
            switch(location.hostname)
            {
                case 'my.kanf.cn':
                    location.href = 'http://my.kanf.cn' ;
                    break;
                    
                default:
                    location.href = '/city/redirect.aspx' ;
                    break;
            }
        }
    },

    //
    setTimer : function(value)
    {
        if(value == -1)
        {
            Menu.timer--;

            if(Menu.timer == 0)
            {
                Menu.close();
            }
        }
        else
        {
            Menu.timer = Menu.timerValue;
        }
    },

    close : function()
    {
        Menu.container.style.display = 'none';

        // Effect.Fade(Menu.container);
    }
}

/*
 * Broadcast
 */
var Broadcast = {
    timer:10,
    timerValue:3,

    container:null,
    pointer:0,

    lockObject:false,

    initializeContext : function()
    {
        var container = $('broadcastContext');

        if(container)
        {
            Broadcast.container = container;
            Broadcast.paser();
        }
    },

    paser : function()
    {
        if(Broadcast.lockObject)
            return;

        Broadcast.lockObject = true;

        var url = "/services/RuanYu/Plugins/Broadcast/Ajax.BroadcastWrapper.aspx";

        var options = {
            method:'post',
            parameters:{
                action:"get_broadcast"
            },
            onSuccess: Broadcast.paser_callback
        }

        new Ajax.Request(url, options);
    },

    paser_callback : function(response)
    {
        Broadcast.lockObject = false;

        var message = response.responseText.evalJSON().ajaxStorage;

        Broadcast.container.innerHTML = '<span class="gainsboro-text">'
            + '[' + message.tag + ']</span> '
            + '<span class="broadcast-text">'
            + '<a href="'+ message.url + '" target="_blank" >' + message.message + '</a>'
            + '</span>';
    }
}

/*
 * SilverWrapper
 *
 * 页面的遮罩封装器
 */
var SilverWrapper = {

    name:'silverWrapper',

    popupWindowName : 'popupWrapper',

    maxOpacity:0.8,

    maxDuration: 0.2,

    /*
     * 显示
     */
    show : function()
    {
        var wrapper = $(SilverWrapper.name);

        if(wrapper == null)
        {
            $(document.body).insert('<div id="silverWrapper" ></div>');

            wrapper = $(SilverWrapper.name);

            Event.observe(wrapper, 'click', SilverWrapper.closePopupWindow);
        }

        wrapper.hide();

        wrapper.className = 'ajax-silver-wrapper';

        wrapper.style.height = SilverWrapper.getHeight();
        wrapper.style.width = SilverWrapper.getWidth();

        new Effect.Appear(SilverWrapper.name, { duration: SilverWrapper.maxDuration, from: 0.0, to: SilverWrapper.maxOpacity });
    },

    /*
     * 隐藏
     */
    hide : function()
    {
        var options = {
            duration: SilverWrapper.maxDuration,
            from: SilverWrapper.maxOpacity,
            to: 0.0
        };

        new Effect.Fade(SilverWrapper.name, options);
    },

    /*
     * 打开弹出窗口
     */
    openPopupWindow : function()
    {
        SilverWrapper.show();

        var container = $(SilverWrapper.popupWindowName);

        var x = 0, y = 40; // 弹出窗口的位置

        var width = 720; // 弹出窗口宽度

        if(container == null)
        {
            container = document.createElement('div');

            container.id = SilverWrapper.popupWindowName;

            container.style.width = width + 'px';
            container.style.background = '#FFFFFF';
            container.className = 'ajax-popup-wrapper';

            document.body.appendChild(container);

            x = (SilverWrapper.getPage().width - width)/2;

            RuanYu.Utility.setLocation(container, x, y);
        }
        else
        {
            container.style.display = '';

            x = (SilverWrapper.getPage().width - 720)/2;

            RuanYu.Utility.setLocation(container, x, y);
        }

        $(SilverWrapper.name).onclick = function()
        {
            SilverWrapper.closePopupWindow();
        }

        return container;
    },

    /*
     * 关闭弹出窗口
     */
    closePopupWindow : function()
    {
        var container = $(SilverWrapper.popupWindowName);

        if(container != null)
            container.hide();

        SilverWrapper.hide();
    },

    /*
     * 页面高度
     */
    getHeight : function()
    {
        return SilverWrapper.getPage().height + "px";
    },

    /*
     * 页面宽度
     */
    getWidth : function()
    {
        return SilverWrapper.getPage().width + "px";
    },

    getPage : function()
    {
	    var xScroll, yScroll;

	    if (window.innerHeight && window.scrollMaxY) {
		    xScroll = window.innerWidth + window.scrollMaxX;
		    yScroll = window.innerHeight + window.scrollMaxY;
	    } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		    xScroll = document.body.scrollWidth;
		    yScroll = document.body.scrollHeight;
	    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		    xScroll = document.body.offsetWidth;
		    yScroll = document.body.offsetHeight;
	    }

	    var windowWidth, windowHeight;

    	//console.log('self.innerWidth:' + self.innerWidth);
    	//console.log('document.documentElement.clientWidth:' + document.documentElement.clientWidth);

	    if (self.innerHeight) {	// all except Explorer
		    if(document.documentElement.clientWidth){
			    windowWidth = document.documentElement.clientWidth;
		    } else {
			    windowWidth = self.innerWidth;
		    }
		    windowHeight = self.innerHeight;
	    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		    windowWidth = document.documentElement.clientWidth;
		    windowHeight = document.documentElement.clientHeight;
	    } else if (document.body) { // other Explorers
		    windowWidth = document.body.clientWidth;
		    windowHeight = document.body.clientHeight;
	    }

	    // for small pages with total height less then height of the viewport
	    if(yScroll < windowHeight){
		    pageHeight = windowHeight;
	    } else {
		    pageHeight = yScroll;
	    }

    	//console.log("xScroll " + xScroll)
    	//console.log("windowWidth " + windowWidth)

	    // for small pages with total width less then width of the viewport
	    if(xScroll < windowWidth){
		    pageWidth = xScroll;
	    } else {
		    pageWidth = windowWidth;
	    }

    	//console.log("pageWidth " + pageWidth)

	    //arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)

	    return {width:pageWidth, height:pageHeight, windowWidth:windowWidth, windowHeight:windowHeight};
    }
}

Event.observe(window, 'load', MasterPage.checkUrl);

//Event.observe(window, 'load', MasterPage.createGoogleSearch);

Event.observe(window, 'load', RuanYu.Web.load);

Event.observe(window, 'load', Menu.initializeContext);

Event.observe(window, 'load', Broadcast.initializeContext);
