$(document).ready(function(){
    $('.switch-video').click(function(){
        var old_code = $('#videoPlayer').attr('vp-codice');
        var old_icon = $('#videoPlayer').attr('vp-icona');
        var old_title = $('#videoPlayerTitle').html();
        var old_text = $('#videoPlayerText').html();
        
        
        
        var new_code = $(this).attr('rel');
        var new_icon = $(this).closest('div').find('.video-icon').attr('rel');
        var new_title = $(this).closest('div').find('.video-title').first().html();
        var new_text = $(this).closest('div').find('.video-text').first().html();

        var vp_string= "<object width=\"393\" height=\"320\">\n"
            vp_string +="<param name=\"movie\" value=\"http://www.youtube.com/v/"+new_code+"\"> </param>\n"
			vp_string +="<param name=\"wmode\" value=\"transparent\"> </param>\n"
             vp_string +="<embed src=\"http://www.youtube.com/v/"+new_code+"\" type=\"application/x-shockwave-flash\" width=\"393\" height=\"320\" wmode=\"transparent\"> </embed>\n"
             vp_string +="</object>";
      
        $('#videoPlayer').attr('vp-codice', new_code);
        $('#videoPlayer').attr('vp-icona', new_icon);
        $('#videoPlayerTitle').html(new_title);
        $('#videoPlayerText').html(new_text);
       /* $('#videoPlayer').find('param').attr('value','http://www.youtube.com/v/'+new_code);
        $('#videoPlayer').find('embed').attr('src','http://www.youtube.com/v/'+new_code);*/
        $('#videoPlayer').html(vp_string);
      
        $(this).closest('div').find('a').each(function(){
            $(this).attr('rel',old_code);
            })
        
        $(this).closest('div').find('.video-icon').attr('src','http://www.gruppocbp.it/wp/wp-content/themes/mioTema/foto/'+old_icon);
        $(this).closest('div').find('.video-title').first().html(old_title);
        $(this).closest('div').find('.video-text').first().html(old_text);

//alert($(this).closest('div').find('.video-title').first().html());
//alert($('#videoPlayer').)
targetOffset = ($('#videoPlayer').offset().top)- 50;
$('html,body').animate({scrollTop: targetOffset}, 1000);
        return false;
    })
})
