$(document).ready(function()
{
	/**
	* Functionality for loading details tabs on detail page 
	*/
	// preload progress image
	$('<img src="/images/progress.gif" />');

	// Tabs
		var tabContainers = $('#tabs > div > div');
		$('#detail_tabs li a').click(function()
		{
			tabContainers.hide().filter(this.hash).show();

			$('#detail_tabs li.ui-state-active').each(function(){ $(this).removeClass('ui-state-active'); });
			$('#detail_tabs li.ui-tabs-selected').each(function(){ $(this).removeClass('ui-tabs-selected'); });
			
			$(this).parent('li').addClass('ui-state-active');
			$(this).parent('li').addClass('ui-tabs-selected');
			
			$('#detail_tabs li.detail_tabs_hover').each(function(){ $(this).removeClass('detail_tabs_hover'); });
			$(this).parent('li').addClass('detail_tabs_hover');

			return false;
		}).filter(':first').click();


	$('.ui-state-default').hover(
	   function(){$(this).addClass('ui-state-hover');}, 
	   function(){$(this).removeClass('ui-state-hover');}
	);

	/**
	* Left menu category drop downs (if no sub categories then leave as a link)
	*/
		
	$('.category>li>a').each(function() 
	{
		mySubCat = $(this).next('.sub_category');
		// add functionality for clicking menu items
		if (mySubCat.length > 0) 
		{
			$(this).click(function() 
			{
				$('.sub_category').each(function(){
					if ($(this).css('display') == 'block') { $(this).slideUp('fast'); }
				});

				mySubCat = $(this).next('.sub_category');
				if (mySubCat.is(':hidden')) { mySubCat.slideDown('fast'); }
				else { mySubCat.slideUp('fast'); }
				return false;
			});
		}
		mySubCat.hide();
		//$('#left_menu_on').parent().show();
		$('.sub_category>li>a.menuactive').parent().parent().show();
	});

	
	/**
	* Functionality to run on page load
	*/
	// click first tab in tab set
	//$('#detail_tabs>a:first-child').click();
	
	$('#bic_lookup').blur(function() { $('#ajax_content').remove() });

	$('.book_finder a').each(function() { $(this).bookFinderIdSet(); });
	
	hideAfter = 3;// only display 5 items on load then the remaining appear when 'more...' is clicked
	$('#sidemenu dl').each(function() 
	{                                                  
		if ($('ul li', this).size() > hideAfter && $('ul li.ui-state-active', this).size() == 0)
		{
			$('ul li', this).each(function(x) { if (x > hideAfter && !$('a.more', this)[0]) $(this).hide() });
			$('ul', this).append('<li><a href="#" class="more">More...</a></li>');
		}
		
	});
	$('#sidemenu a.more').click(function() 
	{ 
		moreLink = this;
		$('li', $(this).parents('ul')).animate({ 'height': 'show', 'opacity': 'show' }, 'fast', function() { $(moreLink).hide(); }); 
		return false;
	});
    
    // make age and grade active too
    $('#bookfinder input[type="text"]').blur(function() { 
        if ($(this).val()=='') {
            if ($(this).attr('name') == 'age') $(this).attr('value','Age');
            else if ($(this).attr('name') == 'grade') $(this).attr('value','Grade');
        }
        $.loadDataSet();
    });
    
    //function to clear the book finder values                         
    $('a.finder_clear').live('click', function() {
        //clear the hidden id
        $(this).parent('div').children('input[type=hidden]').attr('value','');
        
        //load the data while we do the rest
        $.loadDataSet();
        
        finder = $(this).parent('div').children('ul.book_finder');
        
        if ($(finder).attr('id') == 'format_book_finder') 
            this_html = ' -- Format -- ';
        else if ($(finder).attr('id') == 'series_book_finder') 
            this_html = ' -- Series -- ';
        else if ($(finder).attr('id') == 'gender_book_finder') 
            this_html = ' -- Gender -- ';
        else if ($(finder).attr('id') == 'diversity_book_finder') 
            this_html = ' -- Diversity -- '; 
        else 
            this_html = ' -- Subject -- ';
            
        //swap out the html and change the style    
        $(finder).children('li').children('a').html(this_html);
        $(finder).children('li').children('a').attr('class','');
        
        $(this).hide();
    });
	
	// set selected items in book finder
	$.setBookFinderVal('subject_bf_id', '#subject_book_finder');
	$.setBookFinderVal('diversity_bf_id', '#diversity_book_finder');
	$.setBookFinderVal('gender_bf_id', '#gender_book_finder');
    $.setBookFinderVal('series_bf_id', '#series_book_finder');
	$.setBookFinderVal('format_bf_id', '#format_book_finder');
	// -----------
});

$.setBookFinderVal = function(queryStringVar, parentDivId)
{
    if (queryStringVar == 'format_bf_id') var myRegExp = new RegExp('&' + queryStringVar + '=([A-Z]{2})');
    else var myRegExp = new RegExp('&' + queryStringVar + '=([0-9]+)');                                                                 
    
	var getId = document.location.toString().match(myRegExp);
	var hiddenInput = $(parentDivId).prev('input[type="hidden"]');
	
	if (getId && getId[1]) 
	{
		$(parentDivId).children('li').children('a').html($('a[href="#' + getId[1] + '"]', parentDivId).html());
        $(parentDivId).children('li').children('a').attr('class','ui-state-active');
		hiddenInput.val(getId[1]);
	}		
}

$.loadDataSet = function()
{
        //home page so need to paint the background colour      
        if ($('#col3_wrap').css('background') != 'white') $('#col3_wrap').css('background','white');    
        $('#main').html('<div class="main_padd"><center><img src="/images/progress.gif" alt="progress - please wait" style="margin-top:100px;"></center></div>');
        $('#breadcrumbs').html('<li><a href="/home">Home</a></li><li><a href="#">Search</a></li>');
        $.post('/js_index.php', $('#bookfinder').serialize(), function(data) {    
                $('#main div.main_padd').html(data);
        });
} 
        
// for left menu when you choose an item it sets the id to the hidden field 
$.fn.bookFinderIdSet = function()
{
	var ulObject = $(this).parents('ul.book_finder');
	var hiddenInput = ulObject.prev('input[type="hidden"]');
	
	return this.click(function()
	{
        //if there is no id set then there must be no clear function
        if ($(hiddenInput).val() == '') {
            $(ulObject).after('<a href="#" title="Clear selection" class="finder_clear"><span class="bf_clear"></span></a>');
        } 
                
		subjectId = this.href.toString().match(/#([0-9]+)/); 
		if (subjectId && subjectId[1])
		{
			hiddenInput.val(subjectId[1]);
			
			ulObject.children('li').children('a').html($(this).html());
            ulObject.children('li').children('a').attr('class','ui-state-active');  
			ulObject.children('li').children('ul').css('left', '-1000000000000px');
		}
        else 
        {
            subjectId = this.href.toString().match(/#([A-Z]{2})/);
            if (subjectId && subjectId[1])
            {
                hiddenInput.val(subjectId[1]);
                
                ulObject.children('li').children('a').html($(this).html());   
                ulObject.children('li').children('a').attr('class','ui-state-active');
                ulObject.children('li').children('ul').css('left', '-1000000000000px');
            }                                                       
        }   
        
        // load the data
        $.loadDataSet();                                                                                 
	})
	.hover(
		function() { ulObject.children('li').children('ul').css('left', '174px'); },
		function() { ulObject.children('li').children('ul').css('left', '-1000000000000px'); }
	);
}
