// for quick search at top
var inputText = "search by author, title, or keyword";
// for CC signup
var inputText2 = "enter e-mail address";


/**
 * PEPS ROLLOVER FUNCTION
 **/

PEPS = {};
PEPS.rollover = 
{
   init: function()
   {
      this.preload();
      
      $(".ro").children("img").hover(
         function () { $(this).attr( 'src', PEPS.rollover.newimage($(this).attr('src')) ); }, 
         function () { $(this).attr( 'src', PEPS.rollover.oldimage($(this).attr('src')) ); }
      );
   },

   preload: function()
   {
      $(window).bind('load', function() {
         $('.ro').children("img").each( function( key, elm ) { $('<img>').attr( 'src', PEPS.rollover.newimage( $(this).attr('src') ) ); });
      });
   },
   
   newimage: function( src )
   { 
      return src.substring( 0, src.search(/(\.[a-z]+)$/) ) + '1' + src.match(/(\.[a-z]+)$/)[0]; 
   },

   oldimage: function( src )
   { 
      return src.replace(/1\./, '.'); 
   }
};


$(function()
{
  //-------------------------------------------------------------------------------------------
  // IE6 SUPPORT, fix PNG images
  //------------------------------------------------------------------------------------------ 
  if(scripts_isIE6)
  {
      $(window).load( function() { 

		$(document).pngFix();

      });
  }

  //-------------------------------------------------------------------------------------------
  // Initialize PEPS rollover class
  //------------------------------------------------------------------------------------------ 
  PEPS.rollover.init();


  //-------------------------------------------------------------------------------------------
  // Lightbox
  //------------------------------------------------------------------------------------------
  $("a[rel^='lightbox']").prettyPhoto({
			animationSpeed: 'slow',
			padding: 40,
			opacity: 0.85,
			showTitle: false,
			allowresize: true,
			counter_separator_label: '/',
			theme: 'light_rounded'
  });


  //-------------------------------------------------------------------------------------------
  // Bio Overlays
  //------------------------------------------------------------------------------------------
  $('a.readBio').click( function() {

	var which = $(this).attr("rel");

	if(which!="josh") $('div#bio-josh').hide();
	if(which!="sunday") $('div#bio-sunday').hide();

	$('div#bio-'+which).show();

	openDivCenter("overlayBio","close-"+which,"");
  });


  //-------------------------------------------------------------------------------------------
  // Email Overlays
  //------------------------------------------------------------------------------------------
  $('a.sendEmail').click( function() {

	var which = $(this).attr("rel");

	var upair = "";

	switch(which)
	{
	  case 'josh':
		upair = "?sendTo=jmann@bbrarebooks.com";
	    break;

	  case 'sunday':
		upair = "?sendTo=sunday@bbrarebooks.com";
	    break;
	}

	$('#overlayBoxFormID').html("");

	var html = '<iframe src="/email-frame.php'+upair+'" frameborder="0" width="420" height="320" scrolling="no" name="emailFormFrame" id="emailFormFrame"></iframe>';

	$("#overlayBoxFormID").append(html);

	openDivCenter("overlayEmail","close-email","");
  });

  $('a.sendRequest').click( function() {

	var which = $(this).attr("rel");

	var upair = "";

	switch(which)
	{
	  case 'selling':
		upair = "?subject="+escape('Selling Your Books');
	    break;

	  case 'appraisals':
		upair = "?subject="+escape('Appraisals');
	    break;

	  case 'restoration':
		upair = "?subject="+escape('Restoration & Binding Services');
	    break;

	  case 'libraries':
		upair = "?subject="+escape('Private Libraries');
	    break;
	}

	$('#overlayBoxFormID').html("");

	var html = '<iframe src="/email-frame.php'+upair+'" frameborder="0" width="420" height="320" scrolling="no" name="emailFormFrame" id="emailFormFrame"></iframe>';

	$("#overlayBoxFormID").append(html);

	openDivCenter("overlayEmail","close-email","");
  });

   $('#tNavDD').superfish({
            delay:       1000,                            // one second delay on mouseout 
            animation:   {opacity:'show',height:'show'},  // fade-in and slide-down animation 
            speed:       'fast',                          // faster animation speed 
            autoArrows:  false,                           // disable generation of arrow mark-up 
            dropShadows: true                            // enable drop shadows
   });
   
   $(window).bind("resize", resizeSite);
   
   resizeSite();
   
});


function resizeSite(){
   
   var win = $(window).width();
   var doc = $(document).width();   

   if(win < 998) $("body").width(doc);
   else $("body").css("width", "100%");
   
}

//-------------------------------------------------------------------------------------------
// POSITION DIV IN THE CENTER OF WINDOW
//------------------------------------------------------------------------------------------
function centerLyr(divID)
{
    $('#'+divID).css("position","absolute");
 
    var newTop = ( $(window).height() - $('#'+divID).height() ) / 2+$(window).scrollTop();
    if(newTop<0) newTop = 0;

    var newLeft = ( $(window).width() - $('#'+divID).width() ) / 2+$(window).scrollLeft();
    if(newLeft<0) newLeft = 0;

    $('#'+divID).css("top", newTop + "px");
    $('#'+divID).css("left", newLeft + "px");
}

//-------------------------------------------------------------------------------------------
// RE-POSITION DIV IN THE CENTER OF WINDOW ON WINDOW RESIZE
//------------------------------------------------------------------------------------------
function rePositionLyr(status,divID) 
{
    switch(status) {
	case true:
		$(window).bind('resize', function() {
			centerLyr(divID);
		});
		//scroll is not used here
		//$(window).bind('scroll', function() {
		//	centerLyr(divID);
		//});
	    break;
	case false:
		$(window).unbind('resize');
		//scroll is not used here
		//$(window).unbind('scroll');
	    break;
    }
}

//-------------------------------------------------------------------------------------------
// POSITION OVERLAY IN THE CENTER OF WINDOW
//------------------------------------------------------------------------------------------
function showPopupDivCenter(divID) 
{
    if (scripts_isIE6) $('#'+divID).bgiframe();
    var toShow = $("#"+divID); 
    toShow.css({"z-index":"9000","visibility":"visible"});
    centerLyr(divID);
    rePositionLyr(true,divID);
}

//-------------------------------------------------------------------------------------------
// OPEN DIV OVERLAY AND POSITION CENTER
//------------------------------------------------------------------------------------------
function openDivCenter(divID,closeBtnID,removeFlashID)
{
    var toShow="";
    // Build the background overlay div
    toShow += "<div class='site_overlay'></div>";
    $('body').append(toShow);

    $('div.site_overlay').css('height',$(document).height()).bind('click',function(){
	closeDiv(divID);
    });

    $('div.site_overlay').css('opacity',0).fadeTo('slow',0.65, function(){
	showPopupDivCenter(divID);
	$('div.site_overlay').css("z-index","9000");
	$('#'+divID).css("z-index","9500");
    });

	
    if(closeBtnID!=""){
        $("#"+closeBtnID).bind('click',function(){
	        closeDiv(divID);
		if(removeFlashID!=""){
			var html = $('#'+removeFlashID).html();
			if(html.indexOf("noflashcontent")==-1)
			  $('#'+removeFlashID).html("");
		}
        });
    }
}

//-------------------------------------------------------------------------------------------
// CLOSE OPENED DIV OVERLAY
//------------------------------------------------------------------------------------------
function closeDiv(divID)
{
    $('div.site_overlay').remove();
    rePositionLyr(false,divID);
    var toHide = $("#"+divID); 
    toHide.css({"z-index":"0","visibility":"hidden"});
    if(divID=="all-products"){
	toHide.css("width", "1px");
	toHide.css("height", "1px");
    }
}

