jQuery(function ($) {
	// confirm links/buttons
	$('.js-confirm a, .js-confirm input, a.js-confirm').click(function() {
		var str = $(this).attr('confirm') || $(this).closest('[confirm]').attr('confirm');
		return confirm(str);
	});

	$('#post #post\\.type').change(function () {
		if ($(this).val() == 2) {
			$('#post .eventdate-container').show();
		} else {
			$('#post .eventdate-container').hide();
		}

		if ($(this).val() == 1) {
			$('#post\\.body_label').html('Text <span class="description">(Rapportens innehåll, max 1000 tecken. Ej obligatorisk info)</span>');
		} else {
			$('#post\\.body_label').text('Text');
		}

	}).change();

	$('input[name=company-search]').autocomplete({
		source: 'index.php?type=12341&action=company_name_autocomplete&term=',
		select: function (event, ui) {
			location.href = ui.item.link;
		}
	});

	function addCrumb() {
		$('#breadcrumb-container').append(
			$('<span class="separator"></span><span class="crumb tab-crumb" />')
		);
		return $('#breadcrumb-container .crumb.tab-crumb');
	}

	function lastCrumb() {
		var lastCrumb = $('#breadcrumb-container .crumb.tab-crumb');
		return (lastCrumb.length == 0) ? addCrumb() : lastCrumb;
	}

	function setLastCrumb(html) {
		lastCrumb().html(html);
	}

	$('.tab-container').not('.news-single-view').tabs({
		show: function (event, ui) {
			// update breadcrumbs
			var link = $(ui.tab).clone();
			var hash = link.attr('href').match(/(#.*)$/)[1];

			var href = location.protocol + '//' + location.host + location.pathname + location.search;
			link.attr('href', href + hash);

			setLastCrumb(link);
		}
	});

	$('select.transform').selectBox();

	$('select[name=business-select]').change(function () {
		var val = $(this).val();
		$.getJSON(
			'index.php?type=12341&action=refresh_dashboard&business=' + val,
			function (data, status, xhr) {
				$.each(data, function (i, v) {
					var id = $(v).attr('id');
					$('#' + id).html($(v).html());
				});

				$('h1,h2,h3,h4').each(function(){
					jQuery(this).prev('p').css('margin-bottom', '10px');
				});

				bindPagePickers();
			}
		);
	});

	function bindPagePickers() {
		$('.dashboard-pageselect').click(function (event) {
			var business = $('select[name=business-select]').val();
			var page = $(this).attr('page-number');

			$.getJSON(
				'index.php?type=12341&action=refresh_dashboard&business=' + business + '&page=' + page,
				function (data, status, xhr) {
					$.each(data, function (i, v) {
						var id = $(v).attr('id');
						$('#' + id).html($(v).html());
					});

					$('h1,h2,h3,h4').each(function(){
						jQuery(this).prev('p').css('margin-bottom', '10px');
					});

					bindPagePickers();
				}
			);

			event.preventDefault();
		});

		$('.company-pageselect').click(function (event) {
			var company = $(this).closest('.company-content').attr('company');
			var type = $(this).closest('.type-panel').attr('type');
			var page = $(this).attr('page-number');

			$.getJSON(
				'index.php?type=12341&action=refresh_company_view'
					+ '&company=' + company
					+ '&postType=' + type
					+ '&pageNumber=' + page,
				function (data, status, xhr) {
					$.each(data, function (i, v) {
						if (v !== false) {
							var id = $(v).attr('id');
							$('#' + id).html($(v).html());
						}
					});

					$('h1,h2,h3,h4').each(function(){
						jQuery(this).prev('p').css('margin-bottom', '10px');
					});

					bindPagePickers();
				}
			);

			event.preventDefault();
		});	
	}

	bindPagePickers();
});

// To avoid conflicts with formidable which uses Prototype.
jQuery.noConflict();

