var displaytranslation=false;
var timeoutId ;
var timeoutId2 ;
var fontsize=32 ;
var ayehWaitTime;
var currentAyeh=1;
var ghari=new Array(2);
    ghari[1]="shateri";
    ghari[2]="parhizgar";

function showsajdehwindow() {
    window.open("sajdeh.htm","sub","HEIGHT=330,WIDTH=630,LEFT=200,TOP=100,fullscreen")
}


function repos()
{
    var el = document.getElementById("scrolldiv");
    var t = document.body.scrollTop;
    if(t<150)
        t=150;
    else t=t+6;
    document.getElementById("scrolldiv").style.top = t;
}

function getAllTags(tag)
{
    if (document.all) {
        var allTags = document.all.tags(tag)
    } else if (document.getElementsByTagName) {
        var allTags = document.getElementsByTagName(tag)
    }
    return allTags;
}

function findPosY(obj)
{
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
}

function bluecolor(id){

    if(document.getElementById('fnt'+id)){
        spanelem=document.getElementById('fnt'+id);
        spanelem.color ='blue';
    }
    if(document.getElementById('fntbesm'+id)){
        spanelem=document.getElementById('fntbesm'+id);
        spanelem.color ='blue';
    }
    setTimeout("window.scroll(0,findPosY(spanelem)-103)",1000);
    browserName=navigator.appName; 
    if (browserName=="Microsoft Internet Explorer")
    {
        document.getElementById("ayehid").value = ayeh[id].ayehid;
        document.getElementById("soorehid").value = ayeh[id].soorehid;
    }

}

function changecolor(id){
    var i = 0;
    allFonts=getAllTags('font');
    for (var i = 0; i < allFonts.length; i++) { 
        if(allFonts[i].id.search('fnt')>=0){
            allFonts[i].color='#000000';
            if(allFonts[i].id.search('fntbesm')>=0)
                allFonts[i].color='#FF6139';
        }
    }
    if(document.getElementById('fnt'+id)){
        spanelem=document.getElementById('fnt'+id);
        spanelem.color ='#800000';
    }
    if(document.getElementById('fntbesm'+id)){
        spanelem=document.getElementById('fntbesm'+id);
        spanelem.color ='#FF0000';
    }
    if(document.getElementById('Acheckbox').checked)
        window.scroll(0,findPosY(spanelem));
    browserName=navigator.appName; 
    if (browserName=="Microsoft Internet Explorer")
    {
        document.getElementById("ayehid").value = ayeh[id].ayehid;
        document.getElementById("soorehid").value = ayeh[id].soorehid;
    }
}

function getMovie(){
  mov = 'movie'
  if( document.getElementById )
    return document.getElementById(mov)
  else return document[mov]
}

function movieIsLoaded(theMovie) {
  if( theMovie=='[object]' )
    return theMovie.PercentLoaded() == 100
  else return false
}

function runMovies(id,runCount){
        currentAyeh=id+1;
        if(runCount==0){
            if(!ayeh[id].loaded){
                loadframe(id);
            }
            stopMovies();
        }
        if(runCount==0 || !document.getElementById('R1Acheckbox').checked){
            if(ghariId==1)
                time=ayeh[id].time1;
            else
                time=ayeh[id].time2;
            if(ayeh[id].loaded == true){
                loadm(id,true);
                changecolor(id);
                ayehWaitTime=time;
                timerbar(1);
                if(ayeh[id].sajdeh=='true'){
//                  showsajdehwindow();
                    alert('سجده واجب');
                }
                if( (id+1) <ayeh.length){
                    timeoutId = setTimeout("runMovies("+(id+1)+","+(runCount+1)+")",(time*1));
                }
                else if((id+1) == ayeh.length){
                    timeoutId = setTimeout("changecolor(-1)",time);
                    timeoutId = setTimeout("nextpage()",time);
                }
            }
            else
                timeoutId = setTimeout("runMovies("+id+","+(runCount+1)+")",100);
        }
        else{
                document.getElementById('dbar2').style.width='0px';
                clearTimeout(timeoutId2);
            }
}

function loadm(ayeh_id,play) {

  if(navigator.userAgent.indexOf("Mac")>0 )
    return;
  if(ayeh_id==null){
            if(window.movie){
                window.document["movie"].SetVariable("_root.moviename","");
                window.document["movie"].Play();
            }
            else if(document.movie){
                document.movie.SetVariable("_root.moviename","");
                document.movie.Play();
            }
  }
  else{
//  && movieIsLoaded(getMovie(objNum)) 
      if( play ){
            path=(sourceSite+ghari[ghariId]+"/"+(ayeh[ayeh_id].file));
            if(window.movie){
                window.document["movie"].SetVariable("_root.moviename",path );
                window.document["movie"].Play();
            }
            else if(document.movie){
                document.movie.SetVariable("_root.moviename", path);
                document.movie.Play();
            }
      }
   }
}

function nextpage(){
    clearTimeout(timeoutId2);
    clearTimeout(timeoutId);
    if(!document.getElementById('R1Acheckbox').checked && document.getElementById('nplinkn2')){
        var nextlink = document.getElementById('nplinkn2').href;
        window.location=nextlink+'&autoPlay=true#douter'; 
    }
}

function gotopage()
{
        p=document.getElementById('page').value;
        if(p==p*1)
            window.location='?name=quran&page='+p;
        else
            document.getElementById('page').value='';
}

function checkEnter(){
        var keycode;
        if (window.event)
                keycode = window.event.keyCode;

        if(keycode == "13")
            gotopage()       
}

function stopMovies(){
    clearTimeout(timeoutId2);
    clearTimeout(timeoutId);
    loadm(null,true);
}

function confirmloaded(id){
    if(id>=0){
        if((id+1)<= ayeh.length)
            ayeh[id].loaded = true ;
        if ( (id+1) <= ayeh.length )
            loadframe((id+1));
    }
    else loadframe(id);
}

function loadframe(id){

    if(id >=0 &&  (id+1)<= ayeh.length ){
        document.getElementById('frame1').src="index.php?name=Quran&func=swftohtm&id="+id+"&file="+ghari[ghariId]+"/"+ayeh[id].file;
    }
    else if( ayeh.length == (id) ){
        document.getElementById('frame1').src="index.php?name=Quran&func=swftohtm&id=-1&file="+nextpagefile;
    }
    else if(id==-1){
        document.getElementById('frame1').src="index.php?name=Quran&func=swftohtm&id=-2&file="+"dummy.swf";
    }
}


function showhide() {
    var i = 0;
    while( document.getElementById('tayeh'+i) ){
        divelem=document.getElementById('tayeh'+i);
        if(divelem.style.display == 'none'){
            divelem.style.display = '';
            displaytranslation=true;
        }else{
            divelem.style.display = 'none';
            displaytranslation=false;
        }
        i=i+1;
    }
    
    document.getElementById('showTranslation').value=displaytranslation;
    allLinks=getAllTags('a');
    for (var i = 0; i < allLinks.length; i++) {
        if(allLinks[i].id.search('nplink')>=0){
            if(displaytranslation)
                allLinks[i].href=allLinks[i].href.replace('showTranslation=false','showTranslation=true');
            else 
                allLinks[i].href=allLinks[i].href.replace('showTranslation=true','showTranslation=false');
        }
    }
}

function setReadoneayeh()
{
    if(document.getElementById('R1Acheckbox').checked)
        document.getElementById('readoneayeh').value='true';
    else
        document.getElementById('readoneayeh').value='false';
    allLinks=getAllTags('a');
    for (var i = 0; i < allLinks.length; i++) {
        if(allLinks[i].id.search('nplink')>=0){
            if(document.getElementById('R1Acheckbox').checked) {
                allLinks[i].href=allLinks[i].href.replace('readoneayeh=false','readoneayeh=true');
            }
            else{ 
                allLinks[i].href=allLinks[i].href.replace('readoneayeh=true','readoneayeh=false');
            }
        }
    }
}

function setAutoScroll()
{
    if(document.getElementById('Acheckbox').checked)
        document.getElementById('AutoScroll').value='true';
    else
        document.getElementById('AutoScroll').value='false';
    allLinks=getAllTags('a');
    for (var i = 0; i < allLinks.length; i++) {
        if(allLinks[i].id.search('nplink')>=0){
            if(document.getElementById('Acheckbox').checked) {
                allLinks[i].href=allLinks[i].href.replace('AutoScroll=false','AutoScroll=true');
            }
            else{ 
                allLinks[i].href=allLinks[i].href.replace('AutoScroll=true','AutoScroll=false');
            }
        }
    }
}

function setghari(id){
    ghariId=id;
    stopMovies();
    for (var i = 0; i < ayeh.length; i++)
        ayeh[i].loaded=false;
        
    allLinks=getAllTags('a');
    for (var i = 0; i < allLinks.length; i++) {
        if(allLinks[i].id.search('nplink')>=0){
            if(ghariId==1)
                allLinks[i].href=allLinks[i].href.replace('ghariId=2','ghariId=1');
            else 
                allLinks[i].href=allLinks[i].href.replace('ghariId=1','ghariId=2');
        }
    }
    runMovies(currentAyeh-1,0);
}

var min=25;
var max=50;
function increaseFontSize() {
   var font = document.getElementsByTagName('font');
   for(i=0;i<font.length;i++) {
      if(font[i].id){
          if(font[i].style.fontSize) {
             var s = parseInt(font[i].style.fontSize.replace("px",""));
          } else {
             var s = 32;
          }
          if(s<=max) {
             s += 2;
          }
          font[i].style.fontSize = s+"px"
      }
   }
   fontsize=s;
}

function decreaseFontSize() {
   var font = document.getElementsByTagName('font');
   for(i=0;i<font.length;i++) {
      if(font[i].id){
          if(font[i].style.fontSize) {
             var s = parseInt(font[i].style.fontSize.replace("px",""));
          } else {
             var s = 32;
          }
          if(s>=min) {
             s -= 2;
          }
          font[i].style.fontSize = s+"px";
      }
   }   
   fontsize=s;
}

function setFontSizeforfonts() {
   var font = document.getElementsByTagName('font');
   for(i=0;i<font.length;i++) {
      if(font[i].id){
          font[i].style.fontSize = fontsize;
      }
   }   
}

function setFontSizelinks() {
    document.getElementById('fontsize').value=fontsize; 
    allLinks=getAllTags('a');
    for (var i = 0; i < allLinks.length; i++) {
        if(allLinks[i].id.search('nplink')>=0){
                p = allLinks[i].href.indexOf('fontsize=')+9;
                allLinks[i].href=allLinks[i].href.substr(0,p)+fontsize+allLinks[i].href.substr((p+2));
        }
    }
}


//---------------------------------------------------------------------------------
var loadedcolor='#FED556' ;       // PROGRESS BAR COLOR darkgray
var unloadedcolor='white';     // COLOR OF UNLOADED AREA lightgrey
var bordercolor='#97A77B';           // COLOR OF THE BORDER black
var barheight=5;                  // HEIGHT OF PROGRESS BAR IN PIXELS
var barwidth=530;                  // WIDTH OF THE BAR IN PIXELS
function filltimerbardiv(){
    var txt='';
    txt+='<div id="douter" onmouseup="" style="position:relative; background-color:'+bordercolor+'; width:'+barwidth+'px; height:'+barheight+'px;">\n';
    txt+='<div style="position:absolute; top:1px; left:1px; width:'+(barwidth-2)+'px; height:'+(barheight-2)+'px; background-color:'+unloadedcolor+'; font-size:1px;"></div>\n';
    txt+='<div id="dbar2" style="position:absolute; top:1px; right:1px; width:0px; height:'+(barheight-2)+'px; background-color:'+loadedcolor+'; font-size:1px;"></div>\n';
    txt+='</div>\n';
    document.getElementById('timerbardiv').innerHTML=txt;
}

function timerbar(step){
    if(step==1){
        clearTimeout(timeoutId2);
        document.getElementById('dbar2').style.width='0px';
    }
    milisecond = (step)*100; 
    width = ((milisecond*(barwidth+50)/ayehWaitTime));
    if(width>=barwidth-1){
        width = barwidth-2;
        step = 0 ;
    }
    document.getElementById('dbar2').style.width=width +'px';
    f='timerbar('+(step+1)+')';
    timeoutId2=setTimeout(f,100);
}

