function getURLForAppending() {
	var url = document.URL;
	if (url.indexOf('?') != -1 ) {
		url += '&';
	} else {
		url += '?';
	}
	return url;
}

function getDashboardConfig() {
	var rData = new Array();

	$('#tx-enetsaflexiblelayout-clipboard').each(function() {
		var container = 0;

		counter = 1;
		$(this).find('.widget').each(function() {
			var widget = [
				container,
				counter++,
				$(this).attr('id').replace('tx-enetsaflexiblelayout-dashboard-widget-', '')
			];
			rData.push(widget);
		});
	});

	$('.tx-enetsaflexiblelayout-dashboard-container').each(function() {
		var container = $(this).attr('id').replace('tx-enetsaflexiblelayout-dashboard-col', '');
		counter = 1;
		$(this).find('.widget').each(function() {
			var widget = [
				container,
				counter++,
				$(this).attr('id').replace('tx-enetsaflexiblelayout-dashboard-widget-', '')
			];
			rData.push(widget);
		});
	});

	return rData;
}

function getParam(paramName) {
	var SELF_LOCATION = self.location.href;
	var urlSplit = SELF_LOCATION.split('?');
	if (!urlSplit[1]) { // no query
		return '';
	}
	
	var urlQuery = urlSplit[1];
	var paramsSplit = urlSplit[1].split('&');
	for (var i = 0; i < paramsSplit.length; i++) {
		paramSplit = paramsSplit[i].split('=');
		if (paramSplit[0] == paramName) {
			return paramSplit[1] ? paramSplit[1] : '';
		}
	}
	return '';
}

function updateConfig() {
	var rData = getDashboardConfig();

	$.post(document.URL, {
		'tx_enetsaflexiblelayout[dashboardAction]': 'savePositions',
		'tx_enetsaflexiblelayout[dashboardConfig]': getDashboardConfig()
	});
}

function updateImpression(id) {

	var cssLayout = $("input[name='tx_enetsaflexiblelayout[dashboardCssLayout]']:checked").val();
	var request = {
		'tx_enetsaflexiblelayout[dashboardAction]': 'saveImpression',
		'tx_enetsaflexiblelayout[dashboardImpression]': id,
		'tx_enetsaflexiblelayout[dashboardCssLayout]': cssLayout
	};

	$.post(
		document.URL,
		request,
		function(response) {
			if (response.status == 'success' && typeof(response.impression) != 'undefined') {
				$('#tx-enetsaflexiblelayout-impression').html('').html(response.impression.content);
			}
		}
	);
}

function registerRemoveEvent(element) {
	var icon = element.find('.tx-enetflexiblelayout-dashboard-remove');
	icon.unbind('click');
	icon.bind('click', getRemoveFunction());
}

function registerEditEvent(element) {
	//$('.tx-enetflexiblelayout-dashboard-edit').unbind('click');
	//$('.tx-enetflexiblelayout-dashboard-edit').click(getEditFunction());

	var icon = element.find('.tx-enetflexiblelayout-dashboard-edit');
	icon.unbind('click');
	icon.bind('click', getEditFunction());
	
}

function setWidgetContent(widgetName, widgetContent) {
	//alert(widgetName); alert(widgetContent);
	$('#tx-enetsaflexiblelayout-dashboard-widget-' + widgetName).find('.widgetContent').html('').html(widgetContent);
}

function setWidgetLabel(widgetName, widgetLabel) {
	var widget = $('#tx-enetsaflexiblelayout-dashboard-widget-' + widgetName);
	widget.find('.csc-firstHeader').text('').text(widgetLabel);
}

function setWidgetInfoText(widgetName, widgetInfoText) {
	if (widgetInfoText) {
		$('#tx-enetsaflexiblelayout-dashboard-widget-' + widgetName)
			.find('.tx-enetflexiblelayout-dashboard-info')
			.show().find('img').attr('alt', widgetInfoText);
			//.tooltip(getToolTipArray()));
	} else {
		$('#tx-enetsaflexiblelayout-dashboard-widget-' + widgetName)
			.find('.tx-enetflexiblelayout-dashboard-info')
			.hide().find('img').attr('alt', '');
		//.tooltip(getToolTipArray());
	}
}

function getEditFunction() {
	return function(e) {
		var widget = $(this).parents('.widget');
		var widgetName = widget.attr('id').split('-');
		widgetName = widgetName[widgetName.length-1];

		var data = {
			'tx_enetsaflexiblelayout[dashboardAction]': 'executeWidget',
			'tx_enetsaflexiblelayout[dashboardWidget]': widgetName
		};

		if (widget.hasClass('tx-enetsaflexiblelayout-dashboard-widget-editmode')) {
			data['tx_enetsaflexiblelayout[widgetAction]'] = 'index';
			widget.removeClass('tx-enetsaflexiblelayout-dashboard-widget-editmode');
		} else {
			data['tx_enetsaflexiblelayout[widgetAction]'] = 'edit';
			widget.addClass('tx-enetsaflexiblelayout-dashboard-widget-editmode');
		}

		$.post(document.URL, data,
			function(response) {
				widget.find('.widgetContent').html('').html(response.content);

				if(response.label) {
					setWidgetLabel(widgetName, response.label);
				}
				setWidgetInfoText(widgetName, response.infotext);
			}
		);
	};
}

function getIndexFunction() {
	return function(e) {
		var widget = $(this).parents('.widget');
		var widgetName = widget.attr('id').split('-');
		widgetName = widgetName[widgetName.length-1];

		var data = {
			'tx_enetsaflexiblelayout[dashboardAction]': 'executeWidget',
			'tx_enetsaflexiblelayout[dashboardWidget]': widgetName
		};
		data['tx_enetsaflexiblelayout[widgetAction]'] = 'index';

		$.post(document.URL, data,
			function(response) {
				widget.find('.widgetContent').html('').html(response.content);

				if(response.label) {
					setWidgetLabel(widgetName, response.label);
				}
				setWidgetInfoText(widgetName, response.infotext);
			}
		);
	};
}

function getRemoveFunction() {
	return function(e) {
		var widget = $(this).parents('.widget');

		widget.remove().appendTo('#tx-enetsaflexiblelayout-clipboard');
		widget.find('.widgetContentContainer').hide();
		widget.find('.widgetClipboardLabel').show();
		updateConfig();
	};
}

function submitWidgetForm(widgetName) {
	var url = getURLForAppending();
	url += 'tx_enetsaflexiblelayout[dashboardAction]=executeWidget';
	url += '&tx_enetsaflexiblelayout[dashboardWidget]=' + widgetName;
	url += '&tx_enetsaflexiblelayout[widgetAction]=save';

	var form = $('#tx-enetsaflexiblelayout-dashboard-widget-' + widgetName).find('form');
	var widget = form.parents('.widget');

	form.ajaxSubmit( {
		'url' : url,
		'type' : 'post',
		'success' : function(response) {
			setWidgetContent(widgetName,response.content);
			if(response.label) {
				setWidgetLabel(widgetName, response.label);
			}
			setWidgetInfoText(widgetName, response.infotext);

			widget.removeClass('tx-enetsaflexiblelayout-dashboard-widget-editmode');
		}
	});
}

function saveCssLayout() {
	var url = getURLForAppending();
	url += 'tx_enetsaflexiblelayout[dashboardAction]=saveCssLayout';
	var form = $('#tx-enetsaflexiblelayout-layout').find('form');
	form.ajaxSubmit({
		'url' : url,
		'type': 'post',
		'success' : function(response) {
			$('#tx-enetsaflexiblelayout-layout-save-success').show().delay(5000).fadeOut(5000);
				//does the same without jQuery 1.4 --> delay is an jQuery 1.4 function
			//$('#tx-enetsaflexiblelayout-layout-save-success').show();
			//setTimeout(function() {
			//	$('#tx-enetsaflexiblelayout-layout-save-success').fadeOut(5000);
			//}, 5000);
		}
	});
}

function changeCssLayout(layout) {
	var currentLayoutElement = $('#tx-enetsaflexiblelayout-layout-current');
	var currentLayout = currentLayoutElement.val();
	currentLayoutElement.val(layout);

	$('.std-box-' + currentLayout).each(function() {
			$(this).removeClass('std-box-' + currentLayout).addClass('std-box-' + layout);
	});

	var data = {
		'tx_enetsaflexiblelayout[dashboardAction]': 'getImpression',
		'tx_enetsaflexiblelayout[dashboardCssLayout]' : layout
	};

	$.post(document.URL, data,
		function(response) {
			$('#tx-enetsaflexiblelayout-impression').html('').html(response);
		}
	);
}

function getToolTipArray() {
	return {
		track: true,
		fade: 200,
		showURL: false
	};
}

/*
function getShortenerFunction() {
	return function() {
		var textElement = $(this);
		var html = '<div class="align-right"><a class="std-button" href="#" onclick="$(this).parents(\'.align-right\').prev().removeAttr(\'height\').css(\'overflow\',\'visible\').next().remove(); return false;"><em><span>...</span></em></a></p>';

		if (textElement.height() > 230) {
			textElement.css("height", 230)
				.css("overflow", "hidden")
				.after(html);
		}
	}
}
*/

var tx_enetsaflexiblelayout_height = 50;

function adjustMinHeightOnDraggingStart() {
	$('.tx-enetsaflexiblelayout-dashboard-container').each(function() {
		var container = $(this);
		container.css('min-height', container.height() + tx_enetsaflexiblelayout_height);
	});
}

function removeMinHeightOnDraggingStop() {
	$('.tx-enetsaflexiblelayout-dashboard-container').each(function() {
		var container = $(this);
		container.css('min-height', '1px');
	});
}

$(document).ready(function() {
	$('span.tx-enetflexiblelayout-dashboard-info img').tooltip(getToolTipArray());
	if($('#tx-enetsaflexiblelayout-clipboard')) {
			//toogle clipboard
		$('#tx-enetsaflexiblelayout-clipboard-helper-hide').click( function() {
			$('#tx-enetsaflexiblelayout-clipboard').toggle();
			$('#tx-enetsaflexiblelayout-clipboard-helper-hide').hide();
			$('#tx-enetsaflexiblelayout-clipboard-helper-show').show();
		});
		$('#tx-enetsaflexiblelayout-clipboard-helper-show').click( function() {
			$('#tx-enetsaflexiblelayout-clipboard').toggle();
			$('#tx-enetsaflexiblelayout-clipboard-helper-show').hide();
			$('#tx-enetsaflexiblelayout-clipboard-helper-hide').show();
		});
			//toogle layout
		$('#tx-enetsaflexiblelayout-layout-helper-hide').click( function() {
			$('#tx-enetsaflexiblelayout-layout').toggle();
			$('#tx-enetsaflexiblelayout-layout-helper-hide').hide();
			$('#tx-enetsaflexiblelayout-layout-helper-show').show();
		});
		$('#tx-enetsaflexiblelayout-layout-helper-show').click( function() {
			$('#tx-enetsaflexiblelayout-layout').toggle();
			$('#tx-enetsaflexiblelayout-layout-helper-show').hide();
			$('#tx-enetsaflexiblelayout-layout-helper-hide').show();
		});

		$('.tx-enetsaflexiblelayout-layout-checkbox').click( function() {
			changeCssLayout($(this).val());
		});

		$('.tx-enetflexiblelayout-dashboard-remove').click(getRemoveFunction());
		$('.tx-enetflexiblelayout-dashboard-edit').click(getEditFunction());

		$('#tx-enetsaflexiblelayout-clipboard').sortable({
			'connectWith': '.tx-enetsaflexiblelayout-dashboard-container',
			//'handle': '.tx-enetflexiblelayout-dashboard-move',
			'handle': '.label',
			'cancel': '.undraggable',
			'item': '.draggable',
			'tolerance': 'pointer',
			'placeholder': 'widget-placeholder-clipboard',
			'start': function(e, ui) {
				$('#tx-enetsaflexiblelayout-clipboard').addClass('bg-clipboard-drop');
				adjustMinHeightOnDraggingStart();
			},
			'receive': function(e, ui) {
				ui.item.find('.widgetContentContainer').hide();
				var element = ui.item.find('.widgetClipboardLabel');
				element.show();
			},
			'over': function(e, ui) {
				ui.placeholder.removeClass('widget-placeholder').removeClass('widget-wide-placeholder').addClass('widget-placeholder-clipboard');
				$('#tx-enetsaflexiblelayout-clipboard').addClass('bg-clipboard-drop');
			},
			'stop': function(e, ui) {
				$('#tx-enetsaflexiblelayout-clipboard').removeClass('bg-clipboard-drop');
				updateConfig();
				removeMinHeightOnDraggingStop();
			}
		});
	
		var defaultConfiguration = {
			'connectWith': ['#tx-enetsaflexiblelayout-clipboard', '.tx-enetsaflexiblelayout-dashboard-container'],
			//'handle': '.tx-enetflexiblelayout-dashboard-move',
			'handle': '.label',
			'cancel': '.undraggable',
			'item': '.draggable',
			'tolerance': 'pointer',
			'placeholder': 'widget-placeholder',
			'start': function(e, ui) {
				adjustMinHeightOnDraggingStart();
				if (ui.item.hasClass('widget-wide')) {
					ui.placeholder.removeClass('widget-placeholder-clipboard').removeClass('widget-placeholder').addClass('widget-wide-placeholder');
				} else {
					ui.placeholder.removeClass('widget-placeholder-clipboard').removeClass('widget-wide-placeholder').addClass('widget-placeholder');
				}
			},
			'receive': function(e, ui) {
				$('#tx-enetsaflexiblelayout-clipboard').removeClass('bg-clipboard-drop');
				ui.item.find('.widgetClipboardLabel').hide();
				var element = ui.item.find('.widgetContentContainer');
				element.show();
				registerRemoveEvent(element);
				registerEditEvent(element);
			},
			'over': function(e, ui) {
				if (ui.item.hasClass('widget-wide')) {
					ui.placeholder.removeClass('widget-placeholder-clipboard').removeClass('widget-placeholder').addClass('widget-wide-placeholder');
				} else {
					ui.placeholder.removeClass('widget-placeholder-clipboard').removeClass('widget-wide-placeholder').addClass('widget-placeholder');
				}
				$('#tx-enetsaflexiblelayout-clipboard').removeClass('bg-clipboard-drop');
			},
			'stop': function(e, ui) {
				$('#tx-enetsaflexiblelayout-clipboard').removeClass('bg-clipboard-drop');
				updateConfig();
				removeMinHeightOnDraggingStop();
			}
		};

			// old default config changed to two other connector to seperate wide and normal widgets
		$('.tx-enetsaflexiblelayout-dashboard-container').sortable(defaultConfiguration);

		//$('#tx-enetsaflexiblelayout-dashboard #tx-enetsaflexiblelayout-dashboard-widget-salutationtext .std-ce-innerwrap2').each(getShortenerFunction());
	}
});

