var copy_count = 0;

function copy_task_booking(booking_dom_id)
{
	copy_count++;
	var new_dom_id = 'copy_' + copy_count + '_' + booking_dom_id;
	
	var html = $(booking_dom_id).innerHTML;
	html = html.replace(booking_dom_id, new_dom_id);
	html = html.replace('is_copy=false', 'is_copy=true')
	html = '<div class="booking" id="' + new_dom_id + '">' + html + '</div>';
	//alert(html);
	
	new Insertion.After(booking_dom_id, html);

	new Draggable(new_dom_id, {handle:'handle', revert:true});
}

/*
 * Toggle project
 */
function toggle_project(obj, project_id) {
  var but = $(obj);
  var pane = $('project_tasks_pane_' + project_id);

  var open = but.hasClassName('open');
  open = !open;
  
  /* Update arrow and project pane */
  if (open) {
    but.src = '/images/ic/ic_project_open.gif';
    but.addClassName('open');
    but.removeClassName('closed');
    pane.show();
  }
  else {
    but.src = '/images/ic/ic_project_closed.gif';
    but.addClassName('closed');
    but.removeClassName('open');
    pane.hide();
  }
  
  /* Save changes */
  new Ajax.Request('/projects/' + project_id + '/project_toggles', {
    asynchronous: true,
    evalScripts: true,
    method: open ? 'delete' : 'post',
    onComplete: function(request) { Element.hide('throbber') },
    onLoading: function(request) { Element.show('throbber') } });
}

/*
* Toggle display of new task form under projects in task list
*/
function toggle_new_task(link, project_id) {
	var pane = link.parentNode;
	var form_pane = 'quick_create_task_' + project_id;
	var text_field = 'new_task_name_' + project_id;
		
	link.blur();
	
	if (Element.hasClassName(pane, "minus")) {
		Element.removeClassName(pane, "minus");
		$(form_pane).style.display = "none";
		$(text_field).blur();
	}
	else {
		Element.addClassName(pane, "minus");
		$(form_pane).style.display = "block";
		$(text_field).focus();
	}
}

/*
* User autocomplete
*/
function init_user_select() {
  new Autocompleter.Local('user_lookup', 'user_lookup_auto_complete', users, { fullSearch: true, frequency: 0, minChars: 1, partialChars: 1 } );
}

/*
* Toggle display of pane in sidebar
*/
function toggle_sidebar_pane(link) {
	var sidebar_inner_pane = link.parentNode.parentNode;
	
	if (Element.hasClassName(sidebar_inner_pane, 'open')) {
		Element.addClassName(sidebar_inner_pane, 'closed');
		Element.removeClassName(sidebar_inner_pane, 'open');
	}
	else {
		Element.addClassName(sidebar_inner_pane, 'open');
		Element.removeClassName(sidebar_inner_pane, 'closed');
	}
}

/*
* Blur calender note without content (safari dont see a hover-out when hiding div)
*/ 
function blurEmptyNote(id) {
	
	/*$(id).style.opacity = "0.4"; // Tell safari to blur explicitly
	$(id).style.opacity = "1.0"; // Tell safari to blur explicitly
	$(id).style.opacity = "auto";*/
	
	$(id).addClassName('note_empty');
//	$(id).focus();
}

/*
* Toggle user/users aka private/global when creating a new notebook
*/
function toggleNotebookScope(scope) {
	if (scope == 'private') {
		$('notebook_type').value = 'private';
		$('notebook_type_user_icon').src = '/images/ic/ic_user_sel.gif';
		$('notebook_type_users_icon').src = '/images/ic/ic_users.gif';
	}
	else {
		$('notebook_type').value = 'global';
		$('notebook_type_user_icon').src = '/images/ic/ic_user.gif';
		$('notebook_type_users_icon').src = '/images/ic/ic_users_sel.gif';
	}
}

/*
* Toggle display of "other" subnav
*/
function toggleOtherNav(link) {
	link.blur();
	var toggle_state = $('other_sub_nav').style.display;
	var other_but = link.parentNode;
	
	if (toggle_state == "none") {
		Element.addClassName(other_but, 'open');
		$('other_sub_nav').style.display = "block";
	}
	else {
		Element.removeClassName(other_but, 'open');
		$('other_sub_nav').style.display = "none";
	}
}


/* Colorjack colorpicker start */
/* Colorjack colorpicker end */


/*
* Application onload function, called on each page
*/
function init_app() {
}

window.onload = init_app;