﻿if (!Array.prototype.indexOf) {
	Array.prototype.indexOf = function (elt /*, from*/) {
		var len = this.length;
		var from = Number(arguments[1]) || 0;
		from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
		if (from < 0)
			from += len;
		for (; from < len; from++) {
			if (from in this &&
          this[from] === elt)
				return from;
		}
		return -1;
	};
}

var blocks = [];
function makeScroll(scrollPanel) {
	$(scrollPanel).mCustomScrollbar("vertical", 200, /*"easeOutCirc"*/"linear", 1.25, "fixed", "yes", "no", 0);
}
$(function () {
	makeScroll('.page #mcs_container1');
	makeScroll('.page #mcs_container2');
	makeScroll('.page #mcs_container3');

	$("div#slider").slider({
		min: 0,
		max: 2,
		stop: function (event, ui) {
			flagSlider = true;
			moveToBlockIndex(ui.value);
		}
	});

	$('menu div[block-id]').each(function () {
		blocks.push($(this).attr("block-id"));
	});

	$(document).keydown(function (e) {
		var keyCode = e.keyCode || e.which;
		if ($(e.target).is('input') || $(e.target).is('select')) { return; }
		if (flagMoving) { return; }
		if (keyCode == 37) {
			scrollBack();
			return false;
		}
		if (keyCode == 39) {
			scrollNext();
			return false;
		}
	});
});

function scrollNext(complete) {
	var flagMovingSet = false;
	if (!flagMoving) {
		//$("div#slider" ).slider( "option", "disabled", true);
		flagMoving = true;
		flagMovingSet = true;
	}

	var activePage = $('.pageContainer.active');
	var pages = $('.pageContainer');
	var nextPage = pages[pages.length - 1];
	var prevPage = pages[0];

	pages.removeClass('inactive').addClass('active');
	$('.pageButtons').fadeOut(50, function () {
		activePage.parent().animate({ left: "-=550px" }, 150, 'linear', function () {
			$(prevPage).remove().appendTo(activePage.parent());
			makeScroll($('.mcs_container', $(prevPage)));
			activePage.parent().css({ left: "550px" });

			activePage.parent().animate({ left: "-=550px" }, 150, 'linear', function () {
				$('.pageButtons').fadeIn(50);
				pages.removeClass('active').addClass('inactive');
				$(nextPage).removeClass('inactive').addClass('active');

				//if (!flagSlider) { $("div#slider").slider("value", blocks.indexOf($(nextPage).attr('block-id'))); }

				if (flagMovingSet) {
					//$("div#slider" ).slider( "option", "disabled", false);
					flagMoving = false;
				}

				if (complete)
					complete();
			});
		});
		if (!flagSlider) { animateSlider($(nextPage).attr('block-id')); }
	});
	/*var sun = $('#sun');
	if (sun.position().left > sun.parent().width()) {
	sun.css({ left : (-sun.width() + 10) + 'px' });
	}
	sun.animate({left : '+=' + sun.width() / 3 + 'px'}, 400, 'linear');*/

	$('#c1, #c2, #c3, #c4').each(function () {
		if ($(this).position().left < 0) {
			$(this).css({ left: ($(this).parent().width() - 10) + 'px' });
		}
	});
	$('#c1, #c2, #c3, #c4').each(function () {
		$(this).animate({ left: '-=' + $(this).width() * $(this).height() / (200 * 200) * 100 + 'px' }, 400);
	});

	return false;
}

function scrollBack(complete) {
	var flagMovingSet = false;
	if (!flagMoving) {
		flagMoving = true;
		flagMovingSet = true;
		//$("div#slider" ).slider( "option", "disabled", true);
	}

	var activePage = $('.pageContainer.active');
	var pages = $('.pageContainer');
	var nextPage = pages[pages.length - 1];
	var prevPage = pages[0];

	pages.removeClass('inactive').addClass('active');
	$('.pageButtons').fadeOut(50, function () {
		activePage.parent().animate({ left: "+=550px" }, 150, 'linear', function () {
			$(nextPage).remove().insertAfter('.pageButtons');
			makeScroll($('.mcs_container', $(nextPage)));
			activePage.parent().css({ left: "-550px" });

			activePage.parent().animate({ left: "+=550px" }, 150, 'linear', function () {
				$('.pageButtons').fadeIn(50);
				pages.removeClass('active').addClass('inactive');
				$(prevPage).removeClass('inactive').addClass('active');

				//if (!flagSlider) { $("div#slider").slider("value", blocks.indexOf($(prevPage).attr('block-id'))); }

				if (flagMovingSet) {
					flagMoving = false;
					//$("div#slider" ).slider( "option", "disabled", false);
				}

				if (complete)
					complete();
			});
		});
		if (!flagSlider) { animateSlider($(prevPage).attr('block-id')); }
	});
	/*	var sun = $('#sun');
	if (sun.position().left < 0) {
	sun.css({ left : (sun.parent().width() - 10) + 'px' });
	}
	sun.animate({left : '-=' + sun.width() / 3 + 'px'}, 400, 'linear');*/

	$('#c1, #c2, #c3, #c4').each(function () {
		if ($(this).position().left > $(this).parent().width()) {
			$(this).css({ left: $(this).width() + 'px' });
		}
	});
	$('#c1, #c2, #c3, #c4').each(function () {
		$(this).animate({ left: '+=' + $(this).width() * $(this).height() / (200 * 200) * 100 + 'px' }, 400);
	});

	return false;
}

function moreInfoShow(a) {
	var pageContainer = $(a).closest('.pageContainer');
	var pages = $('.page', pageContainer);
	pages.animate({ top: "-=101%" }, 400, 'linear', function () {
		$(pages[0]).css({ top: "-101%" });
		$(pages[1]).css({ top: "0%" });
	});
}

function moreInfoHide(a) {
	var pageContainer = $(a).closest('.pageContainer');
	var pages = $('.page', pageContainer);
	pages.animate({ top: "+=101%" }, 400, 'linear', function () {
		$(pages[1]).css({ top: "101%" });
		$(pages[0]).css({ top: "0%" });
	});
}

var flagMoving = false;
var flagSlider = false;

function menuClick(a) {
	if (flagMoving)
		return false;
	flagMoving = true;
	//$("div#slider" ).slider( "option", "disabled", true);
	return moveToBlock($(a).attr('block-id'));
}

function moveToBlock(newBlockId) {
	return moveToBlockIndex(blocks.indexOf(newBlockId));
}
function animateSlider(newBlock) {
	var menuItem = $('menu div[block-id=' + newBlock + ']');
	var menuSelector = $('#menuSelector');
	menuSelector.css({ left: menuSelector.position().left + 'px' }).animate({ left: menuItem.position().left }, 400);
	moveToMenuItem(menuItem);
}
function moveToBlockIndex(newBlockIndex) {
	if (newBlockIndex < 0) { flagMoving = false; return false; }

	var currentBlockIndex = blocks.indexOf($('.pages .pageContainer.active').attr('block-id'));
	if (currentBlockIndex < 0) { flagMoving = false; return false; }

	if (currentBlockIndex == newBlockIndex) { flagSlider = false; flagMoving = false; /*$("div#slider" ).slider( "option", "disabled", false);*/return false; }

	if (currentBlockIndex > newBlockIndex) {
		scrollBack(function () { moveToBlockIndex(newBlockIndex); });
	} else {
		scrollNext(function () { moveToBlockIndex(newBlockIndex); });
	}
}
function initDragDrop() {
	$(".draggable").draggable({
		axis: 'x',
		revert: "invalid",
		container: "menu",
		start: function (event, ui) {
			$('menu div.droppable').addClass('droppable_inactive');
		},
		stop: function (event, ui) {
			if ($('.menuSelector').length <= 1) {
				ui.helper.clone().insertAfter(ui.helper);
			}
			$('menu div.droppable').removeClass('droppable_inactive');
		},
		helper: function () { return $('#menuSelector'); }
	});
	$(".droppable").droppable({
		drop: function (event, ui) {
			var p = $(this).position();
			$(ui.helper).clone().insertAfter(ui.helper).animate({ top: p.top, left: p.left });
			$(ui.helper).hide();
			moveToMenuItem(this);
			flagSlider = true;
			moveToBlock($(this).attr('block-id'));
		}
	});
}
function moveToMenuItem(menuItem) {
	$('menu div.droppable').removeClass('draggable').draggable('disable').removeAttr('title');
	$(menuItem).addClass('draggable').draggable({
		axis: 'x',
		revert: "invalid",
		container: "menu",
		start: function (event, ui) {
			$('menu div.droppable').addClass('droppable_inactive');
		},
		stop: function (event, ui) {
			if ($('.menuSelector').length <= 1) {
				ui.helper.clone().insertAfter(ui.helper);
			}
			$('menu div.droppable').removeClass('droppable_inactive');
		},
		helper: function () { return $('#menuSelector'); }
	}).draggable('enable').attr('title', 'Drag Me');
}
$(function () {
	initDragDrop();
	$('.droppable:not(.draggable)').live('click', function () {
		moveToBlock($(this).attr('block-id'));
	});

	$('#arrow-right, #arrow-left').hover(function () {
		$(this).find('.arrow').animate({ 'background-color': '#8dc63f' }, { queue: false, duration: 500 });
	}, function () {
		$(this).find('.arrow').animate({ 'background-color' : '#333' }, { queue: false, duration: 500 });
	});
});
