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

var $j = jQuery.noConflict();
var imageWallSortableParams = {
  revert:true,
	update: function (event, ui) {
		var post_path = "/photos/" + ui.item.attr("id") + "/update_order";
		$j.post(post_path, $j(this).sortable('serialize'), null, "script");
	}
};

var imageWallDraggableParams = {
  connectToSortable: "#sortable-wall",
  revert: "invalid"
};

var imageWallDroppableParams = {
 drop: function(event, ui) { 
   if (ui.draggable.parents('ul#sortable-wall').size() > 0) {
     return false;
   } else {
     $j.post($j(".droppable form").attr("action"), { photo_id: ui.draggable.attr("id")} , null, "script");
     return false;
    } 
 }
};

var notImplemented = function() {
	alert("We're in Beta... but stay tuned this feature is coming soon!");
}

var addAdmin = function() {
	$j.post("add_admin", { admin_id: $j(this).attr("value")}, null, "script");
}

$j(document).ready(function(){
	
	initTabs();
	
	$j(document).ajaxSend(function (event, request, settings) {
		if (typeof(auth_token) == "undefined") return;
		settings.data = settings.data || "";
		settings.data += (settings.data ? "&" : "") + "authenticity_token=" + encodeURIComponent(auth_token);
	});

  $j('#create-account-btn').submitOnClick();
  $j('#upload-photo-btn').submitOnClick();
  
  
  // Wall building widgets
  $j('#sortable-wall').sortable(imageWallSortableParams);
  
  $j('.draggable').draggable(imageWallDraggableParams);
  
  $j('.droppable').droppable(imageWallDroppableParams);
  
  // Non-ajax Delete links
	$j('a.delete').live("click", function () {
		if (confirm('Are you sure?')) {
    	$j.post($j(this).attr("href"), { _method: 'delete' }, null, "script");
    	return false;
		}
		return false;
	});
	
  // Delete Wall Images
	$j('#image-wall a.delete.remote').live("click", function () {
		if (confirm('Are you sure?')) {    	
    	$j.post(this.href, { photo_id: $j(this).parents("li").attr("id").split("_")[1]}, null, "script");  
    	return false;
		}
		return false;
	});
	
	// Promote to Featured Images
	// TODO note the {foo:"bar"} that's to flag the fact that the ajaxSetup function is wiping the authtoken out if there's no data there.
	$j('#image-wall a.featured.remote').live("click", function () {
    $j.post(this.href, { foo: "bar"}, null, "script");  
		return false;
	});
	
	// Add not implemented to invite button
	$j('.btn-invite').click(notImplemented);
	$j('.btn-upload-video').click(notImplemented);
	$j('.btn-view').click(notImplemented);
	
	$j('#administrator_id').change(addAdmin);
	// Recent Events on home page hovers
	// $j('div#carousel.gallery img').hover(function () {
	// 	$j(this).parents("li").find(".holder.hover-image").css("background", "url(" + $j(this).attr('src') + ") no-repeat").width(183).height(183);
	// });
	
});