$(document).ready(function(){
        $('.rate img').css('cursor', 'pointer');

        $('.rate img').mouseover(function() {
                var i = parseInt($(this).attr('id').substring(3, 4));
                for (var j = 1; j < 6; j++) {
                        $('#img' + j).attr('rel', $('#img' + j).attr('src'));

                        if (j < (i+1)) {
                                $('#img' + j).attr('src', '/images/star-on.png');
                        } else {
                                $('#img' + j).attr('src', '/images/star-off.png');
                        }
                }
        });

        $('.rate img').mouseout(function() {
                var i = parseInt($(this).attr('id').substring(3, 4));
                for (var j = 1; j < 6; j++) {    
                        $('#img' + j).attr('src', $('#img' + j).attr('rel'));
                }
        });

        $('.rate img').click(function() {
                var rating = parseInt($(this).attr('id').substring(3, 4));
                $.post("/js/ratevideo.php", { id: id, rating: rating }, function(data) {
                        $(".rate").append(data.result);
                        $('.rate img').unbind('click');
                        for (var j = 1; j < 6; j++) {
                                $('#img' + j).attr('src', $('#img' + j).attr('rel'));
                        }
                        $('.rate img').unbind('mouseover');
                        $('.rate img').unbind('mouseout');
                        $('.rate img').css('cursor', 'default');
                }, 'json');
        });
		
        var images;
		var title="";
        var i = 0;
        $('.preview').hover(function(e) {
			images = $(this).children('img');
			title = $(this).parent('a').attr('title');
			$(this).parent('a').attr('title', "");
			
			if (images.length > 1) {
				if ($(images[1]).attr('src') == "") {
					var b = $(images[0]).attr('src');
					var basename = b.substring(0, b.lastIndexOf('.'))

					for(var i = 1; i < 6;i++) {
						$(images[i]).attr('src', basename + "-00" + i + ".jpg");
					}
				}
				i=0;
				$(this).everyTime("1s", function() {
					$(images[i]).css('visibility', 'hidden');
					i=i+1;
					if (i == images.length) {
						i=1;
					}
					$(images[i]).css('visibility', 'visible');
				});
			}


        }, function(e) {
                $(this).stopTime();
                $(this).children('img').css('visibility', 'hidden');
                $(this).children('img:first').css('visibility', 'visible');
				$(this).parent('a').attr('title', title);
        });		
});