var bodyWidth = 0, bodyHeight = 0, scrollLeft = 0, scrollTop = 0;bType=0;var _a=null;
var DBL_PAGE=0;
var DBL_MARGIN=0;
var SYS_DBL_PAGE=0;
var DM_WEATHER=0,DM_POLL=0,DM_EXTMENU=0, LOCKED=0;
function initWinSize(){
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		bodyWidth = window.innerWidth;
		bodyHeight = window.innerHeight;
		scrollLeft= window.pageXOffset;
		scrollTop= window.pageYOffset;
		bType=0;
	}
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		bodyWidth = document.documentElement.clientWidth;
		bodyHeight = document.documentElement.clientHeight;
		scrollLeft= document.documentElement.scrollLeft;
		scrollTop= document.documentElement.scrollTop;
		bType=1;
	}
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		bodyWidth = document.body.clientWidth;
		bodyHeight = document.body.clientHeight;
		scrollLeft= document.body.scrollLeft;
		scrollTop= document.body.scrollTop;
		bType=1;
	}
}
initWinSize();
var stories = new Array();
//SCROLLING FRAME
var iMousePosX=0;var iMousePosY=0;
var iOrigObjTop=0;var iOrigObjLeft=0;
var iDragObjTopDiff=0;var iDragObjLeftDiff=0;
var rightclick=false;var oDragObj=null;
var once=1;var lockLoading=1;
var addedSpace=0;
var topSpacing=0;
var tad='';var bad='';
var ADL_XTRA=0;var ADR_XTRA=0;
var ADS_SPACE=0;

function setFrames(){
	var dif = 64;
	if(bType) {
		document.getElementById('loader').style.height=(bodyHeight-dif)+"px";
		document.getElementById('side').style.height=(bodyHeight-dif)+"px";
	}
}

function initPage(){
	initWinSize();
	window.loader.initWinSize();
	resetPageScroll();
	setFrames();
}

function dragPiece(sourceObject){
	sourceObject.style.cursor="all-scroll";
	var tb=window.loader.document.getElementById("wrapper");
	if(once){
		iOrigObjTop=parseInt(tb.offsetTop);
		iOrigObjLeft=parseInt(tb.offsetLeft);
		once=0;
	}
	iDragObjTopDiff=iMousePosY-iOrigObjTop;iDragObjLeftDiff=iMousePosX-iOrigObjLeft;
	oDragObj=sourceObject;
}
function rePaint(force) {
	var table=window.loader.document.getElementById('panel');
	var not_affected=0;var tdTop=0;var count=0;
	if (table) {
		var idx = 0;
		for (i=0;i<table.rows.length;i++) {
			var tableRow = table.rows[i];
			if (tableRow.cells.length>1) {
				for (j=0;j<tableRow.cells.length;j++){
					idx++;
					tableCell = tableRow.cells[j];
					if(tableCell.innerHTML==""){
						tdTop=parseInt(tableCell.parentNode.offsetTop)+topSpacing;
						if ( (tdTop<bodyHeight+Math.abs(iOrigObjTop)) || (force && force==1)){
							tmpUrl = SLICE_URL;
							if (i % 2 == 0) tmpUrl = SLICE_URL_ALT;
							var res='';
							res += "<img src='"+tmpUrl+tableCell.id+"' ></img>";
							if ( tableCell.className == "blc" && PAGE_NO!=PAGES){
								res += LFC;
							}
							if ( tableCell.className=="brc" && PAGE_NO!=1) {
								res += RFC;
							}
							tableCell.innerHTML=res;
						}
					}
					else not_affected++;
				}
			}
		}
	}
	if (not_affected==idx) lockLoading=0;
}
function scrollHtml(){
	var scrOfX = 0, scrOfY = 0;
	if( typeof( window.loader.pageYOffset ) == 'number' ) {
  		scrOfY = window.loader.pageYOffset;
   		scrOfX = window.loader.pageXOffset;
  	} else if( window.loader.document.body && ( window.loader.document.body.scrollLeft || window.loader.document.body.scrollTop ) ) {
  		scrOfY = window.loader.document.body.scrollTop;
  		scrOfX = window.loader.document.body.scrollLeft;
  } else if( window.loader.document.documentElement && ( window.loader.document.documentElement.scrollLeft || window.loader.document.documentElement.scrollTop ) ) {
  	scrOfY = window.loader.document.documentElement.scrollTop;
    scrOfX = window.loader.document.documentElement.scrollLeft;
  }
	iOrigObjTop=parseInt(scrOfY);
	rePaint();
}
function clearDrawing(){
	if(window.loader.document.getElementById('drawCanvas')) {
		window.loader.document.getElementById('drawCanvas').innerHTML="";
		window.loader.document.getElementById('drawCanvas').style.display="none";
	}
}
function drop(){if(oDragObj){window.loader.document.getElementById('panel').style.cursor="auto";oDragObj=null;}}
function mouseDown(e){
	clearDrawing();
	if (!e) var e=window.loader.event;
	if (e.which) rightclick=(e.which==3);else if (e.button) rightclick=(e.button==2);
	if (rightclick) {
			bMouseRightKeyDown=true;
			if (_a && !SYS_DBL_PAGE) detectStory(e.clientX, e.clientY,0);
			return true;
	}else{
		bMouseLeftKeyDown=true;
	}
	return false;
}

function detectStory(xPos, yPos, redirect){
	var pw = PAGE_WIDTH;
	var banner_extra=0;
	var ratio = document.getElementById('ratios').value;
	if (DBL_PAGE && SYS_DBL_PAGE){
		var bdh=bodyHeight;
		bdh = parseInt(window.loader.bodyHeight / window.parent.ROWS);
		pw = parseInt(bdh * window.parent.COLS * 2);
		ph = parseInt(bdh);
		ratio = parseInt( (ratio * parseInt(bdh * window.parent.COLS)) / PAGE_WIDTH);
	}else if (FIT_HEIGHT==1) {
		var bdh=bodyHeight;
		bdh = parseInt(window.loader.bodyHeight / window.parent.ROWS);
		pw = parseInt(bdh * window.parent.COLS);
		ph = parseInt(bdh);
		ratio = parseInt( (ratio * pw) / PAGE_WIDTH);
	}
	else {
		banner_extra = parseInt(window.loader.document.getElementById('table_top_tr').offsetTop);
	}
	var dif = parseInt((window.loader.bodyWidth - ADL_XTRA - pw)/2);
	if (dif<0) dif=0;
	if (DBL_PAGE && SYS_DBL_PAGE) {
		if ( (PAGE_NO%2!=0 && LANGUAGE=='AR') || (PAGE_NO%2==0 && LANGUAGE!='AR') )
			iOrigObjLeft=0;
	}
	xPos-=iOrigObjLeft;
	yPos-=iOrigObjTop;
	var p = PAGE_NO-1;
	for (var i in _a[p].p){
		var story = getPlan(p, ratio ,i);
		stTop = story.t + banner_extra;
		stBottom = story.b + banner_extra;
		stRight = ADL_XTRA + dif + (pw-story.l);
		stLeft = ADL_XTRA + dif +(pw-story.r);
		if (DBL_PAGE && ( (PAGE_NO%2==1 && LANGUAGE=='AR') || (PAGE_NO%2==0  && LANGUAGE!='AR') ) && SYS_DBL_PAGE ) {
			stRight-=parseInt(bdh * window.parent.COLS);
			stLeft-=parseInt(bdh * window.parent.COLS);
		}
		if (xPos <=stRight && xPos >= stLeft && yPos >= stTop && yPos <= stBottom) {
			if (redirect)window.location='html/story.php?sid='+_a[p].p[i].n;
			else window.loader.markArea(story.l, story.t, story.r, story.b, 1);
			break;
		}
	}
}

function mouseUp(e){
	if (mc) mc();
	if (document.getElementById('pages'))document.getElementById('pages').blur();
	if (!e) var e=window.loader.event;
	if (e.which) rightclick=(e.which==3);else if (e.button) rightclick=(e.button==2);
	drop();
	if (rightclick) {bMouseRightKeyDown=false;}else{bMouseLeftKeyDown=false;}
	return false;
}
function getMouseXYPos(e){
	if (!e) var e=window.loader.event;
	getMouseXY(e.clientX+window.loader.document.body.scrollLeft,e.clientY+window.loader.document.body.scrollTop);
	return false;
}
function getMouseXY(posX, posY){
	iMousePosX=posX;iMousePosY=posY;
	if (DBL_PAGE==0) {
	if(oDragObj!=null){
		var nt=iMousePosY-iDragObjTopDiff;var nw=iMousePosX-iDragObjLeftDiff;
		var pg=document.getElementById('loader');var tb=window.loader.document.getElementById("panel");
		var fh=parseInt(pg.offsetHeight);var ph=parseInt(tb.offsetHeight);var mt=fh-ph;
		var fw=parseInt(pg.offsetWidth);var pw=parseInt(tb.offsetWidth)+ADL_XTRA+ADR_XTRA;
		var mw=fw-pw;
		if(nt>0) nt=0;if(nt<mt-addedSpace) nt=mt-addedSpace;if(nw>0) nw=0;if(nw<mw) nw=mw;
		window.loader.document.getElementById("wrapper").style.top=nt+"px";
		iOrigObjTop=nt;
		if (pw>window.loader.bodyWidth){
			window.loader.document.getElementById("wrapper").style.left=nw+"px";
			iOrigObjLeft=nw;
		}
		if (lockLoading)rePaint();
		clearDrawing();
	}
	}
	return true;
}
function popStory(e){
	if (!e) var e=window.loader.event;
	detectStory(e.clientX, e.clientY,1);
}
function handle(delta){
	if (DBL_PAGE==0) {
		var pg=document.getElementById('loader');var tp=window.loader.document.getElementById("wrapper");
		if (tp.style.top!='')var nt=parseInt(tp.style.top.substring(0, tp.style.top.length-2))+delta;
		else var nt=0;
		var fh=parseInt(pg.offsetHeight);var ph=parseInt(window.loader.document.getElementById("panel").offsetHeight);
		var mt=fh-ph;
		if (tp) {
			if (delta<0){if (nt-20>0) nt=0;else nt=nt-20;}
			else{if (nt+20>0) nt=0;else nt=nt+20;}
			if (nt<mt-addedSpace) nt=mt-addedSpace;
			window.loader.document.getElementById("wrapper").style.top=nt+"px";
			iOrigObjTop=nt;
			clearDrawing();
		}
	}
	if (lockLoading)rePaint();
}
function wheel(event){
	var delta=0;
	if (!event) event=window.loader.event;
	if (event.wheelDelta) {
		delta=event.wheelDelta/120;
		if(window.opera) delta=-delta;
	} else if (event.detail) {delta=-event.detail/3;}
	if (delta) handle(delta);
	if (event.preventDefault) event.preventDefault();
	event.returnValue=false;
}

//BROWSING
SIDEFRAME=1;
function getHeadlines(){
	x_getHeadlines(EDITION_NO, dispHeadlines);
}
function dispHeadlines(res){
	for(var i in res) {
		HEADLINES[i]=new Array();
		HEADLINES[i]['HEADLINE_ID']=res[i].HEADLINE_ID;
		HEADLINES[i]['CONTENT']=res[i].CONTENT;
		HEADLINES[i]['CONTENT']=res[i].CONTENT.replace(/###/, " - ");
		HEADLINES[i]['SECTION_NAME']=res[i].SECTION_NAME;
	}
	document.getElementById('ticker').style.display="inline";
	setHeadline();
}
function nextHeadline(){
	window.clearTimeout(hdlTimer);
	if (++indexHdl == HEADLINES.length) indexHdl=0;
	setHeadline();
}
function prevHeadline(){
	window.clearTimeout(hdlTimer);
	if (--indexHdl == -1) indexHdl=HEADLINES.length-1;
	setHeadline();
}
function setHeadline(){
	document.getElementById('hdlSection').innerHTML=HEADLINES[indexHdl]['SECTION_NAME'];
	document.getElementById('hdlLink').innerHTML="<a href='hd.php?hd="+HEADLINES[indexHdl]['HEADLINE_ID']+"&vw=1' target='loader'>"+HEADLINES[indexHdl]['CONTENT']+"</a>";
	hdlTimer=setTimeout("nextHeadline();", hdltvar);
}
function maximize(){
	clearDrawing();
	if (SIDEFRAME) {
		document.getElementById('side').style.display='none';
		SIDEFRAME=0;
	}else {
		document.getElementById('side').style.display='inline';
		SIDEFRAME=1;
	}
	initPage();
}
var FIT_HEIGHT=0;
var bypass=0;
function fitHeight(){
	if (DBL_PAGE && SYS_DBL_PAGE) {
		return;
	}
	if (window.loader.document.getElementById("wrapper") && FIT_HEIGHT==0) {
		FIT_HEIGHT=1;
		bypass=1;
		setPage(PAGE_NO);
	}
}
function fitWidth(){
	if (DBL_PAGE) {
		dblPage();
		return;
	}
	if (window.loader.document.getElementById("wrapper") && ( (SIDEFRAME && document.getElementById('ratios').value!=BEST_FIT) || (!SIDEFRAME && document.getElementById('ratios').value!=BEST_OFIT) || FIT_HEIGHT==1)  ) {
		clearDrawing();
		FIT_HEIGHT=0;
		var fit=0;
		if (SIDEFRAME) fit=BEST_FIT;
		else {
			fit=BEST_OFIT;
		}
		document.getElementById('ratios').value=fit;
		bypass=1;
		setPage(PAGE_NO);
	}
}
function resetPageScroll(){
	window.loader.scroll(0,0);
	if (window.loader.document.getElementById("wrapper")){
		window.loader.document.getElementById("wrapper").style.top=DBL_MARGIN+"px";
		window.loader.document.getElementById("wrapper").style.left="0px";
	}
	iOrigObjTop=0;iOrigObjLeft=0;
}
function setView(val){
	var value='';var proceed=1;
	switch (val){
		case "+":
			if (document.getElementById('ratios').options[document.getElementById('ratios').selectedIndex+1]) {
				value=document.getElementById('ratios').options[document.getElementById('ratios').selectedIndex+1].value;
				document.getElementById('ratios').value=value;
			}
			else proceed=0;
			break;
		case "-":
			if (document.getElementById('ratios').options[document.getElementById('ratios').selectedIndex-1]) {
				value=document.getElementById('ratios').options[document.getElementById('ratios').selectedIndex-1].value;
				document.getElementById('ratios').value=value;
			}
			else proceed=0;
			break;
		default:
			value=val;
			break;
	}
	if (proceed) {
		document.getElementById('loader').src="page_loader.php?edition="+EDITION_NO+"&page="+PAGE_NO+"&view="+value;
	}
}
function setPage(val){
	var value='';var proceed=1;
	switch (val){
		case "first":
			value=1;
			break;
		case "prev":
			if (PAGE_NO>1) {
				value=PAGE_NO-1;
				if (DBL_PAGE) {
					if (value%2==1)
						value=PAGE_NO-1;
					else value=PAGE_NO-2;
				}
			}
			else proceed=0;
			break;
		case "next":
			if (PAGE_NO<PAGES) {
				value=PAGE_NO+1;
				if (DBL_PAGE) {
					if (value%2==1)
						value=PAGE_NO+2;
					else value=PAGE_NO+1;
				}
			}
			else proceed=0;
			break;
		case "last":
			value=PAGES;
			break;
		default:
			if (val>0 && val<=PAGES && (val != PAGE_NO || bypass)) value=val;
			else proceed=0;
			break;
	}
	if (proceed) {
		bypass=0;
		PAGE_NO=value;
		document.getElementById('pages').value=PAGE_NO+" of "+PAGES;

			var fwidth = window.loader.bodyWidth-ADL_XTRA;
			var link="page_loader.php?edition="+EDITION_NO+"&page="+value+"&view="+document.getElementById('ratios').value+"&dbl="+DBL_PAGE+"&sh="+document.getElementById('loader').offsetHeight+"&sw="+fwidth;
			if (FIT_HEIGHT==1) link += "&fit=1";
			document.getElementById('loader').src=link;
	}
}
function dblPage(){
	DBL_PAGE=(DBL_PAGE+1)%2;
	var elem = document.getElementById("btn_dp");
	if (DBL_PAGE) {
		elem.className = "btn_dp_on";
		elem.title="single page";
	}
	else {
		elem.className = "btn_dp";
		elem.title="two-up";
	}
	if (PAGE_NO!=1 && PAGE_NO!=PAGES) {
		var fwidth = window.loader.bodyWidth-ADL_XTRA;
		var link="page_loader.php?edition="+EDITION_NO+"&page="+PAGE_NO+"&view="+document.getElementById('ratios').value+"&dbl="+DBL_PAGE+"&sh="+document.getElementById('loader').offsetHeight+"&sw="+fwidth;
		document.getElementById('loader').src=link;
	}
}
function flip_in(elem){
	window.loader.oc=0;
	if(window.loader.document.getElementById(elem))
		window.loader.document.getElementById(elem).style.visibility="visible";
}
function flip_out(elem){
	window.loader.oc=1;
	if(window.loader.document.getElementById(elem))
		window.loader.document.getElementById(elem).style.visibility="hidden";
}
function onPage(pageNo){
	if (PAGE_NO!=pageNo) {
		PAGE_NO=pageNo;
		document.getElementById('pages').value=PAGE_NO+" of "+PAGES;
	}
}
function alignPage(side) {
	if (side=="left")
		window.loader.document.getElementById("wrapper").style.left="0px";
	else {
		var tb=window.loader.document.getElementById("panel");
		var pg=document.getElementById('loader');
		var pw=parseInt(tb.offsetWidth)+ADL_XTRA+ADR_XTRA+10;
		var fw=parseInt(pg.offsetWidth);
		var mw=fw-pw;
		if (mw<0)	window.loader.document.getElementById("wrapper").style.left=mw+"px";
	}
}
function savePage(){
	if (AVAIL_PDF) {
		document.getElementById('bin').src="bin/send_pdf.php?edition="+EDITION_NO+"&page="+PAGE_NO;
	}
	else vAlert("Sorry, PDF file does not exist");
}
function noPage(){
	vAlert("Sorry, PDF file does not exist");
}
function vAlert(msg) {
	document.getElementById('alert').innerHTML=msg;
	document.getElementById('alert').parentNode.style.display="inline";
	setTimeout("document.getElementById('alert').parentNode.style.display='none';", 4000);
}
function setPageFilter(elem){elem.focus();elem.select();}
function resetPageFilter(elem){
	var val=elem.value;
	if(!(val==NaN)){val=parseInt(val);setPage(val);}
}
function scrollThumbs(pno){
	window.side.scroll(0,window.side.document.getElementById('thmb'+pno).offsetTop-topSpacing-36);
	window.side.osd();
}
function winLoad(){getHeadlines();}

function clearLRads(){
	if (window.loader.document.getElementById("left_banner")) window.loader.document.getElementById("left_banner").style.display="none";
	if (window.loader.document.getElementById("right_banner")) window.loader.document.getElementById("right_banner").style.display="none";
	ADL_XTRA=0;
	ADR_XTRA=0;
}
document.onmouseup=mc;
window.onload=winLoad;

function alignDDiv(div, elem){
	if (div) {
		var flex=0;
		parElem =elem;
		linkW = parElem.offsetWidth;
		var divWidth = parseInt(div.style.width.substring(0, div.style.width.length-2 ));
		while (parElem.offsetLeft==0) {
			if (parElem.parentNode) {
				parElem = parElem.parentNode;
			}
			else {
				break;
			}
		}
		var left_m = parElem.offsetLeft;
		if (!left_m) left_m=0;
		if (MLANG=='AR') {	
			flex = left_m -divWidth + linkW;
			if (flex < 0) {
				flex = left_m;
			}		
		}
		else {
			flex = left_m;
			if ( (left_m + divWidth) > bodyWidth) {
				flex = left_m - divWidth + linkW;
			}
		}
		div.style.left = flex+"px";
	}
}
var RESET_FLASH=1;
function resetFlashAds(){
	var t = window.loader.document.getElementsByTagName("embed");
	for (var i in t) {
		if (t[i].parentNode && t[i].parentNode.tagName=="DIV") {
		var tmp=t[i].parentNode.innerHTML;
		t[i].parentNode.innerHTML = tmp.substring(0, tmp.length-1)+' wmode="transparent">';
		}
	}
	RESET_FLASH=0;
}
var SUBMENU=0;
function weather(elem){
	if (RESET_FLASH)resetFlashAds();
	if (DM_WEATHER==1) {
		document.getElementById("weather").style.display="none";
		DM_WEATHER=0;
	}
	else {
		if (document.getElementById("poll")) {
			document.getElementById("poll").style.display="none";
			DM_POLL=0;
		}
		if(document.getElementById("extmenu")) {
			document.getElementById("extmenu").style.display="none";
			DM_EXTMENU=0;
		}
		alignDDiv(document.getElementById("weather"), elem);
		document.getElementById("weather").style.display="inline";
		DM_WEATHER=1;
	}
}
function poll(elem){
	if (RESET_FLASH)resetFlashAds();
	if (DM_POLL==1) {
		document.getElementById("poll").style.display="none";
		DM_POLL=0;
	}
	else {
		if (document.getElementById("weather")) {
			document.getElementById("weather").style.display="none";
			DM_WEATHER=0;		
		}
		if(document.getElementById("extmenu")) {
			document.getElementById("extmenu").style.display="none";
			DM_EXTMENU=0;
		}
		alignDDiv(document.getElementById("poll"), elem);
		document.getElementById("poll").style.display="inline";
		DM_POLL=1;
	}
}
function md(elem){
	if (RESET_FLASH)resetFlashAds();
	if (DM_EXTMENU==1) {
		document.getElementById("extmenu").style.display="none";
		DM_EXTMENU=0;
	}
	else {
		if (document.getElementById("weather")) {
			document.getElementById("weather").style.display="none";
			DM_WEATHER=0;		
		}
		if (document.getElementById("poll")) {
			document.getElementById("poll").style.display="none";
			DM_POLL=0;
		}
		alignDDiv(document.getElementById("extmenu"), elem);
		document.getElementById("extmenu").style.display="inline";
		DM_EXTMENU=1;
	}
}
function mc() {
	if (!SUBMENU && !LOCKED){
		if (DM_EXTMENU==1) {
			document.getElementById('extmenu').style.display="none";
			DM_EXTMENU=0;
		}
		if (DM_WEATHER==1) {
			document.getElementById('weather').style.display="none";
			DM_WEATHER=0;
		}
		if (DM_POLL==1) {
			document.getElementById('poll').style.display="none";
			DM_POLL=0;
		}
	}
}