/* Create NetR namespace */
if(typeof NetR == "undefined"){ var NetR = {}; }

/**
 * @requires jQuery
 * Converts plain text to mailto links
 */
NetR.activateEmailLinks = function () {
	var options = {
		emailClass: 'hidden-email', // Class name for elements that contain an optional name and an obfuscated email address
		textClass: 'email-text', // Optional text to be used as the visible link text
		addressClass: 'email-address', // The obfuscated email adress (entity encoded, decimal or hexadecimal)
		salt: 'INGEN_SPAM_' // Optional prefix to further reduce the risk of spam bots picking up addresses
	};
	/**
	* Initialization
	*/
	function init(opts) {
		// If options were supplied, apply them to the option Object.
		for (var key in opts) {
			if (options.hasOwnProperty(key)) {
				options[key] = opts[key];
			}
		}
		$('.' + options.emailClass).each(function () {
		    var textElem = $(this).find('.' + options.textClass + ':first');
		    var addressElem = $(this).find('.' + options.addressClass + ':first');
		    if ($(addressElem).length) {
		        var textText = addressText = $(addressElem).text();
		        if ($(textElem).length) {
		            textText = $(textElem).text();
		        }
                $(this).html('<a href="mailto:' + addressText.replace(options.salt,"") + '">' + textText.replace(options.salt,"") + '</a>');
		    }
		});
	}
	return {
		init: init
	};
}();

/**
 * 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
 */
NetR.InputPopulate = function() {
	var options = {
		sInputClass: 'populate', // Class name for input elements to autopopulate
		sHiddenClass: 'structural', // Class name that gets assigned to hidden label elements
		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 + ' ' + options.sHiddenClass;
			}
		}
	};
	/**
	* Initialization
	*/
	function init(opts) {
		// If options were supplied, apply them to the option Object.
		for (var key in opts) {
			if (options.hasOwnProperty(key)) {
				options[key] = opts[key];
			}
		}
		// Find all input elements with the given className
		var arrInputs = $('input.' + options.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(options.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; }
			});
		}
	}
	return {
		init: init
	};
}();

$(document).ready(function(){
	NetR.activateEmailLinks.init({
	    salt: 'INGEN_SPAM_ '
	});
	NetR.InputPopulate.init();
	// Initialise lightboxes
	//$("a.photo").colorbox();
	//$("a.video").colorbox({iframe:true});
	if ($("a[rel^='prettyPhoto']").length) {
	    $("a[rel^='prettyPhoto']").prettyPhoto({theme:'dark_square'});
	}

	// Convert client list to select element
	var $clients = $("#client-list a");
	if ($clients.length != 0) {
		$("#client-list").append('<form action="#"><div class="select"><label for="clients" class="structural">Välj kund:</label><select name="clients" id="clients"><option value="/jobb/">Kunder</option></select></div></form>');
		var select = $("#client-list select");
		$clients.each(function(){
			$option = '<option value="' + $(this).attr("href") + '"';
			if ($(this).attr("href") == window.location.href) {
			    $option += ' selected="selected"';
			}
			if ($(this).hasClass("no-children")) {
			    $option += ' disabled="disabled"';
			}
			$option += '>' + $(this).text() + '</option>';
			select.append($option);
		});
		var form = $("#client-list form");
		form.find("select").change(function(){
			var clientUrl = $(this).val();
			$("#content").fadeTo("slow", 0.1, function(){
				$(this).load(clientUrl + " #case-list", function(){
					$(this).fadeTo("slow", 1.0);
				});
			});
		});
		$("#client-list h2, #client-list ul").remove();
	}

	// Show alternate portrait photo on hover
	$(".portrait").each(function(){
        var img1 = $(this).find("img.photo-1")[0];
        var img2 = $(this).find("img.photo-2")[0];
        if (img1 && img2) {
            $(img2).hide();
            $(this).addClass("active");
            $(this).mouseover(function(){
                $(img2).show();
                $(img1).hide();
            }).mouseout(function() {
                $(img1).show();
                $(img2).hide();
            });
        }
	});
	// Toggle sharing options
	var $share = $("#share #share-services");
	$share.hide();
	$("#share h2").wrapInner('<a href="#share-services" class="closed"></a>');
	var $shareToggle = $("#share h2 a");
	$shareToggle.click(function(event) {
	    event.preventDefault();
	    $share.slideToggle('slow', function() {
            $shareToggle.toggleClass('closed open');
	    });
	});
	// Hide photos that should only be shown in lightboxes
	$(".photo.lightbox-only").hide();
});