$(document).ready(
	function() {
		registerEvents();
		var page = execQuerystrings();
		// start slideshow in about page
		if(page = "about") {
			$("#about_slideshow img").each(function () { $(this).animate({opacity:0.0},0).hide();});
			$("#about_slideshow img:first").animate({opacity:1.0}, 0).wait().show().animate({opacity:1.0}, 0, "linear", function() { aboutSlideshow(); });
		}
	}
);

function registerEvents() {
	$("img.navigation").hover( function () {
			if(!$(this).hasClass("selected")) {
				var newsrc = $(this).attr("src").replace(".", "_highlighted.");
				$(this).attr("src", newsrc );
			}
		}, function () {
			if(!$(this).hasClass("selected")) {
				var newsrc = $(this).attr("src").replace("_highlighted.", ".");
				$(this).attr("src", newsrc);
			}
		}
	);
}

function showPicture(file, subtitle) {
	$("#picture:first-child").attr("visibility","hidden");
	$("#pictureSubtitle").html(" ");
	
	var img = new Image();
	$(img).load(function() {
		$(this).hide();
		$("#picture").html(this);
		$(this).fadeIn('slow');
		
		$("#pictureSubtitle").hide();
		if(subtitle.length > 1) {
			subtitle = "<div class='letter_" + subtitle.substring(0,1).toLowerCase() + "'>"+ subtitle.substring(0,1).toUpperCase() +"</div>" + subtitle.substring(1,subtitle.length);
		}
		$("#pictureSubtitle").html(subtitle);
		$("#pictureSubtitle").fadeIn('slow');
    }).attr("src", file);
}

function execQuerystrings() {
	var page = qsParm[qspn_page];
	if(stringIsValid(page)) {
		selectNavigationItem(page);
	}
	else {
		selectNavigationItem("home");
	}
	return page;
}

function selectNavigationItem(cid) {
	if(String(cid).length > 0) {
		if(cid == "home"
		|| cid == "about"
		|| cid == "news"
		|| cid == "reviews"
		|| cid == "pictures"
		|| cid == "solutions"
		|| cid == "downloads"
		|| cid == "contact"
		) {} else cid = "home";
	}
	$("#content_"+cid).find(".content").fadeIn('slow');
	var newsrc = $("#navigation_"+cid).attr("src").replace(".", "_highlighted.");
	$("#navigation_"+cid).attr("src", newsrc );
	$("#navigation_"+cid).addClass("selected");
}

function aboutSlideshow() {
	// find which image is visible now and which is next
	var showMe = null;
	var hideMe = null;
	$("#about_slideshow").children().each(function () {
		if($(this).is(":visible") == true) {
			// if the next is not valid...
			if($(this).next("img").attr("src") == undefined) { // ...get first
				showMe = $("#about_slideshow img:first");
			}
			else {
				showMe = $(this).next("img");
			}
			hideMe = $(this);
		}
	});
	
	if(hideMe != null && hideMe != undefined) {
		showMe.addClass("showMe");
		hideMe.addClass("hideMe");
		$("#about_slideshow").attr("style","width:350px;height:490px;background-image: url("+ hideMe.attr("src") +");background-repeat:no-repeat;background-position:0px 0px;");
		hideMe.wait(3000).animate({opacity:0.0},1000,"linear", function() {
			$("img.hideMe").removeClass("hideMe").hide();
			$("img.showMe").removeClass("showMe").show().animate({opacity:1.0},2000,"linear", function() { aboutSlideshow(); });
		});
	}
}
