(function($){  
 $.fn.photoreel = function(options) {  
	var config = {  
			speed: 9500 // default: 2000 ------ 10000 - slidetime 500
		,	autoplay: true
	};  
	var options = $.extend(config, options);  
	var inmedia = false;
	return this.each(function() {  
		var obj = $(this);
		obj.find("div#items div.item").each(function(index) {
			if(index > 0) {
				$(this).hide();
			} else {
				obj.find("div#bttns div").eq(index).addClass("active");
				n = $(this);
				if(n.find(".media").length > 0) {
					initMedia(n, obj);
				}
			}
		});
		
		
		obj.data("over", false);
		obj.data("active", 0);
		obj.timer = setTimeout(function(){ nextImage(obj);}, config.speed);
		obj.mouseover(function() {
			clearTimeout(obj.timer);
			obj.timer = false;
			obj.data("over", true);
			//$(this).css("backgroundColor", "#CCC");
		});
		obj.mouseout(function() {
			//$(this).css("backgroundColor", "");
			if(obj.timer) {
			} else {
				obj.data("over", false);
				obj.timer = setTimeout(function(){ nextImage(obj);}, config.speed);
			}
		});
		obj.find("div#bttns div").each(function(i) {
			$(this).click(function() {
				if($(this).hasClass("active")) {
					document.location.href = $(this).find("a").eq(0).attr("href");
				} else {
					if(inmedia) {
						obj.find(".item:visible a").html("");
						inmedia = false;
					}
					obj.timer = false;
					nextImage(obj, i);
				}
				return false;
			});
			
		});
	});
	function nextImage(obj, newa) {
		clearTimeout(obj.timer);
		if(inmedia) {
			return false;
		}
		
		a = obj.data("active");
		o = obj.find("div#items div.item").eq(a);
		o.stop();
		o.css("zIndex", 10);
		
		ob = obj.find("div#bttns div").eq(a);
		ob.removeClass("active")
		
		if(newa !== undefined) {
			if(a == newa) {
				return false;
			}
			a = newa;
		} else {
			a++;
		}
		if(a >= obj.find("div#items div.item").length) {
			a = 0;
		}
		
		obj.data("active", a);
		obj.find("div#bttns div").eq(a).addClass("active");
		n = obj.find("div#items div.item").eq(a);
		///obj.find("div#items h2").html(obj.find("div#bttns div").eq(a).find("h2").html());
		n.css("zIndex", 11);
		n.css("left", "750px");
		n.show();
		
		n.animate({left:0}, 500, function() { //default: 500sec
			o.hide();
			if(n.find(".media").length > 0) {
				initMedia(n, obj);
			}
			if(obj.data("over") == false && config.autoplay) {
				if(n.find(".media").length > 0) {
					
				} else {
					obj.timer = setTimeout(function(){ nextImage(obj);}, config.speed);
				}
			}
		});	
	}
	
	function initMedia(w, obj) {
		inmedia = true;
		m = w.find(".media");
		flv = $(m).attr("href");
		idname = $(m).attr("id");
		flowplayer(idname, {
			src: "/flash/flowplayer.commercial-3.1.5.swf",
			wmode: "transparent",
			canvas:  {
				// configure background properties
				background: '#FFFFFF',
			
				// remove default canvas gradient
				backgroundGradient: 'none',
			
				// setup a light-blue border
				border:'2px solid #778899'
			},
			version: [9, 115],
			onFail: function()  {}
		}, {
			key: '#@2cbe35e3d49a43f95fd',
			autoHide: {
			    enabled: true,
			    hideDelay: 2000
			},
			clip:{
				url:flv,
				scaling:'fit'
			},
			onFinish: function () {
				m.html("");
				inmedia = false;
				nextImage(obj);
			}
		});
	}
};  
})(jQuery);  
