var settings = {
  exchangeImages: true,
  exchangePeriod: 1800,
  fadeTime: 800
}


function openRegisterForm() {
  $.get("/users/new", function(data){
        $.facebox(data)
  });
}


$(document).ready(function() { 
  
  $("#white_background").corner("round 10px");
  $(".white_background").corner("round 10px");  
  $("#edit_profile_white_background").corner("round 10px");
  $("#left_part").corner("round 5px");
  $("#right_part").corner("round 5px");
  $("#images_wrapper").corner("round 5px");
  $("#statistics").corner("round 5px");
  $("#step_by_step").corner("round 5px");
  $(".small_profile_wrapper").corner("round 8px");
  $(".profile_photo").corner("round 8px");
  $(".single_user_map_wrapper").corner("round 8px");

  $("#loading").ajaxStart(function(){ 
    $(this).show(); 
  }).ajaxStop(function(){ 
    $(this).hide(); 
  });
  
  // $.get("/users/registration_success", function(data){
  //     $.facebox(data)
  // });
  // 

  

    $.timer(1000, function (timer) {
      var fade_time = 200;
      $("#si_selector").animate({opacity: 1.0}, fade_time).animate({opacity: 0.5}, fade_time, function() {
        $("#no_selector").animate({opacity: 1.0}, fade_time).animate({opacity: 0.5}, fade_time)
      })
      timer.stop();
    });  

    $.timer(2000, function (timer) {
      var fade_time = 200;
      $("#no_selector").animate({opacity: 1.0}, fade_time).animate({opacity: 0.5}, fade_time, function() {
        $("#si_selector").animate({opacity: 1.0}, fade_time).animate({opacity: 0.5}, fade_time)
      })
      timer.stop();
    });  

    $.timer(3000, function (timer) {
      var fade_time = 200;
      $("#si_selector").animate({opacity: 1.0}, fade_time).animate({opacity: 0.5}, fade_time, function() {
        $("#no_selector").animate({opacity: 1.0}, fade_time).animate({opacity: 0.5}, fade_time)
      })
      timer.stop();
    });  


    $('.selector').hover(
            function() {
                    $(this).css("opacity", 1);
            },
            function() {
                    $(this).css("opacity", 0.5);
            }
    );
  
   $.timer(1000, function (timer) {
     $("#new_message_icon").Pulsate();
   	timer.stop();
   });  
  
  if (settings.exchangeImages==true) {
     exchange_image();
     $.timer(settings.exchangePeriod, function (timer) {
       exchange_image();
       //timer.stop();
     });
  }
  
  $(".login_input_fields").click(function() {
    this.value=""
  });
  $("#startpage_login_button").focus()

  $(".event_popup_wrapper").click(function() {
      $(this).Shrink();
  });
  
});


function profile_photo_changed_response(json_stuff){

    if (json_stuff.success == true){
        $("#facebox_content").html("<h1>"+json_stuff.headline+"</h1><div class='h_20'></div>"+json_stuff.content)
        
        $(".close_image").click(function() {
            $("#profile_pic").attr("src", json_stuff.profile_photo);
            $("#profile_pic").Grow()
        });

    }
    else if(json_stuff.success == false){
      var object_type=json_stuff.object;
    }
}

function getZoomFactor(precision) {

  switch(precision)
  {
  case "country":
    return 3;
    break;    
  case "city":
    return 6;
    break;
  case "zip":
    return 5;
    break;

  default:
    return 6; 
  }

return 1;
}


function ajaxFeedback(json){
    if (json.success == true){
        
        $("#"+json.id).Fade();
        $(".notice_wrap").html('<div id="notice_content">'+json.content+'</div>')

        $.timer(6000, function (timer) {
          $("#notice_content").SlideUp();
          timer.stop();
        });

        // $(".facebox_content").html("<h1>"+json_stuff.headline+"</h1><div class='h_20'></div>"+json_stuff.content)
    
    }
    else if(json_stuff.success == false){
          renderErrors(json_stuff);
    }
}


function messageFeedback(json_stuff){
    if (json_stuff.success == true){
        $(".facebox_content").html("<h1>"+json_stuff.headline+"</h1><div class='h_20'></div>"+json_stuff.content)
    }
    else if(json_stuff.success == false){
          renderErrors(json_stuff);
    }
}


function renderNewUserErrorsJson(json_stuff) {
  if (json_stuff.success == true){

    if ($("#registration_mode").val() != "from_profile"){
      $.get("/users/registration_success", function(data){
        $(".facebox_content").html(data)    
      });
    }
    else{
      $.get("/users/registration_success?mode=from_profile", function(data){
        $(".facebox_content").html(data)    
      });
    }

  }
  else if(json_stuff.success == false){
/*    renderErrors(json_stuff);*/
  }
}


function renderErrors(json_stuff){
  var object_type=json_stuff.object;

  //remove old errors
  $("#"+object_type+"_form .field_with_error").removeClass("field_with_error");
  $("#"+object_type+"_form .error_message").remove();

  $.each(json_stuff.errors, function(i, error) { 
        
    if ( (error[0] == "date_year") || (error[0] == "date_day") || (error[0] == "date_month") ) {
      var element = $("#"+error[0]);
    } else{
      var element = $("#"+object_type + "_" + error[0]);
    }
    
    $(element).addClass("field_with_error")

    if (error[1].length > 2){
      element.after("<div class='error_message'>"+error[1]+"</div>");
    }
  });
}



function d(output) {
  $("#debugdiv").html(output)
}

function exchange_image() {
  var fading_time = settings.fadeTime;
  var shown_images_array = $("#shown_images > *")
  var random_image = shown_images_array[Math.floor(Math.random() * shown_images_array.length)];
  var hidden_image = $("#hidden_images :first");
    
  $(random_image).fadeTo(fading_time, 0.00001, function () {
    replace($(random_image), $(hidden_image));
    $(hidden_image).fadeTo(fading_time, 0.999999);
  });
}

function replace(what, with_what) {
  what.after(with_what);
  $("#hidden_images").append(what); 
}

function explain_login(email){
  $(".login_heading").Pulsate()
  $("#startpage_login_button").Shake()
  $("#login_login").attr("value", email)
  $("#login_login").Highlight()  
}


/*  $('.message_popup_wrapper').show().Pulsate();*/
/*  $.get("/users/222/small_profile_popup", function(data){
      $.facebox(data)
  });
*/
/*  $.get("/users/new", function(data){
      $.facebox(data)
  });
*/



  function check_regexp(value, regex) {
    if (value.match(regex)) {
      return true;
    } else {
      return false;
    }
  }
