﻿$(document).ready(function (e) {
	if (typeof($.fancybox) == 'function') {
		$('.fancybox').fancybox();

		$('.fancybox.timage').fancybox({
			type: 'image'
		});
	}
});

var SetImagesInPlace = function () {
	var pos = $("#IRImage").offset();

	$(".IRSelectorImagesImage1, .IRSelectorImagesImage2").each(function () {
		var i = $(this);
		i.css("left", pos.left);
		i.css("top", pos.top);
	});

};

var ActivateNextSelection = function (c) {
	var p = $(c).parent();
	var indexofP = $(".IRSelector").index(p);

	var NextIndex = -1;
	if (indexofP === ($(".IRSelector").length - 1)) {
		NextIndex = 0;
	} else {
		NextIndex = indexofP + 1;
	}

	indexofP = (indexofP === ($(".IRSelector").length - 1)) ? 0 : (indexofP + 1);

	var n = $(".IRSelector").eq(NextIndex).children(".IRSelectorHeadline");
	ActivateSection(n, false);
};

var ActivateSection = function (s, clicked) {
	var s = $(s);

	if (s.hasClass("selected") || SelectorAnimationRunning) {
		// this one is already shown or animation is not done yet
		return;
	}

	if (clicked && (SelectorTimeOut > 0)) {
		clearTimeout(SelectorTimeOut);
		SelectorTimeOut = 0;
	}

	$(".IRSelectorHeadline").removeClass("selected").removeClass("IRSelectorHeadlineNextIsSelected");
	SelectorAnimationRunning = true;

	var p = $(s.parent());
	var id = p.attr("id").replace("IRSelector", "");

	var indexofP = $(".IRSelector").index(p);

	i1 = p.find("img").first();
	/*
	i2 = p.find("img").last();

	i2.show();
	$(".IRSelectorImagesImage1:visible").fadeOut(250, function () {
	});
	*/
	$(".IRSelectorImagesImage1:visible").fadeOut(500, function () {
	});
	i1.fadeIn(500);
	$(".IRSelectorContent").slideUp();
	p.children('.IRSelectorContent').slideDown(500, function () {
		s.addClass("selected");

		if (indexofP > 0) {
			$(".IRSelectorHeadline").eq(indexofP - 1).addClass("IRSelectorHeadlineNextIsSelected");
		}

		/*
		i1.fadeIn(250, function () {
		i2.fadeOut(250, function () {
		SelectorAnimationRunning = false;
		});
		});
		*/

		//var to = (clicked ? SelectorWaitUser : SelectorWaitAuto);
		SelectorTimeOut = setTimeout(function () {
			ActivateNextSelection(s);
		}, (clicked ? SelectorWaitUser : SelectorWaitAuto));

		SelectorAnimationRunning = false;
	});

};
