

$(function() {

	setInterval("slideSwitch()", 5000);

	if($('#wrapper_admin #navigation').hasClass('trans')){ $('#wrapper_admin #navigation a').click(function(){ return false; }); }

	$('.media').media({width: 320, height: 240, autoplay: true, caption: false});

	$("a[href^='http:']:not([href*='"  + window.location.host + "']):not([href^='/']):not([href^='#']):not([href^='mailto:']):not([rel^='zoombox'])", "#page").addClass('external').click( function(e) {
		window.open(this.href);
		e.preventDefault();
	});
	$('#adminlogin input[name="aname"]').focus();
	$('input[name="selectallcheckboxes"]').toggle(function(){
		$(this).attr({value:'Alle Checkboxen auswählen'});
		$('input[type="checkbox"]', $(this).parents('form')).attr({'checked':'checked'});
	}, function(){
		$(this).attr({value:'Auswahl entfernen'});
		$('input[type="checkbox"]', $(this).parents('form')).attr({'checked':false});
	});

	$('.markitup').markItUp(mySettings);

	$("#sort_pic").sortable({ 
		update: function(event, ui){
			var list_var = $("#sort_pic").sortable("serialize");
			//alert(list_var);
			$.post("/admin/index.php?seite=bilder&part=edit&action=sortieren", list_var, function(data){ 
				if(data != "true"){ 
					alert('Kein Update möglich, Fehler aufgetreten!'); 
				}else{
					sortstatus('Position gespeichert');
				}
			});
		},
		stop: function(e,ui) {
			ui.item.css({'top':'0','left':'0'});
		}
	}).css({'cursor':'move'});
	$("#sort_kat").sortable({ 
		update: function(event, ui){
			var list_var = $("#sort_kat").sortable("serialize");
			//alert(list_var);
			$.post("/admin/index.php?seite=sortieren&part=sort&action=sortieren", list_var, function(data){ 
				if(data != "true"){ 
					alert('Kein Update möglich, Fehler aufgetreten!'); 
				}else{
					sortstatus('Position gespeichert');
				}
			});
		},
		stop: function(e,ui) {
			ui.item.css({'top':'0','left':'0'});
		}
	}).css({'cursor':'move'});
	$("#partnertabelle").sortable({ 
		items: 'tr', 
		helper: function(event){
			return $('<div><table><colgroup><col width="40%" \/><col width="50%" \/><col width="*" \/><\/colgroup><\/table><\/div>').find('table').append($(event.target)
			.closest('tr').clone()).end().appendTo('body');
		},
		update: function(event, ui){
			//alert(event + '---' + ui);
			var list_var = $("#partnertabelle").sortable("serialize");
			//alert(list_var);
			$.post("/admin/index.php?seite=partner&do=update", list_var, function(data){ 
				if(data != "true"){
					alert('Kein Update möglich, Fehler aufgetreten!'); 
				}else{
					sortstatus('Position gespeichert');
				} 
			});
		}
	}).css({'cursor':'move'});
	$("#videosort").sortable({ 
		items: 'tr', 
		helper: function(event){
			return $('<div><table><colgroup><col width="*" \/><col width="200" \/><col width="50" \/><\/colgroup><\/table><\/div>').find('table').append($(event.target)
			.closest('tr').clone()).end().appendTo('body');
		},
		update: function(event, ui){
			//alert(event + '---' + ui);
			var list_var = $("#videosort").sortable("serialize");
			//alert(list_var);
			$.post("/admin/index.php?seite=referenzen&part=videos&do=sort", list_var, function(data){ 
				if(data != "true"){
					alert('Kein Update möglich, Fehler aufgetreten!'); 
				}else{
					sortstatus('Position gespeichert');
				}
			});
		}
	}).css({'cursor':'move'});
	$("#pressesort").sortable({ 
		items: 'tr', 
		helper: function(event){
			return $('<div><table><colgroup><col width="*" \/><col width="200" \/><col width="50" \/><\/colgroup><\/table><\/div>').find('table').append($(event.target)
			.closest('tr').clone()).end().appendTo('body');
		},
		update: function(event, ui){
			//alert(event + '---' + ui);
			var list_var = $("#pressesort").sortable("serialize");
			//alert(list_var);
			$.post("/admin/index.php?seite=referenzen&part=presse&do=sort", list_var, function(data){ 
				if(data != "true"){
					alert('Kein Update möglich, Fehler aufgetreten!'); 
				}else{
					sortstatus('Position gespeichert');
				}
			});
		}
	}).css({'cursor':'move'});
	$("#kundensort").sortable({ 
		items: 'tr', 
		helper: function(event){
			return $('<div><table><colgroup><col width="40%" \/><col width="18%" \/><col width="35%" \/><col width="*" \/><\/colgroup><\/table><\/div>').find('table').append($(event.target)
			.closest('tr').clone()).end().appendTo('body');
		},
		update: function(event, ui){
			//alert(event + '---' + ui);
			var list_var = $("#kundensort").sortable("serialize");
			//alert(list_var);
			$.post("/admin/index.php?seite=referenzen&part=kunden&do=sort", list_var, function(data){ 
				if(data != "true"){
					alert('Kein Update möglich, Fehler aufgetreten!'); 
				}else{
					sortstatus('Position gespeichert');
				}
			});
		}
	}).css({'cursor':'move'});
	$("a.dellink").click(function(e){
		e.preventDefault();
		var tabelle = $(this).parents('table');
		var zeile = $(this).parents('tr');
		var href = $(this).attr('href');
		var status = confirm('Wirklich löschen?');
		if(status == true){
			$.post(href, function(data){
				if(data != "true"){
					alert('Kein Löschen möglich, Fehler aufgetreten.');
				}else{
					$(zeile).fadeOut(500, function() { 
						$(zeile).remove();
						$("tbody tr", $(tabelle)).removeClass("odd even");
						$("tbody tr:odd", $(tabelle)).addClass("odd");
						$("tbody tr:even", $(tabelle)).addClass("even");
					});
				}
			});
		}
	});
	$("input[name=startpic]").click(function(e){
		var id = $(this).val();
		//alert(id);
		$.post("/admin/index.php?seite=bilder&part=edit&action=startpic", {id: id}, function(data){ 
			if(data != "true"){ 
				alert('Kein Update möglich, Fehler aufgetreten!'); 
			}else{
				sortstatus('Startbild gespeichert');
			} 
		});
	});
	$("#sort input[name^='del'], #sort_pic input[name^='del']").click(function(e){
		var checkbox = $(this);
		var li = $(this).parents('li');
		var backgroundcolor = $(li).css('background-color');
		$(li).css({'background-color':'#ff0000'});
		var status = confirm('Dieses Bild wirklich löschen?');
		//alert(status);
		if(status == true){
			var id = $(this).val();
			//alert(id);
			$.post("/admin/index.php?seite=bilder&part=edit&action=del", {id: id}, function(data){ 
				if(data != "true"){ 
					alert('Kein Löschen möglich, Fehler aufgetreten!'); 
				}else{
					$(li).fadeOut(500, function() { $(li).remove(); });
					sortstatus('Bild gelöscht');
				}
			});
		}else{
			$(li).css({'background-color':backgroundcolor});
			$(checkbox).attr('checked','').val(0);
		}
	});
});

function sortstatus(message){
	if($('.sortstatus').length > 0){ $('.sortstatus').remove(); }
	$('#navigation').append('<p class="sortstatus">' + message +'!<\/p>');
	$('.sortstatus').css({'margin':'20px 0','color':'#ff0000','font-weight':'bold'});
	$('.sortstatus').fadeIn().animate({opacity: 1.0}, 2000).fadeOut('slow', function() { $('.sortstatus').remove(); } );
}

/*** 
    Simple jQuery Slideshow Script
    Released by Jon Raasch (jonraasch.com) under FreeBSD license: free to use or modify, not responsible for anything, etc.  Please link out to me if you like it :)
***/

function slideSwitch() {
    var $active = $('#slideshow img.active');

    if ( $active.length == 0 ) $active = $('#slideshow img:first');

    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next() : $('#slideshow img:first');

    // uncomment the 3 lines below to pull the images in random order
    
    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );


    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}

