/* preload */
preload = {}
preload.img = new Image();
preload.img.src = "/images/sb_sound_on.png";
preload.img2 = new Image();
preload.img2.src = "/images/sb_show_read.png";
preload.img3 = new Image();
preload.img3.src = "/images/sb_hide_read.png";
preload.img4 = new Image();
preload.img4.src = "/images/nl_hide_read.gif";
preload.img4 = new Image();
preload.img4.src = "/images/nl_show_read.gif";
preload.img5 = new Image();
preload.img5.src = "/images/icons/default_ico.gif";
preload.imgs = [];
/* GUI */
var images_path = '/images/';
var nlH = 0;
layout = {};
layout.init=function(){
	layout.wrap = xGetElementById("wrap");
	layout.binder = xGetElementById("binder");

	layout.sb = xGetElementById("sidebar");
	layout.sbsep = xGetElementById("sidebarsep");
	layout.cats = xGetElementById("categories");
	layout.fl = xGetElementById("feedslist");

	layout.content = xGetElementById("content");
	layout.content.intleft = xLeft(layout.content);

	layout.nlsep = xGetElementById("contentsep");
	layout.nlcap = xGetElementById("nl_caption");
	layout.nl = xGetElementById("newslist");

	layout.nccap = xGetElementById("nl_caption");
	layout.nc = xGetElementById("newscontent");
	layout.copy = xGetElementById("copy");
}

var gui= {};
	gui.space = 5;
	gui.offset = 0;
	if(window.opera) {
		document.write('<style>body{margin:2px 2px 2px 2px;}</style>');
		gui.offset = 1;
	}
	gui.wrappadding = 3;
	gui.bodypaddingbottom = 21;
	gui.bodypadding = 14;
	gui.init = 1;
gui.div_loading=function(id){
	var div=xGetElementById(id);
	purge(div);
	div.innerHTML='<img src="/images/loading.gif" alt="loading" style="margin:0 10px;" /> loading';
}
gui.div_clear=function(id){
	var element=xGetElementById(id);
	purge(element);
	element.innerHTML="&nbsp;";
}
gui.div_top=function(id){
	var element=xGetElementById(id);
	element.scrollLeft=0;
	element.scrollTop=0;
}
gui.resize=function() {
	var screenWidth=xClientWidth() - (gui.offset ? 2 : 0);
	var screenHeight=xClientHeight() + 4;
	xTop(layout.copy, screenHeight-20);

	/* move wrap */
	xHeight(layout.wrap, screenHeight - gui.bodypadding - gui.bodypaddingbottom + 2);
	layout.wrap.width = xWidth(layout.wrap, screenWidth - gui.bodypadding - (xIE4Up ? 8 : 4));
	/* get params */
	layout.sb.top = xOffsetTop(layout.sb);
	layout.sb.left = xOffsetLeft(layout.sb);

	layout.nlcap.height = xHeight(layout.nlcap);
	layout.nccap.height = xHeight(layout.nccap);
	/* set state settings */
	layout.sb.width = xWidth(layout.sb, state.sbWidth);
	if(gui.init) {
		layout.sb.initWidth = xWidth(layout.sb, state.sbWidth);
	}
	layout.nl.height = xHeight(layout.nl, state.nlHeight - layout.nlcap.height);
	//Move sidebar separator to a place
	xLeft(layout.sbsep, layout.sb.left + layout.sb.width - gui.offset);
	//Set Content width
	layout.content.width = xWidth(layout.content, layout.wrap.width - state.sbWidth - gui.space - 7);
	//Set contentsep width
	xWidth(layout.nlsep, layout.content.width);
	//Move news list separator to a place
	xTop(layout.nlsep, layout.nl.height + layout.nlcap.height);
	//Move news list separator to a place
	if(xLeft(layout.content)) {
		xLeft(layout.content, state.sbWidth - layout.sb.initWidth + gui.space + 2);
	} else {
		xLeft(layout.content, gui.space + 2);
	}
	//calculate content height
	layout.content.height = screenHeight - layout.sb.top - gui.bodypaddingbottom - gui.wrappadding - 2 - layout.nlcap.height;
	//Set feedslist height
	//separator fixes
	if(xLeft(layout.sbsep) != (layout.sb.left+state.sbWidth)) {
		xMoveTo(layout.sbsep,(layout.sb.left+state.sbWidth-gui.offset),(layout.sb.top - layout.wrap.top+14));
	}
	xTop(layout.nlsep, layout.nl.height+xHeight(layout.nlcap));

	xWidth(layout.fl, state.sbWidth - gui.offset);
	xWidth(layout.cats, state.sbWidth - gui.offset);

	layout.cats.height = xHeight(layout.cats);

	var sbsepHeight = xHeight(layout.sbsep, layout.content.height + layout.nlcap.height - 11);
	xHeight(layout.fl, sbsepHeight-36-layout.cats.height);

	//Set message content height
	xHeight(layout.nc, layout.content.height - layout.nccap.height - layout.nl.height - 17);

	//enable separators drug and drop
	separator.create(layout.sbsep,"v");
	xEnableDrag(layout.sbsep,separator.move_start,separator.on_move,separator.move_end);
	separator.create(layout.nlsep,"h");
	xEnableDrag(layout.nlsep,separator.move_start,separator.on_move,separator.move_end);
}
// separator
var separator={};
separator.create=function(sep,orientation){
	sep.orientation=orientation||"h";
}
separator.move_start=function(sep,x,y){
	sep.total_move_x=0;
	sep.total_move_y=0;
	sep.sep_v=xOffsetTop(sep);
	sep.sep_h=xOffsetLeft(sep);
	//ccc('<span style="color:green;">'+sep.sep_h+'</span>');
	gui.no_auto_resize=1;
}
separator.on_move=function(sep,dx,dy){
	ccc('onMove');
	if(sep.orientation=="h"){
		xMoveTo(sep,sep.sep_h,xTop(sep)+dy);
		state.nlHeight=xTop(sep);
		if(state.nlHeight<state.nlMinHeight)
			state.nlHeight=state.nlMinHeight;
		if(state.nlHeight>state.nlMaxHeight)
			state.nlHeight=state.nlMaxHeight;
	} else {
		xMoveTo(sep,xLeft(sep)+dx,sep.sep_v);
		state.sbWidth=xLeft(sep);
		if(state.sbWidth < state.sbMinWidth)
			state.sbWidth = state.sbMinWidth;
		if(state.sbWidth > state.sbMaxWidth)
			state.sbWidth = state.sbMaxWidth;
	}
}
separator.move_end=function(sep,dx,dy){
	ccc('move_end');
	gui.no_auto_resize=0;
	gui.resize();
}
// /separator
window.onload=function(forceinit){
	state=storage.load_object("state");
	if(state){
		curFeeds=storage.load_object("curFeeds");
		curReadFeeds=storage.load_object("curUnreadFeeds");
		curNews=storage.load_object("curNews");
		curReadNews=storage.load_object("curUnreadNews");
		current_article=storage.load_object("curArticle");
	} else {
		state={};
		//setTimeout("help.draw();gui.resize();",3000);
	}
	if(state.feed_id==undefined)
		state.feed_id=-1;
	if(state.feed_array_index===undefined)
		state.feed_array_index=-1;
	if(state.news_id==undefined)
		state.news_id=0;
	if(state.news_array_index===undefined)
		state.news_array_index=-1;
	if(state.flistShowRead===undefined)
		state.flistShowRead=0;
	if(state.nlistShowRead===undefined)
		state.nlistShowRead=0;
	if(state.sbWidth==undefined)
		state.sbWidth=228+gui.offset;
	if(state.nlHeight==undefined)
		state.nlHeight=140;
	if(state.showOrig===undefined)
		state.showOrig=0;
	if(state.cat_id==undefined) {
		state.cat_id=1;
		state.cat_array_index=0;
	}
	if(state.tag==undefined)
		state.tag="";
	if(state.soundOn==undefined)
		state.soundOn=1;
	if(state.uberCat==undefined)
		state.uberCat=0;//all adult uber cat
	if(state.uberCatshow==undefined)
		state.uberCatshow=[1,0,0];
	storage.save_object("state",state);
	state.nlMinHeight = 60;
	state.nlMaxHeight = 480;
	state.sbMinWidth = 228+gui.offset;
	state.sbMaxWidth = 480;
	if(forceinit != true) {
		gui.init=1;
	} else {
		gui.init=0;
	}
	state.curCatFeedsCount=0;
	state.feedUnreadCount = [0,0];
	hat.register_buttons();

	feeds_list.fetch_and_draw();
	feeds_list.register_buttons();

	news_list.fetch_and_draw();
	news_list.register_buttons();
	article_view.register_buttons();
	//messages.fetch_and_draw();
	layout.init();
	hide_news(true);
	gui.resize();
	gui.init = 0;
	xrefresh(true);
	article_view.fetch_and_draw();
	if(subscribe) {
		xform.subscribe(subscribe);
	}
	xVisibility(layout.wrap, true)
}

function c(message){
	if(0){
		document.getElementById("console").innerHTML="<br />"+message+document.getElementById("console").innerHTML;
	}
}
function ccc(message){
	if(0) {
		document.getElementById("console2").innerHTML="<br />"+message+document.getElementById("console2").innerHTML;
	}
}


