/**
 * Copy the value of an input field's title attribute to its value attribute.
 * Clear the input field on focus if its value is the same as its title.
 * Repopulate the input field on blur if it is empty.
 * Hide the input field's associated label if it has one.
 * @requires jQuery
 */
var NetRInputPopulate = function() {
	var sInputClass = 'populate'; // Class name for input elements to autopopulate
	var sHiddenClass = 'structural'; // Class name that gets assigned to hidden label elements
	var sHideLabelClass = 'hidelabel'; // If the input has this className, its label is hidden
	function hideLabel(sId) {
		var arrLabels = document.getElementsByTagName('label');
		var iLabels = arrLabels.length;
		var oLabel;
		for (var i=0; i<iLabels; i++) {
			oLabel = arrLabels[i];
			if (oLabel.htmlFor == sId) {
				oLabel.className = oLabel.className + ' ' + sHiddenClass;
			}
		}
	}
	// Main function
	return {
		init:function() {
			// Find all input elements with the given className
			var arrInputs = $('input.' + sInputClass);
			var iInputs = arrInputs.length;
			var oInput;
			for (var i=0; i<iInputs; i++) {
				oInput = arrInputs[i];
				// Make sure it's a text input
				if (oInput.type != 'text') { continue; }
				// Hide the input's label
				if ($(oInput).hasClass(sHideLabelClass)) { hideLabel(oInput.id); }
				// If value is empty and title is not, assign title to value
				if ((oInput.value == '') && (oInput.title != '')) { oInput.value = oInput.title; }
				// Add event handlers for focus and blur
				$(oInput).bind('focus', function() {
					// If value and title are equal on focus, clear value
					if (this.value == this.title) {
						this.value = '';
						this.select(); // Make input caret visible in IE
					}
				});
				$(oInput).bind('blur', function() {
					// If the field is empty on blur, assign title to value
					if (!this.value.length) { this.value = this.title; }
				});
			}
		}
	};
} ();

MarkItUp = {
    bbCodeSettings: {
        previewParserPath: '', // path to your BBCode parser
        markupSet: [
			{ name: 'Betoning', className: "italic", key: 'I', openWith: '[i]', closeWith: '[/i]' },
			{ name: 'Stark betoning', className: "bold", key: 'B', openWith: '[b]', closeWith: '[/b]' },
			{ separator: '---------------' },
			{ name: 'Bild', key: 'P', className: "picture", replaceWith: '[img][![Adress]!][/img]' },
			{ name: 'L&auml;nk', key: 'L', className: "link", openWith: '[url=[![Adress]!]]', closeWith: '[/url]', placeHolder: 'Text...' },
			{ separator: '---------------' },
			{ name: 'Lista', className: "bulletlist", openWith: '[list]\n', closeWith: '\n[/list]' },
			{ name: 'Numrerad lista', className: "numericlist", openWith: '[list=[![Startnummer]!]]\n', closeWith: '\n[/list]' },
			{ name: 'Listobjekt', className: "listitem", openWith: '[*] ' },
			{ separator: '---------------' },
			{ name: 'Citat', className: "quote", openWith: '[quote]', closeWith: '[/quote]' }
	    ]
    }
}

jQuery(document).ready(function () {
	$('a.lightbox').lightBox(); // Initialize lightbox
	$('.bbcode').markItUp(MarkItUp.bbCodeSettings);
	NetRInputPopulate.init(); // Populate input fields
});