// rails auth token enabled in jquery
jQuery(document).ajaxSend(function(event, request, settings) {
  if (typeof(AUTH_TOKEN) == "undefined") return;
  // This next line is the key!
  if (settings.type == 'GET') return; // Don't add anything to a get request lest IE turn it into a POST.
  settings.data = settings.data || "";
  settings.data += (settings.data ? "&" : "") + "authenticity_token=" + encodeURIComponent(AUTH_TOKEN);
});


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


// functions I use a lot
jQuery.fn.toggleText = function(a, b) {
  return this.each(function() {
    jQuery(this).text(jQuery(this).text() == a ? b : a);
  });
};

function addMega() {
  $(this).find("div").show();
}

function removeMega() {
  $(this).find("div").hide();
}

$.fn.dropdown = function() {

	$(this).hover(function(){
		$(this).addClass("hover");
		$('> .dir',this).addClass("open");
		$('ul:first',this).css('visibility', 'visible');
	},function(){
		$(this).removeClass("hover");
		$('.open',this).removeClass("open");
		$('ul:first',this).css('visibility', 'hidden');
	});
	
	}
	

// the ready loop!

$(document).ready(function() {

	// set up menu
	var megaConfig = {
	interval: 10,
	sensitivity: 4,
	over: addMega,
	timeout: 50,
	out: removeMega
	};
	$("li.mega").hoverIntent(megaConfig);
	
	// enable hints for textboxes
	$('input:text').hint();
	
	// list options stuff
	$("#list_options select").change(function() {
	this.form.submit();
	});
	
	$("#list_options input").change(function() {
	this.form.submit();
	});
	
	// self-explanatory. this is the date picker helper
	$(".datepicker").datepicker();
	
	// self-explanatory. this is the time picker helper
	$('.timepicker').timepickr({ convention: 12 });
	
	$('a[rel*=facebox]').facebox();
	
	// used by tour new tour edit and various views that have search fields
	$('div#show_list_options_toggle').click(function() {
	$('#list_options').slideToggle(400);
	$('div#show_list_options_toggle').toggleText("Show filter/sort options", "Hide filter/sort options");
	return false;
	});
	
	$('div.gallery_thumbnail').hover(function() {$(this).addClass('gallery_thumbnail_highlighted')},
	                           function() {$(this).removeClass('gallery_thumbnail_highlighted')});
	
	// ajj. dissolves reprocessing notification banner after 30 seconds. current logic blocks display for administrator.
	$("#reproc_notifier").delay(30000).hide(1000);

	// will be useful when I get a button to switch it on and off
	//$("div.header1").delay(100).show('fast');

// Text editor show hide controls. First is in drop down menu, second is a hide link in the editor pane itself
	
	$('div#show_description_editor').click(function() {
	$('#description_editor').slideToggle('slow');
	$('div#show_description_editor').toggleText("Upper Description", "Hide Upper Decription Editor");
	return false;
	});
	
	$('div#hide_description_editor').click(function() {
	$('#description_editor').hide('slow');
	return false;
	});
			
	$('div#show_lower_editor').click(function() {
	$('#lower_text_editor').slideToggle('slow');
	$('div#show_lower_editor').toggleText("Lower Description", "Hide Lower Decription Editor");
	return false;
	});

	$('div#hide_lower_editor').click(function() {
	$('#lower_text_editor').hide('slow');
	return false;
	});

	$('div#show_secondary_editor').click(function() {
	$('#side_bar_editor').slideToggle('slow');
	$('div#show_secondary_editor').toggleText("Sidebar Description", "Hide Sidebar Editor");
	return false;
	});

	$('div#hide_secondary_editor').click(function() {
	$('#side_bar_editor').hide('slow');
	return false;
	});

	$('div#show_addr_replacement_editor').click(function() {
	$('#addr_editor').slideToggle('slow');
	$('div#show_addr_replacement_editor').toggleText("Address Area", "Hide Adrress Editor");
	return false;
	});

	$('div#hide_addr_replacement_editor').click(function() {
	$('#addr_editor').hide('slow');
	return false;
	});

	$('div#show_pano_replacement_editor').click(function() {
	$('#pano_editor').slideToggle('slow');
	$('div#show_pano_replacement_editor').toggleText("Pano_360 Area", "Hide Pano_360 Editor");
	return false;
	});

	$('div#hide_pano_replacement_editor').click(function() {
	$('#pano_editor').hide('slow');
	return false;
	});

// show hide control for slider controls first is drodown entry, second is hide link in panel
	
	// show button in dropdown menu for advanced (formerly slider) panel. 
	$('#show_slide_controls').click(function() {
	$('div#slide_controls').slideToggle('slow');
	$('#show_slide_controls').toggleText("Show Advanced Controls", "Hide Advanced Controls");
	return false;
	});
	// Hide button (advanced) in slider panel
	$('div#hide_slide_controls').click(function() {
	$('#slide_controls').slideToggle('fast');	
	})

	// show button in dropdown menu for color panel. 
	$('#show_color_controls').click(function() {
	$('div#color_controls').slideToggle('slow');
	$('#show_color_controls').toggleText("Show Color Settings", "Hide Color Settings");
	return false;
	});
	// Hide button in slider panel
	$('div#hide_color_controls').click(function() {
	$('#color_controls').slideToggle('fast');	
	})
	
	// show button in dropdown menu for color panel. 
	$('#show_key_toggle').click(function() {
	$('div#icon_key').slideToggle('fast');
	$('#show_key_toggle').toggleText("Show Icon Key", "Hide Icon Key");
	return false;
	});
	// delays show of slideshow container after page load to ameliorate some of the display issue
	$("div.preview").delay(200).fadeIn(500);

	// displays a reminder about where to edit photos
	$("div.photo_edit_note").delay(1000).fadeIn('slow').delay(120000).hide('slow');
	
	//ie8 crossfade bug notice
	$("div.ie8_notice_note").delay(2500).fadeIn('slow');
	
	// ie8 crossfade bug notice acknowledgment
	$('div#hide_ie8_notice').click(function() {
	$('div.ie8_notice_note').delay(0).hide('slow');	
	});
	
// onclick on change savers for individual control elements

	//save color
	$('#bg_main1').blur(function() {
		$('#bg_main1').change(function(){
		this.form.submit();		
		})
	});
	
	// save sliders
	$("#slider1").click(function(){	
	this.form.submit();
	});

	//vtour enable public checkbox update onchange
	$("#enable_public").click(function(){
	this.form.submit();
	});
	
	//vtour show main viewer panel checkbox (large pics) update onchange
	$("#show_main").click(function(){
	this.form.submit();
	});
	
	//vtour show index panel checkbox (image index thumbnails) update onchange
	$("#show_index").click(function(){
	this.form.submit();
	});
	
	//vtour show captions checkbox update onchange
	$("#show_caption").click(function(){
	this.form.submit();
	});
	
	//vtour pause on hover checkbox  update onchange
	$("#hover_pause").click(function(){
	this.form.submit();
	});
	
	//vtour fade effect enable checkbox update onchange
	$("#fade_enable").click(function(){
	this.form.submit();
	});
	
	// vtour show agent picture checkbox  update onchange
	$("#display_avatar").click(function(){
	this.form.submit();
	});
	
	// vtour show agent company logo  update onchange
	$("#display_logo").click(function(){
	this.form.submit();
	});
	
	$("#display_addr").click(function(){
	this.form.submit();
	});

	$("#display_pano").click(function(){
	this.form.submit();
	});

	$("#display_lower").click(function(){
	this.form.submit();
	});
	
	// dropdown menu jquery script from	lwis.com
	$("ul.dropdown li").dropdown();


});
