﻿// JScript File
// To be renamed as defowindow on Aug 18th,2006
// Rewritten with jquery-1.3.2.js on July 27th ,2009, jquery lib should be required

var x0=0,y0=0,x1=0,y1=0;
var ismove=false;
var maxIndex=500;
var scroll_top_start=0;/////模式对话框初始化时的滚动条位置
var dlg_post_top=0;//////模式化对话框的初始位置
var dic = new Dictionary();///////用于保存各个对话框的拖动状态
//////新的模式对话框
function popmodulewindow(url, title, divid, dlgwidth, dlgheight) {
    var sizearray = getPageSize();
    var outer_div_width = sizearray[0];
    var outer_div_height = document.documentElement.scrollHeight;
    //alert("width:" + outer_div_width + ",height:" + outer_div_height);
    //////外面的遮挡层
    var dlg_outer = $("<div id='" + divid + "' style='width:" + outer_div_width + "px;height:" + outer_div_height + "px;z-index:9999;background-color:#e3e3e3;position:absolute;border:solid 1px #64B7D7;text-align:center;vertical-align:middle;padding:0px;opacity:0.95;filter:alpha(opacity=95);'></div>");
    dlg_outer.css("left", 0);
    dlg_outer.css("top", 0);
    dlg_outer.css("border", "solid 1px menu");
    ///////窗体部分
    var scrolltop = document.documentElement.scrollTop;
    var dlg_start_left = parseInt((outer_div_width - dlgwidth) / 2);
    var dlg_start_top = parseInt((sizearray[3] - dlgheight) / 2) + scrolltop;
    scroll_top_start = scrolltop;
    dlg_post_top = dlg_start_top;
    var dlg_wnd = $("<div style='width:" + dlgwidth + "px;height:" + dlgheight + "px;z-index:9999;background-color:#e3e3e3;position:absolute;border:solid 1px #64B7D7;text-align:center;vertical-align:middle;padding:0px;opactity=1.0'></div>");
    dlg_wnd.css("left", dlg_start_left);
    dlg_wnd.css("top", dlg_start_top);
    dlg_wnd.css("border", "solid 1px #3169B5");
    dlg_wnd.css("background-color", "#ffffff");
    dlg_outer.append(dlg_wnd);
    ///////标题栏
    var dlg_title = $("<div style='width:100%;height:20px;background-color:#ffffff;text-align:left;border-bottom:solid 1px #3169B5;'><div style='float:left;padding-left:5px;padding-top:3px;' class='divwindow_title' style='color:black;font-weight:bolder;'>&nbsp;" + title + "</div><div style='float:right;width:30px;text-align:right;padding-right:5px;'><a href=\"javascript:closemodulewindow('" + divid + "')\" title='close window' class='whitebutton'><img src='/images/close_hover.gif' border='0'/></a></div></div>");
    dlg_wnd.append(dlg_title);
    ///////内容区
    var dlg_iframe = $("<div style='width:100%;'></div>");    
    dlg_iframe.html("<iframe width=" + (dlgwidth - 4) + " height=" + (dlgheight - 23) +
        "marginwidth='0' marginheight='0' " +
        " src=" + url + " frameborder=0 allowtransparency='true' scrolling='Auto' style='border:none;padding:0px;margin:0px;'></iframe>");
    dlg_wnd.append(dlg_iframe);
    ///////
    var hd_body = $('body');
    hd_body.append(dlg_outer);
    //////滚动时重设位置
    $(window).scroll(function() {
            var scrolltop = document.documentElement.scrollTop;
            var offset = scrolltop - scroll_top_start;
           dlg_wnd.css("top", dlg_post_top + offset);
    });

}

//////关闭模式对话框
function closemodulewindow(divid)
{
    //alert(divid);
    var dlg_div=window.top.document.getElementById (divid);  
    //alert(dlg_div);          
    if (dlg_div!=null)
    {
        window.top.document.body.removeChild(dlg_div);
    }
}

///////非模式对话框
function popmodulelesswindow(url, title, divid, divposid, dlgwidth, dlgheight) {
    var divpos = $("#" + divposid);
    if (divpos.length == 0) {
        alert('pos ctrl is nothing');
        return;
    }
    //////窗体
    var dlg_div = $("#"+divid);
    if (dlg_div.length>0) {
        return;
    }
    maxIndex++;
    var dlg_wnd =
        $("<div id='" + divid + "' style='width:" + dlgwidth + "px;height:" + dlgheight + "px;z-index:" + maxIndex + ";background-color:#e3e3e3;position:absolute;border:solid 1px #3169B5;text-align:center;vertical-align:middle;'></div>");
    if (divpos.offset().left - dlgwidth > 0) {
        dlg_wnd.css("left", divpos.offset().left - dlgwidth + 20);
    }
    else {
        dlg_wnd.css("left", divpos.offset().left);
    }
    dlg_wnd.css("top", divpos.offset().top);
    dlg_wnd.css("border", "solid 1px #3169B5");
    dlg_wnd.css("background-color", "#ffffff");
    dlg_wnd.mousedown(function(e) {
        startdrg2(dlg_wnd,e);
    });
    dlg_wnd.mouseup(function() {
        stopdrg2(dlg_wnd);
    });
    dlg_wnd.mousemove(function(e) {
        drg2(dlg_wnd,e);
    });
    ///////标题栏
    var dlg_title = $("<div style='width:100%;height:20px;background-color:#ffffff;text-align:left;border-bottom:solid 1px #3169B5;'><div style='float:left;padding-left:5px;padding-top:3px;' class='divwindow_title' style='color:black;font-weight:bolder;'>&nbsp;" + title + "</div><div style='float:right;width:30px;text-align:right;padding-right:5px;'><a href=\"javascript:closemodulewindow('" + divid + "')\" title='close window' class='whitebutton'><img src='/images/close_hover.gif' border='0'/></a></div></div>");
    dlg_wnd.append(dlg_title);
    ///////内容区
    var dlg_iframe = $("<div style='width:100%;'></div>");
    dlg_iframe.html("<iframe width=" + (dlgwidth - 4) + " height=" + (dlgheight - 23) +
        "marginwidth='0' marginheight='0' " +
        " src=" + url + " frameborder=0 allowtransparency='true' scrolling='No' style='border:none;padding:0px;margin:0px;'></iframe>");
    dlg_wnd.append(dlg_iframe);
    ///////
    hd_body = $("body");
    hd_body.append(dlg_wnd);
}
/////开始拖动，鼠标按下
function startdrg2(obj, e) {
    if ($.browser.msie) {
        obj[0].setCapture();
    }
    getfocus2(obj);
    obj.css("border", "solid 1px #80C0FF");
    x0 = e.clientX;
    y0 = e.clientY;
    x1 = obj.offset().left;
    y1 = obj.offset().top;
    dic.put("dlg_" + obj.attr("id"), true);
}
/////结束拖动，鼠标释放
function stopdrg2(obj) {
    var item = dic.fetch("dlg_" + obj.attr("id"));
    if (item == null || !item.value)
        return; 
    obj.css("border", "solid 1px #3169B5");
    //ismove = false;
    dic.put("dlg_" + obj.attr("id"), false);
    if ($.browser.msie) {
        obj[0].releaseCapture();
    }
}
//////拖动中
function drg2(obj,e) {
    var item = dic.fetch("dlg_" + obj.attr("id"));
    if (item==null || !item.value)
        return; 
    obj.css("left", x1 + e.clientX - x0);
    obj.css("top", y1 + e.clientY - y0);
}


////设置窗体顺序
function getfocus2(obj) {
    maxIndex++;
    obj.css("z-index", maxIndex);
}
/////关闭非模式对话框
function closemodulelesswindow(divid)
{
    var dlg_div=document.getElementById (divid);  
    if (dlg_div!=null)
    {
        document.body.removeChild(dlg_div);
    }
}


//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
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){ // 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;
	if (self.innerHeight) {	// all except Explorer
		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;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}



/////产生一个添加好友的对话框
function addfrienddialog(userid)
{
    popmodulewindow('http://www.pkone.cn/user/friend_new_dialog.aspx?userid='+userid,'添加为我的好友','wnd_newfriend',550,230)
    //popmodulewindow('http://localhost:56893/user/friend_new_dialog.aspx?userid='+userid,'添加为我的好友','wnd_newfriend',550,230)
}

//////region:要构造一个类似于字典的表数据类型
////////////////////////一行字典数据
function DictionaryItem(_key, _value) {    
    this.key = _key;
    this.value = _value;
}
///////构造函数
function Dictionary() {
    this.lastindex = 0;
    this.table = new Array();
}

///////添加或者修改
Dictionary.prototype.put = function(key, value) {
    var index = this.get_index(key)
    if (index >= 0) {
        this.table[index].key = key;
        this.table[index].value = value;
    }
    else {
        this._add(key,value);
    }
};
////通过键值查找一行
Dictionary.prototype.fetch = function(key) {
    var index = this.get_index(key);
    if (index >= 0)
        return this.table[index];
    else
        return null;
};
/////添加一行
Dictionary.prototype._add = function(key, value) {

    var isexisted = this._check_key(key)
    if (isexisted) {
        alert('已经存在');
        return;
    }
    var item = new DictionaryItem(key, value);
    this.table.push(item);
    this.lastindex++;
};
/////检查键值存在
Dictionary.prototype._check_key = function(_key) {
    if (this.get_index(_key) >= 0) {
        return true;
    }
    else
        return false;
};
////查找键值所在的索引位置
Dictionary.prototype.get_index = function(_key) {
    for (var a = 0; a < this.table.length; a++) {
        if (this.table[a].key == _key) {
            return a;
        }
    }
    return -1;

};
//////endregion