jQuery.ajaxSetup({ 
  'beforeSend': function(xhr) {
		//xhr.setRequestHeader("Accept", "text/javascript")
		xhr.setRequestHeader("Accept", "text/javascript, text/html, application/xml, text/xml, */*");
	}
})

jQuery(document).ajaxSend(function(event, request, settings) {
  if (typeof(AUTH_TOKEN) == "undefined") return;
	if (settings.type == 'GET') 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);
});
 
jQuery.fn.submitWithAjax = function() {
  this.live('submit', function() {
    $.post(this.action, $(this).serialize(), null, "script");
    return false;
  })
  return this;
};

jQuery.fn.ajaxLink = function() {
	this.click(function() {
		$.ajax({
			url: this.href,
			dataType: "script"
		});
		return false
	});
}
 
jQuery.fn.confirmRequired = function() {
  this.live('click', function (){
    return confirm('Are are you sure');
  });
}

// Plugins
jQuery.fn.fader = function(options) {
	var defaults = {
	    delay: 2500 
	};
	var options = $.extend(defaults, options);
	var el = this;
	setTimeout( function() {$(el).fadeOut()}, options.delay);
}



jQuery(document).ready(function() {
  $('form.remoteForm').submitWithAjax();
	$('a.remote').ajaxLink();
  $('.needs_confirmation').confirmRequired();
  $(".notice").fader({delay: 2500});
  $('form.remoteForm').submitWithAjax();

	// Sortable Images
	// $('#postSortPhotos').sortable({items:'.photo', containment:'parent', update: function() {
	// 	  $.post($(this).parents('form:first').attr('action') + '/photos/sort', '_method=put&authenticity_token='+AUTH_TOKEN+'&'+$(this).sortable('serialize'));
	// 	}});
	
	// $('#postSortPhotos').sortable({items: 'div', axis:'x', update: function() {
	// 		$.ajax({
	// 			url: window.location.href.replace('/edit',''),
	// 			type: 'post',
	// 			dataType: 'script',
	// 			data: $(this).sortable('serialize',{key:'postPicturesList[]'}) + '&_method=put'
	// 		});
	// 	}});
	
	$('#postSortPhotos').sortable({items:'div', containment:'parent', axis:'x', update: function() {
	  $.post('/admin/photos/sort', '_method=put&authenticity_token='+AUTH_TOKEN+'&'+$(this).sortable('serialize'));
	}});
	
	// CONTACT SECTION
	
	$('#contact_wrapper').hide();
	$('#contact').css("opacity", 0);
	$('a.contact_button').click(function() {
		if ($('#contact_wrapper').is(":hidden"))
       {
        	$('#contact_wrapper').slideDown("slow");
					$('#contact').animate({opacity: 1}, 2000 );
					
    } else {
					$('#contact').stop().animate({opacity: 0}, 200 );
          $('#contact_wrapper').slideUp(800);
       }
	});
	
	//scrolable
	
	$("div.scrollable").scrollable({
		size: 4
	});
	$(".items img").click(function() { 
			$('#image_wrap img').fadeOut();
	    $('#image_wrap img:eq(' + $('.items > img').index(this) + ')').fadeIn();
	});
	$(".items img").filter(":first").addClass("active");
	
	// tooltip
	
	$('#social_links li').mouseover(function() {
	        $(this).stop().animate({ 
							marginTop: "0px",
							opacity: 1 
					}, 200);
	});
	$('#social_links li').mouseout(function() {
	        $(this).stop().animate({ 
							marginTop: "10px",
							opacity: 0.7
					}, 300);
	});
		
	$("#social_links li a").tooltip({ 

	        // use single tooltip element for all tips 
	        tip: '#tooltip',  

	        // tweak the position 
	        offset: [-30, 100], 

	        // use "slide" effect 
	        effect: 'slide' 

	    // add dynamic plugin  
	    }).dynamic( { 

	        // customized configuration on bottom edge 
	        bottom: { 

	            // slide downwards 
	            direction: 'down', 

	            // bounce back when closed 
	            bounce: true 
	        } 
	    });
	
	// Post hover animation
	
	$('.post_infos').css({"top": -200} ).find('h3').css({"top": -40, "opacity": 0});
	
	$('.post').hover(function(){
		$(this).find('a.image').stop().animate({opacity: 0.5}, 1000);
		$(this).find('.post_infos').stop().animate({top: 0}, 200 ).find('h3').animate({top: 0, opacity: 1}, 400);
		return false;
	}, function(){
		$(this).find('a.image').stop().animate({opacity: 1}, 1000);
		$(this).find('.post_infos').stop().animate({top: -200}, 1000 ).find('h3').animate({top: -40, opacity: 1}, 500);
		return false;
	});

	$('textarea.tinymce').tinymce({
		// Location of TinyMCE script
		script_url : '/javascripts/tiny_mce/tiny_mce.js',

		// General options
		theme : "advanced",
		plugins : "pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,advlist",

		// Theme options
		theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
		theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
		theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
		theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_resizing : true
	});

	
});

$("a[rel='external']").click(function(){
	window.open($(this).attr("href"));
	return false;
});