// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
var Photo = {
	show: function(url, id) {
		new Ajax.Updater('photo_comments', '/photos/get_comments/' + id + "&authenticity_token=");
		$('photo').src = url;
		$('mask').show();
		$('photo_wrapper').visualEffect('appear', {duration:0.5});
	},
	
	hide: function() {
		$('mask').hide();
		$('photo_wrapper').visualEffect('fade', {duration:0.5});
	},
	
	update_comments: function(id) {
		new Ajax.Updater('photo_comments', '/photos/get_comments/' + id + "&authenticity_token=");
	}
	
}

var community_message_interval = 30000; //30 secs
var community_message_index = 0;
var community_message_timer;

function switch_community_message(num_messages)
{
  if ($('community_message_table')){
      next_community_message(num_messages)
      var recur_call = "switch_community_message(" + num_messages + ")";
      community_message_timer = setTimeout(recur_call, community_message_interval);
    }
}

function next_community_message(num_messages){
  if (num_messages > 1){
    $('community_message_'+community_message_index).visualEffect("fade", {duration:1.0});  //, {"duration": "1000"}
    community_message_index = (community_message_index+1) % num_messages;
  }
  setTimeout( function() {$('community_message_'+community_message_index).visualEffect("appear", {duration:1.0}); }, 1300 );
}

function previous_community_message(num_messages){
  if (num_messages > 1){
    $('community_message_'+community_message_index).visualEffect("fade", {duration:1.0});  //, {"duration": "1000"}
    if (community_message_index != 0)
      community_message_index = community_message_index-1;
    else
      community_message_index = num_messages-1;
  }
  setTimeout( function() {$('community_message_'+community_message_index).visualEffect("appear", {duration:1.0}); }, 1300 );
}


function showFlashMessage( msg )
{
    $('community_message').hide();
    $('flash').innerHTML = msg;
    $('flash').show();
    setTimeout(function() { $('flash').visualEffect('fade', {duration:3.0})}, 1000 );
    setTimeout(function() { $('community_message').visualEffect('appear', {duration:2.0}) }, 4300);
}


function submitCommunityMessageOnEnter(e)
{
  var characterCode; 

  if(e && e.which){ //if which property of event object is supported (NN4)
    characterCode = e.which; //character code is contained in NN4's which property
  }
  else{
    characterCode = e.keyCode; //character code is contained in IE's keyCode property
  }

  if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
      
    if ($('user_feed_message').value != 0)
      new Ajax.Request('/user_feed/ajax_create', 
                       {asynchronous:true, 
                        evalScripts:true,
                        onSuccess:function(request){$('user_feed_message').value = '';},  //showFlashMessage('Nachricht gesendet!')
                        parameters:$('instant_message_form').serialize()}); 
     return false;   
  }
  return true;
  
  //return event.keyCode!=13
}


function submitFormOnEnter(e, form)
{
  var characterCode; 

  if(e && e.which){ //if which property of event object is supported (NN4)
    characterCode = e.which; //character code is contained in NN4's which property
  }
  else{
    characterCode = e.keyCode; //character code is contained in IE's keyCode property
  }

  if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)

     $(form).submit();
     return false;  
  }
 return true;
  
}


function validate_field(regex, field, blank_is_valid)
{
  var regexp = new RegExp(regex)

  if ( regexp.test(field.value) || (field.value == '' && blank_is_valid) )
  {
    field.className = '';
    $(field.id+'_invalid_label').hide(); 
  }
  else
  {
    field.className = 'fieldWithErrors';
    $(field.id+'_invalid_label').show();
  }
}


function empty_city_field(){
  var city_field = $('user_stadt');
  city_field.value = ''; 
  city_field.className = 'fieldWithErrors';
  $(city_field.id+'_invalid_label').show();
}


function validate_zip(zip_field)
{
  if ( $('user_country').value == 'Deutschland' )
    if ( zip_field.value.length != 5 )
    {
      zip_field.className = 'fieldWithErrors';
      $(zip_field.id+'_invalid_label').show();
    }
    else
    {
      zip_field.className = '';
      $(zip_field.id+'_invalid_label').hide();
    }
  else
    if ( zip_field.value.length != 4 )
    {
      zip_field.className = 'fieldWithErrors';
      $(zip_field.id+'_invalid_label').show();
    }
    else
    {
      zip_field.className = '';
      $(zip_field.id+'_invalid_label').hide();
    }
 
}

function form_valid(form)
{
  var spans = form.getElementsByTagName('span');
  for (i=0, numSpans=spans.length; i < numSpans; i++) 
    if (spans[i].id.match(/_invalid_label$/)) {
       if (Element.visible(spans[i]))
         return false;
    }
  
  return true;
}


function setStateDiv(select)
{
    country = select.options[select.options.selectedIndex].value;
    de = document.getElementById('states_germany');
    at = document.getElementById('states_austria');
    ch = document.getElementById('states_switzerland');

    if (country == 'Deutschland' || country == 'Germany')
    {
            de.style.display = '';
            at.style.display = 'none';
            ch.style.display = 'none';


    }	
    else if (country == 'Österreich' || country == 'Austria')
    {
            de.style.display = 'none';
            at.style.display = '';
            ch.style.display = 'none';

    }
    else if (country == 'Schweiz' || country == 'Switzerland')
    {

            de.style.display = 'none';
            at.style.display = 'none';
            ch.style.display = '';

    }
    else
    {
            de.style.display = 'none';
            at.style.display = 'none';
            ch.style.display = 'none';

    }
}

function on_edu_state_change(select_tag)
{
  
}

var next_job_index;

function remove_job_idea(job_index){
  $('job_select_' + job_index).hide();
  $('user_job_idea_' + job_index).value = '';
  $('add_job_idea_link').show();
  next_job_index--;
  if (next_job_index > 1){ 
    $('job_remove_link_' + (next_job_index - 1)).show();
  } 
}


function add_job_idea(){
  $('job_select_' + next_job_index).show();
  if (next_job_index > 1){
    $('job_remove_link_' + (next_job_index - 1)).hide();
  }
  if (next_job_index == 3)
    $('add_job_idea_link').hide();
  next_job_index++;
}


function mark_job_wish_field_invalid(job_index){
  if (job_index > 0){
    $('job_' + job_index).down().className = 'auto_complete_select_box fieldWithErrors';
    $('job_'+job_index+'_error').show();
  }
  else{
    $('job').down().className = 'auto_complete_select_box fieldWithErrors';
    $('job_error').show();
  }
}

function mark_location_fields_invalid(){
  $('user_plz').className = 'fieldWithErrors';
  $('user_plz_invalid_label').show();
  $('user_stadt').className = 'fieldWithErrors';
  $('user_stadt_invalid_label').show();
}



