$(document).ready (function() {

$.ajaxSetup({cache: false});

$("#contact_form #contact_message").autogrow({minHeight: 120, lineHeight: 18});
$("#contact_form #contact_message").maxlength({maxCharacters: 5000, statusClass: 'message_status', statusText: 'pozostało znaków', notificationClass: 'message_error'});
$("#contact_form #contact_name, #contact_form #contact_email, #contact_form #contact_message").defaultvalue("Imię i nazwisko", "Twój adres e-mail", "Twoja wiadomość");


$("#comment_page_form #comment_message").autogrow({minHeight: 120, lineHeight: 18});
$("#comment_page_form #comment_message").maxlength({maxCharacters: 5000, statusClass: 'message_status', statusText: 'pozostało znaków', notificationClass: 'message_error'});
$("#comment_page_form #comment_name, #comment_page_form #comment_message").defaultvalue("Imię i nazwisko, pseudonim", "Twój komentarz");

$("#comment_event_form #comment_message").autogrow({minHeight: 120, lineHeight: 18});
$("#comment_event_form #comment_message").maxlength({maxCharacters: 5000, statusClass: 'message_status', statusText: 'pozostało znaków', notificationClass: 'message_error'});
$("#comment_event_form #comment_name, #comment_event_form #comment_message").defaultvalue("Imię i nazwisko, pseudonim", "Twój komentarz");

$("#comment_poll_form #comment_message").autogrow({minHeight: 120, lineHeight: 18});
$("#comment_poll_form #comment_message").maxlength({maxCharacters: 5000, statusClass: 'message_status', statusText: 'pozostało znaków', notificationClass: 'message_error'});
$("#comment_poll_form #comment_name, #comment_poll_form #comment_message").defaultvalue("Imię i nazwisko, pseudonim", "Twój komentarz");


$("#top_log_in_form #top_username, #top_log_in_form #top_password").defaultvalue("e-mail", "hasło");

$("#announced #announced_title").maxlength({maxCharacters: 100, statusClass: 'title_message_status', statusText: 'pozostało znaków', notificationClass: 'message_error'});
$("#announced #announced_text").maxlength({maxCharacters: 5000, statusClass: 'text_message_status', statusText: 'pozostało znaków', notificationClass: 'message_error'});

$("#events #event_add #event_title, #events #event_edit #event_title").maxlength({maxCharacters: 100, statusClass: 'title_message_status', statusText: 'pozostało znaków', notificationClass: 'message_error'});
$("#events #event_add #event_text, #events #event_edit #event_text").maxlength({maxCharacters: 2000, statusClass: 'text_message_status', statusText: 'pozostało znaków', notificationClass: 'message_error'});

$('form#contact_form').submit(function() {
if($('#contact_name').val() != 'Imię i nazwisko' && $('#contact_email').val() != 'Twój adres e-mail' && $('#contact_message').val() != 'Twoja wiadomość') {
$(this).ajaxSubmit({cache: false, dataType: 'json', beforeSubmit: contact_start, success: contact_response, url: '/ajax/contact_form.php?rand='+new Date().getTime()});
}
return false;
});


$('form#poll_form').submit(function() {
$(this).ajaxSubmit({cache: false, dataType: 'json', beforeSubmit: poll_start, success: poll_response, url: '/ajax/poll_form.php?rand='+new Date().getTime()});
return false;
});

$('form#comment_page_form').submit(function() {
if($('#comment_name').val() != 'Imię i nazwisko, pseudonim' && $('#comment_message').val() != 'Twój komentarz') {
$(this).ajaxSubmit({cache: false, dataType: 'json', beforeSubmit: comment_page_start, success: comment_page_response, url: '/ajax/comment_page_form.php?rand='+new Date().getTime()});
}
return false;
});

$('form#comment_event_form').submit(function() {
if($('#comment_name').val() != 'Imię i nazwisko, pseudonim' && $('#comment_message').val() != 'Twój komentarz') {
$(this).ajaxSubmit({cache: false, dataType: 'json', beforeSubmit: comment_event_start, success: comment_event_response, url: '/ajax/comment_event_form.php?rand='+new Date().getTime()});
}
return false;
});

$('form#comment_poll_form').submit(function() {
if($('#comment_name').val() != 'Imię i nazwisko, pseudonim' && $('#comment_message').val() != 'Twój komentarz') {
$(this).ajaxSubmit({cache: false, dataType: 'json', beforeSubmit: comment_poll_start, success: comment_poll_response, url: '/ajax/comment_poll_form.php?rand='+new Date().getTime()});
}
return false;
});

$('form#log_in_form').submit(function() {
$(this).ajaxSubmit({cache: false, dataType: 'json', beforeSubmit: log_in_start, success: log_in_response, url: '/ajax/panel/log_in.php?rand='+new Date().getTime()});
return false;
});

$('form#top_log_in_form').submit(function() {
if($('#top_username').val() != 'e-mail') {
$(this).ajaxSubmit({cache: false, dataType: 'json', beforeSubmit: top_log_in_start, success: top_log_in_response, url: '/ajax/panel/log_in.php?rand='+new Date().getTime()});
}
return false;
});

$('form#register_form').submit(function() {
$(this).ajaxSubmit({cache: false, dataType: 'json', beforeSubmit: register_start, success: register_response, url: '/ajax/panel/register.php?rand='+new Date().getTime()});
return false;
});

$('form#activation_form').submit(function() {
$(this).ajaxSubmit({cache: false, dataType: 'json', beforeSubmit: activation_start, success: activation_response, url: '/ajax/panel/activation.php?rand='+new Date().getTime()});
return false;
});

$('form#retrieve_password_form').submit(function() {
$(this).ajaxSubmit({cache: false, dataType: 'json', beforeSubmit: retrieve_password_start, success: retrieve_password_response, url: '/ajax/panel/retrieve_password.php?rand='+new Date().getTime()});
return false;
});

$('form#new_password_form').submit(function() {
$(this).ajaxSubmit({cache: false, dataType: 'json', beforeSubmit: new_password_start, success: new_password_response, url: '/ajax/panel/new_password.php?rand='+new Date().getTime()});
return false;
});

$('form#announced_add').submit(function() {
$(this).ajaxSubmit({cache: false, dataType: 'json', beforeSubmit: announced_add_start, success: announced_add_response, url: '/ajax/announced_add.php?rand='+new Date().getTime()});
return false;
});

$('form#announced_edit').ajaxForm({cache: false, dataType: 'json', beforeSubmit: announced_edit_start, success: announced_edit_response, url: '/ajax/announced_edit.php?rand='+new Date().getTime()});

$('form#event_add').submit(function() {
$(this).ajaxSubmit({cache: false, dataType: 'json', beforeSubmit: event_add_start, success: event_add_response, url: '/ajax/event_add.php?rand='+new Date().getTime()});
return false;
});

$('form#event_edit').ajaxForm({cache: false, dataType: 'json', beforeSubmit: event_edit_start, success: event_edit_response, url: '/ajax/event_edit.php?rand='+new Date().getTime()});


$('.comment_content').jTruncate({
length: 700,
minTrail: 300,
moreText: "czytaj więcej »",
lessText: "",
ellipsisText: ""
});


$("div#scrolling_news").smoothDivScroll({scrollingSpeed: 15, autoScroll: "always", autoScrollDirection: "endlessloop", pauseAutoScroll: "mouseover", autoScrollSpeed: 1});


$("#toTop").scrollToTop();


if (input_search = document.getElementById('input_search')) {
//input_search.focus();
}

$('.ad-gallery').adGallery({
loader_image: '/pliki/js/jquery/ad-gallery/loader.gif',
slideshow: {
autostart: false,
start_label: 'start',
stop_label: 'stop'
},
effect: 'fade'
});


$("#admin_edit_link").click(function () { 
$(".admin_edit_link").slideToggle(); 
});


});


function contact_start(formData, jqForm, options) {
  $('#contact_submit').attr("disabled", "true");
  $('#contact_submit').attr("value", "Proszę czekać...");
return true;
} 

function contact_response(data) {
  $('#contact_submit').removeAttr('disabled');
  $('#contact_submit').attr("value", "Wyślij");

  $('#contact_response').html(data.message);

  if(data.css) {
    json2css(data.css);
  }
}

function poll_start(formData, jqForm, options) {
  $('#poll_submit').attr("disabled", "true");
  $('#poll_submit').attr("value", "Proszę czekać...");
return true;
} 

function poll_response(data) {
  $('#poll_submit').removeAttr('disabled');
  $('#poll_submit').attr("value", "Głosuj");

  if(data.results) {
    var ul = $('<ul/>');

    jQuery.each(data.results, function(array, answer) {
      if(answer[1] == 0) { answer[1] = '0'; }
      if(answer[2] == 0) { answer[2] = '0'; }
      if(answer[3] == 0) { answer[3] = '0'; }
      if(answer[4] == 0) { answer[4] = '0'; }

      ul.append($('<li/>').html(answer[0] +' ('+ answer[1] +'%)<br /><div class="clear_both"><div style="width: '+ answer[2] +'px;" class="poll_bar"></div><div style="width: '+ answer[3] +'px;" class="poll_bar_empty"></div></div>'));
    });

    $('#poll_content').html('').append(ul);  
  }

  if(data.message) {
  $('#poll_response').html(data.message);
  }
}

function comment_page_start(formData, jqForm, options) {
  $('#comment_submit').attr("disabled", "true");
  $('#comment_submit').attr("value", "Proszę czekać...");
return true;
} 

function comment_page_response(data) {
  $('#comment_submit').removeAttr('disabled');
  $('#comment_submit').attr("value", "Dodaj komentarz");

  $('#comment_response').html(data.message);

  if(data.waiting) {
    var waiting = $('#comments_waiting').html();
    waiting = parseInt(waiting) + 1;

    $('#comment_waiting').css('visibility', 'visible');
    $('#comments_waiting').html(waiting).fadeOut(400).fadeIn(300).fadeOut(400).fadeIn(300);
  }

  if(data.css) {
    json2css(data.css);
  }
}

function comment_event_start(formData, jqForm, options) {
  $('#comment_submit').attr("disabled", "true");
  $('#comment_submit').attr("value", "Proszę czekać...");
return true;
} 

function comment_event_response(data) {
  $('#comment_submit').removeAttr('disabled');
  $('#comment_submit').attr("value", "Dodaj komentarz");

  $('#comment_response').html(data.message);

  if(data.waiting) {
    var waiting = $('#comments_waiting').html();
    waiting = parseInt(waiting) + 1;

    $('#comment_waiting').css('visibility', 'visible');
    $('#comments_waiting').html(waiting).fadeOut(400).fadeIn(300).fadeOut(400).fadeIn(300);
  }

  if(data.css) {
    json2css(data.css);
  }
}

function comment_poll_start(formData, jqForm, options) {
  $('#comment_submit').attr("disabled", "true");
  $('#comment_submit').attr("value", "Proszę czekać...");
return true;
} 

function comment_poll_response(data) {
  $('#comment_submit').removeAttr('disabled');
  $('#comment_submit').attr("value", "Dodaj komentarz");

  $('#comment_response').html(data.message);

  if(data.waiting) {
    var waiting = $('#comments_waiting').html();
    waiting = parseInt(waiting) + 1;

    $('#comment_waiting').css('visibility', 'visible');
    $('#comments_waiting').html(waiting).fadeOut(400).fadeIn(300).fadeOut(400).fadeIn(300);
  }

  if(data.css) {
    json2css(data.css);
  }
}

function log_in_start(formData, jqForm, options) {
  $('#log_in_submit').attr("disabled", "true");
  $('#log_in_submit').attr("value", "Proszę czekać...");
return true;
} 

function log_in_response(data) {

  if(data.log_in) {
    var get = $.parseQuery();
    if(get.przekieruj) {
      window.top.location = '/' + get.przekieruj;
    }
    else {
      window.top.location = '/panel/';
    }
    return true;
  }
  else {
    $('#log_in_submit').removeAttr('disabled');
    $('#log_in_submit').attr("value", "Zaloguj mnie");
    $('#log_in_response').html(data.message);
  }
}

function top_log_in_start(formData, jqForm, options) {
  $('#top_log_in_submit').attr("disabled", "true");
  $('#top_log_in_submit').attr("value", "czekaj...");
return true;
} 

function top_log_in_response(data) {

  if(data.log_in) {
    window.top.location = location.href;
    return true;
  }
  else {
    $('#top_log_in_submit').removeAttr('disabled');
    $('#top_log_in_submit').attr("value", "zaloguj");
    $('#top_log_in_response').html(data.message);
    $("#top_log_in_response").slideDown("slow");
  }
}

function register_start(formData, jqForm, options) {
  $('#register_submit').attr("disabled", "true");
  $('#register_submit').attr("value", "Proszę czekać...");
return true;
}

function register_response(data) {
  $('#register_submit').removeAttr('disabled');
  $('#register_submit').attr("value", "Rejestruj mnie");

  $('#register_response').html(data.message);

  if(data.css) {
    json2css(data.css);
  }
}

function activation_start(formData, jqForm, options) {
  $('#activation_submit').attr("disabled", "true");
  $('#activation_submit').attr("value", "Proszę czekać...");
return true;
}

function activation_response(data) {
  $('#activation_submit').removeAttr('disabled');
  $('#activation_submit').attr("value", "Wyślij");

  $('#activation_response').html(data.message);

  if(data.css) {
    json2css(data.css);
  }
}

function retrieve_password_start(formData, jqForm, options) {
  $('#retrieve_password_submit').attr("disabled", "true");
  $('#retrieve_password_submit').attr("value", "Proszę czekać...");
return true;
}

function retrieve_password_response(data) {
  $('#retrieve_password_submit').removeAttr('disabled');
  $('#retrieve_password_submit').attr("value", "Odzyskaj");

  $('#retrieve_password_response').html(data.message);

  if(data.css) {
    json2css(data.css);
  }
}

function new_password_start(formData, jqForm, options) {
  $('#new_password_submit').attr("disabled", "true");
  $('#new_password_submit').attr("value", "Proszę czekać...");
return true;
}

function new_password_response(data) {
  $('#new_password_submit').removeAttr('disabled');
  $('#new_password_submit').attr("value", "Zapisz");

  $('#new_password_response').html(data.message);

  if(data.css) {
    json2css(data.css);
  }
}

function announced_add_start(formData, jqForm, options) {
  $('#announced_add_submit').attr("disabled", "true");
  $('#announced_add_submit').attr("value", "Proszę czekać...");
  $('#announced_add_error').css('display', 'none');

  $('#announced_add input').css('border-color', '#CCCCCC');
  $('#announced_add select').css('border-color', '#CCCCCC');
  $('#announced_add textarea').css('border-color', '#CCCCCC');

  $('#announced_title_info').html('');
  $('#announced_text_info').html('');
  $('#announced_email_info').html('');
  $('#announced_www_info').html('');
  $('#filters_3_info').html('');
  $('#filters_5_info').html('');
  $('#filters_6_info').html('');
  $('#filters_8_info').html('');
  $('#filters_9_info').html('');
  $('#filters_10_info').html('');
  $('#filters_19_info').html('');
  return true;
}

function announced_add_response(data) {

  if(data.error) {
    $('#announced_add_submit').removeAttr('disabled');
    $('#announced_add_submit').attr("value", "Dodaj ogłoszenie");
    $('#announced_add_error').css('display', 'block');

    jQuery.each(data.error, function(error_object, error_info) {
      $('#'+ error_object).css('border-color', 'red');
      if(error_info != '') {
        $('#'+ error_object +"_info").html(error_info);
      }
    });    
  }
  else if(data.announced_add) {
    window.top.location = '/ogloszenia/pokaz/' + data.announced_add + '/';
    return true;
  }
}

function announced_edit_start(formData, jqForm, options) {
  $('#announced_edit_submit').attr("disabled", "true");
  $('#announced_edit_submit').attr("value", "Proszę czekać...");

  $('#announced_edit_delete_submit').attr("disabled", "true");
  $('#announced_edit_delete_submit').attr("value", "Proszę czekać...");


  $('#announced_edit_error').css('display', 'none');

  $('#announced_edit input').css('border-color', '#CCCCCC');
  $('#announced_edit select').css('border-color', '#CCCCCC');
  $('#announced_edit textarea').css('border-color', '#CCCCCC');

  $('#announced_title_info').html('');
  $('#announced_text_info').html('');
  $('#announced_email_info').html('');
  $('#announced_www_info').html('');
  $('#filters_3_info').html('');
  $('#filters_5_info').html('');
  $('#filters_6_info').html('');
  $('#filters_8_info').html('');
  $('#filters_9_info').html('');
  $('#filters_10_info').html('');
  $('#filters_19_info').html('');
  return true;
}

function announced_edit_response(data) {

  if(data.error) {
    $('#announced_edit_submit').removeAttr('disabled');
    $('#announced_edit_submit').attr("value", "Dodaj ogłoszenie");

    $('#announced_edit_delete_submit').removeAttr('disabled');
    $('#announced_edit_delete_submit').attr("value", "Usuń ogłoszenie");


    $('#announced_edit_error').css('display', 'block');

    jQuery.each(data.error, function(error_object, error_info) {
      $('#'+ error_object).css('border-color', 'red');
      if(error_info != '') {
        $('#'+ error_object +"_info").html(error_info);
      }
    });    
  }
  else if(data.announced_edit) {
    window.top.location = '/ogloszenia/pokaz/' + data.announced_edit + '/';
    return true;
  }
  else if(data.announced_delete) {
    window.top.location = data.announced_delete;
    return true;  
  }
}

function event_add_start(formData, jqForm, options) {
  $('#event_add_submit').attr("disabled", "true");
  $('#event_add_submit').attr("value", "Proszę czekać...");
  $('#event_add_error').css('display', 'none');

  $('#event_add input').css('border-color', '#CCCCCC');
  $('#event_add select').css('border-color', '#CCCCCC');
  $('#event_add textarea').css('border-color', '#CCCCCC');

  $('#event_title_info').html('');
  $('#event_text_info').html('');
  $('#event_date_start_info').html('');
  return true;
}

function event_add_response(data) {

  if(data.error) {
    $('#event_add_submit').removeAttr('disabled');
    $('#event_add_submit').attr("value", "Dodaj imprezę");
    $('#event_add_error').css('display', 'block');

    jQuery.each(data.error, function(error_object, error_info) {
      $('#'+ error_object).css('border-color', 'red');
      if(error_info != '') {
        $('#'+ error_object +"_info").html(error_info);
      }
    });    
  }
  else if(data.event_add) {
    window.top.location = '/imprezy/' + data.event_add + '/';
    return true;
  }
}

function event_edit_start(formData, jqForm, options) {
  $('#event_edit_submit').attr("disabled", "true");
  $('#event_edit_submit').attr("value", "Proszę czekać...");

  $('#event_edit_accept_submit').attr("disabled", "true");
  $('#event_edit_accept_submit').attr("value", "Proszę czekać...");

  $('#event_edit_delete_submit').attr("disabled", "true");
  $('#event_edit_delete_submit').attr("value", "Proszę czekać...");

  $('#event_edit_off_submit').attr("disabled", "true");
  $('#event_edit_off_submit').attr("value", "Proszę czekać...");

  $('#event_edit_error').css('display', 'none');

  $('#event_edit input').css('border-color', '#CCCCCC');
  $('#event_edit select').css('border-color', '#CCCCCC');
  $('#event_edit textarea').css('border-color', '#CCCCCC');

  $('#event_title_info').html('');
  $('#event_text_info').html('');
  $('#event_date_start_info').html('');
  return true;
}

function event_edit_response(data) {

  if(data.error) {
    $('#event_edit_submit').removeAttr('disabled');
    $('#event_edit_submit').attr("value", "Edytuj imprezę");

    $('#event_edit_accept_submit').removeAttr('disabled');
    $('#event_edit_accept_submit').attr("value", "Edytuj i akceptuj imprezę");

    $('#event_edit_delete_submit').removeAttr('disabled');
    $('#event_edit_delete_submit').attr("value", "Usuń imprezę");

    $('#event_edit_off_submit').removeAttr('disabled');
    $('#event_edit_off_submit').attr("value", "Edytuj i wyłącz imprezę");

    $('#event_edit_error').css('display', 'block');

    jQuery.each(data.error, function(error_object, error_info) {
      $('#'+ error_object).css('border-color', 'red');
      if(error_info != '') {
        $('#'+ error_object +"_info").html(error_info);
      }
    });    
  }
  else if(data.event_edit) {
    window.top.location = '/imprezy/' + data.event_edit + '/';
    return true;
  }
  else if(data.event_delete) {
    window.top.location = data.event_delete;
    return true;  
  }
}

function json2css(data) {
  jQuery.each(data, function(array, css_object) {
    jQuery.each(css_object, function(css, css_value) {
      jQuery.each(css_value, function(css_property, css_command) {
        $(css).css(css_property, css_command);
      });
    });
  });
}