/**
* 解决cookie读中文问题
* URL encode / decode
*
**/ 
var Url = {

    // public method for url encoding
    encode : function (string) {
        return escape(this._utf8_encode(string));
    },

    // public method for url decoding
    decode : function (string) {
        return this._utf8_decode(unescape(string));
    },

    // private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}

/**
* 这段是读取Cookie
*/
function GetCookie(sName)
{
    var mycookie = document.cookie.split("; ");
    for (var i = 0; i < mycookie.length; i++)
    {
        var cookie1 = mycookie[i].split("=");
        if (cookie1[0] == sName)
        {
            return Url.decode(cookie1[1]);
        }
    }
    return null;
}
//=============中文问题结束===========================
zy = function() {
	this.user = {	
	        userId : GetCookie("userId")
	    };
	    this.logined = this.user.userId!=null && this.user.userId > 0;
};
	
var ZY = new zy();

/**
 * dialog
 */
function reinitIframe(){
    var iframe = document.getElementById("zy-ui-model-iframe");
    try{
        var bHeight = iframe.contentWindow.document.body.scrollHeight;
        var dHeight = iframe.contentWindow.document.documentElement.scrollHeight;
        var height = Math.max(bHeight, dHeight);
        iframe.height =  height;
    }catch (ex){}
}

$.extend(ZY, {
    modelSettings : {
        initWidth: 600,
        marginTop: 100,
        container: '<img src="/theme/loading.gif" />',
        allowClickOverlayToCancel: true,
        showButtonDelete: true,
        none : null
    },
    iframeSettings : {
        src: '',
        width : '100%',
        height : '100%'
    },
    infoSettings : {
        initWidth: 480,
        title : null,
        content : null,
        level : 'info',
        buttonOk: '确认',
        buttonCancel: '取消',
        eventOk: function(){},
        eventCancel: function(){},
        none: null
    },
    modal: function(settings) {
        var settings = $.extend({}, ZY.modelSettings, settings);
        if ($('#zy-ui-overlay').length != 0) {
            ZY.modalClose(true);
        }
        $('body').prepend('<div id="zy-ui-overlay"/>');
        var modal = '<div id="zy-ui-modal">' +
        '<table>' + 
        '<tr><th></th><th></th><th></th></tr>';
        if (settings.title && settings.title.length > 0) modal += '<tr><th></th><td><h2>' + settings.title + '</h2></td><th></th></tr>';
        modal += '<tr><th></th><td id="zy-ui-modal-container" width="' + settings.initWidth + '"></td><th></th></tr>' +
        '<tr><th></th><th></th><th></th></tr>' +
        '</table>';
        if(settings.showButtonDelete == true) modal += '<a href="javascript:;" class="del"></a>';	
        modal += '</div>';
        $('body').prepend(modal);
        $('#zy-ui-overlay').css({
            height: $(document).height(),
            width: '100%',
            opacity: 0,
            position: 'absolute',
            zIndex: 1200,
            left: 0,
            top: 0,
            background: '#000000'
        }).animate({
            opacity: 0.5
        });
        $('#zy-ui-modal').css({
            opacity: 0,
            zIndex: 1201,
            position: 'absolute',
            width: settings.initWidth,
            left: ($(document).width() - settings.initWidth) / 2,
            top: (settings.marginTop + document.documentElement.scrollTop + document.body.scrollTop)
        }).animate({
            opacity: 1
        });
        if (typeof settings.container == 'string') {
            $('#zy-ui-modal-container').html(settings.container);
        }
        if (typeof settings.container == 'function') {
            var e = $('#zy-ui-modal-container');
            e.holder = $('#zy-ui-modal');
            settings.container(e);
        }
        if (settings.allowClickOverlayToCancel) {
            $('#zy-ui-overlay').click(function(){
                ZY.modalClose();
            });
        }
        $('#zy-ui-modal .del').click(function(){
            ZY.modalClose();
        });
    },
    modalClose: function(noEffect) {
        // $(document).trigger('zy-modal-closed');
        if (noEffect)
        {
            $('#zy-ui-overlay').remove();
            $('#zy-ui-modal').remove();
        }
        else
        {
            $('#zy-ui-overlay').animate({
                opacity: 0
            }, function() {
                $(this).remove();
            });
            $('#zy-ui-modal').animate({
                opacity: 0
            }, function() {
                $(this).remove();
            });
        }
    },
    // iframe window
    // ZY.ifrmame('http://xx')
    // ZY.ifrmame({src:'', title:'xx'})
    iframe: function(settings) {
        if (typeof settings == 'string') {
            settings = {
                src:settings
            };
        }
        var settings = $.extend({}, ZY.iframeSettings, settings);
        var zoom = 1;
        if(settings.zoom){
            zoom = settings.zoom;
        }
        settings.container = '<iframe id="zy-ui-model-iframe" src="' + settings.src + '" width="' + settings.width + '" height="' + settings.height + '" frameborder="0" scrolling="no" style="zoom:'+zoom+'"></iframe>';
        if (ZY.iframeSettings.width != settings.width)
        {
            settings.initWidth = settings.width;
        }
        ZY.modal(settings);
        //自适应高度
        $('#zy-ui-model-iframe').load(function(){
            reinitIframe();
        //window.setInterval("reinitIframe()", 200);
        });
        // ie6 bug 
        //if (ZY.iframeSettings.height == settings.height && $.browser.msie && $.browser.version < 7)
        //{
        //	$('#zy-ui-model-iframe').load(function(){
        //		if (this.document.documentElement.offsetHeight)
        //		{
        //			this.height = this.document.documentElement.offsetHeight + 'px';
        //		} else {
        //			this.height = this.document.body.scrollHeight + 'px';
        //		}
        //	});
        //}
        if ($.browser.msie && $.browser.version < 7 || navigator.userAgent.indexOf("TencentTraveler")>0)
        {
            setTimeout(function(){
                $('#zy-ui-model-iframe').attr('src', settings.src)
                }, 10);
        }

    },
    lightbox: function(settings) {
        if (typeof settings == 'string') {
            settings = {
                content:settings
            };
        }
        var settings = $.extend({}, ZY.dialogSettings, settings);
        //settings.container = '<h2>' + settings.title + '</h2>';
        settings.container = '';
        if(settings.content!==null){
            settings.container += '<table class="zy-ui-content"><tr><td>' + settings.content + '</td></tr></table>';
            //alert( settings.content);
        }
        var buttonsL = settings.buttons.length;
        if(buttonsL!==0){
            settings.container += '<div class="zy-ui-button r">';
            for(var i =0; i< buttonsL; i++){
                settings.container += '<div id="zy-ui-button-'+i+'" name="'+i+'" class="' + settings.buttons[i].classes + '"><a href="javascript:;">' + settings.buttons[i].title + '</a></div>';
            }
            settings.container += '</div>';
        }
        ZY.modal(settings);
        var buttonsL = settings.buttons.length;
        if(buttonsL!==0){
            for(var i = 0; i< buttonsL; i++){
                $('#zy-ui-button-'+i+'').click(function(){
                    if(settings.buttons[$(this).attr('name')].click() !== false){
                        ZY.modalClose();
                    }
                });
            }
        }
        return false;
    },
    dialog: function(settings) {
        if (typeof settings == 'string') {
            settings = {
                content:settings
            };
        }
        var settings = $.extend({}, ZY.dialogSettings, settings);
        if (settings.title == null || settings.title == undefined) {
            if (settings.level == 'warn') {
                settings.title = '警告';
            } else if (settings.level == 'error') {
                settings.title = '错误';
            } else {
                settings.title = '提示';
            }
        }
        settings.container = '<table class="zy-ui-content"><tr><td width="' + (settings.initWidth) + '">' + settings.content + '</td></tr></table>';
        ZY.modal(settings);
		$('#zy-ui-modal').addClass('zy-ui-modal');
        $('#zy-ui-button-ok').click(function(){
            settings.eventOk();
            ZY.modalClose();
        });
        if (settings.buttonCancel) {
            $('#zy-ui-button-cancel').click(function(){
                settings.eventCancel();
                ZY.modalClose();
            });
        }
        return false;
    },
	  info: function(settings) {
        if (typeof settings == 'string') {
            settings = {
                content:settings
            };
        }
        settings.level = 'info';
        settings.buttonCancel = false;
        return ZY.dialog(settings);
    },
    warn: function(settings) {
        if (typeof settings == 'string') {
            settings = {
                content:settings
            };
        }
        settings.level = 'warn';
        settings.buttonCancel = false;
        return ZY.dialog(settings);
    },
    error: function(settings) {
        if (typeof settings == 'string') {
            settings = {
                content:settings
            };
        }
        settings.level = 'error';
        settings.buttonCancel = false;
        return ZY.dialog(settings);
    },
    loginWindow: function(returnUrl){      
        if(!returnUrl){
            returnUrl = location.href;
        }
        if(returnUrl.indexOf("#")>=0){
            returnUrl = returnUrl.substr(0,returnUrl.indexOf("#"));
        }
        if (!ZY.logined)
        {
            {    
				ZY.iframe({src:'/auth/login',showButtonDelete:false,width:488,height:390});
                return false;
            }
        }
        return true;
    } ,
	registerWindow: function(){ 
		ZY.iframe({src:'/index/register',showButtonDelete:false,width:488,height:455});
        return true;
    } ,
    loginWindow_hd: function(returnUrl){      
        if(!returnUrl){
            returnUrl = location.href;
        }
        if(returnUrl.indexOf("#")>=0){
            returnUrl = returnUrl.substr(0,returnUrl.indexOf("#"));
        }
        if (!ZY.logined)
        {
            {    
				ZY.iframe({src:'/doxhd/auth/login',showButtonDelete:false,width:488,height:390});
                return false;
            }
        }
        return true;
    } ,
	registerWindow_hd: function(){ 
		ZY.iframe({src:'/doxhd/index/register',showButtonDelete:false,width:488,height:455});
        return true;
    } ,
    loss_pwd: function(){ 
		ZY.iframe({src:'/index/lost-pass',showButtonDelete:false,width:488,height:455});
        return true;
    },
    loss_pwd_hd: function(){ 
		ZY.iframe({src:'/doxhd/index/lost-pass',showButtonDelete:false,width:488,height:455});
        return true;
    }
});
$(document).ready(function(){
    if($('#login dd input').val()!=''){
        $(this).next('p').hide();
    }else{
        $(this).next('p').show(); 
    }
    $('#login dd input').click(function(){
        $(this).next('p').hide();
    }); 
    $('#login dd p').click(function(){
        $(this).hide(); 
		$(this).prev('input').focus();
    }); 
    $('#login dd input').blur( function () { 
        if($(this).val()==''){
            $(this).next('p').show();
        }
    } ); 

}); 





