var bodyWidth = 0, bodyHeight = 0, scrollLeft = 0, scrollTop = 0;bType=0;
var _a=null;
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;
        }
        //alert (typeof( window.innerWidth ));
        //alert (bodyWidth);
        //alert (bodyHeight);
        //alert (scrollLeft);
        //alert (scrollTop);
        //alert (bType);

}
//initWinSize();

function setFrames(){
        var sideDif = 130;
        var loaderDif = 130;
        if(bType) {
                if (document.getElementById('loader')) {
                    document.getElementById('loader').style.height=(bodyHeight-loaderDif)+"px";

                }
                if (document.getElementById('side')) {
                    document.getElementById('side').style.height=(bodyHeight-sideDif)+"px";
                }
        }
}

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

function resetPageScroll(){
        window.loader.scroll(0,0);
        if (window.loader.document.getElementById("wrapper")){
                window.loader.document.getElementById("wrapper").style.top="0px";
                window.loader.document.getElementById("wrapper").style.left="0px";
        }
        //iOrigObjTop=0;
        //iOrigObjLeft=0;
}

function scrollThumbs(pno){
        //alert (36);
        window.side.scroll(0,window.side.document.getElementById('thmb'+pno).offsetTop-36);
        //window.side.osd();
}



/*
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(){
        var table=window.loader.document.getElementById('panel');
        var not_affected=0;var tdTop=0;
        if (table){
                tbody=table.getElementsByTagName("tbody")[0];
                cells=tbody.getElementsByTagName("td").length;
                var index = 0;
                var idxLength=cells;
                if (window.loader.document.getElementById('tad'))index++;
                if (window.loader.document.getElementById('bad'))idxLength--;
                for(i=index;i<idxLength;i++) {
                        cell=tbody.getElementsByTagName("td")[i];
                        if(cell.innerHTML==""){
                                tdTop=parseInt(cell.parentNode.offsetTop)+topSpacing;
                                if (tdTop<bodyHeight+Math.abs(iOrigObjTop)){
                                        tmpUrl = SLICE_URL;
                                        if (i % 2 == 0) tmpUrl = SLICE_URL_ALT;
                                //        tmpUrl = "";
                                        cell.innerHTML="<img src='"+tmpUrl+cell.id+"'></img>";
                                }
                        }
                        else not_affected++;
                }
        }
        if (not_affected==cells) 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;
                //////Bass///
                if (_a) detectStory(e.clientX, e.clientY,0);
                ////////////
                return false;
        }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 (TEMP!='') {
                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 - pw)/2);
        if (dif<0) dif=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 = ADS_SPACE + dif+ (pw-story.l);
                stLeft = ADS_SPACE + dif +(pw-story.r);
                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(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);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 (tb.offsetWidth>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){
        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();
        rcp();
}
var TEMP='';
function fitHeight(){
        if (window.loader.document.getElementById("wrapper") && TEMP=='') {
        clearDrawing();
        var page=window.loader.document;
        var bdh=document.getElementById('loader').offsetHeight;
        bdh = parseInt(bdh / ROWS);
        if (page) {
                if (page.getElementById('tad')) {
                        tad=page.getElementById('tad').innerHTML;
                        page.getElementById('tad').innerHTML='';
                }
                if (page.getElementById('bad')) {
                        bad=page.getElementById('bad').innerHTML;
                        page.getElementById('bad').innerHTML='';
                }
                TEMP=window.loader.document.getElementById("wrapper").innerHTML;
                resetPageScroll();
                rcp();
                for (var i=0;i<ROWS*COLS;i++){
                        img=EDITION_NO+"/images/"+PAGE_NO+"-"+document.getElementById('ratios').value+"-"+EDITION_NO+"-"+i+".png";
                        if (page.getElementById(img)){
                                page.getElementById(img).style.height=(100/ROWS)+"%";
                                //alert(page.getElementById(img).innerHTML);
                                if (page.getElementById(img).innerHTML!='') {
                                        tmpUrl=page.getElementById(img).childNodes[0].src;
                                }
                                else {
                                        if (i%2==0) tmpUrl=SLICE_URL;
                                        else tmpUrl=SLICE_URL_ALT;
                                        tmpUrl+=img;
                                }
                                page.getElementById(img).innerHTML="<img src='"+tmpUrl+"' height='"+bdh+"px' width='"+bdh+"px' />";
                        }
                }
        }
        }
}
function fitWidth(){
        clearLRads();
        if (window.loader.document.getElementById("wrapper") ) {
        clearDrawing();
        if ( TEMP=='' && ( (SIDEFRAME && document.getElementById('ratios').value!=BEST_FIT) || (!SIDEFRAME && document.getElementById('ratios').value!=BEST_OFIT)) )
                TEMP=window.loader.document.getElementById("wrapper").innerHTML;
        if(TEMP){
                var fit=0;
                if (SIDEFRAME) fit=BEST_FIT;
                else {
                        fit=BEST_OFIT;
                }
                var page=window.loader.document;
                resetPageScroll();
                var srch="-"+document.getElementById('ratios').value+"-";
                var r = new RegExp(srch, 'gi');
                TEMP=TEMP.replace(r, "-"+fit+"-");
                document.getElementById('ratios').value=fit;
                window.loader.document.getElementById("wrapper").innerHTML=TEMP;
                if (tad!='')page.getElementById('tad').innerHTML=tad;
                if (bad!='')page.getElementById('bad').innerHTML=bad;
                TEMP='';bad='';tad='';
        }
        }
}
function resetPageScroll(){
        window.loader.scroll(0,0);
        if (window.loader.document.getElementById("wrapper")){
                window.loader.document.getElementById("wrapper").style.top="0px";
                window.loader.document.getElementById("wrapper").style.left="0px";
        }
        iOrigObjTop=0;iOrigObjLeft=0;
}

function disp_tcatch(){
        if(this.tcatch){
                document.getElementById('tcatch').innerHTML='<a href="#" onclick="document.getElementById(\'loader\').src=\'bin/showcase.php?pic=catch.jpeg&edition='+EDITION_NO+'\' ;"><img src="'+SLICE_URL + EDITION_NO + '/gallery/extras/catch.jpg" /></a>';
        }else{
                document.getElementById('tcatch').innerHTML='';
        }
}
function disp_rss(){
        var tmp=document.getElementById('rss').style.display;
        if(tmp=="none" || tmp==""){
                document.getElementById('rss').style.display="block";
        }else{
                document.getElementById('rss').style.display="none";
        }
}
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;
                        else proceed=0;
                        break;
                case "next":
                        if (PAGE_NO<PAGES) value=PAGE_NO+1;
                        else proceed=0;
                        break;
                case "last":
                        value=PAGES;
                        break;
                default:
                        if (val>0 && val<=PAGES && val != PAGE_NO) value=val;
                        else proceed=0;
                        break;
        }
        if (proceed) {
                PAGE_NO=value;
                document.getElementById('pages').value=PAGE_NO+" of "+PAGES;
                var link="page_loader.php?edition="+EDITION_NO+"&page="+value+"&view="+document.getElementById('ratios').value;
                if (TEMP!='') link += "&fit=1&sh="+document.getElementById('loader').offsetHeight;
                document.getElementById('loader').src=link;
        }
}
function printPage(pageSize){
        var PDFable=false;
        if(canDetectPlugins()) {
                if (detectPDF()) {
                        PDFable=true;
                }
        }
        if (PDFable && AVAIL_PDF) {
                window.bin.document.location = "text.html";
        //        window.bin.focus();
                //var pdf = document.getElementById("pdfemb");
                //alert(pdf.id);
        }
        else {
                var docWidth=0;docHeight=0;
                switch(pageSize){
                        case "A4":
                                docWidth=(21.0/2.54)*96;
                                docHeight=(29.7/2.54)*96;
                                break;
                        case "A3":
                                docWidth=(29.7/2.54)*96;
                                docHeight=(42.0/2.54)*96;
                                break;
                }
                var temp = "";var idx=1;var p=0;
                var pdfWin = window.open("", "", "scrollbars=yes, toolbar=yes");
                temp += "<html><head><style>";
                temp += "table {border-collapse:collapse;padding:0px;margin:0px;border-spacing:0px;} ";
                temp += "td {padding:0px;margin:0px;} ";
                temp += "img {width:"+(docWidth/COLS)+"px;height:"+(docHeight/ROWS)+"px;}";
                temp += "</style></head><body><center>";
                var srch="-"+document.getElementById('ratios').value+"-";
                var r = new RegExp(srch, 'gi');
                temp += "<table id='panel'>";
                var photos = window.loader.document.getElementsByTagName("td");
                for (var i=0;i < ROWS; i++) {
                        temp += "<tr>";
                        for (var j=0; j< COLS; j++) {
                                var url = SLICE_URL;
                                if (idx%2==0) url =  SLICE_URL_ALT;

                                if (photos[p].id != "" && photos[p].id!="tad" && photos[p].id!="bad") {
                                        temp += "<td><img src='"+url+photos[p].id.replace(r, "-100-")+"' /></td>";
                                }
                                idx++;p++;
                        }
                        temp += "</tr>";
                }
                temp += "</table>";
                temp += "</center></body></html>";
                pdfWin.document.write(temp);
                pdfWin.document.close();
                pdfWin.print();

        }
}
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 md(){
if(document.getElementById('mr')){
        if(LANGUAGE=="AR")
                document.getElementById('mr').style.right=(bodyWidth-(document.getElementById('more-menu').offsetLeft)-document.getElementById('more-menu').offsetWidth)+"px";
        else
                document.getElementById('mr').style.left=((document.getElementById('more-menu').offsetLeft))+"px";
        document.getElementById('mr').style.display="block";
}
}
function mc() {
        if (document.getElementById('mr'))document.getElementById('mr').style.display="none";
}
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;
}
function rcp(){
        if ((ADL_XTRA+ADR_XTRA)>0) {
                ADS_SPACE = parseInt(ADL_XTRA+((window.loader.document.getElementById("wrapper").offsetWidth - PAGE_WIDTH - (ADL_XTRA+ADR_XTRA))/2) - ((window.loader.document.getElementById("wrapper").offsetWidth - PAGE_WIDTH)/2));
                window.loader.document.getElementById("wrapper").style.left = ADS_SPACE+"px";
        }
}

document.onmouseup=mc;
window.onload=winLoad;
*/