function fsb() {
  if (jQuery('#content').length==1) {
    $("#pp").css({"top":jQuery('#content').offset().top,"left":jQuery('#content').offset().left-jQuery('#pp').width()-1,"height":jQuery('#content').height()+30});
    //$('#pp').show();
    
    $("#np").css({"top":jQuery('#content').offset().top,"left":jQuery('#content').offset().left+jQuery('#content').width(),"height":jQuery('#content').height()+30});
    //$('#np').show();
    
    jQuery('.ic').css({"marginTop":(jQuery('#np').height()/2)-jQuery('.ic').height()});
    
    if (jQuery('.cover').length==1) {
      jQuery('#content').css({'overflow':'hidden'});
    } else {
      jQuery('#content').css({'overflow-y':'auto'});
    }
  }

}

(function ($) {
// VERTICALLY ALIGN FUNCTION
$.fn.vAlign = function() {
	return this.each(function(i){
	var ah = $(this).height();
	var ph = $(this).parent().height();
	var mh = (ph - ah) / 2;
	$(this).css('margin-top', mh);
	});
};
})(jQuery);


function fsf() {
    fsb();
}

jQuery(window).load(function() {
  fsf();
});

  function showHide() {
  var d=jQuery('#whereami');
    if (d.attr('p')=="thanks") {
      jQuery('#np').hide();
    } else {
      jQuery('#np').show();
    }
    if (d.attr('p')!="cover") {
      jQuery('#pp').show();
    } else {
      jQuery('#pp').hide();
    }
  }

jQuery(document).ready(function() {

  jQuery('.ctext').vAlign();
  fsf();
  $(window).resize(function() {
    fsb();
  });
  
  
  
  jQuery.navlist=Array();
  var i=1;
  jQuery.navlist[0]='cover';
  jQuery('#chaplist a').each(function() {
    jQuery.navlist[i++]=jQuery(this).attr('sid');
  });
  jQuery.navlist[i++]='thanks';
  //alert(jQuery.navlist[2]);

  if (jQuery('#chaplist').height()<100) {
    jQuery('#chaplistup').hide();
    jQuery('#chaplistdown').hide();
  }
  
  showHide();
  
  
  $("#np").click(function() {
    var d=jQuery('#whereami');
    if (d.attr('p')=="cover") {d.attr('p',"0");}
    d.attr('p',jQuery.navlist[parseInt(d.attr('p'))+1]);
    jQuery('#xg').load("/rsc/pages/inside.php?q="+d.attr('q')+"&t="+d.attr('t')+"&p="+d.attr('p')+"&cc="+d.attr('cc'),function() {
      jQuery('#content').scrollTop(0);
      jQuery('#chaplist a').removeClass('on');
      jQuery('#ch'+d.attr('p')).addClass('on');
      jQuery('#xg').css({"height":"auto"});
      //jQuery('#xg,#content').css({"width":jQuery.defwid});
      //jQuery('#content').css({"left":(jQuery(window).width()/2)-(jQuery('#content').width()/2)});
      fsf();
      showHide();
      keepChapCenter();
      jQuery('.ctext').vAlign();
      jQuery.pageTracker._trackPageview('/'+d.attr('q')+'/'+d.attr('t'));
    });
  });

  $("#pp").click(function() {
    var d=jQuery('#whereami');
    d.attr('p',jQuery.navlist[parseInt(d.attr('p'))-1]);
    jQuery('#xg').load("/rsc/pages/inside.php?q="+d.attr('q')+"&t="+d.attr('t')+"&p="+d.attr('p')+"&cc="+d.attr('cc'),function() {
      jQuery('#content').scrollTop(0);
      jQuery('#chaplist a').removeClass('on');
      jQuery('#ch'+d.attr('p')).addClass('on');
      if (d.attr('p')=="cover") {
        jQuery('#xg').css({"height":jQuery('.cover').height()-30});
        jQuery('#xg,#content').css({"width":jQuery('.cover').width()});
        //jQuery('#content').css({"left":(jQuery(window).width()/2)-(jQuery('#content').width()/2)});
      } else {
        jQuery('#xg,#content').css({"height":"auto"});
        //jQuery('#xg,#content').css({"width":jQuery.defwid});
        //jQuery('#content').css({"left":(jQuery(window).width()/2)-(jQuery('#content').width()/2)});
      }      
      fsf();
      showHide();
      keepChapCenter();
      jQuery('.ctext').vAlign();
    });
  });
  
  jQuery('#chaplist a').live("click",function() {
    var d=jQuery('#whereami');
    var jumpto=jQuery(this).attr('sid');
    d.attr('p',jQuery.navlist[jumpto]);
    jQuery('#xg').load("/rsc/pages/inside.php?q="+d.attr('q')+"&t="+d.attr('t')+"&p="+d.attr('p')+"&cc="+d.attr('cc'),function() {
    jQuery('#content').scrollTop(0);
      jQuery('#chaplist a').removeClass('on');
      jQuery('#ch'+d.attr('p')).addClass('on');
      if (d.attr('p')=="cover") {
        jQuery('#xg').css({"height":jQuery('.cover').height()-30});
      } else {
        jQuery('#xg').css({"height":"auto"});
        //jQuery('#xg,#content').css({"width":jQuery.defwid});
        //jQuery('#content').css({"left":(jQuery(window).width()/2)-(jQuery('#content').width()/2)});
      }      
      fsf();
      showHide();
      keepChapCenter();
      jQuery('.ctext').vAlign();
    });
    return false;
  });

		$('#small,#large').click(function(){
			var ourText = $('#xg p');
			var currFontSize = ourText.css('fontSize');
			var finalNum = parseFloat(currFontSize, 10);
			var stringEnding = currFontSize.slice(-2);
			if(this.id == 'large') {
				finalNum += 1;
			}
			else if (this.id == 'small'){
				finalNum -=1;
			}
			ourText.css({fontSize: finalNum + stringEnding});
			$("#saveto").load("/rsc/segs/sd.php?fontsize="+finalNum);
			jQuery('.ctext').vAlign();
			return false;
		});
		

$('#baseprice').keyup(function() {
  var feeis=($('#baseprice').val()*0.029)+.3; 
  var rem=Math.round(($('#baseprice').val()-(Math.round(feeis*100)/100))*100)/100; 
  $('#afterfees').html(rem);
});

$('#chaplistdown').live("click",function() {
  var isScroll=$('#chaplist').scrollTop();
  var chapsize=$('#chaplist').height();
  $('#chaplist').animate({scrollTop:isScroll+chapsize});
});

$('#chaplistup').live("click",function() {
  var isScroll=$('#chaplist').scrollTop();
  var chapsize=$('#chaplist').height();
  $('#chaplist').animate({scrollTop:isScroll-chapsize});
});

$('#slidemeta').click(function() {
  if ($('#meta').offset().left<0) {
    $('#meta').animate({"left":0});
    jQuery.get("/ui/1889_save.php?meta=on");
  } else {
    $('#meta').animate({"left":$('#meta').width()*-1});
    jQuery.get("/ui/1889_save.php?meta=off");
  }
});

jQuery('.cover').load(function() {
  jQuery.defwid=jQuery('#content').width();
  jQuery('#content,#xg').css({"width":jQuery('.cover').width(),"height":jQuery('.cover').height()-30});
  //jQuery('#content').css({"left":(jQuery(window).width()/2)-(jQuery('.cover').width()/2)});
  fsb();
});

function keepChapCenter() {
  if ($('#chaplist .on').height()>0) {
    if (($('#chaplist .on').position().top-$('#chaplist').position().top)>$('#chaplist').height())  {
      var chaph=$('#chaplist').height();
      $('#chaplist').scrollTop(($('#chaplist .on').position().top-$('#chaplist').position().top)-(chaph/2));
    }
  }
}


});

