var timer;
(function ($) {
	
	$.fn.fadeTransition = function(options) {
	  var options = $.extend({pauseTime: 5000, transitionTime: 2000, ignore: null, delayStart: 0, pauseNavigation: false}, options);
	  var transitionObject;
	  var objTmp = null;

	  Trans = function(obj) {
		timer = null;
		var current = 0;
		var els = (options.ignore)?$("> *:not(" + options.ignore + ")", obj):$("> *", obj);
		$(obj).css("position", "relative");
		els.css("display", "none").css("left", "0").css("top", "0").css("position", "absolute");
		
		if (options.delayStart > 0) {
		  setTimeout(showFirst, options.delayStart);
		}
		else
		  showFirst();

		function showFirst() {
		  if (options.ignore) {
			$(options.ignore, obj).fadeOut(options.transitionTime);
			$(els[current]).fadeIn(options.transitionTime, function(){
				objHead = $(this).children(".txtHead");
				eval("setTimeout( function(){ toggleAnimate_head(objHead, true); }, 500 )");
				objSubHead = $(this).children(".txtSubHead");
				eval("setTimeout( function(){ toggleAnimate_subHead(objSubHead, true); }, 500 )");
			});
		  }
		  else {
			$(els[current]).css("display", "block");
		  }
		}

		function transition(next) {
		  $(els[current]).fadeOut(options.transitionTime, function(){
				//objHead = $(this).children(".txtHead");
				//eval("setTimeout( function(){ toggleAnimate_head(objHead, false); }, 2000 )");				
				//objSubHead = $(this).children(".txtSubHead");
				//eval("setTimeout( function(){ toggleAnimate_subHead(objSubHead, true); }, 2000 )");
		  });
		  $(els[next]).fadeIn(options.transitionTime, function(){
				objHead = $(this).children(".txtHead");
				eval("setTimeout( function(){ toggleAnimate_head(objHead, true); }, 500 )");
				objSubHead = $(this).children(".txtSubHead");
				eval("setTimeout( function(){ toggleAnimate_subHead(objSubHead, true); }, 500 )");
		  });
		  current = next;
		  cue();
		};
		
		function toggleAnimate_head(objChildHead, toShow) {
			if(toShow) {
				objChildHead.css({"display":"block", opacity:0});
				objChildHead.animate( {right: '+=50', opacity: 1}, 1500, function(){
					eval("setTimeout( function(){ toggleAnimate_head(objChildHead, false); }, 4000 )");
				} );
			} else {
				objChildHead.animate( {right: '-=50', opacity: 0}, 1500, function(){
					objChildHead.css({"display":"none"});
				} );
			}
		}
		
		function toggleAnimate_subHead(objChildHead, toShow) {
			if(toShow) {
				objChildHead.css({"display":"block", opacity:0});
				objChildHead.animate( {right: '-=50', opacity: 1}, 1500, function(){
					eval("setTimeout( function(){ toggleAnimate_subHead(objChildHead, false); }, 4000 )");
				} );
			} else {
				objChildHead.animate( {right: '+=50', opacity: 0}, 1500, function(){
					objChildHead.css({"display":"none"});
				} );
			}
		}

		function cue() {
		  if ($("> *", obj).length < 2) return false;
		  if (timer) clearTimeout(timer);
		  if (!options.pauseNavigation) {
			timer = setTimeout(function() { transition((current + 1) % els.length | 0)} , options.pauseTime);
		  }
		};
		
		this.showItem = function(item) {
		  if (timer) clearTimeout(timer);
		  transition(item);
		};

		cue();
	  }

	  this.showItem = function(item) {
		transitionObject.showItem(item);
	  };

	  return this.each(function() {
		transitionObject = new Trans(this);
	  });
	}

  })(jQuery);

  var page = {
	tr: null,
	init: function() {
	  page.tr = $(".area").fadeTransition({pauseTime: 8500, transitionTime: 2000, ignore: "#introslide", delayStart: 2000});
	  $("div.navigation").each(function() {
		$(this).children().each( function(idx) {
		  if ($(this).is("a"))
			$(this).click(function() { page.tr.showItem(idx); return false; })
		});
	  });
	},

	show: function(idx) {		
	  if (page.tr.timer) clearTimeout(page.tr.timer);
	  page.tr.showItem(idx);
	}
  };
