$(document).ready(function() {

    $('#inloggen').click(function() {
        $('#login-form').submit();
        return false;
    });

    $('#aanmeld_email').blur(function() {
        var emailvalue = $(this).val();
        $('#filledemail').html(emailvalue);
    });

	$('#togglepassword').change(function() {
		var pass = $('#password').val();
		var type = $('#password').attr('type');
		var newtype = 'password';
		if ($(this).attr('checked')) {
			newtype = 'text';
		}
		$('#password').replaceWith("<input type='" + newtype + "' name='password' id='password' value='" + pass + "' class='credentialinput right'>");
		return false;
	});

    $('#receive_sms').change(function() {
		if ($(this).attr('checked')) {
            receive_sms_checked();
        } else {
            $('#mobile-container').html('');
        }
        return false;
    });
	
	end_date = null;
	start_date = null;
	
	$.datepicker.setDefaults( $.datepicker.regional[ "nl" ] );	
	$(".datepicker").datepicker({
		dateFormat:'dd-mm-yy',
		minDate: '+4d',
		onSelect: customRange,
		beforeShowDay: highlightOdds	
	});
	
	window.setTimeout("setDateLang()",100);


});

function customRange(value,input) {

		if ($(input).attr("id") == "end_date"){
		
			end_date = $('#end_date').datepicker("getDate");
			if (end_date != null){		
				if (end_date > start_date){			
					$("#start_date").datepicker("option", {maxDate: end_date});
				}
			}

		}else{

			start_date = $('#start_date').datepicker("getDate");		
			if (start_date != null){
				$("#end_date").datepicker("option", {minDate: start_date});
			}
		}

		$(".datepicker").datepicker("refresh");
	}

function highlightOdds(date) {


	cssClass = '';
	if (start_date != null && end_date != null){
	
		if (date.getTime() > start_date.getTime() && date.getTime() < end_date.getTime()){
			cssClass = "inRange";
		}
		if (date.getTime() == start_date.getTime() || date.getTime() == end_date.getTime()){
			cssClass = "inActive";
		}
	}

    return [true,cssClass];
	
}

function setDateLang(){
	end_date = $('#end_date').datepicker("getDate");
	start_date = $('#start_date').datepicker("getDate");
	$(".datepicker").datepicker("option",$.datepicker.regional[ "nl" ]);
	$(".datepicker").datepicker("option",{dateFormat:'dd-mm-yy'});
}



function flash_ok ( msg ) {
    $('.content-wrapper:first').before ( '<div class="flash-message success"><strong>OK</strong>'+msg+'</div>' );
    fadeout ( '.flash-message', 10000 );
}

function flash_error ( msg ) {
    $('.content-wrapper:first').before ( '<div class="flash-message error"><strong>Fout!</strong>'+msg+'</div>' );
    fadeout ( '.flash-message', 10000 );
}

function receive_sms_checked () {
    
    $.get ( base_url + "user/inline_mobile_form", null, function ( data ) {
        var mobile_form_buffer = $('#mobile-container').html ();
        $('#mobile-container').html ( data );
        $('#inline-mobile').val($('#sms_phonenumber').val());
        $('#inline-mobile').keypress ( function ( key ) {
            if (key.keyCode == 13) 
                    save_sms_button_click ();
        });
        $('#inline-mobile-save').click ( save_sms_button_click );
    });
}

function save_sms_button_click () {
    new_mobile = $('#inline-mobile').val ();
    $.post ( base_url + "user/inline_mobile_save", { new_mobile: $('#inline-mobile').val () }, function (data) { 
        //show message
        if ( data == "1" ) {
            //replace form with verification code form
            $("#mobileverified").val($("#inline-mobile").val());
            $.get ( base_url + "user/inline_mobile_verification_code", null, function (data) {
                $( '#mobile-container').html ( data );
                $( '#inline-mobile-verification-code-save' ).click (sms_verification_code_click );
            } );
            //re-enable click
            flash_ok ( "Er is een SMS verstuurd aan uw mobiele nummer met een verificatiecode. U kunt de code in het onderstaande formulier invullen" );
        } else  {
            flash_error ( data );
        }
    });
    return false;
}
	
function sms_verification_code_click () {
    $.post ( base_url + "user/check_mobile_verification_code", { verification_code: $('#verification_code').val () }, function (data) {
        if ( data == "1" ) {		
                flash_ok ( "Uw mobiele nummer is geaccepteerd" );
                $('#mobile-container').html($('#mobileverified').val());
                $('#edit-mobile-button').click(edit_mobile_button_click);
        } else {
            flash_error(data);
        }
    });

}

/*-------------------------------------------*/
/* destination selector */
/*-------------------------------------------*/

	function get_options(target,value){
		if (target == "region"){
			$(".ds_option.city").hide();
			$("#ds_city").val("")
		}
		if (value == ""){
			unset_options(target);
		}else{
			$(".ds_loading").fadeIn();
			url = base_url+"user/get_destination_options/"+target+"/"+value;
			$.get(url,set_options);
			last_target = target;
		}
	}
	function unset_options(target){
		$(".ds_option."+target).hide();
	}

	function set_options(data){
		$(".ds_loading").fadeOut();
		$(".ds_option."+last_target).show();
		$("#ds_"+last_target).html(data);
	}

	function add_destination(){

		setting_key_id = false;
		if ($("#ds_city").val() != '' && $("#ds_city").val() != null){
			setting_key_id = $("#ds_city").val();
		}else if ($("#ds_region").val() != '' && $("#ds_region").val() != null){
			setting_key_id = $("#ds_region").val();
		}else if ($("#ds_country").val() != '' && $("#ds_country").val() != null){
			setting_key_id = $("#ds_country").val();
		}else{
			alert("Maak eerst uw keuze.");
		}

		if (setting_key_id){
			last_setting_key_id = setting_key_id
			url = base_url+"user/get_destination/"+setting_key_id;
			$.get(url,add_destination_done);
			$(".ds_option.region, .ds_option.city").slideUp();
			$("#ds_country, #ds_region, #ds_city").attr("selectedIndex",0);
		}
	}

	function add_destination_done(data){
		$("#ds_destinations_holder").append(data);
		checkEmpty();
		$("#ds_"+last_setting_key_id).hide().slideDown().animate({backgroundColor:'#33CC00'}, 200,function(){
			$(this).animate({backgroundColor:'#EAF2FF'}, 500);
		});
		
	}

	function checkEmpty(){
		if ($(".ds_destination").length == 0){
			$("#ds_none").show();
			$("#ds_add_btn .label").html("Bevestigen");
		}else{
			$("#ds_none").hide();
			$("#ds_add_btn .label").html("<b>+</b> Toevoegen");
		}
	}

	function remove_destination(setting_key_id){
		
		$("#ds_"+setting_key_id).animate({backgroundColor:'#FF0000'}, 200,function(){
			$(this).animate({backgroundColor:'#EAF2FF'}, 500).slideUp(function(){
				$(this).remove();
				checkEmpty();
			});			
		});		
	}

	function showHideDest(is_checked){

		if (is_checked){
			$("#destination-selector").slideUp();
			$("#ds_destinations_holder").html("");
		}else{
			$("#destination-selector").slideDown();	
			
		}
	
	}

	function showHideDates(is_checked){

		if (is_checked){
			$("#date-selector").slideUp();
			$("#start_date,#end_date").val("");
		}else{
			$("#date-selector").slideDown();					
		}		
	}
