/**
  * window for jiayuan.com profile page by Yaosine
  */

//简化document.getElementById
var getById = function(id){
	  if (typeof(id) != "string" || id == "") return false;
	  if (document.getElementById) return document.getElementById(id);
	  if (document.all) return document.all(id);
};

//弹出层 title:标题，url:地址，msg:消息
function openWindow(title,msg,url,width,height){ 
	
	if(!width) width = 400;//默认宽度
	if(!height) height = 200;//默认高度
	if(!title) getById('open_window_head').style.display = 'none';
	setDomainForIframe();

	if(msg != ''){
		getById('open_window_content').innerHTML = msg;
	}
	else if(url != ''){
		getById('open_window_content').innerHTML = '<iframe id="open_window_iframe" scrolling="no" width="'+width+'" height="'+height+'" allowTransparency="true" frameborder="0"></iframe>';
		getById('iframe_loading').style.left = String(width/2 - 104)+'px';
		getById('iframe_loading').style.top = String(height/2 -10)+'px';
		iframeLoad(getById('open_window_iframe'));
		getById('open_window_iframe').src = url; 
	}
	else{
		alert('参数错误');
		return false;
	}
	
	var scrolltop = document.documentElement.scrollTop || window.pageYOffset || 0; //for webkit
	var scroll_height = String(scrolltop + (document.documentElement.clientHeight - height)/2) + "px";
	var _scrollWidth = Math.max(document.body.scrollWidth,document.documentElement.scrollWidth);
	var _scrollHeight = Math.max(document.body.scrollHeight,document.documentElement.scrollHeight);

	getById('bg').style.display = 'block';	
	getById('bg').style.width = _scrollWidth + "px";	
	getById('bg').style.height = _scrollHeight + "px";	

	getById('open_window').style.top = scroll_height; 
	getById('open_window').style.left = String(document.body.clientWidth/2-(width)/2) + "px";
	getById('open_window').style.width = String(width) + "px";
	getById('open_window').style.display = '';
	getById('open_window_title').innerHTML = title;

	//跟随滚动
	/*window.onscroll = function(){
		scroll_height = String(document.documentElement.scrollTop + (document.documentElement.clientHeight - height)/2) + "px";
		getById('open_window').style.top = scroll_height;
	}*/
}

function openWindowById(obj,width,height){ 
	
	if(!width) width = 400;//默认宽度
	if(!height) height = 200;//默认高度

	var scrolltop = document.documentElement.scrollTop || window.pageYOffset || 0; //for webkit
	var scroll_height = String(scrolltop + (document.documentElement.clientHeight - height)/2) + "px";
	var _scrollWidth = Math.max(document.body.scrollWidth,document.documentElement.scrollWidth);
	var _scrollHeight = Math.max(document.body.scrollHeight,document.documentElement.scrollHeight);
	getById('bg').style.display = 'block';	
	getById('bg').style.width = _scrollWidth + "px";	
	getById('bg').style.height = _scrollHeight + "px";	

	getById(obj).style.width = String(width) + "px";
	getById(obj).style.left = String(document.body.clientWidth/2-(width)/2) + "px";
	getById(obj).style.top	= scroll_height; 
	getById(obj).style.display = '';

	//跟随滚动
	/*window.onscroll = function(){
		scroll_height = String(document.documentElement.scrollTop + (document.documentElement.clientHeight - height)/2) + "px";
		getById(obj).style.top	= scroll_height;
	}*/
}

//关闭弹出窗
function closeWindow(){
	getById('bg').style.display = 'none';	
	getById('open_window').style.display = 'none';
}

//兼容旧方法
function close_popup(flag){
	closeWindow();
}

function popup_bgdiv(url, msg, flag, _width, _height, type)
{ 
	if(!_height) _height = 600;//默认高度 
	if(_width == 651) _width = 612;
	openWindow('', msg, url, _width, _height);
}

//兼容礼物页
function close_window(){
	closeWindow();
}

//关闭指定ID弹出窗
function closeWindowById(obj){
	getById('bg').style.display = 'none';	
	getById(obj).style.display = 'none';
}

//iframe预加载
function iframeLoad(iframe){
	var iframe_loading = getById('iframe_loading');
	iframe.style.display = 'none';
	iframe_loading.style.display = 'block';
	if (iframe.attachEvent){    
		iframe.attachEvent("onload", function(){        
			iframe_loading.style.display = 'none';
			iframe.style.display = 'block';
		});
	} else {    
		iframe.onload = function(){        
			iframe_loading.style.display = 'none';
			iframe.style.display = 'block';
		};
	}
	iframe.style.display = 'block';
}
