function loginInit(){
  jQuery('.show_login').click(function() {
     $('#login').show('slow'); return false;
      })
}

function toggleMeInit(){
  jQuery('.toggle_me').click(function(){
     $(this).parent().next().toggle('slow');
     return false;})
}

function questionPreviewInit(){
  jQuery('.question_preview').click(function() {
    jQuery.ajax(
      {url: '/question_preview', 
       data: {title: $('#title')[0].value, body: $('#body')[0].value }, 
       success: function(response) { jQuery('#preview').show(), jQuery('#preview').html(response) }}); return false;})
}

function answerPreviewInit(){
  jQuery('.answer_preview').click(function() {
    jQuery.ajax(
      {url: '/answer/preview', 
       data: {body: $('#body')[0].value }, 
       success: function(response) { jQuery('#preview').show(), jQuery('#preview').html(response) }}); return false;})
}

function bookmarkPreviewInit(){
  jQuery('.question_preview').click(function() {
    jQuery.ajax(
      {url: '/bookmark/preview', 
       data: {title: $('#title')[0].value, body: $('#body')[0].value, bookmark: $('#bookmark')[0].value }, 
       success: function(response) { jQuery('#preview').show(), jQuery('#preview').html(response) }}); return false;})
}

function hideLoginInit(){
  jQuery('.hide_login').click(function(){jQuery('#login').hide('slow')})
}

function markAnswerInit(){
  jQuery('.mark_answer')
     .click(function(){
     var my_url = 'http://'+location.host+'/answer/toggleMarked/id/'+/\d+/.exec(this.id)[0];
     var refresh = '#a_body_' + /\d+/.exec(this.id)[0];
     var ma = '#mark_answer_' + /\d+/.exec(this.id)[0];
     jQuery.ajax({url: my_url,
     type: 'POST',
     dataType: 'json',
     success: function(j){ 
      for (var i = 0; i < j.length; i++) {
        if (j[i].option) { 
          $(refresh).addClass('marked-answer');
          $(ma)[0].innerHTML = '[usuń wyróżnienie]';
          }
        else {
          $(refresh).removeClass('marked-answer');
          $(ma)[0].innerHTML = '[wyróżnij odpowiedź]';
          }
        }}
      });
     return false;})
}

function reportAnswerInit(){
jQuery('.report_answer', this)
         .click(function(){
         if (confirm('Jesteś pewien?'))
         {
          var url = 'http://'+location.host+'/report_answer';
          var refresh = '#mod_options_' + /\d+/.exec(this.id)[0];
          jQuery(refresh).load(url, {id: /\d+/.exec(this.id)[0] }, resetReportAnswerInit)
         }; return false;})
}

function resetReportAnswerInit(){
jQuery('.reset_answer', this)
         .click(function(){
         if (confirm('Jesteś pewien?'))
         {
          var url = 'http://'+location.host+'/moderator/resetAnswerReports';
          var refresh = '#mod_options_' + /\d+/.exec(this.id)[0];
          jQuery(refresh).load(url, {id: /\d+/.exec(this.id)[0] }, reportAnswerInit)
         }; return false;})
}

function reportQuestionInit(){
jQuery('.report_question', this)
         .click(function(){
         if (confirm('Jesteś pewien?'))
         {
          var url = 'http://'+location.host+'/report_question';
          var refresh = '#report_question_' + /\d+/.exec(this.id)[0];
          jQuery(refresh).load(url, {id: +/\d+/.exec(this.id)[0] }, resetReportQuestionInit)
         }; return false;})
}

function resetReportQuestionInit(){
jQuery('.reset_question', this)
         .click(function(){
         if (confirm('Jesteś pewien?'))
         {
          var url = 'http://'+location.host+'/moderator/resetQuestionReports';
          var refresh = '#report_question_' + /\d+/.exec(this.id)[0];
          jQuery(refresh).load(url, {id: +/\d+/.exec(this.id)[0] }, reportQuestionInit)
         }; return false;})
}

function deleteTagInit(){
  jQuery('.delete_tag').click(function(){
    var my_url = 'http://'+location.host + '/moderator/deleteTagForQuestion';
    var me = this;
    jQuery.ajax({url: my_url,
      type: 'POST',
      dataType: 'json',
      data: {tag: me.rel, question_id: $('#question_id').val()},
      success: function(j){
        for (var i = 0; i < j.length; i++)
        {
          if (j[i].option == 1){ $(me).parents('li').remove();}
        }
      }
    });
    return false;})
}

function removeTagInit(){
  jQuery('.remove_tag')
         .click(function(){
         var my_url = 'http://'+location.host + '/question/' + this.rel + '/tag/remove/' + this.id;
         var me = this;
         jQuery.ajax({url: my_url,
           type: 'POST',
           dataType: 'json',
           success: function(j)
           { 
            for (var i = 0; i < j.length; i++) 
            {
              switch(j[i].option)
              {
                case 1: 
                  $(me).parents('li').remove();
                  break;
                case -1:
                  $(me).remove();
                  break;
              }
            }
           }
          });
         return false;})
}

function addTagForm(){
  var options = { 
        target:    '#question_tags',
        success:   function(){removeTagInit(); deleteTagInit()},
        type:      'post',
        resetForm: true  
    }; 
     // bind form using 'ajaxForm' 
    jQuery('.tag_form').ajaxForm(options); 
}

function postMsgForm(){
  var options = { 
        type:      'post',
        resetForm: true  
    }; 
     // bind form using 'ajaxForm' 
    jQuery('#new_msg').ajaxForm(options); 
}

function autocompleteTag(){
  jQuery('.autocomplete_me').autocomplete('http://' + location.host + '/tag_autocomplete', 
    { minChars:3, matchSubset:1, matchContains:1, cacheLength:0, selectOnly:1 });
}

function removeInterestedInit(e){
  jQuery('.remove_interested')
     .click(function()
     {
       var my_url = 'http://'+location.host+'/user_remove_interested/'+/\d+/.exec(this.id)[0];
       var me = this;
       jQuery.ajax({url: my_url,
       type: 'POST',
       dataType: 'json',
       success: function(j){ 
        for (var i = 0; i < j.length; i++) {
          if (j[i].option) { 
            $(me).parents('li').remove();
            }
        }
       }
      });
      return false;
     })
}

function removeSubscribedInit(e){
  jQuery('.remove_subscription')
     .click(function(){
       var my_url = 'http://'+location.host+'/user_not_subscribed/'+/\d+/.exec(this.id)[0];
       var me = this;
       jQuery.ajax({url: my_url,
       type: 'POST',
       dataType: 'json',
       success: function(j){ 
        for (var i = 0; i < j.length; i++) {
          if (j[i].option) {
            $(me).parents('li').remove();
            }
          }}
      });
     return false;})
}

function interestedQuestionInit(){
  jQuery('.interested_question')
     .click(function(){
     var my_url = 'http://'+location.host+'/user_interested/'+/\d+/.exec(this.id)[0];
     var refresh = '#interested_' + /\d+/.exec(this.id)[0];
     jQuery.ajax({url: my_url,
     type: 'POST',
     dataType: 'json',
     success: function(j){ 
      for (var i = 0; i < j.length; i++) {
      $(j[i].option).html(j[i].value)}}
     });return false;})
}

function subscribeQuestionInit(){
  jQuery('.subscribe_question')
     .click(function(){
     var url = 'http://'+location.host+'/subscribe_question';
     var refresh = '#subscribe_' + /\d+/.exec(this.id)[0];
     jQuery(refresh).load(url, {id: /\d+/.exec(this.id)[0] });
     return false;})
}

function optionVoteInit(){
  jQuery('.option_vote')
     .click(function(){
     var url = 'http://'+location.host+'/option_vote';
     jQuery('#survey').load(url, {id: /\d+/.exec(this.id)[0] });
     return false;})
}

function createCookie(name,value,days) 
{
  if (days) 
  {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else var expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function voteAnswerInit()
{
  jQuery('.vote_me')
     .click(function(){
     var score = /down/.exec(this.id) ? -1 : 1;
     var url = 'http://'+location.host+'/vote_answer';
     var refresh = '#vote_' + /\d+/.exec(this.id)[0];
     jQuery(refresh).load(url, { id: /\d+/.exec(this.id)[0], score: score}, votedAnswerTooltipInit);
     
     setTimeout('wylogowanywinnejzakladce()',2000);

     return false;})
}

function wylogowanywinnejzakladce()
{
     if( document.getElementById('login_form') != null)
     {
       createCookie('strona', window.location, 365);
       window.location.replace('/login');
     }
}     

function votedAnswerTooltipInit(){
  jQuery('.voted_answer').hover(function(){
      var score = /down/.exec(this.id) ? 'down' : 'up';
      var url_for = 'http://'+location.host+'/answer/userVoted?id=' + /\d+/.exec(this.id)[0] + '&score=' + score;
      ajax_showTooltip(url_for, this)},
      function(){ajax_hideTooltip();})
}

function promoteQuestionTooltipInit(){
  jQuery('.promote_img').hover(function(){
      var url_for = 'http://'+location.host+'/question_promoted_by/' + /\d+/.exec(this.id)[0];
      ajax_showTooltip(url_for, this)},
      function(){ajax_hideTooltip();})
}

function supportQuestionInit(){
  jQuery('.support_question')
     .click(function(){
     var url = 'http://'+location.host+'/support_question/' + /\d+/.exec(this.id)[0];
     var refresh = '#block_' + /\d+/.exec(this.id)[0];
     jQuery.post(url,
     function(response){
     $(refresh).fadeOut('slow').html(response).fadeIn('slow');}
     ); return false;})
}

function promoteQuestionInit(){
  jQuery('.promote_question')
     .click(function(){
     var url = 'http://'+location.host+'/promote_question/' + /\d+/.exec(this.id)[0];
     var refresh = '#block_' + /\d+/.exec(this.id)[0];
     ajax_hideTooltip();
     jQuery.post(url,
     function(response){
     $(refresh).fadeOut('slow').html(response).fadeIn('slow');}
     ); return false;})
}

function registerAjaxIndicator(){
  jQuery('#indicator').ajaxStart(function(){
   $(this).show();
 }).ajaxStop(function(){$(this).hide()});
}

function toggleLoginForm()
{
  jQuery('#new_account').toggle('slow');
    if ($('#new_account').is(':hidden'))
    {
      $('#login_form').attr('action', '/login');
    }
    else
    {
      $('#login_form').attr('action', '/add_user');
    }
    return false;
}

function showSearchTab(){
  jQuery('#tab_container ul').tabs();
}

function addSurveyOption(){
  jQuery('#add_survey_option').click( function() { 
  var count = parseInt(jQuery('#count_survey_options').val())+1;
  jQuery('#count_survey_options').val(count);
  jQuery("#survey_options").append('<div id="opt'+count+'" style="display:none"><label>'+count+'.</label><input type="text" name="survey_option['+count+']" id="survey_option_'+count+'" value="" size="50" /><br class="clearleft" /></div>');;
  jQuery('#opt'+count).toggle('slow');
  return false;
  } );
}

function toggleSurveyOption()
{
  var text = jQuery("#option_form").text();
  if(text=="Dodaj własną ankietę")
  {
  jQuery('#option_form').click( function()
  {
    jQuery("#survey_form").toggle('slow');
  }).toggle(function(){
	jQuery('#option_form').text('Domyślna ankieta');
	
	}, function(){jQuery('#option_form').text('Dodaj własną ankietę');
	$("#survey_options").empty();
	jQuery("#survey_options").append('<input id="count_survey_options" type="hidden" value="2" name="count_survey_options"/>');
	jQuery("#survey_options").append('<label>1.</label><input id="survey_option_1" type="text" size="50" value="" name="survey_option[1]"/><br class="clearleft"/><label>2.</label><input id="survey_option_2" type="text" size="50" value="" name="survey_option[2]"/><br class="clearleft"/>');
	}); 
   }
   else
   {
   jQuery('#option_form').click( function()
    {
    jQuery("#survey_form").toggle('slow');
    }).toggle(function(){
	jQuery('#option_form').text('Dodaj własną ankietę');
	$("#survey_options").empty();
	$("#error_for_empty_option").empty();
	jQuery("#survey_options").append('<input id="count_survey_options" type="hidden" value="2" name="count_survey_options"/>');
	jQuery("#survey_options").append('<label>1.</label><input id="survey_option_1" type="text" size="50" value="" name="survey_option[1]"/><br class="clearleft"/><label>2.</label><input id="survey_option_2" type="text" size="50" value="" name="survey_option[2]"/><br class="clearleft"/>');
	
	}, function(){jQuery('#option_form').text('Domyślna ankieta');
		});
   }
  }

function menu(){
		var selected = $("#navigation ul li").siblings(".selected").attr("id")||"top_menu_1";
		var timeoutID = null;
		var delay = 500;  //zmienia opoznienei
		$('#navigation ul li').hover(function(){
		showSubMenu(this, selected);
			return false;
		}, function(){
			//nothing
		});
		
		$('#topNavigation').hover(function(){
				//nothing
				if (timeoutID) {
			clearTimeout(timeoutID);
			}
		}, function(){
			timeoutID = setTimeout(function() {
			hideSubMenu(this, selected);
			}, delay);
			return false;
		});
	}
function showSubMenu(element, selected){
			$.each( [1,2,3,4,5,6,7,8,9,10,11], function(i, n){
  			$("#top_menu_"+n).removeClass("selected");
			$("#sub_menu_"+n).removeClass("selected");
			});
			$('#top_menu_1').removeClass("firstselected");
			var id = '#sub_menu_' + /\d+/.exec(element.id)[0];
			$(element).addClass("selected");
			$("#sub_menu_1").removeClass("selected");
			$(id).addClass("selected");
}
function hideSubMenu(element, selected){
			$.each( [1,2,3,4,5,6,7,8,9,10,11], function(i, n){
  			$("#top_menu_"+n).removeClass("selected");
			$("#sub_menu_"+n).removeClass("selected");
			});
			
			if (selected == "top_menu_1") {
				$("#top_menu_1").addClass("firstselected");
				$("#top_menu_1").addClass("selected");
				$("#sub_menu_1").addClass("selected");
			}
			else
			{
				$('#'+selected).addClass("selected");
				$("#sub_menu_"+/\d+/.exec(selected)[0]).addClass("selected");
			}
}