//You need an anonymous function to wrap around your function to avoid conflict
(function($){

	//Attach this new method to jQuery
 	$.fn.extend({

 		//This is where you write your plugin's name
 		FLEXDownloadBasket: function() {
			function getBasketText(element) {
				var returnValue = element.text();
				//Downloads-Template
//				var hauptUeberschrift = element.closest("div#boxModellBugInhaltMitte").find("h1.hauptUeberschrift");
//				if(hauptUeberschrift.length>0) returnValue = hauptUeberschrift.text() + " " + returnValue;
				return returnValue;
			}
			//Iterate over the current set of matched elements
    		return this.each(function() {
    			if(
    				$(this).attr("href")!="javascript:history.back()" &&
    				$(this).attr("href")!="?" &&
    				$(this).closest("div").attr("id") != "reiterABC" &&
    				!$(this).closest("div").hasClass("noDownloadBasket")
    			) {
					$(this).css("position","relative").contextMenu(
						{
							menu: "myMenu"
				    	},
				        function(action, el, pos) {
				        	switch(action) {
				        		case "download":
				        			location.href = $(el).attr("href");
				        			break;
				        		case "add":
				        			$("#FLEXDownloadBasketInner").load(
				        				"/lib/inc.FLEXDownloadBasket.php",
				        				{
				        					a : $(el).attr("href"),
				        					'data[text]' : getBasketText($(el))
				        				},
				        				function(responseText, textStatus, XMLHttpRequest) {
				        					initFLEXDownloadBasketInnerst();
				        				}
				        			);
				        			break;
				        	}
				   		}).click(function() {return false;});
    			}
    		});
    	}
	});


})(jQuery);

function initFLEXDownloadBasketInnerst() {
	jQuery("div#FLEXDownloadBasketInnerDetails input").click(function() {
		if(jQuery(this).attr("checked")) {
			jQuery(this).closest("li").attr("class","checked");
		} else {
			jQuery(this).closest("li").attr("class","");
		}
	});
	jQuery("a#FLEXDownloadBasketEdit").click(function() {
		jQuery(this).hide();
		jQuery("div#FLEXDownloadBasketInnerDetails").slideDown(420, function() {
			jQuery("div#FLEXDownloadBasket").addClass("open");
			jQuery("a#FLEXDownloadBasketSave").show();
		});
	});
	jQuery("a#FLEXDownloadBasketSave").click(function() {
		var restItems = [];
		jQuery("div#FLEXDownloadBasketInnerDetails li.checked a").each(function() {
			restItems.push(jQuery(this).attr("href"));
		});
		jQuery("#FLEXDownloadBasketInner").load(
			"/lib/inc.FLEXDownloadBasket.php",
			{
				a : "setRestItems",
				data : restItems.join("|")
			},
			function(responseText, textStatus, XMLHttpRequest) {
				initFLEXDownloadBasketInnerst();
			}
		);
		jQuery(this).hide();
		jQuery("div#FLEXDownloadBasket").removeClass("open");
//		jQuery("div#FLEXDownloadBasketInnerDetails").slideUp(420, function() {
//			jQuery("a#FLEXDownloadBasketEdit").show();
//		});
	});
}

jQuery(document).ready(function(){
	initFLEXDownloadBasketInnerst();
	jQuery("div#boxModellBugInhaltMitte a").FLEXDownloadBasket();
});
