//(function($) {
//////////////////////////////



function badge(obj,objId,makedraggable,makedeletable,showCoords,fullbadgeDraggable) {

	// cookie period
	var days = 7;
	var badgeCookieName = 'im_t3v-badge-'+objId+'_hidden';
	
	// var obj = $('#t3v-badge-1104');
	// var objId = '1104';
	// var makedraggable = '1';
	// var makedeletable = '1';
	// var showCoords = '1';
	// var fullbadgeDraggable = 'true';
	
	 
	 
	
	// load positions form cookies
	obj.each( function( index ){					   
		$(this).css( 'left',
			$.cookie( 'im_' + this.id + '_left') );		
		$(this).css( 'top',
			$.cookie( 'im_' + this.id + '_top') );
	});
	 
	 
	// create movebutton
	 if (makedraggable== '1'){
	// move button einfügen	
	$('#t3v-badge-'+objId+'').append('<div id="t3v-badge-move-'+objId+'" class="draggable t3v-badge-move-'+objId+'">' + '<img src="typo3conf/ext/t3v_badge/res/img/move3.png" width="16" height="16" border="0" title="bewegen" alt="bewegen">' + '</div>');
	
	 }
	 
	 
	 // createn deletebutton
	 if (makedeletable== '1'){
	// delete button einfügen	
	$('#t3v-badge-'+objId+'').append('<div id="t3v-badge-close-'+objId+'" class="draggable t3v-badge-close-'+objId+'">' + '<a href="#">' + '<img src="typo3conf/ext/t3v_badge/res/img/close2.png" width="16" height="16" border="0" title="schließen" alt="schließen">' + '</a>' + '</div>');
	 }


	 // create container für um move/delete buttons
	  if ((makedraggable== '1')||(makedeletable== '1')){
			$('.draggable').wrapAll('<div id="t3v-badge-function-container-'+objId+'" class="draggable t3v-badge-function-container-'+objId+'" style="" ></div>');
	  }


// make draggable, show, bind event
	//
	if((fullbadgeDraggable) == '1' ) {
		obj.draggable({cursor: 'move'});
	} else {
		$('div.#t3v-badge-move-'+objId).hover(					
						function () {
							obj.draggable({cursor: 'move'});
						}
		)
	}
	
	// make visible
	obj.show();
	obj.bind('dragstop', savePos);
	 
	// save positions into cookies
	function savePos( event, ui ){
		$.cookie('im_' + this.id + '_left',
		$(this).css('left'), { path: '/', expires: days });
		$.cookie('im_' + this.id + '_top',
		$(this).css('top'), { path: '/', expires: days });
	}
	
	
	// fade in/out - move button
						
	$('div.#t3v-badge-move-'+objId)
		// ausblenden, sonst erscheint movebutton kurz
		.css({display:'none'})
		.css({opacity:'0'});
		//.animate({opacity:'0'});
		
	// fade in/out - close button
	$('div.#t3v-badge-close-'+objId)
		// ausblenden, sonst erscheint closebutton kurz
		.css({display:'none'})
		.css({opacity:'0'});
		//.animate({opacity:'0'});

/////////////////////////////////////////////////////////////////
// wenn plugin jquery.hoverIntent.js, dann nutze es

if($.fn.hoverIntent){
	$('div.#t3v-badge-'+objId).hoverIntent(					
		function () {
			$('div.#t3v-badge-move-'+objId)
				// movebutton display aktivieren
				.css({display:'block'})
				// movebutton einblenden
				.animate({opacity:'1'});
			
			$('div.#t3v-badge-close-'+objId)
				// closebutton display aktivieren
				.css({display:'block'})
				// closebutton einblenden
				.animate({opacity:'1'});	
		},
		function () {
		$('div.#t3v-badge-move-'+objId)
			// movebutton ausblenden
			.animate({opacity:'0'});
		
		$('div.#t3v-badge-close-'+objId)
			// closebutton ausblenden
			.animate({opacity:'0'});
		}	
	)
}else{
	$('div.#t3v-badge-'+objId).hover(					
		function () {
			$('div.#t3v-badge-move-'+objId)
				// movebutton display aktivieren
				.css({display:'block'})
				// movebutton einblenden
				.animate({opacity:'1'});
			
			$('div.#t3v-badge-close-'+objId)
				// closebutton display aktivieren
				.css({display:'block'})
				// closebutton einblenden
				.animate({opacity:'1'});	
		},
		function () {
		$('div.#t3v-badge-move-'+objId)
			// movebutton ausblenden
			.animate({opacity:'0'});
		
		$('div.#t3v-badge-close-'+objId)
			// closebutton ausblenden
			.animate({opacity:'0'});
		}	
	)
}
/////////////////////////////////////////////////////////////////
	
	
	
		
	// cookieabfrage, wenn cookie = hidden, dann badge ausgeblendet
	if($.cookie(badgeCookieName) == 'hidden') {
		$('div.#t3v-badge-'+objId)
			.hide();
	} else {
		$('div.#t3v-badge-'+objId)
			.show();
	}
	
	// fade in/out - close button
	$('div.#t3v-badge-close-'+objId).click(					
				function () {
					$('div.#t3v-badge-'+objId)
						.animate({opacity:'0'})
						.hide();
					$.cookie(badgeCookieName, 'hidden', { path: '/', expires: days });
				}
	)
	
	
	
	// wenn showCoords aktive, dann Koordinaten anzeigen
	if (showCoords == '1'){		
		$('.t3v-badge-inner-'+objId).append(
									'<div id="t3v-coordinates" class="t3v-coordinates"><span class="direction">left:</span> ' 
									// Werte auf/abrunden
									+ Math.round($('div.#t3v-badge-'+objId).position().left) + ' px'
									+ '<br /> <span class="direction">top:</span> '
									// Werte auf/abrunden
									+ Math.round($('div.#t3v-badge-'+objId).position().top) + ' px'
									+ '</div>'
									);
	}
	// nach drag die Koordinatenwerte updaten
	$('div.#t3v-badge-'+objId).mouseup(					
				function () {
					$('.t3v-coordinates').replaceWith(
									'<div class="t3v-coordinates"><span class="direction">left:</span> ' 
									// Werte auf/abrunden
									+ Math.round($('div.#t3v-badge-'+objId).position().left) + ' px'
									+ '<br /><span class="direction">top:</span> '
									// Werte auf/abrunden
									+ Math.round($('div.#t3v-badge-'+objId).position().top) + ' px'
									+ '</div>');					
				})

};
//////////////////////////////
//})(jQuery);
