var to_eval;
//////
//popup handling
var ajax_form_options = {beforeSubmit:hide_popup_content, success: fill_popup_content_with_post_result};

function hide_popup(){
  $('#popup').slideUp('slow');
  $('#chat').show();
}

function hide_popup_content(){
  $('#popup-content').css('display', 'none');
  $('#popup-wait').css('display', 'block');
}
function fill_popup_content_with_post_result(responseText, statusText){
  $('#popup-content').html(responseText).css('display', 'block');
  $('#popup-wait').css('display', 'none');
  $('#popup-content form').ajaxForm(ajax_form_options);
}
function popup_load(url, form, cache_clear){
  cache_clear = typeof(cache_clear) != 'undefined' ? cache_clear : 1;
  $('#chat').hide();
  $('#popup').css('display', 'block');
  $('#popup-wait').css('display', 'block');
  $('#popup-content').css('display', 'none');

  if(cache_clear){
    var currentDate = new Date();
    if(url.indexOf('?')==-1){
      sep='?';
    }else{
      sep='&';
    }
    url_add_on = sep+'stmp='+currentDate.valueOf();
  }else{
    url_add_on = '';
  }

  $('#popup-content').load('layout-plain/'+url+url_add_on, {}, function(){
      $('#popup-wait').css('display', 'none');
      $('#popup-content').css('display', 'block');
      $('#popup-content form').ajaxForm(ajax_form_options);
    });
}

//end popup handling
//////

function assign_reveals(){
    $('.reveal-target').css('display', 'none');
    $('.reveal-displayed').css('display', 'block');
    $('.reveal-reveal-menu').css('display', 'none');

    $('.reveal').click(function(){
	if(!($(this).attr('event-applied')==1)){
	    $(this).attr('event-applied', 1);
	    $('.reveal-target').slideUp('slow');
	    $($(this).attr('reveals')).slideDown('slow');
	    $.scrollTo($(this).attr('reveals'), 500);
	    $('.reveal-menu').hide();
	    $('.reveal-reveal-menu').slideDown('slow');
	  }
      });
    $('.reveal-reveal-menu').click(function(){
	$('.reveal-menu').slideDown('slow');
	$('.reveal-reveal-menu').slideUp('slow');
      });
    $('.hide').click(function(){
	$($(this).attr('hides')).slideUp('slow');
    });
}


$(document).ready(function() {

    eval(to_eval);
    assign_reveals();
    
  //popups

    $('.popup-close').click(function(){
	$('#popup').css('display','none');
        $('#chat').show();
      });

    $('.image-button').hover(function(){
	$(this).addClass('button-hover');
      }, function(){
	$(this).removeClass('button-hover');
      });


//textarea maxlength
    function limitChars(text, infodiv)
    {	

      var limit = text.attr('maxlength');
      var textlength =  text.val().length;
      if(textlength > limit)
	{

	  $('#' + text.attr('id') + '-max-note').html('Limit '+limit+' characters.');
	  text.val(text.val().substr(0,limit));
	  return false;
	}
      return true;
    }


    $('textarea').keyup(function(){
	limitChars($(this), 'charlimitinfo');
      });



    
  });
