﻿/*
	(c)2010, Michael A. Vesely, ART-ist - CreativeCommunication
*/
var slots = ['#Img1', '#Img2', '#Img3', '#Img4'/*, '#Img5'*/];
var images = [];
var slot;
var image;

function ShowImage(ImageToShow, SlotToUse) {
	var img = new Image();
	// wrap our new image in jQuery, then:
	$(img)
		.load(function () { // once the image has loaded, execute this code    
			// set the image hidden by default          
			$(this)
				.hide();
			// then insert the image     
			$(SlotToUse)
				.append(this);
			// fade our image in to create a nice effect
			$(this)
				.fadeIn(5000)
				.delay(3000)
				.fadeOut(5000, function () {
					$(SlotToUse)
						.empty();
				})
				;
		})
		.error(function () { // if there was an error loading the image, react accordingly  
			//alert('Error loading ' + image);
		})
		.attr('src', ImageToShow) // after the events are attached, set the src attribute of the new image to our image  
		;
}  //ShowImage

function GetImage() {
	if (imageCount < 3) 
		return;
	while (true) {
		var newSlot = slots[Math.floor(Math.random() * (slots.length + 1))];
		if (newSlot != slot) {
			slot = newSlot;
			break;
		}
	}
	while (true) {
		var newImage = images[Math.floor(Math.random() * (imageCount + 1))];
		if (newImage != image) {
			image = newImage;
			break;
		}
	}
	ShowImage(image, slot);
} //GetImage

$(document).ready(function () {

	//Load Navigation
	$('#nav')
		.hide()
		.load('nav.html #nav_list', function () {
			//Select current Link
			var loc = location.pathname;
			var nav = loc.substring(loc.lastIndexOf('/') + 1);
			if (nav) {
				$("#nav a[href$='" + nav + "']").css('color', '#ffffff'); //.addClass('selected');
			}

			$('#nav')
				.fadeIn('slow');
		});

	//Email-Links aktivieren
	$('.email').each(function () {
		$(this)
			.text($(this).text().replace('[at]', '@').replace('[dot]', '.').replace('mailto: ', 'mailto:'))
			.attr('href', $(this).attr('href').replace('[at]', '@').replace('[dot]', '.').replace('mailto: ', 'mailto:')
			);
	});

	//ToolTip
	$('.toolTip')
		.css('cursor', 'default')
		.hover(
			function () {
				this.tip = this.title;
				$(this).append(
					'<div class="toolTipWrapper">'
						+ this.tip
					+ '</div>'
				);
				this.title = "";
				this.width = $(this).width();
				$(this).find('.toolTipWrapper').css({ left: this.width - 175 })
				$('.toolTipWrapper').fadeIn(300);
			},
			function () {
				$('.toolTipWrapper').fadeOut(100);
				$(this).children().remove();
				this.title = this.tip;
			}
		);

	$('.openNew')
		.click(function () {
			window.open(this.href); return false;
		});

	//Image Animation
	for (var i = 0; i < imageCount; ++i) {
		images.push("images/" + folder.toString() + "/" + i.toString() + ".jpg");
	}
	GetImage();
	$(this).everyTime(8000, function () {
		GetImage();
	});

});
