(function($) {
    $.fn.stripHtml = function() {
        var regexp = /<("[^"]*"|'[^']*'|[^'">])*>/gi;
        this.each(function() {
            $(this).html(
                $(this).html().replace(/<("[^"]*"|'[^']*'|[^'">])*>/g,"")
            );
        });
        return $(this);
    }
})(jQuery);


$(document).ready(function(){
	
	$('a.preview').each(function(){
		
		$(this).bind('mouseenter', function (event) {
	        
			// don't use mouse pointer
			$(this).css('cursor', 'default');
			
			// get id
			var id = $(this).attr("id").replace('page-', '');
			var titleId = 'person-title-'+id;
			var synopsisId = 'person-synopsis-'+id;
			var html = '';
			
			if ($('#'+titleId).length < 1) {
				// buid html
				html += '<div id="'+titleId+'" class="person-preview-title">'+$(this).find('span.name').text()+'</div>';	
			}
			
			if ($('#'+synopsisId).length < 1) {
				var synopssis = $(this).find('span.synopsis').html().replace(/<("[^"]*"|'[^']*'|[^'">])*>/g,"");
				// buid html
				html += '<div id="'+synopsisId+'" class="person-preview-synopsis">'+synopssis+'</div>';
			}
			
			if (html != '') {
				$("body").append(html);
			}
			
			var offset = $(this).offset();
			
			//  ($('#'+personId).width()/2)
			
			// stop any existing animations
			$('#'+titleId).stop(true);
			$('#'+synopsisId).stop(true);
			$(this).find('span.img img').stop(true);
			
			$('#'+titleId)
				// .hide()
				.css("top", (offset.top - ($('#'+titleId).height()+20)) + 'px')
				.css("right", "auto")
				// .css("left",((offset.left/2) - ($('#'+personId).width()/2)) + 'px')
				// .css("left",(offset.left - 90) + 'px')
				.css('left', (offset.left - 11)+'px')
				.animate({
					opacity: 1,
					top: '+=5'
					}, 300, function() {
			});
			
			$('#'+synopsisId)
				// .hide()
				// .css("top", (offset.bottom + ($('#'+synopsisId).height()+0)) + 'px')
				.css("top", (offset.top) + 90 + 'px')
				.css("right", "auto")
				// .css("left",((offset.left/2) - ($('#'+personId).width()/2)) + 'px')
				// .css("left",(offset.left - 90) + 'px')
				.css('left', (offset.left - 11)+'px')
				.animate({
					opacity: 1,
					top: '-=5'
					}, 300, function() {
			});
				
			$(this).find('span.img img').animate({
				opacity: 0,
				}, 300, function() {
			});
			
	    })
		.bind('mouseleave', function (event) {
		
			// get id
			var id = $(this).attr("id").replace('page-', '');
			var titleId = 'person-title-'+id;
			var synopsisId = 'person-synopsis-'+id;
			
			// stop any existing animations
			$('#'+titleId).stop(true);
			$('#'+synopsisId).stop(true);
			$(this).find('span.img img').stop(true);
			
			if (!jQuery.browser.msie) {
				$('#'+titleId).animate({
					opacity: 0.5,
					top: '-=5'
					}, 200, function() {
						$(this).remove();
				});
			} else {
				$('#'+titleId).remove();
			}
			
			if (!jQuery.browser.msie) {
				$('#'+synopsisId).animate({
					opacity: 0.5,
					top: '+=5'
					}, 200, function() {
						$(this).remove();
				});
			} else {
				$('#'+synopsisId).remove();
			}
			$(this).find('span.img img').animate({
				opacity: 1,
				}, 200, function() {
			});
	    })
	
		$(this).click(function(){
			return false;
		});

	});
	
});
