$(function() {
	$("a[rel^='big']").prettyPhoto({showTitle: true, theme: 'light_rounded'});

	var galleryInitLink = function(a) {
		$(a).attr('title', $(a).text()).css('visibility', 'visible')
		galleryLinkSwitch(a)
	}
	
	var galleryLinkSwitch = function(a) {
		if ($(a).hasClass('a-open-closed')) {
			$(a).removeClass('a-open-closed').addClass('a-open-opened')
			$('span', a).text( $(a).attr('title') + ': скрыть')
		}
		else {
			$(a).removeClass('a-open-opened').addClass('a-open-closed')
			$('span', a).text( $(a).attr('title') + ': показать')
		}
	}
	
	$('table.pok-h .div-open-td > .div-open').each(function(i, div) {
		$('>span.a-open', div).each(function(i, a) {
			var gallery = $('#gallery-'+$(a).attr('rel'))

			var needInit = true
			var large = $('<div class="photo"></div>')
			var largeOpen = $('<span class="div-open"></span>').insertAfter(div)
			var largeA = $('<span class="a-open"></span>').append('<span>крупно</span>').appendTo(largeOpen)
			largeA.click(function() {
				if (needInit) {
					$('a', gallery).each(function() {
						var item = $('<div class="photo-large"></div>').appendTo(large)
						$('i', this).each(function() {
							$('<span></span>').text($(this).text()).appendTo(item)
						})
						$('img', this).each(function() {
							$('<img/>').attr('src', $(this).attr('rel')).appendTo(item)
						})
					})
					gallery.after(large)
					needInit = false
				}

				galleryLinkSwitch(largeA)
				if (largeA.hasClass('a-open-closed')) {
					gallery.show()
					large.hide()
				}
				else {
					large.show()
					gallery.hide()
				}
				return false
			})
			galleryInitLink(largeA)
			
			$('#gallery-'+$(a).attr('rel')+'-open').hide().each(function(i, open) {
				galleryInitLink(a)
				$(a).click(function() {
					if (largeA.hasClass('a-open-opened')) largeA.click()

					if ($(a).hasClass('a-open-closed')) $(open).slideDown('fast')
					else $(open).slideUp('fast')

					galleryLinkSwitch(a)
					return false
				})
			})
		})
	})


	$('#request-price').each(function(i,f) {
		var t = $('textarea', f)
		
		var val = t.prev().appendTo('<div></div>').text()
		var reset = function() {
			if (val == t.val()) t.val('')
		}

		if (!t.val()) t.val(val).focus(reset)

		$(f).submit(reset)
	})
	
	// gallery on main page
	;(function() {
		var table = $('table.main-gallery');
		var selector = 'a.main-gallery-item'
		table.delegate(selector, 'mouseenter', function() {
			$('>span', this).stop().animate({opacity: 1, backgroundColor: '#fff'})
		})
		table.delegate(selector, 'mouseleave', function() {
			$('>span', this).stop().animate({opacity: 0.4, backgroundColor: '#000'})
		})
		if ($.browser.msie && /^[5-7]\./.test($.browser.version)) {
			table.delegate(selector, 'click', function() {
				location = this.href
			})
		}
		table.find(selector+'>span').css('opacity', 0.4)
	})()

	$('#search').each(function(i,node) {
		var org = $(node).attr('value')
		$(node).focus(function() {
			if (org === this.value) this.value = ''
		}).blur(function() {
			if ('' === this.value) this.value = org
		})
	})
})

function sub_toggle(who){
	var subid = who.id.substring(6);
	$('#sub_'+subid).toggle();
}
