$(document).ready( function() {
	$('span.checkbox').click( function() {
		$('input["type=checkbox"].styled').trigger( 'change' );
	});

	$('#header .select').toggle( function() {
		$('.drop-down-top').slideDown( 10, function() {
			$('.drop-down').slideDown( 'normal' );
		});
	}, function() {
		$('.drop-down').slideUp( 'normal' , function() {
			$('.drop-down-top').slideUp( 10 );
		});
	});

	$('#top-search').focus( function() {
		if( $(this).val() == 'Търсене' )
			$(this).val('');
	});

	$('#top-search').blur( function() {
		if( $(this).val() == '')
			$(this).val('Търсене');
	});

	if( $('#search').size() > 0 ) {
		$('#search').focus( function() {
			$(this).val('');
		});

		$('#search').blur( function() {
			if( $(this).val() == '')
				$(this).val('Ключова дума');
		});
	}

	$('input["type=checkbox"].styled').change( function() {
		if( $(this).attr('id') != 'option-0' && $(this).attr('id') != 'h-option-0') {
			$('#option-0').removeAttr('checked');
			$('#h-option-0').removeAttr('checked');

		} else {
			if( $(this).attr('checked') ) {
				$('input["type=checkbox"].styled').each( function() {
					$(this).removeAttr('checked');
				});

				$('#option-0').attr('checked', 'checked');
				$('#h-option-0').attr('checked', 'checked');
			}
		}

		if( $(this).get(0).checked == true ) {
			$('#' + $(this).attr('id').replace( 'h-', '' ) ).attr('checked', 'checked');
			$('#h-' + $(this).attr('id').replace( 'h-', '' ) ).attr('checked', 'checked');
		} else {
			$('#' + $(this).attr('id').replace( 'h-', '' ) ).removeAttr('checked');
			$('#h-' + $(this).attr('id').replace( 'h-', '' ) ).removeAttr('checked');
		}

		var text = '';
		$('#header input["type=checkbox"].styled').each( function(i) {

			if( $(this).get(0).checked == true ) {
				text += (i > 0 && text != '' ? ( !text.match(/\.\.\./) ? '... ' : '' ) : $(this).next().text() );
			}

		});

		$('#header .select').text(text);
	});

	$('form.poll').submit( function() {
		var id = $(this).attr( 'id' );

		var vote = false;

		$('#' + id + ' .styled').each( function() {
			if( $(this).get(0).checked == true ) {
				vote = true;

				$('#answer_id' + $(this).attr('name').replace(/\D*/, '') ).val( $(this).val() );
			}
		});

		if( vote == false ) {
			jAlert( 'Моля изберете отговор', '' );

			return false;
		}

		createCookie( id, 'vote', '90' );
	});

 	$('.ajax-poll').submit( function() {
		var id = this.id.replace( 'poll', '' );

		return jform.submit(this, {

			onComplete: function( response ) {
				jAlert(response, '');

				$.get( '/polls/vote/' + id, { }, function(votes) {
					$('.index-poll .submit').remove()
					$('#place-stats-here-' + id).html(votes)
				});
			}
		});
	});

	if( $('#bulletin-email').size() > 0 ) {
		$('#bulletin-email').focus( function() {
			$(this).val('');
		});

		$('#bulletin-email').blur( function() {
			if( $(this).val() == '')
				$(this).val('Въведете вашия e-mail адрес');
		});
	}
	
	$('#bulletin').submit( function() {

		if( $('#bulletin-email').val() == 'Въведете вашия e-mail адрес' ) {

			jAlert( 'Моля въведете вашия e-mail адрес', '', function() {
				return false;
			});

			return false;
		}

		if ( !validate.email( 'bulletin-email' ) ) {
			jAlert( 'Моля въведете валиден e-mail адрес', '', function() {
				return false;
			});

			return false;
		}
	});
	
	$('.bulletin-index').submit( function() {
		return jform.submit(this, {

			onComplete: function( response ) {
				jAlert(response, '', function() {
					$('#bulletin-email').val('Въведете вашия e-mail адрес');
				});
			}
		});
	});
	
	$('.place-calendar-here .arrow-prev a').click( function() {

		$('.loader').show();

		$.get( $(this).attr( 'href' ).replace( '/events', '/events/calendar' ), { }, function( calendar ) {

			$('.place-calendar-here').html( calendar );
			$('.loader').hide();
		});

		return false;
	});
	
	$('.place-calendar-here .arrow-next a').click( function() {

		$('.loader').show();

		$.get( $(this).attr( 'href' ).replace( '/events', '/events/calendar' ), { }, function( calendar ) {

			$('.place-calendar-here').html( calendar );
			$('.loader').hide();
		});

		return false;
	});

	if( $('.polls').size() > 0 ) {
		var height = more = max = z = 0;

		$('.polls form').each( function(i) {
			height = parseFloat( $(this).height() );

			if( height > max ) max = height;

			if( z > 0 ) {
				more = ( $('.polls form:eq(' + i + ')').size() > $('.polls form:eq(' + (i - 1) + ')').size() ? parseFloat( $('.polls form:eq(' + i + ')').size() ) : parseFloat( $('.polls form:eq(' + (i - 1) + ')').size() ) );

				$('.polls form:eq(' + (i - 1) + ')').height( max + more * 15 );
				$('.polls form:eq(' + i + ')').height( max + more * 15 );

				z = max = more = 0;

			} else
				z++;
		});
	}
	
	$('.ajax-prayers a').click( function() {
		$.get( $(this).attr( 'href' ) + '/ajax:on/', { }, function( data ) {
			$('.ajax-prayers').html( data );
			//$('.loader').hide();
		});

		return false;
	});
});
