// bbnslideshow- jQuery plugin from adaption of JonDesign's SmoothSlideshow and Amit Kumar Singh AKSildeShow
// http://www.babilonnet.pl
// Copyright (c) 2009 Adam Michalak
// http://www.gnu.org/licenses/gpl.html

jQuery.fn.bbnslideshow = function(data) 
{
	// GETTING THE ELEMENTS FOR THE GALLERY FROM THE PAGE
	var gall;
	var currentX = 0;
	//var totalslides = settings.src.length;
	//var i = -1;
	
	this.options = {
		startFrom: 0,
		iconFolder:"/public_html/elem",
		slideTimer: 5000,
		startTimer:0,
		fadeTime: 200,
		startFrom:0,
		autoSlide:true
		
	}
	$.extend(this.options, data);
	var opt= this.options;
	if(opt.slideTimer < 2000) opt.slideTimer = 2000;
			
	var items = opt.items;
	var loading = false;
	
	return this.each( function(){
		gall=this;
		var control = '<div class="controllerBG"></div>';
		control += '<div class="controller">';
		control += '<div id="prev"></div>';
		if(startSlide) control += '<div id="play"></div>';
		else control += '<div id="play" style="display:none;"></div>';
		if(startSlide) control += '<div id="stop" style="display:none;"></div>';
		else control += '<div id="stop"></div>';
		control += '<div id="next"></div>';
		control += '</div>';
		$(gall).append(control);
		
		$(gall).hover(function() {$(".controllerBG").stop().fadeTo(300, 0.6); $(".controller").fadeIn(300);}, function() {$(".controller").fadeOut(300); $(".controllerBG").stop().fadeOut(300);});
		
		function preloadImg(i) {
			loading = true;
			
			$(gall).append("<img src='"+opt.iconFolder+"/loading.gif' alt='loading' id='load' style='width:32px; height:32px; position:absolute; left:150px; top:150px;' />");
			var IMG_URL = items[i].src;

			if(items[i].ext == 'swf') {
				changePhoto(i);
			} else {
				var IMGOBJ = new Image();
				IMGOBJ.onload = function() {
					changePhoto(i);
				};
				IMGOBJ.onerror = function() {
					$('#load').remove();
					stopShow();
					alert("can't load image");
				};
				IMGOBJ.src = IMG_URL;
			}
		}
		
		function changePhoto(i) {
			loading = false;
			//if(startSlide) startShow();
			
			$('#load').remove();
			
			$("#fadeIt").fadeTo(opt.fadeTime, 0, function() {
				var img = $(gall).find(".foto");
				var flash = $("#flashme");
				if(items[i].ext == 'swf') {
					img.css({'display':'none'});
					flash.css({'display':'block'});
					swfobject.embedSWF(items[i].src, "flashme", "335", "335", "9.0.0","/public_html/js/expressInstall.swf", null, {wmode:'transparent'}, null);
				} else {
					flash.remove();
					$("#fadeIt").prepend('<div id="flashme" class="flash" style="display:none;"></div>');
					
					img.css({'display':'block'});
					img.attr("src", items[i].src);
					img.attr('alt', items[i].desc);
				}
				
				if(items[i].desc != null && items[i].desc != "" ) {
					$(gall).find(".img_desc").css({'display':'block'});
				} else {
					$(gall).find(".img_desc").css({'display':'none'});
				}
				$(gall).find(".img_desc").html(items[i].desc);
				try {
					$('#photo_section').jScrollPane({ verticalDragMinHeight: 30, verticalDragMaxHeight: 30, horizontalDragMinWidth: 30, horizontalDragMaxWidth: 30 });
				} catch(e) {}
				
				//setTimeout(function() {
					try {
						$('.imggo img').css({'border':'2px solid #fff'});
						$('#img_'+x +' img').css({'border':'2px solid #000'});
					} catch(e) {}
					$("#fadeIt").fadeTo(opt.fadeTime, 1)
				//}, 300);
			});
			
		}
		$('#play').click(function() {
			startSlide = ! startSlide;
			if(startSlide) {
				startShow();
				$('#play').hide();
				$("#stop").show();
			} else {
				stopShow();
				$('#play').show();
				$("#stop").hide();
			}
		})
		
		$('#stop').click(function() {
			stopShow();
			$('#play').show();
			$("#stop").hide();
		});
		
		$('#next').click(function() {
			stopShow();
			$('#play').show();
			$("#stop").hide();
			x++;
			if(x == items.length) x= 0;
			preloadImg(x);
		});
		$('#prev').click(function() {
			stopShow();
			$('#play').show();
			$("#stop").hide();
			x--;
			if(x < 0) x = items.length-1;
			preloadImg(x);
		});
		
		$(".imggo").click(function() {
			var imgid = $(this).attr('id').split("_");
			if(imgid[1] != null) {
				stopShow();
				$('#play').show();
				$("#stop").hide();
				x = imgid[1];
				preloadImg(x);
			}
			return false;
		});
		
		
		var slideShowTimer, x = opt.startFrom,startSlide = opt.autoSlide, startShow = function() {
			//$(slideShow).find("img").attr("src", opt.iconFolder+"/stop.gif")
			try {
				$('.imggo img').css({'border':'2px solid #fff'});
				$('#img_'+x +' img').css({'border':'2px solid #000'});
			} catch(e) {}
			if(!loading) {
				slideShowTimer = setTimeout(function(){
					x ++;
					if(x == items.length) x = 0;
					preloadImg(x);
					startShow();
				}, opt.slideTimer)
			} else {
				setTimeout(startShow, 1000);
			}
			
		};
		function stopShow() {
			clearTimeout(slideShowTimer);
			//$(slideShow).find("img").attr("src", opt.iconFolder+"/play.gif");
			startSlide = false;
		}	
		
		if(startSlide) {
			setTimeout(startShow, opt.startTimer);
		} else {
			setTimeout(function() {
				preloadImg(opt.startFrom)
			}, opt.startTimer);
		}
		
		
	})
}


