$(document).ready(function() {
	
	// Formfieldhighlighter
	if($("form.fieldform").length){
		$.getScript( "/cms/js/jquery/formfieldfocus.js" ) ;
	};

	// Klickfelder
	if($(".biglinks").length){
		$.getScript("/cms/js/jquery/plugins/jquery.biggerlink.pack.js", function(){
			$('.biglinks div').biggerlink() ;
		});
	};

	// Klappboxen
	if( $(".klappbox").length || $(".klappboxsingle").length || $(".singlebox").length){
		$.getScript( "/cms/js/jquery/klappbox.js" ) ;
	};

	// Autotagger
	if( $("input.autotagging").length ){
		$.getScript("/cms/js/jquery/plugins/jQuery.jTagging.js", function(){
			var tagDivList = new Array( $("#taglist") ) ;
			var normalClass = { padding : "1px 5px", color : "#444", backgroundColor : '#b2cfff' } ;
			var selectedClass = { padding : "1px 5px", color : "#444", backgroundColor : "#9bff38"} ;
			var normalHoverClass = { padding : "1px 5px", color : "#444", backgroundColor : "#e3ff6b"} ;
			$("input.autotagging").jTagging(tagDivList, " , ", normalClass, selectedClass, normalHoverClass) ;
		});
	}

	// Styleswitcher
	if( $("#styleswitcher").length ){
		$.getScript("/cms/js/jquery/plugins/jquery.cookie.js", function(){
			$.getScript( "/cms/js/jquery/styleswitcher.js" ) ;
		});
	};

	// Tooltips
	if( $(".tooltip").length ){
		$.getScript("/cms/js/jquery/plugins/jquery-tooltip/jquery.tooltip.min.js", function(){
			
			$(".tooltip").tooltip(	{
										track: true,
										showURL: false,
    									delay: 1000, 
    									fade: 350
    								});
			
			a = document.createElement('link') ;
			a.setAttribute('rel','stylesheet') ;
			a.setAttribute('type','text/css') ;
			a.setAttribute('href','/cms/js/jquery/plugins/jquery-tooltip/jquery.tooltip.css') ;
			document.getElementsByTagName('head')[0].appendChild(a) ;
			
		});
	};

	// McDropDownMenus
	if( $(".mddmenu").length ){
		$.getScript("/cms/js/jquery/plugins/jquery.bgiframe.js", function(){
			$.getScript("/cms/js/jquery/plugins/mcdropdown/jquery.mcdropdown.min.js", function(){
				
				if( $(".mddmenu #mddwahl").length ) {
					$(".mddmenu #mddwahl").mcDropdown("#bildwahlalben", 	{
																				valueAttr: "value" ,
																				delim: ">" ,
																				hoverOverDelay: 350 ,
																				allowParentSelect: true ,
																				select: mddchange
	        																}
					);
				}
				if( $(".mddmenu #mddwahl_downloadkategorien").length ) {
					$(".mddmenu #mddwahl_downloadkategorien").mcDropdown("#downloadkategorien", 	{
																				valueAttr: "value" ,
																				delim: ">" ,
																				hoverOverDelay: 350 ,
																				allowParentSelect: true ,
																				select: mddchange_downloadkategorien
	        																}
					);
				}
				
				a = document.createElement('link') ;
				a.setAttribute('rel','stylesheet') ;
				a.setAttribute('type','text/css') ;
				a.setAttribute('href','/cms/js/jquery/plugins/mcdropdown/css/jquery.mcdropdown.css') ;
				document.getElementsByTagName('head')[0].appendChild(a) ;
			});
		});
	};

	// Scrollable ohne Init!
	if( $("#scrollable").length ){
		$.getScript("cms/js/jquery/plugins/jquery.mousewheel.js", function(){
			$.getScript("cms/js/jquery/plugins/scrollable/jquery.scrollable.js", function(){
				
				a = document.createElement('link') ;
				a.setAttribute('rel','stylesheet') ;
				a.setAttribute('type','text/css') ;
				a.setAttribute('href','cms/js/jquery/plugins/scrollable/scrollable.css') ;
				document.getElementsByTagName('head')[0].appendChild(a) ;
			});
		});
	};

	// asmSelects
	if( $(".asmselector").length ){
		$.getScript("cms/js/jquery/plugins/jquery.hotkeys.js", function(){
			$.getScript("cms/js/jquery/plugins/asmselect/jquery.asmselect.js", function(){
				
				$( ".asmselector" ).each(function(){
					var count = $(this).children().length ;
					if ( count != 0 ) {
						
						$(this).asmSelect({
							animate: true,
							highlight: false,
							removeLabel: "Entfernen",
							sortable: true
						});
						
						// Pruefen ob Addbutton vorhanden zur Liste
						var asmbox = $(this).parents("div.asmbox") ;
						var button = asmbox.contents().find("button") ;
						if ( button.length ) {
							
							// Addbutton-Funktion hinzufuegen
							button.click(function() {
								
								var id = $(this).attr("name") ;
								
								var entry = $("#" + id + " .neuerauswahlwert").val();
								var $option = $("<option></option>").text(entry).attr("selected", true); 
								$("#" + id + " .asmselector").append($option).change();
								$("#" + id + " .neuerauswahlwert").val("");
								return false; 
								
							});
							
						}
					}
		      	});
				
		      	// Formsubmit unterdruecken und propid für Keybinding festhalten
		      	$("input[type='text']").focus(function () {
					$("input[type='submit']").attr("disabled", "true");
					var propbox = $(this).parents("div.propid") ;
					var id = propbox.attr("id") ;
					$('form').data('neuauswahldata', id );
		      	});
		      	$("input[type='text']").blur(function () {
		      		$("input[type='submit']").removeAttr("disabled")
		      	});
		      	$(".submitfield").mouseover( function(){
		      		$("input[type='submit']").removeAttr("disabled") ;
		      	});
	            $(document).bind('keyup', 'return', function(){
	            	var id = $('form').data('neuauswahldata');
	            	 $("#" + id + " .addauswahlwert").trigger('click');
	        	});
	            
				a = document.createElement('link') ;
				a.setAttribute('rel','stylesheet') ;
				a.setAttribute('type','text/css') ;
				a.setAttribute('href','cms/js/jquery/plugins/asmselect/jquery.asmselect.css') ;
				document.getElementsByTagName('head')[0].appendChild(a) ;
			});
		});
	};

	// Nested Sortable
	if($("#nestedsort").length){
		
		a = document.createElement('link') ;
		a.setAttribute('rel','stylesheet') ;
		a.setAttribute('type','text/css') ;
		a.setAttribute('href','/cms/js/jquery/plugins/nested_sortable/inestedsortable.css') ;
		document.getElementsByTagName('head')[0].appendChild(a) ;
		
		$.getScript("/cms/js/jquery/plugins/interface-1.2.js", function(){
			$.getScript("/cms/js/jquery/plugins/nested_sortable/inestedsortable.js", function(){
				
				$('#nestedsort').NestedSortable({
													accept: 'page-item',
													noNestingClass: "no-nesting",
													opacity: .8,
													nestingPxSpace: 10 ,
													revert: true,
													helperclass: 'nestedhelper',
													onChange: function(serialized) {
														$('#nestedsort-ser').html( serialized[0].hash.replace(/&/g, "<br />") ) ;
													},
													autoScroll: true,
													handle: '.sort-handle'
												});
			});
		});
	};

	// Date-Input
	if( $("input.date_input").length ){
		
		a = document.createElement('link') ;
		a.setAttribute('rel','stylesheet') ;
		a.setAttribute('type','text/css') ;
		a.setAttribute('href','/cms/js/jquery/plugins/date-input/date_input.css') ;
		document.getElementsByTagName('head')[0].appendChild(a) ;
		
		$.getScript("/cms/js/jquery/plugins/date-input/jquery.dimensions.js", function(){
			
			$.getScript("/cms/js/jquery/plugins/date-input/jquery.date_input.js", function(){
				
				jQuery.extend(DateInput.DEFAULT_OPTS, {
														month_names: ["Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"] ,
														short_month_names: ["Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"] ,
														short_day_names: ["So", "Mo", "Di", "Mi", "Do", "Fr", "Sa"] ,
														stringToDate: function(string) {
															var matches ;
															if (matches = string.match(/^(\d{2,2}).(\d{2,2}).(\d{4,4})$/)) {
																return new Date(matches[3], matches[2] - 1, matches[1]) ;
															} 
															else {
																return null ;
															} ;
														} ,
														dateToString: function(date) {
															var month = (date.getMonth() + 1).toString() ;
															var dom = date.getDate().toString() ;
															if (month.length == 1) month = "0" + month ;
															if (dom.length == 1) dom = "0" + dom ;
															return dom + "." + month + "." + date.getFullYear() ;
														}
				});
				
				$("input.date_input").date_input();
			});
		});
	};

	// Waehrungskonvertierungen
	if( $(".currency").length){
		$.getScript( "/cms/js/jquery/currency.js" ) ;
	};

	// Countdown
	if($("#countdown").length){
		
		a = document.createElement('link') ;
		a.setAttribute('rel','stylesheet') ;
		a.setAttribute('type','text/css') ;
		a.setAttribute('href','/cms/js/jquery/plugins/countdown/jquery.countdown.css') ;
		document.getElementsByTagName('head')[0].appendChild(a) ;
		
		$.getScript("/cms/js/jquery/plugins/countdown/jquery.countdown.pack.js", function(){
			$.getScript("/cms/js/jquery/plugins/countdown/jquery.countdown-de.js", function(){
				
				// Gucken ob Liftofftime gesetzt , wenn nicht dann den 31. des Monats
				if ( typeof liftoffTime == "undefined") {
					
					var liftoffTime = new Date() ;
					liftoffTime.setDate(31) ;
				}
				
				$('#countdown').countdown({until: liftoffTime, format: 'dHMS'});
				
			});
		});
	};

	// Newsvorschauscript
	if( $("form#newsedit").length ){
		$.getScript("/cms/js/jquery/plugins/jquery.form.js") ;
		$.getScript("/cms/js/jquery/admin_news_preview.js") ;
	};

	// Bildermodulvorschauscript
	if( $("form#bildkategorieedit").length ){
		$.getScript("/cms/js/jquery/admin_bildkategorie.js") ;
	};

	// Downloadkategorievorschauscript
	if( $("form#downloadkategorieedit").length ){
		$.getScript("/cms/js/jquery/admin_downloadkategorien.js") ;
	};
	
	// Downloadvorschauscript
	if( $("form#downloadedit").length ){
		$.getScript("/cms/js/jquery/admin_downloads.js") ;
	};

	// Produktgruppenvorschauscript
	if( $("form#produktgruppenedit").length ){
		$.getScript("/cms/js/jquery/plugins/jquery.form.js") ;
		$.getScript("/cms/js/jquery/admin_produktgruppe_preview.js") ;
	};

	// Produktvorschauscript
	if( $("form#produktedit").length ){
		$.getScript("/cms/js/jquery/plugins/jquery.form.js") ;
		$.getScript("/cms/js/jquery/admin_produkt_preview.js") ;
	};

	// Seitenvorschauscript
	if( $("form#seitenedit").length ){
		$.getScript("/cms/js/jquery/plugins/jquery.form.js") ;
		$.getScript("/cms/js/jquery/admin_seiten_preview.js") ;
	};

/* End */
});
