// crud

/* generic functions */

function save_order(){
	var output = JSON.stringify(
		$('#item_list').sortable('toArray')
	).replace(/c/g,'');
	
	$("#ordered_ids").val(output);
	$('#save_order_form').ajaxSubmit({});
}

$(document).ready(function() {
  $('.toolTip').hover(
    function() {
    this.tip = this.title;
    $(this).append(
     '<div class="toolTipWrapper">'
        +'<div class="toolTipTop"></div>'
        +'<div class="toolTipMid">'
          +this.tip
        +'</div>'
        +'<div class="toolTipBtm"></div>'
      +'</div>'
    );
    this.title = "";
    this.width = $(this).width();
    $(this).find('.toolTipWrapper').css({left:this.width-22})
    $('.toolTipWrapper').fadeIn(300);
  },
    function() {
      $('.toolTipWrapper').fadeOut(100);
      $(this).children().remove();
        this.title = this.tip;
      }
  );
});

$(document).ready(function() {
  $('.menuToolTip').hover(
    function() {
    this.tip = this.title;
    $(this).append(
     '<div class="toolTipWrapper">'
        +'<div class="toolTipTop"></div>'
        +'<div class="toolTipMid">'
          +this.tip
        +'</div>'
        +'<div class="toolTipBtm"></div>'
      +'</div>'
    );
    this.title = "";
    this.width = $(this).width();
    $(this).find('.toolTipWrapper').css({left:this.width-22})
    $('.toolTipWrapper').fadeIn(300);
  },
    function() {
      $('.toolTipWrapper').fadeOut(100);
      $(this).children().remove();
        this.title = this.tip;
      }
  );
});

function my_debug(msg) {
	// TODO: put in browser check and do not do in IE
//	console.debug(msg);
}

function confirm_delete(url, msg) {
	var question = msg ?
	'Do you really want to delete ' + msg + '?':
	'Do you really want to delete?';
	var answer = confirm(question)
	if(answer) {
		document.location = url;
	}
}

function refresh_chunk_list () {
    // refresh the item array so that it knows something has changed
	$("#item_list").sortable('refresh');	
}

function get_into_dom(conf) {
	var url 		   = conf.url;
	var action    	   = conf.action;
	var element   	   = conf.element;
	var add_class      = conf.add_class;
	var remove_class   = conf.remove_class;

	$.get(url, function(data) {
		var new_element = $(data);
	   // Not sure why we need it hidden - and it breaks stuff
       // new_element.hide();
		
		if(add_class) {
				new_element.addClass(add_class);
		}
		if(remove_class) {
				new_element.removeClass(remove_class);
		}

		if(action == 'replace') {
			my_debug('replacing');
			element.replaceWith(new_element);
		} else {
			element.after(new_element);
		}

        // let the new element appear and then scroll to it.
        new_element.slideDown(
            'slow',
            function () {
                $.scrollTo(
                    new_element,
                    { duration: 'slow' }
                );
            }
        );
		refresh_chunk_list();
	});
}

// fade out error/status/success messages after a while
$(function(){
	if ($(".error_msg, .status_msg, .success_msg").size() > 0) {
		var removeMsg = setTimeout(function(){
			$(".error_msg, .status_msg, .success_msg")
			.animate({"opacity":0, "height":0, "margin":0, "padding":0}
			,1000
			,function(){
				// callback for end of animation
				$(this).hide();
			});
			clearTimeout(removeMsg);
		}, 7000);
	}
})
