$(document).ready(function () {
  $('.closemsg, #msg, .errormsg').click(function () {
    $('#msg').remove();
  });
  
  $('.reportbtn').click(function() {
    $(this).blur();
    
    if ($(this).hasClass('reported_ok')) {
      return false;
    }
    
    if (confirm('Ако искате да обърнете вниманието на DataBG към тази страница, то натиснете "OK"')) {
      $.getJSON('/report/'+ submodule +'/'+ $(this).attr('id'), function(data) {
        if (data.ok == true) {
          $('.reportbtn').addClass('reported_ok');
          $('.reportbtn').removeClass('reportbtn');
          $('.reported_ok').html('<b>Сигнал подаден</b>');
          $('.reported_ok').removeAttr('href');
          $('.favebtn').remove();
        } else {
          alert('Не успях да приема сигнала за нередност. Моля, опитайте по-късно!');
        }
      });
    }
    
    return false;
  });
  
  $('.comment_del').click(function() {
    $(this).blur();
    obj = $(this);
    $.getJSON('/delcomment/'+ $(obj).attr('id'), function(data) {
      if (data.ok == true) {
        obj.parent().parent().parent().remove();
      } else {
        alert('Не успях да изтрия коментара - '+ data.ok);
      }
    });
    
    return false;
  });
  
  $('.comment_report').click(function() {
    $(this).blur();
    
    if ($(this).hasClass('report_done')) {
      return false;
    }
    obj = $(this);
    if (confirm('Ако искате да обърнете вниманието на DataBG към този коментар, то натиснете "OK"')) {
      if (module == 'info') {
        url = '/'+ submodule +'/reportcomment/'+ obj.attr('id');
      } else {
        url = '/reportcomment/'+ obj.attr('id');
      }
      $.getJSON(url, function(data) {
        if (data.ok == true) {
          obj.addClass('report_done');
          obj.removeClass('comment_report');
          obj.removeAttr('href');
          obj.html('<b>Сигнал подаден</b>');
        } else {
          alert('Не успях да приема сигнала за нередност. Моля, опитайте по-късно!');
        }
      });
    }
    
    return false;
  });
  
  $('.favebtn').click(function() {
    if ($(this).hasClass('unfavebtn')) {
      return false;
    }
    
    $(this).blur();
    obj = $(this);
    $.getJSON('/addtofav/'+ submodule +'/'+ obj.attr('id'), function(data) {
      if (data.ok == true) {
        obj.addClass('unfavebtn');
        obj.removeClass('favebtn');
        obj.html('<b>Изтрий от любими</b>');
        obj.attr('onclick', 'javascript:unfave(this)');
        $('.reportbtn').css('display', 'none');
      } else {
        alert('Не успях да добавя снимката към твоята колекция');
      }
    });
  });
  
  $('.unfavebtn').click(function() {
    $(this).blur();
    
    obj = $(this);
    
    $.getJSON('/delfromfav/'+ submodule +'/'+ obj.attr('id'), function(data) {
      if (data.ok == true) {
        obj.addClass('favebtn');
        obj.removeClass('unfavebtn');
        obj.html('<b>Любими</b>');
        obj.attr('onclick', 'javascript:fave(this)');
        $('.reportbtn').css('display', '');
      } else {
        alert('Не успях да добавя снимката към твоята колекция');
      }
    });
  });
  
  $('#topsearch_input').click(function() {
	$(this).val('');
  });
  
});

function fave(obj) {
  $(obj).blur();
  $.getJSON('/addtofav/'+ submodule +'/'+ $(obj).attr('id'), function(data) {
    if (data.ok == true) {
      $(obj).addClass('unfavebtn');
      $(obj).removeClass('favebtn');
      $(obj).html('<b>Изтрий от любими</b>');
      $(obj).attr('onclick', 'javascript:unfave(this)');
      $('.reportbtn').css('display', 'none');
    } else {
      alert('Не успях да добавя снимката към твоята колекция');
    }
  });
}

function unfave(obj) {
  $(obj).blur();
  $.getJSON('/delfromfav/'+ submodule +'/'+ $(obj).attr('id'), function(data) {
    if (data.ok == true) {
      $(obj).addClass('favebtn');
      $(obj).removeClass('unfavebtn');
      $(obj).html('<b>Любими</b>');
      $(obj).attr('onclick', 'javascript:fave(this)');
      $('.reportbtn').css('display', '');
    } else {
      alert('Не успях да добавя снимката към твоята колекция');
    }
  });
}

function alterscore(score) {
  $('#score').html(score);
  $('#rate1').removeClass('full03 full05 full08 full1');
  $('#rate2').removeClass('full03 full05 full08 full1');
  $('#rate3').removeClass('full03 full05 full08 full1');
  $('#rate4').removeClass('full03 full05 full08 full1');
  $('#rate5').removeClass('full03 full05 full08 full1');
  
  if (score < 1) {
    if (score < 0.75) {
      if (score < 0.45) {
        if (score > 0.24) {
          $('#rate1').addClass('full03');
        }
      } else {
        $('#rate1').addClass('full05');
      }
    } else {
      $('#rate1').addClass('full08');
    }
  } else {
    $('#rate1').addClass('full1');
  }
  
  if (score < 2) {
    if (score < 1.75) {
      if (score < 1.45) {
        if (score > 1.24) {
          $('#rate2').addClass('full03');
        }
      } else {
        $('#rate2').addClass('full05');
      }
    } else {
      $('#rate2').addClass('full08');
    }
  } else {
    $('#rate2').addClass('full1');
  }
  
  if (score < 3) {
    if (score < 2.75) {
      if (score < 2.45) {
        if (score > 2.24) {
          $('#rate3').addClass('full03');
        }
      } else {
        $('#rate3').addClass('full05');
      }
    } else {
      $('#rate3').addClass('full08');
    }
  } else {
    $('#rate3').addClass('full1');
  }
  
  if (score < 4) {
    if (score < 3.75) {
      if (score < 3.45) {
        if (score > 3.24) {
          $('#rate4').addClass('full03');
        }
      } else {
        $('#rate4').addClass('full05');
      }
    } else {
      $('#rate4').addClass('full08');
    }
  } else {
    $('#rate4').addClass('full1');
  }
  
  if (score < 5) {
    if (score < 4.75) {
      if (score < 4.45) {
        if (score > 4.24) {
          $('#rate5').addClass('full03');
        }
      } else {
        $('#rate5').addClass('full05');
      }
    } else {
      $('#rate5').addClass('full08');
    }
  } else {
    $('#rate5').addClass('full1');
  }
}

function dump(arr,level)
{
	var dumped_text = "";
	if(!level) level = 0;
	
	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++)
		level_padding += "    ";
	
	if(typeof(arr) == 'object')
	{ //Array/Hashes/Objects 
		for(var item in arr)
		{
			var value = arr[item];
			
			if(typeof(value) == 'object')
			{ //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			}
			else
			{
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	}
	else
	{ //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}
