// JavaScript Document

//
//	Main Picture Fadein 
//
(function($)
{
	$.fn.fadeTransition = function(options)
	{
		var options = $.extend({pauseTime: 5000, transitionTime: 1000}, options);

		Trans = function(obj)
		{
			var timer = null;
			var current = 0;

			var buttons = $('div.fadebuttons').children('div');
			
			var els = $('> *', obj).css({
				display: 'none',
				left: 0,
				top: 0,
				position: 'absolute'
			});

			$(obj).css({position: 'relative'});
			$(els[current]).css({display: 'block'});

			function fade(index1, index2)
			{
				buttons.eq(index1).children('div').removeClass('active').addClass('inactive');
				buttons.eq(index2).children('div').addClass('active').removeClass('inactive');

				$(els[index1]).fadeOut(options.transitionTime);
				$(els[index2]).fadeIn(options.transitionTime);
			};

			function transition()
			{
				var next = (current + 1) % els.length | 0;
				fade(current, next);
				current = next;
				cue();
			};

			function cue()
			{
				if ( els.length < 2 ) {
					return false;
				}

				if ( timer ) {
					clearTimeout(timer);
				}

				timer = setTimeout(transition, options.pauseTime);
			};

			buttons.click(function()
			{
				var next = $(this).prevAll('div').length;
				if ( next === current ) {
					return false;
				}

				if ( timer ) {
					clearTimeout(timer);
				}
				
				fade(current, next);
				current = next;
				cue();
				
				return false;
			});
			
			cue();
		}

		return this.each(function()
		{
			var t = new Trans(this);
		});
	}

})(jQuery);

//
//	Popup Trick
//
$.fn.imgWithPopup = function()
{
	return this.each(function()
	{
		var popup       = $('div.popuptext', this).eq(0);
		var popupshadow = $('div.popupShadow', this).eq(0);
		
		if ( popup.length === 0 ) {
            return;
        }

		$('img', this)
			.mouseover(function()
			{
				$('div.popuptext').css({display: 'none'});
				$('div.popupShadow').css({display: 'none'});

				popup.css({top:  $(this).offset().top - popup.outerHeight()+10, opacity: 0.96}).fadeIn(300);
				popup.css({left: $(this).offset().left + 10});

				popupshadow.css({top: $(this).offset().top - popup.outerHeight() + 13});
				popupshadow.css({left: $(this).offset().left + 15});
				popupshadow.css({width: popup.width()});
				popupshadow.css({height: popup.height()+2});
				popupshadow.css({display: 'block'});
			})
			.mouseout(function()
			{
				popup.css({display: 'none'});
				popupshadow.css({display: 'none'});
			});
	});
};

$(document).ready(function()
{
	$('.mainpict').fadeTransition();
	$('.img_with_popup').imgWithPopup();
});

