// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

jQuery.ajaxSetup({
	'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}  
})

$(document).ready(function() {
	$('#new_reference').submit(function(){
		$.post($(this).attr("action"), $(this).serialize(), null, "script");
		return false;
	})
	
	/*
	$('#references a.remove').click(function(event) {
	    //alert('remove?');
		$(this).parent().hide();
		var $prev = $(this).prev("input[@type=hidden]");
		//alert($prev.val());
		$prev.val('1');
		//alert($prev.val());
		//if(hidden_input = $(this).previous("input[type=hidden]")) hidden_input.value = '1'
	})
	*/
	
	$('#search_btn').click(function(){
		jQuery('#search').show('slow');
	});
	
	$('#lnkUseOpenID').click(function(){
		$('#user_pass_box').hide(); // hide
		$('#open_id_box').show(); // show
		
		$('#user_pass_help').hide();
		$('#open_id_help').show();
		
		$('#user_session_username').val('');
		$('#user_session_password').val('');
		
		return false;
	})
	
	$('#lnkUseUserPass').click(function(){
		$('#user_pass_box').show(); // hide
		$('#open_id_box').hide(); // show
		
		$('#user_pass_help').show();
		$('#open_id_help').hide();
		
		$('#user_session_openid_identifier').val('');
		
		return false;
	})
	
	$("#tabs").tabs();
	
	$('#flash_notice').hide();
	$('#flash_notice').fadeIn(1000,function(){ 
		setTimeout(
			function(){
				$('#flash_notice').fadeOut(1000,
					function(){
						$('#flash_notice').hide();
					}
				);
			},
			6000
		)}
	);
	
	// setting up placeholder copy on text inputs
	var input_box_focus = function() {                
		var el = $(this);                
		if (el.hasClass('placeholder')) {                        
			if (this.value === this.title) {                                
				this.value = '';                                
				el.removeClass('placeholder');                        
			}                
		}        
	};        
	
	var input_box_blur = function() {                
		if (this.value === '') {                        
			this.value = this.title;                        
			$(this).addClass('placeholder');                
		}
	};        
	
	// placeholder        
	input_boxes = $('input.input');        
	input_boxes.focus(input_box_focus);        
	input_boxes.blur(input_box_blur);        
	
	// Add placeholder on load if empty        
	$.each(input_boxes, function() {                
		if (this.value === '' || this.value === this.title) {                        
			this.value = this.title;                        
			$(this).addClass('placeholder');                
		}        
	});
	
	$('#listen').click(function(){
		$('#player_box').fadeIn(1000); // show
		return false;
	});
	
	$("#previous_search_page").live("click", previousPage);	
	$("#next_search_page").live("click", nextPage);
	$("#previous_search_page_btm").live("click", previousPage);	
	$("#next_search_page_btm").live("click", nextPage);
	
	function previousPage(){
		var current_page = $('#page').val();
	  	$('#page').val(String(Number(current_page)-1));
		$('#old_reference_keywords').val( jQuery('#keywords', '#search_esv_form').val() );
		//$('#search_esv_form').submit();
		$('#esv_search_btn').click();
		return false;
	}
	
	function nextPage(){
		//alert(jQuery('#keywords', '#search_esv_form').val());
		
		var current_page = $('#page').val();
	  	$('#page').val(String(Number(current_page)+1));
		$('#old_reference_keywords').val( jQuery('#keywords', '#search_esv_form').val() );
		$('#esv_search_btn').click();
		return false;
	}
})