// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "").replace("\n", "");
}

function fixTabHeights() {
  $(".tabs:not(.no-resize)").each(function(tabsidx, tabs) {
    var children = $(tabs).find(".tab-contents");
    var heights = children.map(function(idx, elem) { return $(elem).height(); });
    var maxHeight = Math.max.apply(Math, jQuery.makeArray(heights));
    $(tabs).height(maxHeight);
  });
}

function newTaskSuccess(responseText, statusText)  { 
    $(".newTaskForm").hide(); 
}

function newCategorySuccess(responseText, statusText)  { 
    $("#newCategoryForm").hide(); 
}

$(document).ready(function() {
  fixTabHeights();

  $("#tabs").tabs();   
  $(".date-pick").livequery(function(){
    $(".date-pick").datepicker({dateFormat: 'dd-mm-yy'});
  });
  $(".date-pick-button").livequery("click", function() {
    $(".date-pick").datepicker('show');
  });
  
});


// form stuff used across multiple pages
$(document).ready(function() {        
  var taskOptions = { 
    dataType: "script", 
    target:        '#todo-list',   // target element(s) to be updated with server response  
    success:       newTaskSuccess,  // post-submit callback 
    resetForm: true        // clear all form fields after successful submit 
  }; 
    
  var categoryOptions = { 
    dataType: "script", 
    target:        '#todo-list',   // target element(s) to be updated with server response  
    success:       newCategorySuccess,  // post-submit callback  
    resetForm: true        // clear all form fields after successful submit 
  };
    
  $(".newTaskForm").livequery(function(){    
    $(".newTaskForm").ajaxForm(taskOptions);
  });

	$(".propagatable").livequery(function () {
		var propagate = function() {
			var submitted_element_id = this.id.replace('user_', '');
			var submitted_element = $(this).parents('tr').find('#' + submitted_element_id)
			submitted_element.attr('value', this.value);
		}
		propagate.call(this);
		$(this).change(propagate);
	});

	
	$(".formSubmitter").livequery('click', function() {
		$(this).parents('form').find('input[type=submit]').click();
		return false;
	});
	
  
  $("#newCategoryForm").ajaxForm(categoryOptions);
  
  $(".newTaskForm").livequery(function() {
      $(this).hide();
  });
  
  $(".newCategoryForm").hide();
      
	$(".editRow,.addRow").livequery('click', function() {
		$(this).parents("tr").hide();
		var target = $(this).attr('href');
		if (target.match(/#$/)) {
			var elt = $(this).parents("tr").next();
		} else {
			var elt = $(target);	
		}
		elt.show();
		elt.find(".will-become-date-pick").addClass("date-pick")
		return false;
	});

	$(".cancelEditRow,.cancelAddRow").livequery('click', function() {
		$(this).parents("tr").hide();
		var target = $(this).attr('href');
		if (target.match(/#$/))
			$(this).parents("tr").prev().show();
		else
			$(target).show();
		return false;
	});

  $(".addTask").livequery('click',function(){
    $(this).siblings(".task_new").find('form').show();
    return false;
  });
  
  $("#addCategory").click(function(){
    $(".newCategoryForm").show();
    return false;
  });
  
  $(".discard").livequery('click',function() {
    $(this).parents('form').resetForm().hide();
    return false;
  });

  $(".lightboxit").colorbox({scalePhotos: true, maxWidth: "100%", maxHeight: "100%"}).bind('cbox_complete', function() { 
    var image_link = $.fn.colorbox.element();
    var description_div = document.createElement("div");
    var supplier_link = document.createElement("a");
    var link_div = document.createElement("div");
    var scrapbook_link = document.createElement("a");  

    $(description_div).text($(image_link).find(".description").text());
    $(description_div).addClass('titleCenter');

    $(supplier_link).text("View supplier details »");
    $(supplier_link).attr('href', $(image_link).find(".supplier_path").text());

    $(scrapbook_link).text("Save to scrapbook »");
    var form = $(image_link).find("form");
		$(scrapbook_link).attr('href', '#');
    $(scrapbook_link).click(function () { $(image_link).find("form").submit() } );

    $(link_div).html($(image_link).find(".scrapbook").html());
    $(link_div).append(scrapbook_link);
    $(link_div).append(document.createElement("br"));
    $(link_div).append(supplier_link);
    $(link_div).addClass('titleRight');

    $("#cboxTitle").empty();
    $("#cboxTitle").append(description_div);
    $("#cboxTitle").append(link_div);
	} );
	
	$(".expand_articles").click(function() {
		$("#teaser_articles,#all_articles").toggle();
		return false;
	});
	
	$(".save_scrapbook_link").click(function () {
		$(this).parent().siblings("form").submit();
		return false;
	});
});

function toggleTabClass (obj) {
	var li = $(obj).parent()[0];
	var ul =  $(li).parent()[0];

	var children = $(ul).children("LI");

	//Remove the "current" class from other li's
	for (i=0; i<children.length; i++) {
		if (children[i].className.indexOf("current") >= 0) {
			children[i].className = children[i].className.substring(0, children[i].className.indexOf("current"));
			break;
		}
	}

	//Apply the "current" class to the selected li
	li.className = li.className + " current";
}

function print() {
	$("div#content").printArea({popTitle : 'Modern Weddings'});
}