﻿//=============popup 开始=============
/**
 * 版本：CFPL popup 1.0
 * 参数：data.title                  标题
 *      (data.url || data.content)  内容，popup根据输入内容自动调整宽高。
 *                                  如：<div style="width:350px">内容内容内容</div>
 *      data.black_bg               不显示灰色背景 == 'no'， 默认yes
 *      data.offset_id              相对位置 不设定则位于正中 或定义data.of_top data.of_left
 * 框架：jquery-1.4.2.min.js
 * 作者：乘风破浪 QQ：121866618
 * 日期：2010-12-15
 */
function popup(data) {
    hide_selects();
    if (!data.title) data.title = '提示';

    var pageWidth = $(document).width();
    var pageHeight = $(document).height();
    var windowWidth = $(window).width();
    var windowHeight = $(window.parent).height();
    var scrollTop = $(window.parent).scrollTop();

    //插入背景
    if (data.black_bg != 'no' && !document.getElementById('popup_bg')) {
        $('<div id="popup_bg"></div>')
        .css({
            position: 'absolute',
            zIndex: 500,
            top: 0,
            left: 0,
            width: pageWidth,
            height: pageHeight,
            opacity: 0
        })
        .appendTo(document.body)
        .fadeTo(300, 0.8)
		.dblclick(close_popup);
    }
     //默认值，会根据输入内容的尺寸，自动调整。
   // data.width = window.popup_last_width || 565;
  //  data.height = window.popup_last_height || 400;
	if (data.offset_id){
   		var o = typeof(data.offset_id) == 'object' ? $(data.offset_id) : $('#' + data.offset_id);
   		//typeof："number"、"string"、"boolean"、"object"、"function"、"undefined"
 	   data.of_top = parseInt(o.offset().top);
 	   data.of_left = parseInt(o.offset().left);
	}

    data.top = data.of_top || parseInt(scrollTop + (windowHeight - data.height) / 2);
    data.left = data.of_left || parseInt((windowWidth - data.width) / 2);
	
    //插入popup
    $('#popup').remove();
    $('<div id="popup"></div>')
    .css({
        position: 'absolute',
        zIndex: 5001,
        top: data.top,
       left:data.left,
        width: data.width,
        height: data.height
        
    })
    .html('<div id="popup_content"></div>')
    .appendTo(document.body);
    show_popup_loading();
    //插入内容
    var inner_content = function(content) {
        hide_popup_loading();
        $('#popup_content')
        .html(content)
        .css({position: 'relative'})
        .fadeTo(300, 1);
        //popup_resize(data); //自动适应宽高
        setTimeout(function(){
			popup_resize(data); //自动适应宽高
		}, 100);
		//move_div('#popup_top', ['#popup', '.tips_box']);
		//move_div('#popup_top', ['#popup']);
		
    };
    //输入内容的两种方法
    if (data.url) {
        $.get(data.url, {}, inner_content);
    } else if (data.content){
        inner_content(data.content);
    }
}
// 计算新的位置
function popup_resize(data, callback) {
    var windowWidth = $(window).width();
    var windowHeight = $(window.parent).height();
    var scrollTop = $(window.parent).scrollTop();
    //新的宽度等
	
	var popup_content = $('#popup_content');
    var widtht = 570;
    var heightt = 400;
    popup_content.css({
        width: widtht,
        height: heightt
    });
    //把标题的高度加上  height = height + 34;
  
    var top = data.of_top || parseInt(scrollTop + (windowHeight - heightt) / 2);
    var left = data.of_left || parseInt((windowWidth - widtht) / 2);
    window.popup_last_width = widtht;
    window.popup_last_height = heightt;
    //动画到新的大小
	$("#popup_bg").animate({height: windowHeight},300);
    $('#popup').animate({
        top: top,
        left: left,
        width: widtht,
        height: heightt
    }, 300, callback);
}
// 关闭popup
function close_popup() {
    $('#popup_bg').remove();
	var popup = $('#popup');
    popup.animate(500, function(){
        popup.remove();
    });
    show_selects();

}
//-------------loading-------------
// 显示loading
function show_popup_loading() {
    if (!document.getElementById('popup_loading')) {
        var width = 75;
        var height = 75;
		
//		if (document.getElementById('popup')) { //防止在iframe下错位
			var p = $('#popup');
		
			var windowWidth = $(window).width();
			var windowHeight = $(window.parent).height();
			var scrollTop = $(window.parent).scrollTop();
//		} else {
//	        var windowWidth = $(window).width();
//	        var windowHeight = $(window).height();
//	        var scrollTop = $(window).scrollTop();
//        	var top = parseInt((windowHeight - height) / 2 + scrollTop);
//        	var left = parseInt((windowWidth - width) / 2);
//		}
			var top =parseInt(scrollTop + (windowHeight - height) / 2);
			var left = parseInt((windowWidth - width) / 2);
        $('<div id="popup_loading"></div>')
        .css({
            position: 'absolute',
            zIndex: 100003,
            top: top,
            left: left,
            width: width,
            height: height
        })
        .appendTo(document.body);
    }
}
function hide_popup_loading() {
    $('#popup_loading').remove();
}
//-------------下拉列表-------------
function hide_selects() {
    $('select').hide();
}
function show_selects() {
    $('select').show();
}


// 计算新的位置
function resizes(data, callback) {
	
	var asdd=getPageSize();
    var windowWidth = asdd[2];
    var windowHeight = asdd[3];
    var scrollTop = $(window.parent).scrollTop();
    //新的宽度等
	
    var width = data.width;
       
    var height = 450;
  
    var top = data.of_top || parseInt(scrollTop + (windowHeight - height) / 2);
    var left = data.of_left || parseInt((windowWidth - width) / 2);
    window.popup_last_width = width;
    window.popup_last_height = height;
    //动画到新的大小

    if (top < 90) {
        $("." + data.id).css({
        top:"20%"
        
        });

    } else {

        $("." + data.id).animate({
            top: top,
            left: left
        }, 300, callback);
    }
  
}




        //获取滚动条的高度
        function getPageScroll() {
            var yScroll;
            if (self.pageYOffset) {
                yScroll = self.pageYOffset;
            } else if (document.documentElement && document.documentElement.scrollTop) {
                yScroll = document.documentElement.scrollTop;
            } else if (document.body) {
                yScroll = document.body.scrollTop;
            }
            arrayPageScroll = new Array('', yScroll);
            return arrayPageScroll;
        }
        //获取页面实际大小
        function getPageSize() {
            var xScroll, yScroll;
            if (window.innerHeight && window.scrollMaxY) {
                xScroll = document.body.scrollWidth;
                yScroll = window.innerHeight + window.scrollMaxY;
            } else if (document.body.scrollHeight > document.body.offsetHeight) {
                sScroll = document.body.scrollWidth;
                yScroll = document.body.scrollHeight;
            } else {
                xScroll = document.body.offsetWidth;
                yScroll = document.body.offsetHeight;
            }

            var windowWidth, windowHeight;
            //var pageHeight,pageWidth; 
            if (self.innerHeight) {
                windowWidth = self.innerWidth;
                windowHeight = self.innerHeight;
            } else if (document.documentElement && document.documentElement.clientHeight) {
                windowWidth = document.documentElement.clientWidth;
                windowHeight = document.documentElement.clientHeight;
            } else if (document.body) {
                windowWidth = document.body.clientWidth;
                windowHeight = document.body.clientHeight;
            }
            var pageWidth, pageHeight;
            if (yScroll < windowHeight) {
                pageHeight = windowHeight;
            } else {
                pageHeight = yScroll;
            }
            if (xScroll < windowWidth) {
                pageWidth = windowWidth;
            } else {
                pageWidth = xScroll;
            }
            arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight);
            return arrayPageSize;
        }
//=============popup 结束=============



