var email_clicked = false;

function emptyListBoxes()
{
	$('#city option + option').remove();
	$('#city').selectbox_reset( );
	$('#congregation option + option').remove();
	$('#congregation').selectbox_reset( );
}

$(document).ready( function() {
	$('#submitBtn').hide();
	$('body').selectbox_all();

	$('#submitBtn').mousedown( function() {
  		$(this).addClass( 'pushed' ).addClass( 'pushed_' );
	}).mouseup( function() {
		$(this).removeClass( 'pushed' ).removeClass( 'pushed_' );
	}).mouseout( function( e ) {
		$(this).removeClass( 'pushed' );
	}).mouseover( function( e ) {
		if ($(this).hasClass( 'pushed_' )) {
		  	$(this).addClass( 'pushed' );
		}
	});

  //$('#contact_link').attr( 'href', $('#contact_link').attr( 'href' ) + '?ajax' );
  $('#contact_link').fancybox( {
		hideOnContentClick:	false,
		linkAppendix:		'?ajax',
		callbackOnShow: function() {
			$('#fancy_title_main div').html( $('#contact_link').attr( 'alt' ) );
	    	$('#contact_sendmail').hide();
	        email_clicked = false;
	    	$('#email a').click( function() {
        	if (email_clicked) {
            	return false;
            }
        	email_clicked = true;
            $('<img />').attr( 'src', k_img_path + 'ui/ani/ajax-loader.gif' ).appendTo( '#email' );
        	url	= $('#sendmail_link').attr( 'href' ) + '?ajax';
            $.get( url, function( data ) {
            	loadIntoFancy( data );
                $('#contact_form').submit( contact_form_submit );
            } );
        	return false;
        } );
	} } );

	emptyListBoxes();
	$('#city_label').disable();
	$('#city').disable();
	$('#congregation_label').disable();
	$('#congregation').disable();
  
	$('#course').change( function() {
	    console.log( 'CourseChange' );
		var sel = $(this).val();
		$('#city_label').disable();
		$('#city').disable();
		$('#congregation_label').disable();
		$('#congregation').disable();
		$('#submitBtn').hide();
		$('#congregation,#city').selectbox_reset( );

		if (sel > 0) {
		    var url = host + $('#loadCities_url').html().replace( ':_', sel ) + '?ajax';
			$('<div />')
			    .html('&nbsp;')
			    .attr('id', 'city_loading')
			    .addClass('loadingAni')
				.appendTo('#city_wrap');

		    $.getJSON( url, null, function( data ) {
				$('#city option + option').remove();
				var enable = false;

		    	$.each( data, function( i, item ) {
						$('<option/>').attr("value", i + 1).html( item ).appendTo('#city');
						enable = true;
		        } );
				$('#city_loading').remove();

		        if (enable) {
					$('#city_label').enable();
					$('#city').selectbox_reset( );
					$('#city').enable();
				}
		    } );
		} else {
			emptyListBoxes();
		    return true;
		}
	} );

	$('#city').change( function() {
		$('#congregation_label').disable();
		$('#congregation').disable();
		$('#congregation').selectbox_reset();
		$('#submitBtn').hide();
		var course	= $('#course').val();
		var sel		= $('#city option:selected').html();

		if ($(this).disabled() || ($(this).val() == 0)) {
			return true;
		}
		$('<div />')
		    .attr('id', 'congregation_loading')
		    .addClass('loadingAni')
			.appendTo('#congregation_wrap');

		var url = host + $('#loadCongregations_url').html().replace( ':_', course ).replace( '##', sel ) + '?ajax';

		$.getJSON( url, null, function( data ) {
			$('#congregation option + option').remove();
			var enable = false;

			$.each( data, function( i, item ) {
		    	$('<option/>').attr("value", item.id).html( item.name ).appendTo('#congregation');
		        enable = true;
			} );
		    $('#congregation_loading').remove();

		    if (enable) {
					$('#congregation_label').enable();
					$('#congregation').selectbox_reset( );
					$('#congregation').enable();
				}
		} );
	} );

	$('#submitBtn').hover(	function() { $(this).addClass( 'hover' ); },
  							function() { $(this).removeClass( 'hover' ); } );

	$('#congregation').change( function() {
		$('#submitBtn').hide();

		if ( $(this).val() > 0 ) {
			$('#submitBtn').show();
		}
	} );

	$('#county_form').submit( function() {
	    if ( $('#course option:selected').val() <= 0 ) { alert( 'Du måste välja en kurs' ); }
	    else if ( $('#city option:selected').val() <= 0 ) { alert( 'Du måste välja en ort' ); }
	    else if ( $('#congregation option:selected').val() <= 0 ) { alert( 'Du måste välja en plats' ); }
	    else {
	        id  = $('#congregation option:selected').val();
	        url = $('#county_form').attr('action').replace( ':_', id );
	        document.location = url;
			return false;
		}
		return false;
	} );

	console.log( 'len:' + $('#course option').length );
	if ( $('#course option').length == 2 ) {
		$('#course option')[1].selected = true;
		$('#course').selectbox_reset( );
		$('#course').change();
	} else {
		$('#course option')[0].selected = true;
		$('#course').selectbox_reset( );
	}
} );

function contact_form_submit()
{
    action		= $(this).attr('action') + '?ajax';
    contact_id	= $('#contact_id').val(); 
    //alert( action + ';#' + contact_id + ';;' + $('#message').val() + '!' + $('#message').html());

    post_data	= {
    	cid:		contact_id,
        name:		$('#name').val(),
        email:		$('#emailaddress').val(),
        telephone:	$('#telephone').val(),
        subject:	$('#subject').val(),
        message:	$('#message').val()
    };

    $.post( action, post_data, function( data ) {
    	loadIntoFancy( data );
        $('#back_link').click( function() {
			$('#fancy_close').close();
    	/*	$.get( $(this).attr('href'), null, function( data ) {
    			loadIntoFancy( data );
            } );*/
        	return false;
        } );
    }, 'html' );

    return false;
}