﻿var oldValue,
	sexOldValue

$(document).ready(
	function ()
	{
		filterSelect = $('#f_filter')
		oldValue = filterSelect.val()

		selectReplacement(
			filterSelect,
			function ( value )
			{
				if ( value != oldValue )
				{
					$.cookie('chooseFilter', value, {expires: 365, path: '/'})
					window.location = window.location
				}
			}
		)

		var sexFilterSelect = $('#f_sexFilter')
		sexOldValue = sexFilterSelect.val()

		selectReplacement(
			sexFilterSelect,
			function ( value )
			{
				if ( value != sexOldValue )
				{
					$.cookie('chooseSexFilter', value, {expires: 365, path: '/'})
					window.location = window.location
				}
			}
		)
		
		var designsContainer = $('.collection'),
			designRateControls = $('.change-decision, .rate-control', designsContainer)
			
		designRateControls.hide()
		
		$('.design-link').hover(
			function()
			{
				$(this).siblings('.change-decision, .rate-control').show()
			},
			function()
			{
				$(this).siblings('.change-decision, .rate-control').hide()
			}
		)
		designRateControls.hover(
			function()
			{
				$(this).show()
			},
			function()
			{
				$(this).hide()
			}
		)
			
		if ( loggedIn == 0 )
		{
			$('a', designRateControls).click(
				function()
				{
					tb_show(null, '/?TB_inline&height=300&width=600&inlineId=modal-loginForm&modal=true&showHandler=modalLoginContent.reset()', null)
					return false
				}
			)
		}
		else
		{
			// Назначаем обработчики событий голосования для существующих элементов
			$('.rate-control a, .change-decision', designsContainer).click(
				function()
				{
					rate(this)
					return false
				}
			)
			
			// Создаём шаблоны контролов голосования
			var changeDecisionSample = $('<a class="change-decision" href="#">Передумали?</a>'),
				rateControlSample = $('<span class="rate-control"><strong>Нравится?</strong><a class="yes" href="#">Да</a><a class="no" href="#">Нет</a></span>')
			
			changeDecisionSample.click(
				function()
				{
					rate(this)
					return false
				}
			)
			
			$('a', rateControlSample).click(
				function()
				{
					rate(this)
					return false
				}
			)
			
			changeDecisionSample.hover(
				function()
				{
					$(this).show()
				},
				function()
				{
					$(this).hide()
				}
			)
			
			rateControlSample.hover(
				function()
				{
					$(this).show()
				},
				function()
				{
					$(this).hide()
				}
			)	
			
			function rate( _link )
			{
				var thisLink = $(_link),
					imageContainer = thisLink.parents('p.image'),
					designLink = $('a.design-link', imageContainer),
					earlierRateContainer = $('strong', designLink),
					yesCountContainer = $('em', designLink)
				
				$.post(
					'/choose/rate.html',
					{
						designId: thisLink.attr('rel'),
						decision: thisLink.attr('class')
					},
					function (response)
					{
						response = parseInt(response)
						
						switch ( response )
						{
							// Проголосовал за
							case 1:
								if( yesCountContainer.length )
									yesCountContainer.text( parseInt(yesCountContainer.text()) + 1 )
								else
									designLink.append('<em title="Готовых купить">1</em>')
									
								designLink.append('<strong class="yes" title="Вы проголосовали за этот дизайн"><span class="hidden">Да</span></strong>')
								
								thisLink.parent().remove()
								
								imageContainer.append( changeDecisionSample.clone(true).attr('rel', thisLink.attr('rel')) )
							break;
							
							// Проголосовал против
							case 2:
								designLink.append('<strong class="no" title="Вы проголосовали против этого дизайна"><span class="hidden">Нет</span></strong>')
								
								thisLink.parent().remove()
								
								imageContainer.append( changeDecisionSample.clone(true).attr('rel', thisLink.attr('rel')) )
							break;
							
							// Решил подумать ещё
							case 3:
								if( earlierRateContainer.hasClass('yes') )
								{
									if( yesCountContainer.text() == '1' )
										yesCountContainer.remove()
									else
										yesCountContainer.text( parseInt(yesCountContainer.text()) - 1 )
								}
								
								earlierRateContainer.remove()
								
								thisLink.remove()
								
								imageContainer.append( rateControlSample.clone(true).children('a').attr('rel', thisLink.attr('rel')).end() )
							break;
						}
					}
				)
			}
		}
		
		$('#mc-chooseNotLoggedIn-auth-link').click(
			function ()
			{
				modalLoginContent.select( 'mc-loginForm' )
				return false
			}
		)
	}
)
