// for quick search at top
var inputText = "search by author, title, or keyword";
// for CC signup
var inputText2 = "enter e-mail address";

var currTray = 1;        //Current tray is always number 1
var numTrays = 0;        //Number of trays is determined when the pages loads or tab is clicked

$(function()
{
     $('a.ro').mbRollover();

     if(scripts_isIE6) {
	 $(window).load(function(){
		$(document).pngFix();
	 });
     }

    if ($.fn.multiSelect) {     
        $('.arc90_multiselect').multiSelect();
    }

    if ($.fn.prettyPhoto) {
        $("a[rel^='lightbox']").prettyPhoto({
            animationSpeed: 'slow',
            padding: 40,
            opacity: 0.85,
            showTitle: false,
            allowresize: true,
            counter_separator_label: '/',
            theme: 'light_square'
        });
    }

    if ($.fn.mbBookShowcase) {
        var showcase = $("#tray-wrapper").mbBookShowcase({
            itemClass: 'book',
            imgClass: 'bookImg',
            mOvrClass: 'book-ovr',
            selClass: 'book-sel',
            nextId: "a#bookNext",
            prevId: "a#bookBack",
            phpFetchFile: 'trays.php',
            followLink: false,
            loadTrayFromExternalFile: true,
            scrollTrayClass: "bookTray",
            trayOffset: 20,
            nextBook: 'a#nextBook',
            prevBook: 'a#lastBook',
            nextPreviousCallback: 'description',
	    descriptionOnHover: true,
            descriptionDest: 'home-book-desc',
	    descriptionTarget: 'home-book-desc-content',
	    phpDescFile: 'ShowcaseDetails.php',
	    tabs: {'tabFeatured':'featuredTrays', 'tabNew': 'newArrivalsTrays'},
            bookId: g_showcaseRecord
        }, function() {
            var str = this.clickid;
            str = str.substring(1);
            $("#showcaseHiddenRecord").val(str);
            if (this.event == "click") {
            	//Show detail image
            	showLargerImg(str);
                $.ajax({
                    url: "/showcaseSaveItem.php?record=" + str
                });
            }
        }, function(){
        	loadDesc(this.clickid);
        });
    }
    
    $(".qsrch-input").click(function(){
          if(inputText == $(this).val()){
	     $(this).val("");
	     $(this).css("color","#333");
	  }
    });
     
    $(".qsrch-input").blur(function(){
          if($(this).val() == "" ){
               $(this).val(inputText);
	       $(this).css("color","#a4a3a3");
	  }
    });

    //onload
    $(window).load(function(){ $(".qsrch-input").val(inputText); });

    $(".signup-input").click(function(){
         if(inputText2 == $(this).val()){
	    $(this).val("");
	    $(this).css("color","#333");
	 }
    });
     
    $(".signup-input").blur(function(){
         if($(this).val() == "" ){
              $(this).val(inputText2);
	      $(this).css("color","#a4a3a3");
	 }
    });

    //onload
    $(".signup-input").val(inputText2);

    $("#topSearchForm").submit(function(){
	var inptxt = $("#qsrchInputID").val();

	//alert(inptxt + "|" + inputText);

	if(inptxt==inputText)
	   $("#qsrchInputID").val("");
    });  
     
});

function checkSrchFrmSbmt()
{
	var inptxt = $("#qsrchInputID").val();

	//alert(inptxt + "|" + inputText);

	if(inptxt==inputText)
	   $(".qsrch-input").val("");

	document.qSearchForm.submit();
}


function loadDesc(cBook, theBook)
{
	if(theBook == null) theBook = 1;
	var phpFile = "ShowcaseDetails.php?recordNum="+cBook;
   	loadContent("home-book-desc", phpFile, "home-book-desc-content"); 
  	
}

//-------------------------------------------------------------------------------------------
// SHOWS A LARGER IMAGE FOR SHOWCASE
//-------------------------------------------------------------------------------------------
function showLargerImg(bookid)
{    
     
	closeLargerImg();
	
	var toShow="";
    	// Build the background overlay div
    	toShow += "<div class='site_overlay'></div>";
    	$('#home-main').append(toShow);
    	   
     	$('div.site_overlay').css('opacity',0).fadeTo('slow',0.65, function(){
     	
		$('div.site_overlay').css("z-index","2000");
		$('#hs-book-wrapper').css("z-index","3000");
			
   	});
   	
   	loadLargerImg(bookid);
   	
	$('div.site_overlay').css('height',$("#home-main").height()).bind('click',function(){
		closeLargerImg();
   	});
   	
     $("#hs-close, #hs-book-wrapper").bind('click',function(){
		closeLargerImg();
     });
   	
}

//-------------------------------------------------------------------------------------------
// LOADS THE LARGER IMAGE FROM AN EXTERNAL FILE
//-------------------------------------------------------------------------------------------
function loadLargerImg(bookid){

	$('#hs-book-wrapper').show();
     var phpFile = "ShowcaseImage.php?recordNum="+bookid;
     
     //var phpFile2 = "ShowcaseImageDetail.php?recordNum="+bookid;
	//loadContent("hs-book-details", phpFile2, "bookimg-detail");
	
     loadContent("hs-book", phpFile, "bookimg", function(){
     
     	$("#hs-loading").remove();
     
     	var imgWidth = $("#bookimg > a img").width();
     	var imgHeight = $("#bookimg > a img").height();
     	var paddingTop = (400 - (imgHeight+22))/2;
     	
     	$(".hs-book-tbl").width(imgWidth+339);
     	$("#hs-book").width(imgWidth);
     	$(".hs-book-tbl").css("position", "relative");
     	$(".hs-book-tbl").css("left", "169px");
     	$("div#hs-book-wrapper").css("padding-top", paddingTop+"px");
     	
     });
    
}

//-------------------------------------------------------------------------------------------
// CLOSES THE LARGER IMAGE
//-------------------------------------------------------------------------------------------
function closeLargerImg()
{
	$("#hs-loading").remove();
	$('#hs-book-wrapper').append('<div id="hs-loading"><img src="/images/loading.gif" alt="Loading" border="0" width="32" height="32" /></div>');
	
	$('div.site_overlay').remove();

	$('#hs-book').html("");
	$('#hs-book-details').html("");
	
	$('#hs-book-wrapper').hide();

}

//-------------------------------------------------------------------------------------------
// LOAD HTML CONTENT FROM EXTERNAL FILE
//-------------------------------------------------------------------------------------------

function loadContent(divID,fileName,fileDivID,callback)
{
  if(fileDivID!="" && callback=="")
     $("#"+divID).load(fileName+" #"+fileDivID);
  else if(callback!="" && fileDivID!="")
     $("#"+divID).load(fileName+" #"+fileDivID, callback);
  else
     $("#"+divID).load(fileName);
}

function removeContent(fileDivID)
{
  $("."+fileDivID).remove();
}

//-------------------------------------------------------------------------------------------
// INITIALIZES THE ACCORDIAN FOR THE BROWSE SECTION
//-------------------------------------------------------------------------------------------
function initAccordian()
{
	// mika commented this out
	//$(".accordian-sublevel").parent().parent().parent().hide();
	//$(".accordian-sublevel2").parent().parent().parent().hide();
	
	$(".accordianUL li").live("mouseover", function(){
		$(this).css("cursor", "pointer");
		
		
		if($(this).children("div").children("a").attr("class") == "accordian")
		{
			if(!($(this).hasClass("accordSel")))
			{
				$(this).children("div").children("a").css("color", "#3F5469");			
			}
		}
		else if($(this).children("div").children("div.subarrowDiv2").children("a").attr("class") == "accordian-sublevel")
		{
			
			if(!($(this).hasClass("accordSel2")))
			{
				$(this).children("div").children("div.subarrowDiv2").children("a").css("color", "#3F5469");			
			}
		}
		else if($(this).children("div").children("div.subarrowDiv2").children("a").attr("class") == "accordian-sublevela")
		{
			
			if(!($(this).hasClass("accordSel2")))
			{
				$(this).children("div").children("div.subarrowDiv2").children("a").css("color", "#3F5469");			
			}
		}
		else if($(this).children("div").children("div").children("a").attr("class") == "accordian-sublevel2")
		{
			
			if(!($(this).hasClass("accordSel2")))
			{
				$(this).children("div").children("div").children("a").css("color", "#3F5469");			
			}
		}
		else
		{
			if(!($(this).hasClass("accordSel")))
			{
				$(this).children("div").children("a").css("color", "#3F5469");
			}
		}
		
		
	});	
	
	$(".accordianUL li").live("mouseout", function(){
		$(this).css("cursor", "pointer");
		
		
		if($(this).children("div").children("a").attr("class") == "accordian")
		{
			if(!($(this).hasClass("accordSel")))
			{
				$(this).children("div").children("a").css("color", "#000");			
			}
		}
		else if($(this).children("div").children("div.subarrowDiv2").children("a").attr("class") == "accordian-sublevel")
		{
			
			if(!($(this).hasClass("accordSel2")))
			{
				$(this).children("div").children("div.subarrowDiv2").children("a").css("color", "#000");			
			}
		}
		else if($(this).children("div").children("div.subarrowDiv2").children("a").attr("class") == "accordian-sublevela")
		{
			
			if(!($(this).hasClass("accordSel2")))
			{
				$(this).children("div").children("div.subarrowDiv2").children("a").css("color", "#000");			
			}
		}
		else if($(this).children("div").children("div").children("a").attr("class") == "accordian-sublevel2")
		{
			
			if(!($(this).hasClass("accordSel2")))
			{
				$(this).children("div").children("div").children("a").css("color", "#666");			
			}
		}
		else
		{
		
			if(!($(this).hasClass("accordSel")))
			{
				$(this).children("div").children("a").css("color", "#000");
			}
		}
		
	});	
		
	
	/*$(".accordianUL li").live("click", function(){
		
		
		if($(this).children("div").children("a").attr("class") == "accordian")
		{
			openClosest($(this).children("div").children("a"));
		}
		else if($(this).children("div").children("div.subarrowDiv2").children("a").attr("class") == "accordian-sublevel")
		{

			openClosest2($(this).children("div").children("div").children("a"));
		}
		else if($(this).children("div").children("div.subarrowDiv2").children("a").attr("class") == "accordian-sublevela")
		{
			window.location = $(this).children("div").children("div").children("a").attr("href");
		}
		else if($(this).children("div").children("div").children("a").attr("class") == "accordian-sublevel2")
		{
			window.location = $(this).children("div").children("div").children("a").attr("href");
		}
		else
		{
			window.location = $(this).children("div").children("a").attr("href");
		}
		
	});*/
	$("a.accordian-sublevel").live("click", function(){
		openClosest2($(this));
	});
	
	$("a.accordian-sublevela").live("click", function(){
		window.location = $(this).attr("href");
	});
	
	$(".accordian").live("click", function(){
		openClosest($(this));
	});
	
	$("a.accordian-sublevel2").live("click", function(){
		window.location = $(this).attr("href");
	});
	
}

function openClosest(currSel)
{
	$(currSel).parent("div").parent("li").addClass("accordSel");
	$(currSel).css("color", "#fff");
	
	//$(currSel).children("li:first").css("border", "none");
	
	if(!($(currSel).parent("div").parent("li").children("ul").hasClass("opened")))
	{
		closeOpened();
		$(currSel).parent("div").parent("li").children("ul").addClass("opened");
		$(currSel).parent("div").parent("li").children("ul").slideDown("normal");
	}else{
		closeOpened();
	}
}

function openClosest2(currSel)
{
	$(currSel).parent("div").parent("div").parent("li").addClass("accordSel2");
	//HACK so this will work in IE6
	/*setTimeout(function(){ $(currSel).parent("div").parent("li").children("div.subarrowDiv").children("a").children("img").attr("src","/images/arrow-down.gif") }, 10);*/
	$(currSel).parent("div").parent("div").parent("li").children("div").children("div.subarrowDiv").children("a").children("img").attr("src","/images/arrow-down.gif");
	/*$(".accordSel2").children("div").children("div.subarrowDiv").children("a").children("img").attr("src","/images/arrow-down.gif");*/

	if(!($(currSel).parent("div").parent("div").parent("li").children("ul").hasClass("opened2")))
	{
		closeOpened2();
		$(currSel).parent("div").parent("div").parent("li").children("ul").addClass("opened2");
		$(currSel).parent("div").parent("div").parent("li").children("ul").slideDown("normal");
	}else{
		closeOpened2();
	}
}

function closeOpened()
{

	//$(".opened").parent("li").css("background", "url('../images/arrow.gif') no-repeat 0px 3px");
	$(".opened").parent("li").removeClass("accordSel");
	$(".opened").parent("li").children("div").children("a").css("color", "#000");
	
	$(".opened").each(function(){
		$(this).slideUp("normal");
		$(this).hide("normal");
		$(this).removeClass("opened");
	});
	
	closeOpened2();
}

function closeOpened2()
{
	$(".opened2").parent("li").removeClass("accordSel2");
	//HACK so this will work in IE6
	//setTimeout(function(){ $(".opened2").parent("li").children("div.subarrowDiv").children("a").children("img").attr("src","/images/arrow.gif") }, 10);
	$(".opened2").parent("li").children("div").children("div.subarrowDiv").children("a").children("img").attr("src","/images/arrow.gif");

	$(".opened2").each(function(){
		$(this).slideUp("normal");
		$(this).hide("normal");
		$(this).removeClass("opened2");
	});
}

function lockLevel0(catid)
{
	$("#tab0_"+catid).show();
	$("#lvl1_"+catid).addClass("opened");
	$("#li_"+catid).addClass("accordSel");
}

function lockLevel1(catid,allid)
{
	$("#lvl1_"+catid).show();
	$("#lvl1_"+catid).addClass("opened");
	$("#li_"+catid).addClass("accordSel");
	$("#tab1_"+catid).show();
	if(allid!="")
	{
	  $("#li_all_"+allid).addClass("accordSel2");
	}
}

function lockLevel2(catid,subcatid,subcatid2,tablockid,allid)
{
	$("#lvl1_"+catid).show();
	$("#lvl1_"+catid).addClass("opened");
	$("#li_"+catid).addClass("accordSel");


	if(allid!="")
	{
		if(allid==catid)
		{
		  $("#li_all_"+allid).addClass("accordSel2");
		}
	}

	$("#lvl2_"+subcatid).show();
	$("#lvl2_"+subcatid).addClass("opened2");
	$("#li_"+subcatid).addClass("accordSel2");

	if(tablockid==subcatid2)
	  $("#tab2_"+tablockid).show();
	else
	  $("#tab1_"+tablockid).show();

	if(subcatid2!="") $("#li_"+subcatid).children("div").children("div.subarrowDiv").children("a").children("img").attr("src","/images/arrow-down.gif");
}

