﻿function randomMinToMax(min, max) {
	var range = max - min + 1;
	return Math.floor(Math.random() * range) + min;
}

var backgroundChangeInterval = 3000; // change background each 3000 miliseconds
var backgroundImageIndex = 0; // first background image to be shown
$(document).ready(function() {
    setTimeout("loadBackgroundImage();", backgroundChangeInterval);
	$('.ZoneDesigner').hide();
	loadPanels(800);
	fixmenu();
	fixMainContent();
	if (typeof (casesPageLoad) != "undefined") {
		casesPageLoad();
	}
	if (typeof (activateScroll) != "undefined") {
		activateScroll();
	}

	// hide disclaimer in edit mode
	if ($('.ZoneName').length > 0) {
		$('.disclaimer').hide();
	}
	if ($('.mainContent .item').length == 0){
		$('#ScrollingPane').hide();
	}
	else{
		$('#ScrollingPane').show();	
	}
	
});

function fixmenu() {
	$('.AspNet-TreeView-Root').find('a:first').attr('href', 'javascript:void(0);');
	$('.AspNet-TreeView-Root:first').css('background-image', 'none');
}

function fixMainContent() {
	var maxWidthContent = parseInt($(window).width()) - 240;
	var maxWidthValue = maxWidthContent.toString() + 'px';
	//$('.mainContent').css('width', maxWidthValue.toString());
	//$('.mainContent').css('min-width', '500px');
}

function windowResize() {
	fixMainContent();
	if (typeof (activateScroll) != "undefined") {
		activateScroll();
	}
}

var timeout = null;

$(window).resize(function() {
	if (timeout != null) {
		clearTimeout(timeout);
	}
	timeout = setTimeout("windowResize()", 10);
});


function loadPanels(t) {
	$('.item').each(function() {
		var maxWidth = $(this).find('.mwidth').val();
		$(this).animate({
			width: '+=' + maxWidth
		},
		t,
		function() {
			$(this).find('.ZoneDesigner').show();
		});
	});
}

function loadBackgroundImage_func(currentCase, imgPath){
	$("body").css("background-image", "url('" + imgPath + "')");
	$("body").css("background-attachment", "fixed");
	$("body").css("background-repeat", "no-repeat");
	$("body").css("background-position", "center");
	$("body").css("-moz-background-size", "cover");
	$("body").css("background-size", "cover");

	elem = $("#caselink");
	elem.empty();
	newText = "<span class='mastercustomertitle'>" + unescape(currentCase.Customer) + "</span>";
	$(newText).appendTo(elem);
	newText = "<span class='mastercasetitle'> - " + unescape(currentCase.Title) + "</span>";
	$(newText).appendTo(elem);
	elem.die('click');
	elem.live('click', function() {
		window.location.href = ((typeof (LinkToCasesPage) != "undefined") ? LinkToCasesPage : "") + '?CaseID=' + currentCase.Id;
	});

	$('#backgroundEffectChanger').animate(
		{ opacity: 0},
		500,
		function () {
			// animation complete
		}
	);
}
function loadBackgroundImage() {
	if (typeof (cases) != "undefined") {
	    //var ran = randomMinToMax(0, cases.length - 1); // random image selection
	    var ran = backgroundImageIndex;
		var currentCase = cases[ran];
		backgroundImageIndex++;
		if (backgroundImageIndex >= cases.length) backgroundImageIndex = 0;
		
		if (typeof (currentCase) == "undefined") {
			return;
		}

		var _img = new Image();
		_img.src = currentCase.BackgroundImage;
		$(_img).hide();
		
		_img.onerror = function() {
			loadBackgroundImage();
			return;
		}
		if (!_img.complete) {
		    $(_img).load(function() {
			
				$('#backgroundEffectChanger').animate(
					{ opacity: '+=1'},
					500,
					function () {
						// animation complete
						  $(window).css('backgroung-image', 'null');
						  loadBackgroundImage_func(currentCase, currentCase.BackgroundImage);
					}
				);
				setTimeout("loadBackgroundImage();", backgroundChangeInterval);
		    });
		}
		else {
			$('#backgroundEffectChanger').animate(
				{ opacity: '+=1'},
				500,
				function () {
					// animation complete
					  $(window).css('backgroung-image', 'null');
					  loadBackgroundImage_func(currentCase, currentCase.BackgroundImage);
				}
			);
			setTimeout("loadBackgroundImage();", backgroundChangeInterval); 
		}
		
	}
}

function pausecomp(millis) {
	var date = new Date();
	var curDate = null;

	do { curDate = new Date(); }
	while (curDate - date < millis);
}

function ChangeOpacity(objElem, opacityValue) {
	objElem.style.opacity = opacityValue / 10;
	objElem.style.filter = 'alpha(opacity=' + opacityValue * 10 + ')';
}

function ShowTooltip(objElem, showTooltip, mouseX, mouseY) {
	var employeeId = objElem.id.split('_')[1];
	var itemPosition = $('#employee_' + employeeId).offset();
	
	var tooltipId = 'employeeTooltip_' + employeeId;
	$('#' + tooltipId).css('position', 'absolute');
	
	//$('#' + tooltipId).css('top', itemPosition.top + 12 + 'px');
	//$('#' + tooltipId).css('left', itemPosition.left + 12 + 'px');
	
	$('#' + tooltipId).css('top', mouseY - 30 + 'px');
	$('#' + tooltipId).css('left', mouseX - 20 + 'px');
	
	$('#' + tooltipId).css('display', 'block');
	$('#' + tooltipId).css('z-index', 9999);
	var opacityValue = 10;
	if (!showTooltip) {
		$('#' + tooltipId).css('display', 'none');
	}	
}

function FixLightboxOverlay() {
	$('#overlay').css('width', $(document).width() + 'px');
	$('#overlay').css('height', $(document).height() + 'px');
}

function ShowLightbox(show, contentElemId) {
	if (show && typeof (contentElemId) != 'undefined') {
		var htmlContent = $('#' + contentElemId).html();
		$('#imageContainer').html(htmlContent);
	}
	else {
		$('#imageContainer').html('');
	}
	$('#overlay').css("display", show ? 'block' : 'none');
	$('#lightbox').css("display", show ? 'block' : 'none');
}

function ShowEmployeeLightbox(employeeId) {
	$('html,body').animate({ scrollTop: 0 }, 100);
	//UpdateEmployeeLightbox(employeeId);
	ShowLightbox(true, 'employeeLightBoxTemplate');
	UpdateEmployeeLightbox(employeeId);
}

function CloseEmployeeLightbox() {
	ShowLightbox(false);
}

function ShowDepartmentEmployeesLightbox(departmentId) {
	UpdateDepartmentEmployeesLightbox(departmentId);
	ShowLightbox(true, 'departmentEmployeesLightBoxTemplate');
}

function CloseDepartmentEmployeesLightbox() {
	cbDepartmentClientID = $('#departmentEmployeesCloseButtonParam').val();
	SetComboInputText(cbDepartmentClientID, 'Selecteer afdeling');	
	ShowLightbox(false);
}

function UpdateEmployeeLightbox(employeeId) {
	var videoUrl = arrEmployees[employeeId].Video;
	//$('#employeeVideoContent').attr('src', video);
	
	if (typeof (AddVideoPlayer) != "undefined") {
		AddVideoPlayer('employeeVideoContent', videoUrl, arrEmployees[employeeId].PreviewImage);
	}
	
	var employeeName = arrEmployees[employeeId].Name;
	$('#employeeNameContent').text(employeeName);
	var departmentName = arrEmployees[employeeId].Department;
	$('#departmentNameContent').text(departmentName);
	$('#departmentNameContent2').text(departmentName);
	var employeeFunction = arrEmployees[employeeId].Funct;
	$('#employeeFunctionContent').text(employeeFunction);
	var employeeQuote = arrEmployees[employeeId].Quote;
	$('#employeeQuote').text(employeeQuote);
	var htmlContent = GenerateColleaguesTemplate(employeeId);
	$('#employeesContainer').html(htmlContent);
}

function UpdateDepartmentEmployeesLightbox(departmentId) {
	var departmentName = arrDepartments[departmentId].Name;
	$('#selectedDepartmentNameContent').text(departmentName);	
	var htmlContent = GenerateDepartmentEmployeesTemplate(departmentId);
	$('#departmentEmployeesContainer').html(htmlContent);
}

function GenerateEmployeeTemplate(employeeName, employeeImageUrl, elemAttr) {
	var attrs = typeof (elemAttr) != 'undefined' ? elemAttr : '';
	var html = '';
	html += '<div class="employeeImage" ' + attrs + ' >';
	html += ' <img style="border-width: 0px;" alt="' + employeeName + '" src="' + employeeImageUrl + '" class="image" />';
	html += '</div>';
	return html;
}

function GenerateColleaguesTemplate(employeeId) {
	var content = '';
	var departmentId = arrEmployees[employeeId].DepartmentId;
	$.each(arrEmployees, function(employeeKey, employeeObjValue) {
		if (departmentId == employeeObjValue.DepartmentId && employeeId != employeeObjValue.Id) {
			var clickEvent = 'onclick="UpdateEmployeeLightbox(\'' + employeeObjValue.Id + '\')" ';
			var style = 'style="cursor: pointer;" ';
			content += GenerateEmployeeTemplate(employeeObjValue.Name, employeeObjValue.Thumbnail, clickEvent+style);
		}
	});		
	return content;
}

function GenerateDepartmentEmployeesTemplate(departmentId) {
	var content = '';
	$.each(arrEmployees, function(employeeKey, employeeObjValue) {
	if (departmentId == employeeObjValue.DepartmentId) {
			var clickEvent = 'onclick="CloseDepartmentEmployeesLightbox(); ShowEmployeeLightbox(\'' + employeeObjValue.Id + '\')" ';
			var style = 'style="cursor: pointer;" ';
			content += GenerateEmployeeTemplate(employeeObjValue.Name, employeeObjValue.Thumbnail, clickEvent + style);
		}
	});
	return content;
}

function Employee(id, name, department, thumbnail, video, funct, quote, departmentId, previewImage) {
	
	if (typeof(previewImage) == 'undefined'){
		previewImage = '';
	}
	
	this.Id = id;
	this.Name = name;
	this.Department = department;
	this.Thumbnail = thumbnail;
	this.Video = video;
	this.Funct = funct;
	this.Quote = quote;
	this.DepartmentId = departmentId;
	this.PreviewImage = previewImage;
}

function Department(id, name) {
	this.Id = id;
	this.Name = name
}

function OnDepartmentChanged(item) { 
	if (item != null) {
		if (item.get_value() != null)
		{
			var departmentId = item.get_value();
			ShowDepartmentEmployeesLightbox(departmentId);
		}
		$('.rcbHovered').addClass('rcbItem');
		$('.rcbHovered').removeClass('rcbHovered');
	}
}

function SetComboInputReadOnly(clientId) {
	var combo = $find(clientId);
	var input = combo.get_inputDomElement();
	input.onkeydown = OnDepartmentKeyDown;
}

function SetComboInputText(comboId, text) {
	var combo = $find(comboId);
	combo.clearSelection();
	//combo.set_emptyMessage(text);
} 	

function OnDepartmentKeyDown(e) {	
	if (!e) {
		e = window.event;
	}	
	e.returnValue = false;
	if (e.preventDefault) {
		e.preventDefault();
	}
}

function GetEmployeesAutocomplete() {
	var employees = new Array();
	$.each(arrEmployees, function(employeeKey, employeeObjValue) {
	employees[employees.length] = employeeObjValue.Name + "|" + employeeObjValue.Id + "|" + employeeObjValue.Department;
	});
	return employees;
}
var isCaseSlide = false;
/**** Cases animation ****/

//function MoveToPage_old(index, enableEffect) {
//	if ((typeof (imgCases) == "undefined") || (imgCases == null)) {
//		return;
//	}

//	if (typeof (enableEffect) == 'undefined') enableEffect = true;

//	var currentPageIndex = parseInt($('#pager>span.active').attr('id').replace('p', ''), 10);
//	if (currentPageIndex > 0) {
//		currentPageIndex = currentPageIndex - 1; // zero based
//	}

//	if (!enableEffect) {
//		var filename = imgCases[index - 1];
//		AddVideoPlayer('videoContent', filename);

//		$('#pager>span.active').removeClass('active');
//		$("#pager>span[id='p" + (index) + "']").addClass("active");
//	}
//	else {
//		if (index > currentPageIndex + 1) {
//			currentPageIndex++;

//			var filename = imgCases[currentPageIndex];
//			AddVideoPlayer('videoContent', filename);

//			$('#pager>span.active').removeClass('active');
//			$("#pager>span[id='p" + (currentPageIndex + 1) + "']").addClass("active");
//			
//			setTimeout(function() { MoveToPage(index); }, 700);
//		}
//		else if (index < currentPageIndex + 1) {
//			currentPageIndex--;
//			
//			var filename = imgCases[currentPageIndex];
//			AddVideoPlayer('videoContent', filename);

//			$('#pager>span.active').removeClass('active');
//			$("#pager>span[id='p" + (currentPageIndex + 1) + "']").addClass("active");			
//			
//			setTimeout(function() { MoveToPage(index); }, 700);
//		}
//	}
//}

//function SlideToPage(index, enableEffect) {
//	if ((typeof (imgCases) == "undefined") || (imgCases == null)) {
//		return;
//	}
//	
//	isCaseSlide = true;
//	if (typeof (enableEffect) == 'undefined') enableEffect = true;

//	var currentPageIndex = parseInt($('#pager>span.active').attr('id').replace('p', ''), 10);
//	
//	var _img = new Image();
//	if (index > currentPageIndex) {

//		$('.imgCase').hide();
//		_img.src = imgCases[currentPageIndex];
//		
//		if (!_img.complete) {
//			$(_img).load(function() {
//				$('.imgCase').attr('src', _img.src);
//				$('.imgCase').show();
//				
//				var width = parseInt($('.singlecasecontent').width(), 10);
//				$('.singlecasecontent').css('width', '0px');
//				$('.singlecasecontent').animate({
//					width: '+=' + width
//				},
//				300,
//				function() {
//					// complete
//				});

//				$('#pager>span.active').removeClass('active');
//				if (enableEffect) {
//					$("#pager>span[id='p" + (currentPageIndex + 1) + "']").addClass("active");
//					setTimeout(function() { SlideToPage(index); }, 700);
//				}
//				else {
//					$("#pager>span[id='p" + (index) + "']").addClass("active");
//				}
//			});
//		}
//		else {
//			$('.imgCase').attr('src', _img.src);
//			$('.imgCase').show();
//			
//			var width = parseInt($('.singlecasecontent').width(), 10);
//			$('.singlecasecontent').css('width', '0px');
//			$('.singlecasecontent').animate({
//				width: '+=' + width
//			},
//				300,
//				function() {
//					// complete
//			});
//							
//			$('#pager>span.active').removeClass('active');
//			if (enableEffect) {
//				$("#pager>span[id='p" + (currentPageIndex + 1) + "']").addClass("active");
//				setTimeout(function() { SlideToPage(index); }, 700);
//			}
//			else {
//				$("#pager>span[id='p" + (index) + "']").addClass("active");
//			}
//		}
//		isCaseSlide = false;
//	}
//	else if (index < currentPageIndex) {
//	$('.imgCase').hide();
//		_img.src = imgCases[currentPageIndex - 2];
//		
//		if (!_img.complete) {
//			$(_img).load(function() {
//				$('.imgCase').attr('src', _img.src);
//				$('.imgCase').show()

//				var width = parseInt($('.singlecasecontent').width(), 10);
//				$('.singlecasecontent').css('width', '0px');
//				$('.singlecasecontent').animate({
//					width: '+=' + width
//				},
//					300,
//					function() {
//						// complete
//				});			
//				
//				$('#pager>span.active').removeClass('active');
//				if (enableEffect) {
//					$("#pager>span[id='p" + (currentPageIndex - 1) + "']").addClass("active");
//					setTimeout(function() { SlideToPage(index); }, 700);
//				}
//				else {
//					$("#pager>span[id='p" + (index) + "']").addClass("active");
//				}
//			});
//		}
//		else {
//			$('.imgCase').attr('src', _img.src);
//			$('.imgCase').show();

//			var width = parseInt($('.singlecasecontent').width(), 10);
//			$('.singlecasecontent').css('width', '0px');
//			$('.singlecasecontent').animate({
//				width: '+=' + width
//			},
//			300,
//			function() {
//				// complete
//			});			
//			
//			$('#pager>span.active').removeClass('active');
//			if (enableEffect) {
//				$("#pager>span[id='p" + (currentPageIndex - 1) + "']").addClass("active");
//				setTimeout(function() { SlideToPage(index); }, 700);
//			}
//			else {
//				$("#pager>span[id='p" + (index) + "']").addClass("active");
//			}
//		}
//	}
//}
///**** Cases animation ****/

//function SetCaseNavigationSizes() {
//	var imgBorder = parseInt($('.imgCase').css("borderLeftWidth"), 10) + parseInt($('.imgCase').css("borderRightWidth"), 10);
//	var imgTop = $('.imgCase').offset().top;
//	var imgLeft = $('.imgCase').offset().left;
//	var imgWidth = $('.imgCase').outerWidth();
//	var imgHeight = $('.imgCase').outerHeight();

//	if (imgWidth == 0 || imgHeight == 0 || imgTop == 0) {
//		setTimeout("SetCaseNavigationSizes()", 10);
//		return;
//	}
//	
//	imgWidth += imgBorder;
//	imgHeight += imgBorder;
//	
//	$('.nav').css('top', imgTop + 'px');
//	$('.nav').css('left', imgLeft + 'px');
//	$('.nav').css('width', imgWidth + 'px');
//	$('.nav').css('height', imgHeight + 'px');

//	var arrowHeight = 102;
//	var arrowBottomMargin = 25;

//	var arrowTop = imgHeight / 2 - arrowHeight / 2;
//	var arrowLeftLeft = imgWidth / 4 - 25;
//	var arrowRightLeft = imgWidth * 3 / 4 - 25;

//	$('#leftNavArrow').css('top', arrowTop + 'px');
//	$('#leftNavArrow').css('left', arrowLeftLeft + 'px');

//	$('#rightNavArrow').css('top', arrowTop + 'px');
//	$('#rightNavArrow').css('left', arrowRightLeft + 'px');

//	$('#leftOverlay').css('width', imgWidth / 2 + 'px');
//	$('#leftOverlay').css('height', imgHeight + 'px');

//	$('#rightOverlay').css('width', imgWidth / 2 + 'px');
//	$('#rightOverlay').css('height', imgHeight + 'px');
//}

//function ShowCaseNavigation(show, direction) {
//	var cssClassArrow = direction + 'Arrow';
//	var arrowElemId = direction + 'NavArrow';
//	var cssClassOverlayElem = direction + 'Overlay'
//	if (show) {
//		var imgWidth = $('.imgCase').outerWidth();
//		var imgHeight = $('.imgCase').outerHeight();
//		if (imgHeight != 0 && imgHeight != 0) {
//			$('.nav .' + cssClassOverlayElem).css('width', imgWidth / 2 + 'px');
//			$('.nav .' + cssClassOverlayElem).css('height', imgHeight + 'px');
//			$('.nav .' + cssClassOverlayElem).addClass('imgOverlay');
//			$('#' + arrowElemId).addClass(cssClassArrow);
//		}
//	}
//	else {
//		$('.nav .' + cssClassOverlayElem).removeClass('imgOverlay');
//		$('#' + arrowElemId).removeClass(cssClassArrow);
//	}
//}

//function CaseNavigateTo(direction) {
//	var count = $('#pager>span').length;
//	var currentPageIndex = parseInt($('#pager>span.active').attr('id').replace('p', ''), 10);
//	if (direction == 'left') {
//		if (currentPageIndex == 1) {
//			SlideToPage(count, false);
//		}
//		else {
//			SlideToPage(currentPageIndex - 1, false);
//		}
//	}
//	else if (direction == 'right') {
//		if (currentPageIndex == count) {
//			SlideToPage(1, false);
//		}
//		else {
//			SlideToPage(currentPageIndex + 1, false);
//		}
//	}
//}

function CaseItem(id, name, title, images, customer) {
	this.Id = id;
	this.Name = name;
	this.Title = title;
	this.BackgroundImage = images;
	this.Customer = customer;
}

function VideoItem(filename, showControls, previewImage) {
	if (typeof (previewImage) == "undefined") {
		previewImage = "";
	}
	this.Filename = filename;
	this.ShowControls = showControls;
	this.PreviewImage = previewImage;
}

function PlaySound()
{
    try
    {
        var player = document.getElementById('mediaspace').firstChild;
        if(player != null)
        {
            player.sendEvent('PLAY', 'true');         
        }
    }
    catch(e)
    {
    }
}

function StopSound()
{
    try
    {
        var player = document.getElementById('mediaspace').firstChild;
        if(player != null)
        {
            player.sendEvent('STOP');         
        }
     }
     catch(e)
     {
     }
}

function mobileRedirect(screenWidth, forwardUrl) {
	if (screen.width < screenWidth) {
		if (cookieName('mobileForwarded') != '1') {
			var date = new Date();
			date.setTime(date.getTime()+(1825*24*60*60*1000)); // expires after 5 years
			var expires = date.toGMTString();
			document.cookie = 'mobileForwarded=1; expires=' + expires + '; path=/';
			document.location = forwardUrl;
		}
	};

	function cookieName(name) {
		var dc = document.cookie;
		var prefix = name + "=";
		var begin = dc.indexOf("; " + prefix);
		if (begin == -1) {
			begin = dc.indexOf(prefix);
			if (begin != 0) return null;
		} else {
			begin += 2;
		}
		var end = document.cookie.indexOf(";", begin);
		if (end == -1) {
			end = dc.length;
		}
		return decodeURIComponent(dc.substring(begin + prefix.length, end));
	}
}

