jQuery(document).ready(function($) {
    
  //Remove webkit autocomplete yellow bg
  
  if (navigator.userAgent.toLowerCase().indexOf("chrome") >= 0) {
	$(window).load(function(){
		$('input:-webkit-autofill').each(function(){
			var text = $(this).val();
			var name = $(this).attr('name');
			$(this).after(this.outerHTML).remove();
			$('input[name=' + name + ']').val(text);
		});
	});
  }
  
  //Notifications
  if ( $('.canhide').length != 0 ) $(".canhide").click(function() {        
        $(this).fadeOut(500, function() {
            if ( $('.notification_lightbulb').length != 0 ) {
                $.cookie('notification_lightbulb', 'TRUE', {expires: 1});
            document.location = 'http://nebeher.cz/register';
            }
        });
        
    });
	
	//Kategorie slide
	
	$('#leftpanel h2').click(function(data){
	console.log(data);
		$('#leftpanel .categories').not(":animated").slideToggle('slow', function(){
			if ($.cookie('slide_categories') == 0)
				$.cookie('slide_categories', 1, {expires: 24*365});
			else
				$.cookie('slide_categories', 0, {expires: 24*365});
		});
	});
    
  // Facebox
  $('a[rel*=facebox]').facebox();
  //$('#facebox').click(function(){$('.close').trigger('close.facebox')});
  
  // TOOLTIP
  if ($('#a1_up').length != 0) $('#a1_up').bubbletip($('#tip1_up'));
  //$('#login').bubbletip($('#login_up'),{deltaDirection : 'down'});
  if ( $('#login').length != 0 ) {$('#login').bubbletip($('#login_up'),{deltaDirection : 'down'});}
  if ( $('#a1_up').length != 0 ) {$('#a1_up').bubbletip($('#tip1_up'));}
  
  // Main page slider
  var   curslide = 1,
        numslides = 3;
  $('#slideright').click(function(){
      if (curslide >= numslides) { 
          $('#slideitems').not(':animated').animate({"left": "0px"}, 1000,function () {curslide = 1;});  
      } else {
          $('#slideitems').not(':animated').animate({"left": "-=714px"}, 1000, function() {curslide++;});
      }
  });
  $('#slideleft').click(function(){
      if (curslide <= 1) {
           $('#slideitems').not(':animated').animate({"left": "-="+(numslides-1)*6*119+"px"}, 1000, function () {curslide = numslides;}); 
      } else {
      $('#slideitems').not(':animated').animate({"left": "+=714px"}, 1000, function () {curslide--;});
      }
  });
  
  // Bestgames slider
  var   bestcurslide = 1,
        bestnumslides = 3;
  function bestheading() {
    switch (bestcurslide) {
            case 1:
                text = 'Nejlepší hry';
                break;
            case 2:
                text = 'Nejnovější hry';
                break;
            case 3:
                text = 'Nejhranejší hry';
                break;
            default:
                text = 'Nejlepší hry';
                break;
        }

    $('#bestheading').text(text).fadeIn(300);
  }
  $('#bestright').click(function(){
      $('#bestheading').fadeOut(1000);
      if (bestcurslide >= bestnumslides) { 
          $('#bestgamesitems').not(':animated').animate({"left": "0px"}, 1000,function () {bestcurslide = 1;bestheading();});  
      } else {
          $('#bestgamesitems').not(':animated').animate({"left": "-=216px"}, 1000, function() {bestcurslide++;bestheading();});
      }
  });
  $('#bestleft').click(function(){
      $('#bestheading').fadeOut(1000);
      if (bestcurslide <= 1) {
           $('#bestgamesitems').not(':animated').animate({"left": "-="+(bestnumslides-1)*216+"px"}, 1000, function () {bestcurslide = bestnumslides;bestheading();}); 
      } else {
      $('#bestgamesitems').not(':animated').animate({"left": "+=216px"}, 1000, function () {bestcurslide--;bestheading();});
      }
  });
  
  // Medails slider
  
  var   medals = [],
        actual = 0,
        i = 0;
        run = 1;
        clicks = 0;
        
  function Actualminus(actual) {
              actual--;
              if (actual < 0)
                  actual = medals.length-1;  
              return actual;
  }
  
   function Actualplus(actual) {
              actual++;
              if (actual > medals.length-1)
                  actual = 0;  
              return actual;
  }
        
  $('#medalitems').find('.medal').each(function(){
    medals[i] = $(this).find('img').attr('src');    
    i++;
  });
  
  $('#medalsleft').click(function(){
      if ($('#medalitems').is(':animated')) {
        } else {
        $("#tip_medail_"+(Actualminus(actual)+1)).prependTo('#medalitems');
        $('#medalitems').css({'left' : '-68px'}).animate({'left' : '+=68px'}, 500, function(){
          actual = Actualminus(actual);
        });
            
        }
  });
 
  $('#medalsright').click(function(){
        $('#medalitems').not(':animated').animate({'left' : '-=68px'}, 500, function(){
         $('#medalitems').css({'left' : '0px'});  
        $("#tip_medail_"+(Actualplus(actual-1)+1)).appendTo('#medalitems');
          actual = Actualplus(actual);
       });         
  }); 
  
  //Favorites

    $('#add_favorite').click(function(){
        if ($(this).attr("href") != "#") {
        $.ajax({
          url: $(this).attr("href"),
          success: function(data) {
                $('#add_favorite').attr("href", "#")
                $('#add_favorite').html('<img src="http://nebeher.cz/images/success.png" alt="accept">Hra úspešne pridaná');
          }
        });
        } 
        return false;
    });
    
    $('#remove_favorite').click(function(){
        if ($(this).attr("href") != "#") {
        $.ajax({
          url: $(this).attr("href"),
          success: function(data) {
                $('#remove_favorite').attr("href", "#")
                $('#remove_favorite').html('<img src="http://nebeher.cz/images/fav_minus.png" alt="accept">Hra úspešne odobraná');
          }
        });
        } 
        return false;
    });
  
  
  //Stars
  var original = [];
  var number = 0;
  $("#stars img").each(function(i) {
        original[i+1] = $(this).attr("src");
  });
  $("#stars").mouseleave(function(){
    if ($(this).attr("class") != "playstars starred")
        for (j = 1; j <= 5; j++)
            $("#star_"+j).attr("src", original[j]);
  });
  $("#stars img").mouseenter(function(){
    if ($("#stars").attr("class") != "playstars starred") {
    number = ($(this).attr("id").substr($(this).length-2,1));
    link = ($(this).parent().attr("href"));
    }
    $(this).click(function() {
        if ($("#stars").attr("class") != "playstars starred") {
            for (j = 1; j <= number; j++) {
                    $("#star_"+j).attr("src", "http://nebeher.cz/images/bigstar.png");
            }
            for (i = j; i <= 5; i++) {
                    $("#star_"+i).attr("src", "http://nebeher.cz/images/bigstar_empty.png");
            }
            $.ajax({
              url: link,
              success: function(data) {
                if (Number(data) != 0) {
                $("#stars").addClass("starred");
                for (j = 1; j <= Number(data); j++)
                    $("#star_"+j).attr("src", "http://nebeher.cz/images/bigstar.png");
                for (i = Number(data)+1; i <= 5; i++) 
                    $("#star_"+i).attr("src", "http://nebeher.cz/images/bigstar_empty.png");
                add_xp_bar(20)
                }
              }
            });
        } 
        return false;
    });
    if ($("#stars").attr("class") != "playstars starred") {
    for (j = 1; j <= 5; j++)
        $("#star_"+j).attr("src", original[j]);
    for (i = 1; i <= number; i++) {
        $("#star_"+i).attr("src", "http://nebeher.cz/images/bigstar.png");
        }
    }
    
  });
  
  
  // Seach
  $('#search .menusearch_middle').focus(function(){
      if ($(this).val() == "Hledat") {
          $(this).val("");
      }
  });
  
  $('#search .menusearch_middle').focusout(function(){
      if ($(this).val() == "") {
          $(this).val("Hledat");
      }
  });
  
 
  
});

 //Bridge
  function refresh_score(params) {
      if (params.scoreLabel == "Score") {
          var new_score = params.score;
          var new_name = params.name;
          var new_place;
          var names = [];
          var scores = [];
          var i = 0;
          var j = 0;
          htmls = "";
          $("#topten .topten_name").each(function(){
              j = 0;
              $(this).children().each(function() {
                  if (j == 0) {
                      names[i] = $(this).text();
                  }else {
                      scores[i] = Number($(this).text());
                  }
                  j++;
              });
              i++;
          });
          
          for (i = 0; i < scores.length; i++)
              if (new_score > scores[i]) {
                  new_place = i;
                  break;
              } 

          var neico = (new_place+1);
          var dlzka = scores.length;

          for (i = dlzka; i >= neico; i--) {
              scores[i] = scores[i-1];
              names[i] = names[i-1];
          }
          
          scores[new_place] = new_score; 
          names[new_place] = new_name; 
          
          for (i = 0; i < scores.length; i++) {
            htmls += '<div class="topten_name medal'+(i+1)+'"><a href="http://nebeher.cz/user/'+names[i]+'">'+names[i]+'</a><span class="topten_small">'+scores[i]+'</span></div>';
            if (i == 9)
                break;
          }
          $("#topten .topten_list").fadeOut(1).fadeIn(800);
          add_xp_bar(30);
          $("#topten .topten_list").html(htmls);
      } else if (params.submission == "medal") {
          jQuery.facebox('Ziskal si medailu<br />'+params.name+'<br /><center><img src="'+params.thumbnail+'" /></center><br /><i>'+params.description+'</i>', 'medal_popup');
      }
  }
 
  
  //XP bar
  function add_xp_bar(XP) {
    original = $("#level .level_bar_mid").width();
    one = Number($("#level .level_bar_xp").text().split("/")[0]);
    two = $("#level .level_bar_xp").text().split("/")[1];
    j = 1;
    if (XP+one > two)
        XP = two - one;
    original_level = $("#a1_up").text();
    $("#a1_up").text("+"+XP).css({
        color:"#4d4", 
        'textShadow':'#222 1px 1px 0px'
    });
    for (i = 1; i <= XP;i++)
        $("#level .level_bar_xp").delay(2000/XP).fadeIn(1, function() {
            $("#level .level_bar_xp").text((one+j)+"/"+two);
            j++;
        });
    i = 0;
    var new_width = (original+(XP*((538-original)/(two-one))));    
    $("#level .level_bar_mid").animate({
         width: new_width
    }, { 
        step : function(now) {
            i++;
            if (i == XP) {
            $("#a1_up").delay(2000).fadeIn(1, function() {
                $("#a1_up").text(original_level).css({color:"white", 'textShadow':'black 1px 1px 0px'});
            });                
            }
            if (now == 538) {
                original_level++;
                $("#a1_up").text(original_level);
                $("#loginbox .user_level").text(original_level);
            }
        },
        duration : 2000
    });

  
  }

/**/
