(function($) {
	$.fn.pfSlider = function(options) {
		// default configuration properties
		var defaults = {			
			prevId: 		'prev_btn',
			nextId: 		'next_btn',	
			speed: 			800
		}; 
		
		var options = $.extend(defaults, options);  
				
		this.each(function() {  
			var obj = $(this);
			var locked = false;

			$('img.loading', obj).each(function() {
				var img = $(this);
				$('<img />').css('display', 'none').load(function () {
					var new_img = $(this);
					img.parents('li').removeClass('loading');
					img.replaceWith(new_img);
					new_img.fadeIn('fast');
				}).attr('src', img.attr('src'));
			});
			var s = $("li", obj).length;
			var w = obj.width();
			var ts = s - 1;
			var t = 0;
			var total_pages = parseInt(ts / 7);
			$("ul", obj).css('width', s * w);			
			
			$("a", "#" + options.nextId).click(function(){		
				animate("next");
				return false;
			});
			$("a", "#" + options.prevId).click(function(){		
				animate("prev");
				return false;
			});	
			obj.hover(
				function() {
					$('#media_controls').stop().animate({opacity:1}, 300);
				},
				function() {
					$('#media_controls').stop().animate({opacity:0}, 300);
				}
			);
			var thumb_page = 0;
			var thumb_imgs = $('#thumb_scroller img').each(function(i) {
				var img = $(this);
				var id = parseInt(this.id.substr(7));
				$('<img />').css('display', 'none').attr('id', 'thumb_' + id).load(function () {
					var new_img = $(this);
					img.parents('li').removeClass('loading');
					img.replaceWith(new_img);
					new_img.fadeIn('fast').click(function() {
						animate(id);				
					});
				}).attr('src', img.attr('src'));
			});
			$('#thumb_scroller ul').width(thumb_imgs.length * 92);
			function stop_movie() {
				$('li:nth-child(' + (t + 1) + ') div.movie', obj).each(function() {
					var div = $(this);
					$('div', div).hide().empty();
					$('img', div).fadeIn('slow');
				});
			}
			
			function start_movie() {
				var m = $('li:nth-child(' + (t + 1) + ') div.movie', obj);
				m.each(function() {
					locked = true;
					var div = $(this);
					$('div', div).html(QT_GenerateOBJECTText_XHTML(
						div.attr('movie'), 642, 373, '',
						'scale', 'aspect',
						'bgcolor', 'ffffff',
						'autoplay', 'true',
						'cache', 'true',
						'controller', 'false',
						'enablejavascript', 'true',
						'kioskmode' , 'true',
						'loop', 'true'
					));
					$('img', div).fadeOut('slow', function() {
						$('div', div).show();
						locked = false;
					});
				});
				return (m.length > 0);
			}
			
			function update_thumbnails(speed) {
				$('#thumb_scroller li.active').removeClass('active');
				$('#thumb_' + t + ', #_thumb_' + t).parent().addClass('active');
				var page = parseInt(t / 7); 
				if (page > 0) {
					$('#slide_prev a').show();
				} else {
					$('#slide_prev a').hide();
				}
				if (page < total_pages) {
					$('#slide_next a').show();
				} else {
					$('#slide_next a').hide();
				}
				if (thumb_page == page) return;
				thumb_page = page;
				$('#thumb_scroller ul').animate(
					{marginLeft: -(92 * 7) * page}, 
					speed / 1.5 
				);
			}
			
			function animate(dir) {
				if (locked) return;
				locked = true;
				var ot = t;
				stop_movie();
				switch (dir) {
					case "next":
						t = (ot >= ts) ? ts : t + 1;						
						break; 
					case "prev":
						t = (t <= 0) ? 0 : t - 1;
						break;
					case "next_page":
						var cur_page = parseInt(t / 7);
						if (cur_page < total_pages) {
							t = (cur_page + 1) * 7;
						}
						break; 
					case "prev_page":
						var cur_page = parseInt(t / 7);
						if (cur_page > 0) {
							t = cur_page * 7 - 1;
						}
						break;
					default:
						t = dir;
						break; 
				};	
				
				var diff = Math.abs(ot - t);
				var speed = (diff - 1) * (options.speed / 4) + options.speed;
				p = (t * w * - 1);
				if (t == ts) {
					$("a", "#" + options.nextId).hide();
				} else {
					$("a", "#" + options.nextId).show();
				};
				if (t == 0) {
					$("a", "#" + options.prevId).hide();
				} else {
					$("a", "#" + options.prevId).show();
				};					
				update_thumbnails(speed);
				$("ul", obj).animate(
					{marginLeft: p}, 
					speed,
					function() {
						if (!start_movie()) {
							locked = false;
						}
					}
				);				
			};

			// init
			$('#slide_prev a').click(function() {
				animate('prev_page');
				return false;
			});
			$('#slide_next a').click(function() {
				animate('next_page');
				return false;
			});
			update_thumbnails();
			start_movie();
			$("a", "#" + options.prevId).hide();
			if (s <= 1)  {
				$("a", "#" + options.nextId).hide();
			}
		});
	};
})(jQuery);



