 $(document).ready(function(){
	
	// Open links with class .external in new windows
	$("a.external").click(function(){
        window.open(this.href);
        return false;
    });
	
	// ******* Search field ******* //

	// set the default text
	$('.keywords').focus(function(){
		var value = this.value;
		var title = this.title;
		if (value == title) {
			$(this).val("");
		};
		$(this).addClass("selected");
	});
	
	// if it's blank then reset back the title attribute
	$('.keywords').blur(function(){
		$(this).removeClass("selected");
		if(!$(this).val().length){
			$(this).val(this.title);
		}
	});
	
	// ******** Tabs ********** //
	    $("div#welcome_box ul").tabs();
	$("div#news_box ul").tabs();
	
	//***** Accept/Decline Partner Affiliate Request *****//
	$('a.affiliate_accept').click(function() {
		var member_id = $(this).attr('id').split('_');
		member_id = member_id[1];
		var form_id = $(this).attr('href');
				
		$.get("/members/process_request/"+'accept/'+member_id+'/'+form_id);
		
		$('#affiliate_requests').load("/members/awaiting_verification #affiliate_requests table");
		
		return false;
	});
	
	$('a.affiliate_decline').click(function() {
		var member_id = $(this).attr('id').split('_');
		member_id = member_id[1];
		var form_id = $(this).attr('href');
		
		$.get("/members/process_request/"+'decline/'+member_id+'/'+form_id);
		
		$('#affiliate_requests').load("/members/awaiting_verification #affiliate_requests table");
		
		return false;
	});
	
	// ******** Print page ********** //
	
	if (document.getElementById("page_options")) {
		var page_options = $("#page_options");
		$(page_options).prepend("<li id=\"print_page\"><a href=\"javascript:window.print()\">Print page</a></li>");
	};
	
	// ******* Search box ******* //
	
	//hide advanced search box
	$("#search_advanced").hide();
	
	//wrap h3 contents in a span
	$("#search_box h3").wrapInner("<span></span>");
	
	//toggle advanced search box
	$("#search_box h3 span").click(function(){
		$("#search_advanced").slideToggle(500);
		$(this).toggleClass("open");
		return false;
	});
	
	// ******* Forum show/hide thread review ******* //
	
	//hide advanced search box
	$("#thread_reviews").hide();
	
	//wrap h3 contents in a span
	$("h3#toggle_review").wrapInner("<span></span>");
	
	//toggle advanced search box
	$("h3#toggle_review span").click(function(){
		$("#thread_reviews").slideToggle(800);
		$(this).toggleClass("open");
		return false;
	});
	
	// ******* People show/hide locations list ******* //
	
	//hide advanced locations list children
	$("ul#people_locations_list ul").hide();
	
	//Add class "js" to the list items to change appearance
	$("ul#people_locations_list li span").addClass("js");
	
	//toggle list children
	$("ul#people_locations_list li").click(function(){
		$(this).children('ul').toggle();
		return false;
	});
	
	// ******* Apply a class to specific images that are narrowere than we would like ******* //
	    function image_too_narrow (div_id, min_width, give_class) {
	    
	        var image_width = $('div#'+div_id+' img').width();
	               
	        if(image_width < min_width) {
	        
	            $('div#'+div_id).addClass(give_class);
	            $('div#'+div_id).width(image_width);
	        
	        }else {
			$('div#'+div_id+' p').width(image_width);
	
		}
	    
	    }
	    
	    // run the too narrow function on required elements
	    image_too_narrow('article_img','240', 'narrow_img')
	
	// add class to make stripey tables
	$('#content_pri .body_text table tr:odd').addClass('alt');
	
	// ******* Apply a class to a link depending on filetype ******* //
	function add_file_class(got_class) {
		
		$('a.'+got_class).each(function (i) {
			
			var href = $(this).attr('href').split('.');
			
			$(this).addClass(href[href.length - 1]);
		});
	
	}
	
	add_file_class('file');
	
	
});