// JQUERY TIME FUNCTIONS //

jQuery.fn.extend({
	everyTime: function(interval, label, fn, times, belay) {
		return this.each(function() {
			jQuery.timer.add(this, interval, label, fn, times, belay);
		});
	},
	oneTime: function(interval, label, fn) {
		return this.each(function() {
			jQuery.timer.add(this, interval, label, fn, 1);
		});
	},
	stopTime: function(label, fn) {
		return this.each(function() {
			jQuery.timer.remove(this, label, fn);
		});
	}
});

jQuery.extend({
	timer: {
		guid: 1,
		global: {},
		regex: /^([0-9]+)\s*(.*s)?$/,
		powers: {
			// Yeah this is major overkill...
			'ms': 1,
			'cs': 10,
			'ds': 100,
			's': 1000,
			'das': 10000,
			'hs': 100000,
			'ks': 1000000
		},
		timeParse: function(value) {
			if (value == undefined || value == null)
				return null;
			var result = this.regex.exec(jQuery.trim(value.toString()));
			if (result[2]) {
				var num = parseInt(result[1], 10);
				var mult = this.powers[result[2]] || 1;
				return num * mult;
			} else {
				return value;
			}
		},
		add: function(element, interval, label, fn, times, belay) {
			var counter = 0;
			
			if (jQuery.isFunction(label)) {
				if (!times) 
					times = fn;
				fn = label;
				label = interval;
			}
			
			interval = jQuery.timer.timeParse(interval);

			if (typeof interval != 'number' || isNaN(interval) || interval <= 0)
				return;

			if (times && times.constructor != Number) {
				belay = !!times;
				times = 0;
			}
			
			times = times || 0;
			belay = belay || false;
			
			if (!element.$timers) 
				element.$timers = {};
			
			if (!element.$timers[label])
				element.$timers[label] = {};
			
			fn.$timerID = fn.$timerID || this.guid++;
			
			var handler = function() {
				if (belay && this.inProgress) 
					return;
				this.inProgress = true;
				if ((++counter > times && times !== 0) || fn.call(element, counter) === false)
					jQuery.timer.remove(element, label, fn);
				this.inProgress = false;
			};
			
			handler.$timerID = fn.$timerID;
			
			if (!element.$timers[label][fn.$timerID]) 
				element.$timers[label][fn.$timerID] = window.setInterval(handler,interval);
			
			if ( !this.global[label] )
				this.global[label] = [];
			this.global[label].push( element );
			
		},
		remove: function(element, label, fn) {
			var timers = element.$timers, ret;
			
			if ( timers ) {
				
				if (!label) {
					for ( label in timers )
						this.remove(element, label, fn);
				} else if ( timers[label] ) {
					if ( fn ) {
						if ( fn.$timerID ) {
							window.clearInterval(timers[label][fn.$timerID]);
							delete timers[label][fn.$timerID];
						}
					} else {
						for ( var fn in timers[label] ) {
							window.clearInterval(timers[label][fn]);
							delete timers[label][fn];
						}
					}
					
					for ( ret in timers[label] ) break;
					if ( !ret ) {
						ret = null;
						delete timers[label];
					}
				}
				
				for ( ret in timers ) break;
				if ( !ret ) 
					element.$timers = null;
			}
		}
	}
});

if (jQuery.browser.msie)
	jQuery(window).one("unload", function() {
		var global = jQuery.timer.global;
		for ( var label in global ) {
			var els = global[label], i = els.length;
			while ( --i )
				jQuery.timer.remove(els[i], label);
		}
	});

// END JQUERY TIME FUNCTIONS //



$(document).ready(function()
{
	<!-- "Hide Email" replacement-script -->
	
		var at = / at /;
		var dot = / dot /g;
		$('span.mailme').each(function () {
			var addr = $(this).text().replace(at,"@").replace(dot,".");
			$(this).after('<a href="mailto:'+ addr +'" title="Send e-mail">Send e-mail</a>');
			$(this).remove();
		});

	<!-- END "Hide Email" replacement-script -->
	
	<!-- Footer click to open -->
	$("#Foottop").click(function(){
		switch($("#Foottop").width()){
			case 550:
			$("#Foottop").animate({width:"934px"}, 200, function(){
				$("#Footbottom").slideToggle( 200, function(){ $.scrollTo($(document).height(), 200); });
			});
			break;
			case 934:
			$("#Footbottom").slideToggle( 200, function(){
				$.scrollTo($(document).height(), 200);
				$("#Foottop").animate({width:"550px"}, 200);
			});
			break;
		}
	});
	<!-- END Footer click to open -->
	
	<!-- Fade & slide toggle -->
	jQuery.fn.fadeSliderToggle = function(settings) {
 	 	settings = jQuery.extend({
			speed:500,
			easing : "swing"
		}, settings)
		
		caller = this
 		if($(caller).css("display") == "none"){
 			$(caller).animate({
 				opacity: 1,
 				height: 'toggle'
 			}, settings.speed, settings.easing);
		}else{
			$(caller).animate({
 				opacity: 0,
 				height: 'toggle'
 			}, settings.speed, settings.easing);
		}
	};
	<!-- END Fade & slide toggle -->
	
	<!-- Function for "+"-button (for News, Jobs etc. -->
	$("a.btn-slide").click(function () {
		$(this).parent().children(".btn-slide").toggle();
		//$(this).parent().children(".collapsed").fadeSliderToggle();
		$(this).parent().children(".collapsed").slideToggle(500);
	});
	<!-- END Function for "+"-button (for News, Jobs etc. -->
					

				
	<!-- Function for Slide OPEN  -->
	$("a.btn-open").click(function () {
					  
		$(this).parent().children(".collapsed").slideToggle(500);
	});
	<!-- END Function for Slide OPEN  -->
	
	<!-- Function for Slide CLOSED  -->
	$("a.btn-close").click(function () {
		$(".collapsed").slideUp(500);
	});
	<!-- END Function for Slide CLOSED  -->
	


	
	<!-- Picture Carousel on front page -->
	if($("div#galleryfrontpage").length){
		$("div#galleryfrontpage > div.galleryblock:first").fadeIn(1000);
		$("div#gallerythumbs > div#galleryscroller > div.gallerythumb:first").append("<img src=\"/niras/gfx/gallery-border.gif\" />");
		
		$.fadeGalleryImage = function(newnr, prevnr) {
			$("div#galleryfrontpage > div.galleryblock").eq(prevnr).fadeOut(200, function() {
				$("div#galleryfrontpage > div.galleryblock").eq(prevnr).css("display", "none");
				$("div#galleryfrontpage > div.galleryblock").eq(newnr).fadeIn(200);
				$("div#gallerythumbs > div#galleryscroller > div.gallerythumb > img").remove();
				$("div#gallerythumbs > div#galleryscroller > div.gallerythumb").eq(newnr).append("<img src=\"/niras/gfx/gallery-border.gif\" />");
			});
		}
		
		var prevDivNr = 0;
		
		$("div#gallerythumbs > div#galleryscroller > div.gallerythumb").click(function () {
			var i = $(this).prevAll().length;
			$.fadeGalleryImage(i, prevDivNr);
			prevDivNr = i;
			$("div#galleryfrontpage").stopTime('controlled');
			$("div#galleryfrontpage").everyTime(10000, 'controlled', function() {
				$.nextGalleryImage();
			});
		});
		$.nextGalleryImage = function() {
			if(prevDivNr < ($("div#gallerythumbs > div#galleryscroller > div.gallerythumb").size() - 1)){
				var i = prevDivNr + 1;
			} else {
				var i = 0;
			}
			$.fadeGalleryImage(i, prevDivNr);
			prevDivNr = i;
			var thumbLeft = $("div#gallerythumbs > div#galleryscroller > div.gallerythumb").eq(i).position().left;
			var scrollerLeft = $("div#gallerythumbs > div#galleryscroller").position().left;
			var thumbPosition = scrollerLeft + thumbLeft;
			if(thumbPosition >= 336){
				$("div#gallerythumbs").scrollTo( "+=336px", 500 );
				//alert($("div#gallerythumbs > div#galleryscroller").position().left);
			}
			if(thumbLeft == 0){
				$("div#gallerythumbs").scrollTo( 0, 500 );
			}
			//$("div#gallerythumbs").scrollTo( $("div#gallerythumbs > div#galleryscroller > div.gallerythumb").eq(i).position().left, 500 );
		}
		$("div#galleryfrontpage").everyTime(10000, 'controlled', function() {
			$.nextGalleryImage();
		});
		$("div.galleryblock").mouseover(function(){
			$("div#galleryfrontpage").stopTime('controlled');
		});
		$("div.galleryblock").mouseout(function(){
			$("div#galleryfrontpage").everyTime(10000, 'controlled', function() {
				$.nextGalleryImage();
			});
		});
			
		
		if($("div#gallerythumbs > div#galleryscroller > div.gallerythumb").size() > 7){
			$("div#gallerynav").prepend("<div id=\"galleryprev\" class=\"png_bg\"></div>");
			$("div#gallerynav").append("<div id=\"gallerynext\" class=\"png_bg\"></div>");
			$("div#gallerynav > div#gallerynext").click(function () {
				$("div#gallerythumbs").scrollTo( "+=336px", 500 );
			});
			$("div#gallerynav > div#galleryprev").click(function () {
				$("div#gallerythumbs").scrollTo( "-=336px", 500 );
			});
		};
	}
	<!-- End Picture Carousel on front page -->
	
	<!-- Picture Carousel on content pages -->
	if($("div#gallerycontent").length){
		$("div#gallerycontent > div.galleryblock:first").fadeIn(1000, function() {
			$("div#gallerycontent").height($(this).height() + 48);
			$(this).bind('click', $.nextGalleryImage);
		});
		$("div#gallerythumbs > div#galleryscroller > div.gallerythumb:first").append("<img src=\"/niras/gfx/gallery-border.gif\" />");
		
		$.fadeGalleryImage = function(newnr, prevnr) {
			$("div#gallerycontent > div.galleryblock").eq(prevnr).fadeOut(200, function() {
				$("div#gallerycontent > div.galleryblock").eq(prevnr).css("display", "none");
				$("div#gallerycontent > div.galleryblock").eq(newnr).fadeIn(200, function(){
					$.galleryheight = $(this).height() + 48;
					$("div#gallerycontent").animate({height: $.galleryheight}, 200);
					$(this).bind('click', $.nextGalleryImage);
				});
				$("div#gallerythumbs > div#galleryscroller > div.gallerythumb > img").remove();
				$("div#gallerythumbs > div#galleryscroller > div.gallerythumb").eq(newnr).append("<img src=\"/niras/gfx/gallery-border.gif\" />");
			});
		}
		
		var prevDivNr = 0;
		
		$("div#gallerythumbs > div#galleryscroller > div.gallerythumb").click(function () {
			var i = $(this).prevAll().length;
			$.fadeGalleryImage(i, prevDivNr);
			prevDivNr = i;
		});
		$.nextGalleryImage = function() {
			$("div#gallerycontent > div.galleryblock").unbind('click', $.nextGalleryImage);
			if(prevDivNr < ($("div#gallerythumbs > div#galleryscroller > div.gallerythumb").size() - 1)){
				var i = prevDivNr + 1;
			} else {
				var i = 0;
			}
			$.fadeGalleryImage(i, prevDivNr);
			prevDivNr = i;
			var thumbLeft = $("div#gallerythumbs > div#galleryscroller > div.gallerythumb").eq(i).position().left;
			var scrollerLeft = $("div#gallerythumbs > div#galleryscroller").position().left;
			var thumbPosition = scrollerLeft + thumbLeft;
			if(thumbPosition >= 528){
				$("div#gallerythumbs").scrollTo( "+=528px", 500 );
			}
			if(thumbLeft == 0){
				$("div#gallerythumbs").scrollTo( 0, 500 );
			}
		}
		
		/*
		$("div#gallerycontent").everyTime(10000, 'controlled', function() {
			$.nextGalleryImage();
		});
		$("div.galleryblock").mouseover(function(){
			$("div#gallerycontent").stopTime('controlled');
		});
		$("div.galleryblock").mouseout(function(){
			$("div#gallerycontent").everyTime(10000, 'controlled', function() {
				$.nextGalleryImage();
			});
		});
		*/
			
		
		if($("div#gallerythumbs > div#galleryscroller > div.gallerythumb").size() > 11){
			$("div#gallerynav").prepend("<div id=\"galleryprev\" class=\"png_bg\"></div>");
			$("div#gallerynav").append("<div id=\"gallerynext\" class=\"png_bg\"></div>");
			$("div#gallerynav > div#gallerynext").click(function () {
				$("div#gallerythumbs").scrollTo( "+=528px", 1000 );
			});
			$("div#gallerynav > div#galleryprev").click(function () {
				$("div#gallerythumbs").scrollTo( "-=528px", 1000 );
			});
		};
	}
	<!-- End Picture Carousel on content pages -->
	
	$("a.arrow").click(function flip(){
		$(this).unbind("click");
		$.flipScroller = $(this).siblings("div.flipscroller");
		$.blockWidth = $.flipScroller.children("div.flipblock:first").width();
		$.flipScroller.children("div.flipblock:first").animate( { marginLeft:-$.blockWidth}, 200, function(){
			$.blockHeight = $.flipScroller.children("div.flipblock").eq(1);
			$(this).appendTo($.flipScroller);
			$(this).css({marginLeft:0});
			$("a.arrow").bind("click", flip);
		});
	});	
});
	
	
		
