// JScript source code

//debugger;

jQuery.initPanel = function(options) {
	this.links = $(options.links);
	this.links.each(function(i, e) {
		var wrap	= $(e);
		var hover	= wrap.find(options.hover);
		hover.hide();
		wrap.mouseenter(function(ev) {
			hover.fadeTo(200, 0.9);
		});
		wrap.mouseleave(function(ev) {
			hover.fadeTo(200, 0.0);
		});
		/*
		var anchor	= wrap.find('a');
		var ci		= anchor.css('counter-increment');
		if(ci)
			var found	= ci.match(/vis/i);
		if(found && found.length) {
			var inner = anchor.find('img');
			if(inner) {
				inner.css('opacity', 0.5);
			}
		}*/
		
	});
};

jQuery.switchset = function(options) {
	
	if(options.handles && options.handles.length && options.panels && options.panels.length) {
		this.options		= options;
		this.handles		= new Array();
		this.panels			= new Array();
		for(var i=0; i< Math.min(options.handles.length, options.panels.length); i++) {
			this.handles[i] = {handle:$(options.handles[i]), panel:$(options.panels[i])};
			this.handles[i].handle.data('index', i+1);
			if(this.handles[i].handle.hasClass(options.activeclass)) {
				this.handles[i].panel.show();
			} else {
				this.handles[i].panel.hide();
			}
			
			
			this.handles[i].handle.bind('click', {self:this}, function(ev) {
				var self	= ev.data.self;
				var index	= $(ev.currentTarget).data('index');
				
				for(i = 0; i< self.handles.length; i++){
					if(self.handles[i].handle.data('index') == index) {
						self.handles[i].panel.show();
						if(self.options.inactiveclass)
							self.handles[i].handle.removeClass(self.options.inactiveclass);
						if(self.options.activeclass)
							self.handles[i].handle.addClass(self.options.activeclass);
					}
					else {
						self.handles[i].panel.hide();
						if(self.options.inactiveclass)
							self.handles[i].handle.addClass(self.options.inactiveclass);
						if(self.options.activeclass)
							self.handles[i].handle.removeClass(self.options.activeclass);
					}
					
				}
			});
		}
	} 
	
};


var runningGallery = null;

jQuery.gallery = function(options) {
	
	this.runSlide = function(){
	};
	this.onForward = function(ev) {
		var _g = ev.data.gallery;
		if(_g.g_current < _g.g_imagecount - 1) {
			jQuery(_g.g_imagewraps[_g.g_current]).fadeOut();
			jQuery(_g.g_imagewraps[_g.g_current + 1]).fadeIn();
			_g.g_current += 1;
		}
		_g.checkCurrent();
		return true;
	};
	
	this.onBackward = function(ev) {
		var _g = ev.data.gallery;
		if(_g.g_current > 0) {
			jQuery(_g.g_imagewraps[_g.g_current]).fadeOut();
			jQuery(_g.g_imagewraps[_g.g_current - 1]).fadeIn();
			_g.g_current -= 1;
		}
		_g.checkCurrent();
		return true;
	};
	
	this.checkCurrent = function() {
		if(this.g_current <= 0) {
			this.g_backward.hide();
		} else {
			this.g_backward.show();
		}
		if(this.g_current >= this.g_imagecount -1) {
			this.g_forward.hide();
		} else {
			this.g_forward.show();
		}
	}
	this.g_container		= $(options.container);
	this.g_imagebox			= $(options.imagebox);
	this.g_imagewraps		= $(options.imagewrapper);
	this.g_imagecount		= this.g_imagewraps.length;
	this.g_backward			= $(options.backward);
	this.g_forward			= $(options.forward);
	this.g_width			= this.g_container.width();
	this.g_height			= this.g_container.height();
	var w_forward			= this.g_forward.width();
	var h_forward			= this.g_forward.height();
	var t_forward			= (this.g_height - h_forward) / 2;
	var l_forward			= this.g_width - w_forward - options.mright;
	
	this.g_forward.css('left', l_forward + "px");
	this.g_forward.css('top', t_forward + "px");
	
	var w_backward				= this.g_backward.width();
	var h_backward				= this.g_backward.height();
	var t_backward				= (this.g_height - h_backward) / 2;
	var l_backward				= options.mleft;
	this.g_backward.css('left', l_backward + "px");
	this.g_backward.css('top', t_backward + "px");
	
	this.g_forward.css('opacity', options.initialopacity);
	this.g_backward.css('opacity', options.initialopacity);
	
	this.g_forward.mouseenter( function(ev) {
			jQuery(ev.target).css('opacity', options.hoveropacity);
			runningGallery.suspended = true;
		});
	this.g_forward.mouseleave( function(ev) {
			jQuery(ev.target).css('opacity', options.initialopacity);
		});
	this.g_backward.mouseenter( function(ev) {
			jQuery(ev.target).css('opacity', options.hoveropacity);
			runningGallery.suspended = true;
		});
	this.g_backward.mouseleave( function(ev) {
			jQuery(ev.target).css('opacity', options.initialopacity);
		});
	this.g_forward.bind('click', {'gallery':this}, this.onForward );
	this.g_backward.bind('click', {'gallery':this}, this.onBackward );
	for(var i = 1 ; i < this.g_imagecount; i++) {
		$(this.g_imagewraps[i]).hide();
	} 
	
	
	this.g_current = 0;
	
	this.checkCurrent();
	
	runningGallery = this;
	if(options.autoplay) {
		window.setInterval("doSlide()", options.autoplay);
		this.g_imagewraps.bind('mouseenter', function() {runningGallery.suspended = true});
		this.g_imagewraps.bind('mouseleave', function() {runningGallery.suspended = false});
	}
	return this;
};

function doSlide() {
	if(runningGallery.suspended)
		return;
	var count = runningGallery.g_imagewraps.length;
	
	if(count > 1) {
		var current = runningGallery.g_current;
		if(current >= count - 1)
			current = 0;
		else
			current += 1;
			
		jQuery(runningGallery.g_imagewraps[runningGallery.g_current]).fadeOut();
		runningGallery.g_current = current;
		jQuery(runningGallery.g_imagewraps[runningGallery.g_current]).fadeIn();
		
		runningGallery.checkCurrent();
	}
	
}

