/*================================================================================
/ Funktionen für User-Clipboard
================================================================================*/

var callback = '/callback/ajax-clipboard.asp'

/*--------------------------------------------------------------------------------
clipboardInit
--------------------------------------------------------------------------------*/
function clipboardInit() {
    $('.clipboards').sortable( {
        cursor: 'move', 
        update: clipboardSort
    } );

    $('.clipboardEntries').sortable( {
        connectWith: '.clipboardEntries',
        cursor: 'move', 
        update: clipboardEntrySort
    } );

    $('.clipboards, .clipboardEntries').disableSelection();
}

/*--------------------------------------------------------------------------------
clipboardSort
--------------------------------------------------------------------------------*/
function clipboardSort(event, ui) {
    sort = $(this).sortable('toArray');
    $.get(callback + '?t=' + $.cookie('useraccesstoken') + '&m=clipboardSort&s=' + escape(sort) + '&d=' + Math.random(), function(data) {
        if (data) alert(data)
    } )
}

/*--------------------------------------------------------------------------------
clipboardEntrySort
--------------------------------------------------------------------------------*/
function clipboardEntrySort(event, ui) {
    board = $(this).attr('id')
    sort = $(this).sortable('toArray')
    $.get(callback + '?t=' + $.cookie('useraccesstoken') + '&m=clipboardEntrySort&b=' + board + '&s=' + escape(sort) + '&d=' + Math.random(), function(data) {
        if (data) alert(data)
    } )
}

/*--------------------------------------------------------------------------------
clipboardAdd
--------------------------------------------------------------------------------*/
function clipboardAdd() {
}

/*--------------------------------------------------------------------------------
clipboard (jQuery)
--------------------------------------------------------------------------------*/
;(function($) {
    $.clipboard='clipboard';
    function removemenu(e){
	    var el = e.target;
	    if( $(el).parent('#'+$.clipboard).size()==0) {
	        $('#'+$.clipboard).remove();
	    }
	    return false;
    }
    function firemenu(e, menu, el) {
	    removemenu(e);
    	$(el).data('clickphase', true);
    	setTimeout(function(){$(el).removeData('clickphase');},300);
    	var m = $('<ul id="'+$.clipboard+'">');
    	//menu['neue Liste'] = [0, 0]
    	$.each(menu, function(n, a) {
    	    id = 'board' + a[0]
	        m.append($('<li id="' + id + '">').html(n).hover(
	            function(){
		            $(this).addClass("hover");
	            },
	            function(){
		            $(this).removeClass("hover");
	            })
            .click(
                function(evt){
                    alert("click")
                    $('#'+$.clipboard).remove();
                    $(el).data('served', true);
                    //a(evt, el);
                    $(el).removeData('served');
		         })
            .mouseup(
                function(evt){
                    // Clip / Unclip
                    board = $(evt.target).attr('id').replace(/board/, '')
                    toclip = $(el).attr('id').replace(/clipboard\-\d+\-(\d+)/, '$1')
                    type = $(el).attr('id').replace(/clipboard\-(\d+)\-\d+/, '$1')
                    url = callback + '?ty=' + type + '&b=' + board + '&id=' + toclip + '&t=' + $.cookie('useraccesstoken') + '&d=' + Math.random()
                    if ($(evt.target).hasClass('clipped')) {
                        url += '&m=clipboardRemove'
                    } else {
                        url += '&m=clipboardAdd'
                    }
                    $.get(url, function(data) {
                        if (data) alert(data)
                        else {  // hat geklappt, jetzt das Menu neu einlesen
                            $.get(callback + '?ty=' + type + '&id=' + toclip + '&t=' + $.cookie('useraccesstoken') + '&m=clipboardGetLists&d=' + Math.random(), function(data) {
                                newmenu = new Object();
                                cc = 0
                                $.each(data.split(/\r\n/), function(i, v) {
                                    if (v) {
                                        eval(v)
                                        c = parseInt(clipped)
                                        cc += c
                                        newmenu[board_name] = [ id, c ];
                                    }
                                } )
                                // Clipboard neu erstellen
                                $(el).clipboard(newmenu, 'hover')
                                if (cc) {
                                    $(el).removeClass('notclipped').addClass('clipped')
                                } else {
                                    $(el).removeClass('clipped').addClass('notclipped')
                                }
                            } )
                        }
                    } )

                    
                    if($(el).data('clickphase')) return true;
                    $('#'+$.clipboard).remove();
                    $(el).data('served', true);
                    //a(evt, el);
                    $(el).removeData('served');
		         })
            .addClass((a[1] ? 'clipped' : 'notclipped'))
            );
        });
        $('<div class="title">Ihre Merklisten</div>').prependTo(m)
	    var pageX = e.pageX;
	    var pageY = e.pageY;
	    if(window.event) {
	        pageX = $(window).scrollLeft()+e.clientX;
	        pageY = $(window).scrollTop()+e.clientY;
	    }
	    m.css({'position':'absolute',top:pageY-3,left:pageX-3});
	    $("body").append(m);
	    document.getElementById($.clipboard).oncontextmenu=function(){return false;};
	    $("body").one('click', removemenu);
	    $(document).one('mousedown', removemenu);
    }
    $.fn.clipboard=function(menu, mode, timeout) {
	var els = this;
	$.each(els, function(i,el) {
	    switch(mode) {
    	    case 'right':
	        	el.oncontextmenu = function(e){
	        	    if(e && !e.bubbles) {return true;} 
	        	    if(!e) e=window.event; 
	        	    if(!e.target) e.target = e.srcElement; 
	        	    firemenu(e,menu,el); 
	        	    if(window.event) 
	        	        window.event.cancelBubble = true; 
	        	    else 
	        	        e.stopPropagation();
                    return false;
                };
    	    	break;
	        case 'hold':
		        var timeoutfun = null;
        		$(el).mousedown(function(e) {
		            if(!e.bubbles) return;
		            if(e.button == 2) return ;
		            timeoutfun = setTimeout(function() {
			            $(el).data('menu',true);
			            firemenu(e,menu,el);
			            return false;
		            },timeout);
		        }).mouseup(function(e){
		            clearTimeout(timeoutfun);
		            if($(el).data('menu')) {
		        }
	            }).bind('dragstart',function(e){
		            clearTimeout(timeoutfun);
		            if($(el).data('menu')==true) return false;
		            return true;
		        });
		        break;
	        case 'hover':
		        var timeoutfun = null;
		        $(el).mouseover(function(e){
		            //if(!e.bubbles) return;
		            if($('#'+$.clipboard).size()>0) return ;
		            if($(el).data('served')==true) return ;
		            timeoutfun = setTimeout(function() {
			            $(el).data('menu',true);
    			        firemenu($(el).data('e'), menu,el);
		            }, timeout);
		            return false;
		        }).mousemove(function(e){
		            $(el).data('e',e);
		        }).mouseout(function(e){
		            clearTimeout(timeoutfun);
		            $(el).removeData('e');
		        }).mousedown(function(e){
		            clearTimeout(timeoutfun);
		            $(el).removeData('e');
		        });
		    break;
	    }
	    return $(this);
	});
    }
})(jQuery);

