// JavaScript Document
// Play well with other libraries, use jQuery instead of $
jQuery.noConflict();

// Set some global vars
var showmore;
var kbOptionsHide;
var kbOptionsInProgress;


jQuery(document).ready(function(){
	showMore = 0;
	//kbOptionsHide = jQuery.cookie('copkbsearch'); 
	kbOptionsHide=0;
	kbOptionsInProgress = 0;
	jQuery('.current').click(function(){
		return false;								  
	});
	jQuery('#kbSearchTopMenuMore > span').hover(function(){
		jQuery(this).css({textDecoration : 'underline'});	
	},function(){
		jQuery(this).css({textDecoration : 'none'});	
	});	
	jQuery(document).click(function(){
		if(showMore == 1){
			showMore = 0;
			jQuery('#kbSearchTopMenuMore > ul').css({display : 'none'});
		}
	});
	jQuery('#kbSearchTopMenuMore > span, #kbSearchTopMenuMore > small').click(function(){
		if(showMore == 0){	
			showMore = 1;
			jQuery('#kbSearchTopMenuMore > ul').css({display : 'block'});
			jQuery('#kbSearchTopMenuMore > ul').focus();
		}else{
			showMore = 0;
			jQuery('#kbSearchTopMenuMore > ul').css({display : 'none'});
			jQuery('#kbSearchTopMenuMore > ul').focus();
		}
		return false;
	});
	jQuery('#kbSearchTopMenuMore > ul > li').click(function(){
		window.location=jQuery(this).find("a").attr("href");
		return false;																				   
	});
	if(kbOptionsHide == 1){
		jQuery('#kbSearchOptions').hide();
		jQuery('#kbSearchResultsHeader > img').replaceWith('<img src="'+siteurl+'images/kbs+.png" alt="show options" width="14" height="14" />');
		jQuery('#kbSearchResultsHeader > a').text('Show Options');
		jQuery('#kbResultsListing').css({width:'778px'});
	}
	jQuery('#kbSearchResultsHeader > img, #kbSearchResultsHeader > a').live('click',function(){
		if(kbOptionsInProgress != 1){
			kbOptionsInProgress = 1;
			if(kbOptionsHide != 1){
				kbOptionsHide = 1;
				jQuery.cookie('copkbsearch','1',{ expires: 365 });
				jQuery('#kbSearchOptions').hide('slide',{direction:'left'},600,function(){jQuery('#kbResultsListing').animate({width:'778px'},600,function(){kbOptionsInProgress = 0;})});
			
				jQuery('#kbSearchResultsHeader > img').replaceWith('<img src="'+siteurl+'images/kbs+.png" alt="show options" width="14" height="14" />');
				jQuery('#kbSearchResultsHeader > a').text('Show Options');
			}else{
				kbOptionsHide = 0;
				jQuery('#kbResultsListing').animate({width:'588px'},600,function(){jQuery('#kbSearchOptions').show('slide',{direction:'left'},600,function(){kbOptionsInProgress = 0;})});
			
				jQuery('#kbSearchResultsHeader > img').replaceWith('<img src="'+siteurl+'images/kbs-.png" alt="hide options" width="14" height="14" />');
				jQuery('#kbSearchResultsHeader > a').text('Hide Options');
				jQuery.cookie('copkbsearch', null);
			}
		}
		return false;
	});
	
	jQuery('.kbTypeResults').each(function(){
		var resultCount = jQuery('ul > li',this).size();
		if(resultCount > 3){
			group = jQuery('ul',this).find('li:gt(2)').remove();
			jQuery('<div/>').append('<ul/>').append(group).appendTo(jQuery(this));
			jQuery('div',this).hide();
			jQuery(this).prepend('<span class="more">[ <u>view more</u> ]</span>');
			jQuery('.more', this).click(function(){
				jQuery(this).parent().find('div').slideToggle(600);
			});
		}
	});
	
	// Pring Page Button
	jQuery('.kbPrintButton').hover(function(){
		jQuery(this).addClass('o');									
	},function(){
		jQuery(this).removeClass('o');
	});
	jQuery('.kbPrintButton').click(function(){
		window.print();
		return false;									
	});
	
	//Select Permalink in Firefox
	if(!jQuery.browser.msie){
	jQuery('#permalinkText').click(function(){
		var r = document.createRange();
    	var w=document.getElementById("permalinkText");  
    	r.selectNodeContents(w);  
    	var sel=window.getSelection(); 
    	sel.removeAllRanges(); 
    	sel.addRange(r);									
	});
	jQuery('#permalinkText2').click(function(){
		var r = document.createRange();
    	var w=document.getElementById("permalinkText2");  
    	r.selectNodeContents(w);  
    	var sel=window.getSelection(); 
    	sel.removeAllRanges(); 
    	sel.addRange(r);									
	});
	}
	
});
