jQuery(document).ready(function(){

});

function app_treeView() {

     
	app_sortable('.treeView');
	app_sortable('.treeView ol');

	jQuery('.treeView li div.node > div.left > a.expandReduce').click(function() {
		$this = jQuery(this);
		if ($this.hasClass('expand')) { 
			$this.removeClass('expand'); 
			$this.addClass('loader'); 
			jQuery.ajax({
				type: 'POST',
				url: '/website/index/structure/id/' + $this.attr('rel'),
				success: function(msg) {
					$this.removeClass('loader');
					$this.addClass('reduce');
					$this.parent().parent().siblings('ol').show();
				}
			});
		}
		else if ($this.hasClass('reduce')) { 
			$this.removeClass('reduce'); 
			$this.addClass('loader'); 
			jQuery.ajax({
				type: 'POST',
				url: '/website/index/structure/id/' + $this.attr('rel'),
				success: function(msg) {
					$this.removeClass('loader');
					$this.addClass('expand'); 
					$this.parent().parent().siblings('ol').hide();
				}
			});
		}
	});
	
	jQuery('.treeView .right ul li a.delete').click(function() {
		$this = jQuery(this);
		if (confirm('Wollen sie dieses Struktur-Element wirklich löschen?')) {			
			jQuery.ajax({
				type: 'POST',
				url: $this.attr('href'),
				success: function(msg) {
					$this.parents('.node').parent().fadeOut(250, function() { jQuery(this).remove(); });
				}
			});
		}
		return false;
	});
}

function app_sortable(cssClass) {
	jQuery(cssClass).sortable({
		cursor: 'move',
		opacity: 0.7,
		update: function() {
			var order = jQuery(this).sortable("toArray");
			serStr = '';
			var sorterCounter = 0;
			jQuery.each(order, function(index, value) {
				valueSplit = value.split('_');
				if (sorterCounter > 0) { serStr += '&'; }
				serStr = serStr + 'id[' + valueSplit[2] + ']=' + valueSplit[1];
				sorterCounter++;
			});
			jQuery.ajax({
				type: 'POST',
				url: '/website/index/structureSorter',
				data: serStr,
				success: function(msg) {
					
				}
			});
		}
	});	
}

function app_deletePic(el, id, typePic) {
	if (confirm('Wollen Sie das Element wirklich löschen?')) {
		$this = jQuery(el);
		jQuery.ajax({
			type: 'GET',
			url: '/admin/structure/deletePic/id/' + id + '/type/' + typePic,
			success: function(msg) {
				$this.parent().fadeOut(250, function() { jQuery(this).remove(); });
			}
		});
	}
}

function app_deletePicSlider(el, id) {
	if (confirm('Wollen Sie das Element wirklich löschen?')) {
		$this = jQuery(el);
		jQuery.ajax({
			type: 'GET',
			url: '/admin/slider/deletePic/id/' + id,
			success: function(msg) {
				$this.parent().fadeOut(250, function() { jQuery(this).remove(); });
			}
		});
	}
}
