// JavaScript Document

(function($) { // hide the namespace
					
function FrameBox() {
	
	this.name = '';
	this.curIndex = 0;
	this.imageArray = new Array();
	this.galleryTimer;
};
	
$.extend(FrameBox.prototype, {
					 
	init: function(name,index) {
		
		this.name = name;
		
		if (index == null) index = 0;
		
		this.curIndex = index;
		
		
		this.imageArray.length = 0;
		var jQueryMatchedObj = $("#" + this.name + " a");
		
		for ( var i = 0; i < $("#" + this.name + " a").length; i++ ) {
			this.imageArray.push(new Array(jQueryMatchedObj[i].getAttribute('href'),jQueryMatchedObj[i].getAttribute('title'),jQueryMatchedObj[i].getAttribute('link')));
		}

		
		if ($("#" + this.name + "_left").length > 0) {
			$("#" + this.name + "_left").click(function () {
				
				$.framebox._stopSlideShow();
				
				$.framebox._previus();
				
				return false;
			});
		}

		if ($("#" + this.name + "_right").length > 0) {
			$("#" + this.name + "_right").click(function () {
				
				$.framebox._stopSlideShow();
				
				$.framebox._next();
				
				return false;
			});
		}

		if ($("#" + this.name + "_animation").length > 0) {
			$("#" + this.name + "_animation").click(function () {
				
				if ($(this).html() == "Start listanje") {
					$.framebox._slideShow();
				} else {
					$.framebox._stopSlideShow();
				}
				
				return false;
			});
		}

		$("#" + this.name + " a").click(function () {
			
			$.framebox._stopSlideShow();
			
			$.framebox.curIndex = 0;
			while ( $.framebox.imageArray[$.framebox.curIndex][0] != this.getAttribute('href') ) {
				$.framebox.curIndex++;
			}
			
			$.framebox._select(this);
			
			return false;
		});
		
		$("#" + this.name + " a:eq(" + this.curIndex + ")").click();
		
	},
	
	_select: function(obj) {
		
		$("#" + this.name + " span#on").removeAttr("id");
		$(obj).parents("span").attr("id", "on");
		
		if ($("#" + this.name + "_status").length > 0) {
			
			$("#" + this.name + "_status").html((this.curIndex + 1) + " od " + $("#" + this.name + " a").length);
			
		}

		$('#' + this.name + '_view').fadeOut(function() {

			var objImagePreloader = new Image();
			objImagePreloader.onload = function() {
				
				$('#' + $.framebox.name + '_view').attr('src',$.framebox.imageArray[$.framebox.curIndex][0]);
				$('#' + $.framebox.name + '_view').attr('title',$.framebox.imageArray[$.framebox.curIndex][1]);
				
				if ($('#' + $.framebox.name + '_view').parents("a").length > 0 && $.framebox.imageArray[$.framebox.curIndex][2] != null) {
					$('#' + $.framebox.name + '_view').parents("a").attr('href',$.framebox.imageArray[$.framebox.curIndex][2]);
				}
				
				$('#' + $.framebox.name + '_view').fadeIn(function() {
					$.framebox._preload_neighbor_images();
				});
				//	clear onLoad, IE behaves irratically with animated gifs otherwise
				objImagePreloader.onload=function(){};
			};
			
			objImagePreloader.src = $.framebox.imageArray[$.framebox.curIndex][0];
			
		});
	},
	
	_next: function() {
		this.curIndex = this.curIndex + 1;
		
		if (this.curIndex >= $("#" + this.name + " a").length) this.curIndex = 0;
		
		$.framebox._select($("#" + this.name + " a:eq(" + this.curIndex + ")"));
	},

	_previus: function() {
		this.curIndex = this.curIndex - 1;
		
		if (this.curIndex < 0) this.curIndex = $("#" + this.name + " a").length-1;
		
		$.framebox._select($("#" + this.name + " a:eq(" + this.curIndex + ")"));
	},

	_preload_neighbor_images: function() {
		if ( (this.imageArray.length -1) > this.curIndex ) {
			objNext = new Image();
			objNext.src = this.imageArray[this.curIndex + 1][0];
		}
		if ( this.curIndex > 0 ) {
			objPrev = new Image();
			objPrev.src = this.imageArray[this.curIndex -1][0];
		}
	},
	
	_stopSlideShow: function() {
		clearInterval(this.galleryTimer);
		
		$("#" + this.name + "_animation").html("Start listanje");
	},
	_slideShow: function() {
		this.galleryTimer = setInterval("$.framebox._next()",3 * 1000);
		
		$("#" + this.name + "_animation").html("Stop listanje");
	}

});

$.framebox = new FrameBox(); // singleton instance

})(jQuery);
