$(document).ready(function(){
  Cufon.replace('h2');
  Cufon.replace('h1');
  Cufon.replace('h3');
  // Cufon.replace('h1',{fontFamily: 'Essays1743'});
  $("DIV.joke").corner('round 16px');
  if(!$.browser.msie){
    $("#search input").corner('round 8px');
  }
  $(".vote_stars").vote();
  if( $("#spo").length >0){
    $("#spo").stickyfloat({duration: 400,offsetY:0,lockBottom:false,startOffset:0});
  }
});

$.fn.vote = function() {
    return $(this).each(function(){
        var rating =  $(this).parent('.vote').find('strong').html() || 0;
        var joke_prefix = $(this).attr('id');

        if( $.cookie(joke_prefix.replace(/^d/,'v') ) > 0){
            return;
        }
        $(this).html("Zag\u0142osuj: ");
        for(var i=1;i<=5;i++){
            var orig_class='';
            
            if(i<=rating){orig_class = 'star_on'}else{orig_class ='star_off'}

            var tag = '<span class="'+orig_class+'" id="'+joke_prefix+'_'+i+'"></span>';
            $(this).append($(tag).data('i',i).data('orig_class',orig_class).data('joke_prefix',joke_prefix).mouseover(function(){
                                    $(this).parent().find('span.star_value').html($(this).data('i'));
                                    for(var z=1;z<=5;z++){
                                        if(z<=$(this).data('i')){
                                            $("#"+$(this).data('joke_prefix')+'_'+z).attr('class','star_on');
                                        }else{
                                            $("#"+$(this).data('joke_prefix')+'_'+z).attr('class','star_off');
                                        }
                                    }
                                    //$(this).attr('class','star_on');
                               }).mouseout(function(){
                                    $(this).parent().find('span.star_value').html('');
                                    for(var z=1;z<=5;z++){
                                            $("#"+$(this).data('joke_prefix')+'_'+z).attr('class', $("#"+$(this).data('joke_prefix')+'_'+z).data('orig_class') );
                                    }
                                    
                               }).click(function(){
                                   var data_id  = $(this).data('joke_prefix').replace(/^d/,'');
                                   var data_vote= $(this).data('i');
                                   
                                   $.ajax({
                                       'url': "/ajax/vote",
                                       'data': {
                                           'id':data_id,
                                           'vote':data_vote
                                       },
                                       'type': 'post',
                                       'success': function(response){
                                           $.cookie('v'+response,response,{expires:1});
                                           $("#d"+response).html("Dzi\u0119kujemy!");
                                       }
                                   });
                               })
                          );
        }
        $(this).append($('<span class="star_value"></span>'));
        //$(this).html(html);
    });
}

