HLF = {};

/**
 * @fileoverview Global functions
 */
/**
 * Hack to reduce background image flickering in IE 6
 */
/*@cc_on
	@if (@_jscript_version == 5.6)
		try {
			document.execCommand("BackgroundImageCache", false, true);
		} catch(err) {}
	@end
@*/

// AddThis
var addthis_config = {
	services_compact: 'facebook, twitter, linkedin, live, myspace',
	services_exclude: 'print',
	ui_language: 'sv'
};

HLF.FormFixer = (function () {
	return {
		// Pass in any form elements as jQuery objects or selector strings
		init: function () {
			if (arguments.length) {
				$.each(arguments, function () {
					$(this).each(function () {
						SolnaStad.FormFixer.fixButtons(this);
					});
				});
			}
		},
		// Replace submit-inputs with spans and buttons for better styling
		fixButtons: function (form) {
			$('input[type=submit]', form).each(function () {
				HLF.fixButtons(this);
			});
		},
		fixButton: function (element) {
			var oldButton = $(element),
			    newButton = $('<button type="submit">' + oldButton.val() + '</button>');
			// Copy attributes from old button to new button
			$.each(['id', 'class', 'name', 'value'], function () {
				var attr = oldButton.attr(this.toString());
				if (attr) {
					newButton.attr(this.toString(), attr);
				}
			});
			// Insert new button
			oldButton.after(newButton);
			newButton.wrap('<span class="button"><span><span><span><span></span></span></span></span></span>');
			// Add clearfix to submit-area
			oldButton.closest('.submit-area').addClass('cf');
			// Hide old button. If it is removed from the DOM, pressing enter in the form won't work in IE
			oldButton.hide();
		},
		fixLink: function (element) {
			var oldLink = $(element),
			    newLink = $('<a>' + oldLink.html() + '</a>');
			// Copy attributes from old button to new button
			$.each(['id', 'class', 'href', 'rel'], function () {
				var attr = oldLink.attr(this.toString());
				if (attr) {
					newLink.attr(this.toString(), attr);
				}
			});
			// Insert new button
			oldLink.after(newLink);
			newLink.wrap('<span class="button"><span><span><span><span></span></span></span></span></span>');
			oldLink.remove();
		}
	};
})();

$(function () {

	// AddThis
    $('.addthis-placeholder').html('<p>Dela med din minnesfond och insamling till dina nära och kära via sociala medier:</p><div class="btn cf"><a href="#" class="addthis_button">Dela ut via sociala medier</a></div>');
	$('.addthis-placeholder-box').replaceWith('<div class="box"><div class="box-content"><h2 class="structural">Dela</h2><p>Dela ut en länk hit till dina vänner och bekanta via sociala medier.</p><div class="btn cf"><a href="#" class="addthis_button">Dela ut via sociala medier</a></div></div></div>');
	// Kill AddThis iframe
	var killer = setInterval(function () {
		var iframe = $('#_atssh');
		if (iframe.size()) {
			iframe.remove();
			clearInterval(killer);
		}
	}, 500);

	$('input[type=submit]').each(function () {
		HLF.FormFixer.fixButton(this);
	});

	$('.btn a').each(function () {
		HLF.FormFixer.fixLink(this);
	});

	$('.red-btn a').each(function () {
		HLF.FormFixer.fixLink(this);
	});

	// Rounded corners!
	$('.box-content').each(function () {
		var t = $(this);
		t.before('<div class="box-top"><div></div></div>');
		t.after('<div class="box-bottom"><div></div></div>');
	});
	$('.text input').each(function () {
		var t = $(this);
		t.wrap('<div class="i-bg-1"><div class="i-bg-2"></div></div>');
	});

});