var initiateAll = function() {
	// Insert some code here

	// Service logging function
	var log = function(some) {
			if (typeof console == "undefined") {
				alert(some);
			}
			else {
				console.log(some);
			}
		},

		trim = function(string) {
			return string
							.replace(/\s+/, " ")
							.replace(/^\s/, "")
							.replace(/\s$/, "")
		};

	$(".placeholder").click(function() {
		$(this).find("input").focus();

		return false;
	});

	$(".with-placeholder")
		.blur(function() {
			var input = $(this),
				placeholder = input.closest(".placeholder").children(".title");

			if (trim(input.val()) == "") {
				placeholder.show();
			}
		})
		.focus(function() {
			$(this).closest(".placeholder").children(".title").hide();
		})
		.focus().blur();

	var submit_button = $(".js-submit-button");

	submit_button.click(function() {
		var form = $(this).closest("form");

		if (form.length != 0 && !$(this).hasClass("disabled")) {
			form.submit();
		}

		return false;
	});

	var selects = $(".js-select"),
			values_lists = $(".js-select-values");

	selects.click(function() {
		var select = $(this),
			values = select.parent().next().children(".values");

		values_lists.hide();
		values.toggle();

		return false;
	});

	var values = $(".js-select-values a");

	values.click(function() {
		var value = $(this),
			value_text = value.text(),

			value_list_item = value.parent(),
			value_list = value_list_item.parent(),

			select = value_list.parent().prev().children().children(".sprited-content").text(value_text),

			input = value_list.next();

		value_list_item
			.addClass("selected")
		.siblings()
			.removeClass("selected");

		value_list.hide();

		input.val(value_list.children().index(value_list_item) + 1);

		return false;
	});


	$("body").add("html").click(function() {
		values_lists.hide();
	});
}

$(document).ready( initiateAll );


function selectLink(selObj, taregtself) {
	a = Math.random();
	a = 100 * a;
	a = Math.round(a);

	if (taregtself) {
		location.href = selObj.options[selObj.selectedIndex].value
	}
	else {
		// modification: open popup only if the value of the selected option is not empty
		var theURL = selObj.options[selObj.selectedIndex].value;

		if (theURL != "") {
			if (theURL.indexOf('http') == 0)
				popUp = window.open(selObj.options[selObj.selectedIndex].value,'NewWindow'+a,'toolbar=yes,location=yes,directories=no,status=yes,menubar=yes,scrollbars=yes,resizable=yes');
			else {
				document.location.href = selObj.options[selObj.selectedIndex].value;
			}
		}
	}

	selObj.selectedIndex = 0;
}

