$(document).ready(function() {
  
  $('.edtme').click(function(){
    var textToChange = $(this).prev().text();
    $(this).prev().replaceWith('<input class="tempinput" type="text" value="'+textToChange+'" />');
    
    $('.tempinput').focus();
    
    $('.tempinput').focusout(function() {
      var changedText = $(this).val();
      $(this).css('background-color', 'blue');
      $(this).replaceWith(changedText);
    });
  });
  $('#tallenna').click(function() {
    var testii = $('#ostoskori ol').html();
    var StrippedString = testii.replace(/(<([^>]+)>)/ig,"");
    StrippedString = StrippedString.toLowerCase();
    StrippedString = StrippedString.replace(/edit/ig,"");
    StrippedString = StrippedString.replace(/ x /ig,"\n");
    StrippedString = StrippedString.replace(/\n/g,",");
    StrippedString = StrippedString.replace(/  /g,",");
    $('#tallenna').attr('href', '?save&items='+StrippedString);
  });
  
  $('.delme').click(function(){
    $(this).parent().remove();
  });
  
  $('.unregistered').submit(function() {
    $('#commentForm').replaceWith('<div id="modal"><div id="message"><a href="/kirjaudu">Kirjaudu</a> ensin sisään.<br /> Jos et ole rekisteröitynyt sinun tulee ensin <a href="/register">rekisteröityä</a>.</div></div>');
    return false;
  });
  
  $('.close').click(function() {
    $(location).attr('href');
  })
  
  $('.recipe-list li a').each(function(index) {
    var test = $(this).attr('href');
    $(this).parent().click(function() {
      open(test,'_self',false);
    });
  });
  
  $(".makemebigger").hover(function(){
    var source = $(this).attr('src');
    source = source.replace('mini/','');
    $("#video .photo").attr('src', source);
  });
	
  $("#method").focus(function(){
    var aineet = $('#incredients').val();
    aineet = aineet.replace(/\n/gi,", ");
    aineet = "Reseptissä on käytetty "+aineet;
    
    $("#meta_desc").val(aineet);
  });
  
  $('#uppic').css('display','none');
  
  $('#kuva').change(function() {
    $('#edtPic form').submit();
    $('#loader').css('visibility','visible');
  });
  $('#uppic').click(function() {
    $('#loader').css('visibility','visible');
  });
  
  
  $('#cat_id').bind('change', function () {
    var url = $(this).val(); // get selected value
    if (url != '') { // require a URL
        window.location = '/raaka-aineet/?cat_id='+url; // redirect
    }
    return false;
  });
  
  $('#tulosta').click(function() {
      window.print();
      return false;
  });
  $('.del').click(function() {
      var del = confirm('Haluatko varmasti poistaa?');
      if (del == true) {
          return true;
      } else {
          return false;
      }
  });
  $('#regme').click(function(event) {
    event.preventDefault();
    $('#regform').load('/users/new.html');
  });    
  $('#ostoskori ol li').click(function() {
      $(this).addClass('ostettu');
  });
  
  $('#kirjaudu').click(function() {
    $('#loginbox').addClass('visible');
    var checkName = $('.logname').val();
    if (checkName != '') {
      $('.newpass').focus();
    }
  });
  $('#username').focus();
  $("input#name").focus();
  $('.loginButton').click(function(){
    this.form.password.value = MD5(this.form.newpass.value)
  });
  $('.newpass').change(function(){
    this.form.password.value = MD5(this.form.newpass.value)
  });    
  $("#msg").ready(function() {
      $("#dmsg").fadeOut(5000);
  });
  $("#annoksia").click(function() {
      alert(this.innerHTML.replace(/([a-z\:\<\>\/]+)/g, ""));
  });
});
