$(document).ajaxSend(function(event, request, settings) {
  if (typeof(AUTH_TOKEN) == "undefined") return;
  // settings.data is a serialized string like "foo=bar&baz=boink" (or null)
  settings.data = settings.data || "";
  settings.data += (settings.data ? "&" : "") + "authenticity_token=" + encodeURIComponent(AUTH_TOKEN);
});

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


// JRPASS SPECIFIC JS - PLACE ALL JQUERY INSIDE THIS FUNCTION
$(function(){
	$('ul.sf-menu').superfish();
	$('.list_table').colorize({hoverColor:'#CCFFFF', hiliteColor:'#FFFF99'}); 
	$('.list_table').tablesorter();
	//Cufon.replace('#menu a, .cufon a, h5, h4, h3, h2, h1, h6');
	
	//PASSENGER ENTRY SCREEN
	$(".date").datepicker({showAnim: 'fadeIn', minDate: '+3d', maxDate: '+3m', numberOfMonths: [1,4], duration: 'fast' });
	$(".dob_date").datepicker({showAnim: 'fadeIn', changeYear: true, yearRange: '-90:+30', defaultDate: '-30Y', duration: 'fast'});
	$('.dob_popup').qtip({
	   style: { name: 'dark', tip: true, 	border: { width: 2,radius: 5 } },
	   position: { corner: { target: 'topMiddle', tooltip: 'bottomMiddle' } },
	   content: 'We need to record your date of birth to ensure that your ticket age class is correct.',
	   show: 'mouseover',
	   hide: 'mouseout'
	});	
	$('.departure_popup').qtip({
	   style: { name: 'dark', tip: true, 	border: { width: 2,radius: 5 } },
	   position: { corner: { target: 'topMiddle', tooltip: 'bottomMiddle' } },
	   content: 'Japan Rail requires that you start using your rail pass within 3 months of purchase.',
	   show: 'mouseover',
	   hide: 'mouseout'
	});
	
	//QTIP
  // Use the each() method to gain access to each elements attributes
  $('#product a[rel]').each(function()
  {
     $(this).qtip(
     {
        content: {
           // Set the text to an image HTML string with the correct src URL to the loading image you want to use
           text: '<img class="throbber" src="images/loading.gif" alt="Loading..." />',
           url: $(this).attr('rel'), // Use the rel attribute of each element for the url to load
           title: {
              text: $(this).attr('name'), // Give the tooltip a title using each elements text
              button: 'Close' // Show a close link in the title
           }
        },
        position: {
           corner: {
              target: 'bottomMiddle', // Position the tooltip above the link
              tooltip: 'topMiddle'
           },
           adjust: {
              screen: true // Keep the tooltip on-screen at all times
           }
        },
        show: { 
           when: 'click', 
           solo: true // Only show one tooltip at a time
        },
        hide: 'unfocus',
        style: {
           tip: true, // Apply a speech bubble tip to the tooltip at the designated tooltip corner
           border: {
              width: 0,
              radius: 4
           },
           name: 'light', // Use the default light style
           width: 570 // Set the tooltip width
        }
     })
  });
	
	//background highlight
	$('.sweeney_form input,.sweeney_form textarea, .sweeney_form select').focus(
	      function () {
			$('.input_wrap').css('background', "#C7D3DB")
			$(this).parents('.input_wrap').css("background", '#99CCFF');
			curvyCorners.redraw();
	      }
	    );
		
	//GENERAL 
	//$("select:first", document.forms[0]).focus();
	
	//flash message		
	setTimeout(function() {$("#flash").fadeOut("fast")}, 3000);
	
	
	//menu bar
	$('img#nav1').hover(function() {
		$(this).attr("src","/images/aboutuslogocl140.png");
			}, function() {
		$(this).attr("src","/images/aboutuslogo140.png");
	});
	$('img#nav2').hover(function() {
		$(this).attr("src","/images/productlogocl140.png");
			}, function() {
		$(this).attr("src","/images/productlogo140.png");
	});
	$('img#nav3').hover(function() {
		$(this).attr("src","/images/contactlogocl140.png");
			}, function() {
		$(this).attr("src","/images/contactlogo140.png");
	});
	$('img#nav4').hover(function() {
		$(this).attr("src","/images/presslogocl140.png");
			}, function() {
		$(this).attr("src","/images/presslogo140.png");
	});
	$('img#nav5').hover(function() {
		$(this).attr("src","/images/letterlogocl140.png");
			}, function() {
		$(this).attr("src","/images/letterlogo140.png");
	});
	$('img#nav6').hover(function() {
		$(this).attr("src","/images/linklogocl140.png");
			}, function() {
		$(this).attr("src","/images/linklogo140.png");
	});

	//header bar
	$('#header .grey').hover(
		function(){
			if ($.support.hrefNormalized == true){
				$(this).css("background","#ddd");
				$(this).children('a').css("color","#666");
			}
		},
		function(){
			if ($.support.hrefNormalized == true){
				$(this).css("background","#bbb");
				$(this).children('a').css("color","#fff");
			}
		}
	);
	
	$('#header .green').hover(
		function(){
			if ($.support.hrefNormalized == true){
				$(this).css("background","#CCFF99");
				$(this).children('a').css("color","#669933");
			}
		},
		function(){
			if ($.support.hrefNormalized == true){
				$(this).css("background","#669933");
				$(this).children('a').css("color","#fff");
			}
		}
	);
	
	//ADD/REMOVE IMAGES FROM ITEM
	$('#add_image_link').click(function(){
		var new_input = $('#image_upload_area > p:first').clone();
		$('#image_upload_area').append(new_input);
		new_input.fadeIn();
	});

	$('.delete_image_link').livequery(function(){
		$(this).click(function(){
			$(this).parent().remove();
		});
	});

	$('a.delete_item_image').livequery(function(){
		$(this).click(function(){
			var delete_link = $(this);
			delete_link.parent().fadeOut("slow", function(){
				$.post(delete_link.attr("href"), {_method: "delete"}, function(data){
					$("#thumbnail_area").html(data);
				});
			});
			return false;
		});
	});

	$('a.edit_item_image').livequery(function(){
		$(this).click(function(){
			var update_link = $(this);
				$.post(update_link.attr("href"), {_method: "put"}, function(data){
					$("#thumbnail_area").html(data);
			});
			return false;
		});
	});
	
	$('div.navigation').css({'width' : '300px', 'float' : 'left'});
	$('div.content').css('display', 'block');

	// Initially set opacity on thumbs and add
	// additional styling for hover effect on thumbs
	var onMouseOutOpacity = 0.67;
	$('#thumbs ul.thumbs li').css('opacity', onMouseOutOpacity)
		.hover(
			function () {
				$(this).not('.selected').fadeTo('fast', 1.0);
			}, 
			function () {
				$(this).not('.selected').fadeTo('fast', onMouseOutOpacity);
			}
		);

	$(document).ready(function() {
		// Initialize Advanced Galleriffic Gallery
		var galleryAdv = $('#gallery').galleriffic('#thumbs', {
			delay:                  5000,
			numThumbs:              18,
			preloadAhead:           10,
			enableTopPager:         false,
			enableBottomPager:      true,
			imageContainerSel:      '#slideshow',
			controlsContainerSel:   '#controls',
			captionContainerSel:    '#caption',
			loadingContainerSel:    '#loading',
			renderSSControls:       true,
			renderNavControls:      false,
			playLinkText:           'Play Slideshow',
			pauseLinkText:          'Pause Slideshow',
			prevLinkText:           '&lsaquo; Previous Photo',
			nextLinkText:           'Next Photo &rsaquo;',
			nextPageLinkText:       'Next &rsaquo;',
			prevPageLinkText:       '&lsaquo; Prev',
			enableHistory:          true,
			autoStart:              false,
			onChange:               function(prevIndex, nextIndex) {
				$('#thumbs ul.thumbs').children()
					.eq(prevIndex).fadeTo('fast', onMouseOutOpacity).end()
					.eq(nextIndex).fadeTo('fast', 1.0);
			},
			onTransitionOut:        function(callback) {
				$('#caption').fadeTo('fast', 0.0);
				$('#slideshow').fadeTo('fast', 0.0, callback);
			},
			onTransitionIn:         function() {
				$('#slideshow').fadeTo('fast', 1.0);
				$('#caption').fadeTo('fast', 1.0);
			},
			onPageTransitionOut:    function(callback) {
				$('#thumbs ul.thumbs').fadeTo('fast', 0.0, callback);
			},
			onPageTransitionIn:     function() {
				$('#thumbs ul.thumbs').fadeTo('fast', 1.0);
			}
		});
	});
	
});

















//LIBRARY SETTINGS
$.fn.superfish.defaults = { 
    hoverClass:    'sfHover',          // the class applied to hovered list items 
    pathClass:     'overideThisToUse', // the class you have applied to list items that lead to the current page 
    pathLevels:    1,                  // the number of levels of submenus that remain open or are restored using pathClass 
    delay:         500,                // the delay in milliseconds that the mouse can remain outside a submenu without it closing 
    animation:     {opacity:'show'},   // an object equivalent to first parameter of jQuery’s .animate() method 
    speed:         'fast',           // speed of the animation. Equivalent to second parameter of jQuery’s .animate() method 
    autoArrows:    true,               // if true, arrow mark-up generated automatically = cleaner source code at expense of initialisation performance 
    dropShadows:   true,               // completely disable drop shadows by setting this to false 
    disableHI:     false,              // set to true to disable hoverIntent detection 
    onInit:        function(){},       // callback function fires once Superfish is initialised – 'this' is the containing ul 
    onBeforeShow:  function(){},       // callback function fires just before reveal animation begins – 'this' is the ul about to open 
    onShow:        function(){},       // callback function fires once reveal animation completed – 'this' is the opened ul 
    onHide:        function(){}        // callback function fires after a sub-menu has closed – 'this' is the ul that just closed 
};
