function updateScrollbar(){
	try{
		var scrollbar = getFoxWBScrollbar('scrollbar');
		if (scrollbar){
			scrollbar.update();	
		}
	}catch(e){
		WriteLog('RSSUpdateScrollbar', e.message);
	}
}
function updateHorzScrollbar(){
	try{
		var scrollbar = getFoxWBScrollbar('horzScrollbar');
		if(scrollbar){
			scrollbar.update();
		}
	}catch(e){
		WriteLog('RSSUpdateHorzScrollbar', e.message);
	}
}
function setupHorzScrollbar(obj){
	try{
		var scrollbar = getFoxWBScrollbar('horzScrollbar');
		if (!scrollbar){
			scrollbar = createFoxWBScrollbar({
				id: 'horzScrollbar',
				Parent: 'mail_frame',
				HS: true,
				HSTarget: obj
			});
			scrollbar.setHorzOffset(0, 0, -17, 0);
			scrollbar.fixedHorzScrollbar(true);
		}
	}catch(e){
		WriteLog('RSSSetupHorzScrollbar', e.message);
	}
}

function AdjustFrameSize(){
	try{
		var height = document.documentElement.clientHeight;
		$('#mail_session_container').height(height);
	}catch(e){
		WriteLog('RSSAdjustFrameSize', e.message);
	}
}

function AdjustContentSize(){
	try{
		var obj = document.getElementById('iframeObj');
		var doc = obj.contentWindow.document;
		var height = doc.documentElement.scrollHeight;
		var documentHeight = document.documentElement.clientHeight;
		var counter = 0;
		var maxCounter = 100;
		while (height != doc.documentElement.scrollHeight){
			height = doc.documentElement.scrollHeight;
			if (counter > maxCounter) break;
			else counter ++;
		}
		//minheight
		var mailBody = document.getElementById('mail_body');
		if (mailBody){
			$(mailBody).css('min-height', (documentHeight-90)+'px');
		}
		//
		$(obj).height(height);
	}catch(e){
		WriteLog('RSSAdjustContentSize', e.message);
	}
}

function setupVertScrollbar(){
	try{
		var scrollbar = getFoxWBScrollbar('scrollbar');
		if (!scrollbar){
			scrollbar = createFoxWBScrollbar({
				id:'scrollbar',
				Parent: 'mail_frame',
				VS: true,
				VSTarget: $('#mail_session_container')
			});
		}
	}catch(e){
		WriteLog('RSSSetupVertScrollbar', e.message);
	}
}

function UpdateBody(){
	try{
		releaseAllFoxWBScrollbar();
		AdjustFrameSize();
		setupVertScrollbar();
		scrollbar.update();
	}catch(e){
		WriteLog('RSSUpdateBody', e.message);
	}
}

function RssClear(){
	try{
		//document.selection.empty();
	}catch(e){
		WriteLog('RSSClear', e.message);
	}
}

function doOpenURL(){
	try{
		var obj = $('#mail_info_rss_details');
		var url = obj.attr('url');
		invokeAppFunction('OpenRssLink', {url: url});
	}catch(e){
		//alert(e.message);
		WriteLog('RSSDoOpenURL', e.message);
	}
}

$(document).ready(function(){
	try{
		registerJSFunction('UpdateBody', UpdateBody);
		registerJSFunction('RssClear', RssClear);
		$(document).delegate('#mail_info_rss_details', 'click', doOpenURL);
		//
		setupFoxWBScrollbar();
		initFoxWBScrollbar();	
		$(window).resize(function(){
			try{
				AdjustFrameSize();
				AdjustContentSize();
				updateScrollbar();
				updateHorzScrollbar();
			}catch(e){
				WriteLog('RSSResize', e.message);
			}
		});
		AdjustFrameSize();
		setupVertScrollbar();
		updateScrollbar();
	}catch(e){
		WriteLog('RSSReady', e.message);
	}
});

function iframeReadyStateChange(obj){
	try{
		if (obj.readyState === 'interactive'){
			setTimeout(function(){
				AdjustContentSize();
				updateScrollbar();
				setupHorzScrollbar($(obj));
				updateHorzScrollbar();
			}, 100);
		}else if (obj.readyState === 'complete'){
			//
			var doc = obj.contentWindow.document;
			var rssDetails = doc.getElementById('rss_details');
			if (rssDetails){
				var detailsBtn = document.getElementById('mail_info_rss_details');
				if (detailsBtn){
					//$(detailsBtn).attr('url', rssDetails.href);
					detailsBtn.innerHTML = '阅读原文';
				}
			}
			//mail_info_rss_details
		}
	}catch(e){
		WriteLog('RSSReadyStateChange', e.message);
	}
}

function iframeLoad(obj){
	try{
		if ($(obj).attr('loadState') === 'unload'){
			$(obj).attr('loadState', 'loading'); 
			var content = invokeAppFunction('GetRSSContent', {});
			obj.contentWindow.document.write(content);
			obj.contentWindow.document.close();
			
			var body = $(obj.contentWindow.document.body);
			body.bind('mousedown',function(){
				body.focus();
				body.unbind('mousedown');	
			});
		}else{
			AdjustContentSize();
			updateScrollbar();
			setupHorzScrollbar($(obj));
			updateHorzScrollbar();
		}
	}catch(e){
		WriteLog('RSSIFrameLoad', e.message);
	}
}