$(function() {
	// добавляем тени где нужно
	init_textshadow()
	init_overflow()
	// хитромудро пытаемся обмануть отрисовку теней
	$(document).ajaxComplete(function() {
		init_textshadow()
		init_overflow()
	})

	// верхнее меню
	$(".top_menu > div > ul > li").mouseover(function() {
		$(this).addClass("hover")
		$("> a", this).addClass("hover")
	}).mouseout(function() {
		$(this).removeClass("hover")
		$("> a", this).removeClass("hover")
	})

	// определяем работу табов
	$(".tabs .tab").click(function() {
		$(this).siblings(".tab").removeClass("active")
		$(this).addClass("active")
		$("div[tabblock]").hide()
		$("div[tabblock='" + $(this).attr("item") + "']").show()
	})
	$(".tabs").each(function() {
		var el = $(".tab.active", this)
		if (!el.size()) {
			el = $(".tab:eq(0)", this)
		}
		el.click()
	})

	// заменяем input:file на красивый аналог
	$(".file_placeholder").live("click", function() {
		return false
	})
	$(":file").live("change", function() {
		$(this).next("input.file_placeholder").val($(this).val())
	})

	// делаем красивые selectbox'ы
	$("select").selectbox()

	// показ дополнительных кОнтролов для списка
	$(".showcontrol").live("mouseover", function() {
		$(".control", this).css({ "visibility" : "visible" })
	}).live("mouseout", function() {
		$(".control", this).css({ "visibility" : "hidden" })
	})

	// схлопывающиеся блоки
	$(".expand").each(function() {
		$("div[collapsable='" + $(this).attr("item") + "']").hide()
	})
	$(".collapse").live("click", function() {
		$(this).removeClass("collapse").addClass("expand")
		$("div[collapsable='" + $(this).attr("item") + "']").slideUp(300)
	})
	$(".expand").live("click", function() {
		$(this).removeClass("expand").addClass("collapse")
		$("div[collapsable='" + $(this).attr("item") + "']").slideDown(300)
	})
})

function init_overflow() {
	$("*[overflow]").each(function() {
		if (!$(".jquery-overflow", this).size()) {
			$(this).html('<div class="jquery-overflow" style="width:' + $(this).attr("overflow") + 'px"><div class="jquery-overflow-fade"></div><div class="jquery-overflow-text">' + $(this).html() + '</div></div>')
		}
	})
}

function init_textshadow() {
	$(".shadow").each(function() {
		// защищаемся от повторной обработки теней
		if (!$(".jquery-text-shadow", this).size()) {
			$(this).html('<span class="jquery-text-shadow"><span class="jquery-text-shadow-text">' + $(this).text() + '</span><span class="jquery-text-shadow-shadow">' + $(this).text() + '</span></span>')
			$(".jquery-text-shadow-shadow, .jquery-text-shadow-text", this).css({ "width" :  $(this).width() + "px" }) // выставляем ширину оригинального элемента, чтобы избежать лишних переносов
		}
	})
}
