// JavaScript Document

Cufon.replace('h1, h2, h3, div.quarter-billet h4, div.right-green-column-holder a.btnTips, div.search-vacancies-holder div.main-search div.example span.tip, .article-quote-holder .text .phrase, .article-quote-holder .text .author, div.employers-items-holder div.item div.popup p, div.billets-holder div.quarter-billet a.image-block span.title, div.home-content-holder div.center-part div.person, div.home-content-holder div.center-part div.function, .heineken-paragraph', { fontFamily: 'Heineken' });
Cufon.replace('div.layout2 div.billet-left div.menu ul li', { fontFamily: 'Heineken', hover: true });
Cufon.replace('.btnHolder', { fontFamily: 'Heineken', textShadow: '1px 1px rgba(0, 0, 0, 0.2)' });

var homeRotatorTime = 5;  // home page rotator time in seconds
var brandsRotatorTime = 5;  // brands gallery rotator time in seconds





/* ================================================= */
/* ================================================= */

var mainBgImgWidth;
var mainBgImgHeight;
var isIE;
var homeBilletsHeight = new Array();

window.onunload = function(){}; 

//$(document).ready(function(){
$(window).load(function(){
	isIE = ($.browser.msie)?true:false;
	
	mainBgImgWidth = $('div.main-background img').width();
	mainBgImgHeight = $('div.main-background img').height();
	
	makingTopMenu();
	$('.btnHolder').append('<ins><!--  --></ins>');
	
	customSelect();
	
	stretchMainBackground();
	$(window).resize(function() { stretchMainBackground(); });
	$('div.main-background img').load(function() {
		stretchMainBackground();
	});
	
	if ($('div.brands-gallery-holder').length > 0) {
		brandsGallery();
	}
	
	if ($('div.areas-items-holder').length > 0) {
		expertiseAreas();
	}

	if ($('div.employers-items-holder').length > 0) {
		employersWall();
	}
	
	if ($('div.activiti-calendar-holder').length > 0) {
		activitiesCalendar();
	}
	
	if (isIE) {
		$('div.pager-holder div.buttons input:disabled').addClass('disabled');
	}
	
	/*
	if ($('div.home-content-holder').length > 0) {
		makeHomePage();
		initHomeRotator();
	}
	*/
});


function stretchMainBackground() {
	$('div.content-holder').css('height', '');
	if ($('div.content-holder').height() < $(window).height() - 488) {
		$('div.content-holder').height($(window).height() - 488);
	}
	
	$('div.home-content-holder div.center-part').css('height', '');
	$('div.home-rotator-images div.item').css('height', '');
	if ($('div.home-content-holder').height() < $(window).height() - 363) {
		$('div.home-content-holder div.center-part').height($(window).height() - 449);
		$('div.home-rotator-images div.item').height($(window).height() - 220);
	}
	
	$('div.header-holder').css('width', '');
	if ($('div.header-holder').width() < 988) {
		$('div.header-holder').width(988);
	}
	$('div.main-background').css('width', '');
	if ($('div.main-background').width() < 988) {
		$('div.main-background').width(988);
	}
	
	$('div.home-rotator-images').height()
	
	var $bgImg = $('div.main-background img');
	$bgImg.removeAttr("width").removeAttr("height").css({ width: "", height: "" });

	var docWidth = $(document).width();
	var docHeight = $(document).height() - 100;
	
	$('div.main-background').height(docHeight);
	
	if ((docWidth > mainBgImgWidth) || (docHeight > mainBgImgHeight)) {
		$bgImg.css('margin-left', 0).css('width', '').css('height', '');
		if (docWidth / mainBgImgWidth > docHeight / mainBgImgHeight) {
			$bgImg.css('width', docWidth).show();
		} else {
			$bgImg.css('height', docHeight).show();
			$bgImg.css('margin-left', - Math.round(($bgImg.width() - docWidth) / 2));
		}
	} else {
		$bgImg.hide();
	}
}

function onFocusInput(inputObj, defaultText) {
    if ($.trim($(inputObj).val()) == defaultText) {
		$(inputObj).val("").removeClass("inactive").parent().addClass('inputGlow');
	}
}

function onBlurInput(inputObj, defaultText) {
    if (($.trim($(inputObj).val()) == "") || ($(inputObj).val().toLowerCase() == defaultText.toLowerCase())) {
		$(inputObj).val(defaultText).addClass("inactive").parent().removeClass('inputGlow');
	}
}

function makingTopMenu() {
	$('div.top-menu-holder ul li:first-child').addClass('home').find('a').text('').clone(true).insertAfter('div.top-menu-holder ul li:first-child a').addClass('hover');
	
	$('div.top-menu-holder ul li:not(.home) a').each(function(){
		$(this).addClass('null').clone(true).insertAfter(this).addClass('hover').removeClass('null');
		Cufon.replace('div.top-menu-holder ul li a.null', { fontFamily: 'Heineken' });
		Cufon.replace('div.top-menu-holder ul li a.hover', { fontFamily: 'Heineken', textShadow: '#15480f 0px 1px' });
	});
	
	$('div.top-menu-holder ul li:not(.active)').hover(
		function() {
			$('a.hover', this).stop(true, true);
			if (isIE) {
				$('a.hover', this).show();
			} else {
				$('a.hover', this).fadeIn(200);
			}
		},
		function() {
			if (isIE) {
				$('a.hover', this).hide();
			} else {
				$('a.hover', this).fadeOut(100);
			}
		}
	);
	//$('div.top-menu-holder ul li.active a').click(function() {return false;});	
}

function checkSearchExampleFocus(inputObj) {
	if ($(inputObj).val() == '') {
		$('div.search-vacancies-holder div.main-search div.example').hide();
		$('div.search-vacancies-holder div.main-search').addClass('infocus');
	}
}

function checkSearchExampleBlur(inputObj) {
	if ($.trim($(inputObj).val()) == "") {
		$(inputObj).val('');
		$('div.search-vacancies-holder div.main-search div.example').show();
		$('div.search-vacancies-holder div.main-search').removeClass('infocus');
	}
}

function customSelect() {
	$('select').each(function() {
		var orig_name = $(this).attr('name');
		var orig_text = $('option:selected', this).text();
		var orig_value = $(this).val();
		var html = '<span class="CSTinputHolder"><input type="text" value="' + orig_value + '" name="' + orig_name + '" /></span>' + 
				'<span class="CSTcurrentVal">' + orig_text + '<ins>' + orig_text + '</ins></span>' +
				'<span class="CSTarrow"><!--  --></span>' +
				'<span class="CSTlistHolder"><span class="list">' + 
				customSelectList(this) +
				'</span><span class="tl"><!--  --></span><span class="t"><!--  --></span><span class="l"><!--  --></span></span>';
		$(this).wrap('<span class="CSTselectHolder"></span>');
		$(this).after(html);
		var $orig_select = $(this);
		var $cst_holder = $orig_select.parent();
		var $cst_input = $('span.CSTinputHolder input', $cst_holder);
		var $cst_currentval = $('span.CSTcurrentVal', $cst_holder);
		var $cst_arrow = $('span.CSTarrow', $cst_holder);
		var $cst_list = $('span.CSTlistHolder', $cst_holder);
		var listHeight;
		
		$('span.list span', $cst_list)
		.mousedown(function() {
			var this_text = $('em.text', this).text();
			var this_val = $('em.val', this).text();
			$cst_currentval.html(this_text + '<ins>' + this_text + '</ins>');
			$('span.list span', $cst_list).removeClass('selected');
			$(this).addClass('selected');
			$cst_holder.addClass('CSTchanged');
			$orig_select.val(this_val);
			$orig_select.change();
			$cst_input.blur();
		});
		
		$cst_input
		.focus(function() {
			$cst_holder.addClass('CSTposRelative');
			listHeight = $cst_list.height();
			$('span.l', $cst_list).css('height', listHeight);
			$cst_list.css('top', 17 - (listHeight - 14) / 2);
			if (isIE) {
				$cst_list.show();
			} else {
				$cst_list.fadeIn(300);
			}
		})
		.blur(function() {
			$cst_list.hide();
			$cst_holder.removeClass('CSTposRelative');
		});
		
		$cst_arrow.click(function() {
			$cst_input.focus();
		});
		
		$cst_currentval.click(function() {
			$cst_input.focus();
		});
	});
}

function customSelectList(selectObj) {
	var html = '';
	$('option', selectObj).each(function() {
		var opt_text = $(this).text();
		var opt_val = $(this).val();
		html += '<span' + (($(this).attr('selected')) ? ' class="selected"' : '') + '><em class="text">' + opt_text + '</em><em class="val">' + opt_val + '</em></span>'
	});
	return html;
}

function brandsGallery() {
	var brandsCount = $('div.brands-gallery-holder div.items-holder div.item').length;
	if (brandsCount < 7) {
		$('div.brands-gallery-holder a.left-btn, div.brands-gallery-holder a.right-btn').hide();
		$('div.brands-gallery-holder div.items-holder').css('left', 70 + (840 - brandsCount * 140) / 2);
		return;
	}
	for (var i = 0; i < 6; i++) {
		$('div.brands-gallery-holder div.items-holder').prepend($('div.brands-gallery-holder div.items-holder div.item:nth-child(' + brandsCount + ')').clone(true));
	}
	
	for (var i = 7; i < 13 ; i++) {
		$('div.brands-gallery-holder div.items-holder').append($('div.brands-gallery-holder div.items-holder div.item:nth-child(' + i + ')').clone(true));
	}
	
	var isMoving = false;
	var currentX = -840;
	$('div.brands-gallery-holder div.items-holder').css('width', (brandsCount + 12 ) * 140).css('left', currentX + 70);
	
	function checkX() {
		if (currentX == - (brandsCount + 6) * 140) {
			currentX = - 840;
			$('div.brands-gallery-holder div.items-holder').css('left', currentX + 70);
		}
		if (currentX == 0) {
			currentX = - brandsCount * 140;
			$('div.brands-gallery-holder div.items-holder').css('left', currentX + 70);
		}
	}
	
	function rotate() {
		if (isMoving) { return; }
		isMoving = true;
		if (itemHover) {
			clearInterval(rotateBrandsTimer);
			rotateBrandsTimer = setInterval(rotate, brandsRotatorTime * 1000);
			return;
		}
		clearInterval(rotateBrandsTimer);
		currentX -= 140;
		$('div.brands-gallery-holder div.items-holder').animate({left : currentX + 70}, 500, 
			function() {
				isMoving = false;
				checkX();
				rotateBrandsTimer = setInterval(rotate, brandsRotatorTime * 1000);
			}
		);
	}
	
	$('div.brands-gallery-holder a.left-btn').click(function() {
		if (isMoving) { return; }
		isMoving = true;
		clearInterval(rotateBrandsTimer);
		currentX -= 140;
		$('div.brands-gallery-holder div.items-holder').animate({left : currentX + 70}, 300, 
			function() {
				isMoving = false;
				checkX();
				rotateBrandsTimer = setInterval(rotate, brandsRotatorTime * 1000);
			}
		);
	});
	
	$('div.brands-gallery-holder a.right-btn').click(function() {
		if (isMoving) { return; }
		isMoving = true;
		clearInterval(rotateBrandsTimer);
		currentX += 140;
		$('div.brands-gallery-holder div.items-holder').animate({left : currentX + 70}, 300, 
			function() {
				isMoving = false;
				checkX();
				rotateBrandsTimer = setInterval(rotate, brandsRotatorTime * 1000);
			}
		);
	});
	
	$('div.brands-gallery-holder div.items-holder div.item').hover(
		function() {
			itemHover = true;
			clearInterval(rotateBrandsTimer);
			$(this).css('opacity', 0.85);
		},
		function() {
			itemHover = false;
			$(this).css('opacity', 1);
			rotateBrandsTimer = setInterval(rotate, brandsRotatorTime * 1000);
		}
	);
	
	var rotateBrandsTimer;
	var itemHover = false;
	rotateBrandsTimer = setInterval(rotate, brandsRotatorTime * 1000);
}

function expertiseAreas() {
	$('div.areas-items-holder div.item:gt(3)').addClass('second-line');
	$('div.areas-items-holder div.item')
	.hover(
		function() {
			$('div.areas-items-holder div.item').addClass('disabled');
			$(this).removeClass('disabled');
			$('div.popup', this).hide();
			$('div.popup', this).stop(true, true);
			if (isIE) {
				$('div.popup', this).show();
				$(this).addClass('relPosition');
			} else {
				$('div.popup', this).fadeIn(200);
			}
		},
		function() {
			$('div.areas-items-holder div.item').removeClass('disabled');
			$('div.popup', this).hide();
			if (isIE) { $(this).removeClass('relPosition'); }
		}
	)
	.click(
		function() {
			document.location.href = $('a.arrow', this).attr('href');
		}
	);
}

function employersWall() {
	$('div.employers-items-holder div.item:nth-child(6n-1)').addClass('sublast');
	$('div.employers-items-holder div.item:nth-child(6n)').addClass('last');
	$('div.employers-items-holder div.item').each(function() {
		Pixastic.process($('img', this)[0], "desaturate", { average : false });
	});

	$('div.employers-items-holder div.item')
	.hover(
		function() {
			$('div.employers-items-holder div.item').addClass('disabled');
			$(this).removeClass('disabled').addClass('hover');
			$('div.popup', this).hide();
			$('div.popup', this).stop(true, true);
			if (isIE) {
				$('div.popup', this).show();
			} else {
				$('div.popup', this).fadeIn(200);
			}
		},
		function() {
			$('div.employers-items-holder div.item').removeClass('disabled hover');
			$('div.popup', this).hide();
		}
	)
	.click(
		function() {
			document.location.href = $('a.arrow', this).attr('href');
		}
	)
	.each(function() {
		if ($(this).hasClass('has-video')) {
			var posX = $(this).position().left;
			var posY = $(this).position().top;
			$(this).append('<span class="video-icon"><!--  --></span>');
			var $icon = $('span.video-icon', this);
			$icon.css({'left': posX, 'top': posY});
		}
	});
}

function activitiesCalendar() 
{
    $('div.activiti-calendar-holder ul li div.tab').click(function() 
	{	    
	    if (!$(this).hasClass('opened')) 
		{
		    $('div.activiti-calendar-holder ul li div.opened').removeClass('opened').next('div.description').slideUp('fast');		    
		}
		$(this).toggleClass('opened');
		$(this).next('div.description').stop(true, true);

		$('html').scrollTop($('html').scrollTop() - 1);
		$(this).next('div.description').slideToggle('fast', function() 
		{		    
		    if (isIE) 
			{
			    $('h3', this).hide().show();
			}
		});		
	});
}

function makeHomePage() {
	if (isIE) {
		Cufon.replace('div.home-content-holder div.billet h2', { fontFamily: 'Heineken' });
	}
	
	$('div.billets-holder div.billet').each(function(i) {
		homeBilletsHeight[i] = $('div.hover-state', this).height();
		$(this).attr('innerId', i);
		
	});
	
	$('div.billets-holder div.billet').hover(
		function() {
			var $initObj = $('div.initial-state', this);
			var $hoverObj = $('div.hover-state', this);
			var id = parseInt($(this).attr('innerId'));
			$initObj.stop(true, true);
			$hoverObj.stop(true, true);
			$hoverObj.hide();
			if (isIE) {
				$initObj.hide();
				$hoverObj.height(80);
				$hoverObj.show().animate({top:-(homeBilletsHeight[id] - 80) / 2, height:homeBilletsHeight[id]}, 200);
			} else {
				$initObj.fadeOut(200);
				$hoverObj.height(80).css('opacity', 0).show();
				$hoverObj.animate({top:-(homeBilletsHeight[id] - 80) / 2, height:homeBilletsHeight[id], opacity:1}, 200);
			}
		},
		function() {
			$('input', this).blur();
			var $initObj = $('div.initial-state', this);
			var $hoverObj = $('div.hover-state', this);
			var id = parseInt($(this).attr('innerId'));
			if (isIE) {
				$initObj.show();
				$hoverObj.animate({top:0, height:80}, 100, function() {$(this).height(homeBilletsHeight[id]).hide();});
			} else {
				$initObj.fadeIn(200);
				$hoverObj.animate({top:0, height:80, opacity:0}, 100, function() {$(this).height(homeBilletsHeight[id]).hide();});
			}
		}
	);
}

function makeHomeRotator() {
	var itemsCount = $('div.home-rotator-images div.item').length;
	var currentRotItem;
	var isFading = false;
	var homeRotatorTimer = 0;
	
	for (var i = 0; i < itemsCount; i++) {
		$('div.home-rotator-pager').append('<div class="item"><!--  --></div>');
	}
	
	$('div.home-rotator-pager div.item').each(function(i) {
		$(this)
		.hover(
			function() {
				$(this).addClass('hover');
			},
			function() {
				$(this).removeClass('hover');
			}
		)
		.click(
			function() {
				if ($(this).hasClass('active')) return;
				if (isFading) return;
				isFading = true;
				$('div.home-rotator-pager div.item').removeClass('active');
				$(this).addClass('active');
				clearInterval(homeRotatorTimer);
				$('div.home-rotator-slogans div.item:nth-child(' + currentRotItem + ')').fadeOut(400);
				$('div.home-rotator-images div.item:nth-child(' + currentRotItem + ')').fadeOut(400);
				currentRotItem = i + 1;
				$('div.home-rotator-slogans div.item:nth-child(' + currentRotItem + ')').fadeIn(400);
				$('div.home-rotator-images div.item:nth-child(' + currentRotItem + ')').fadeIn(400, 
					function() {
						isFading = false;
						homeRotatorTimer = setInterval(rotate, homeRotatorTime * 1000);
					}
				);
			}
		)
	});
	
	currentRotItem = 1;
	$('div.home-rotator-pager div.item:nth-child(1)').addClass('active');
	$('div.home-rotator-images div.item:nth-child(1)').show();
	$('div.home-rotator-slogans div.item:nth-child(1)').show();
	
	homeRotatorTimer = setInterval(rotate, homeRotatorTime * 1000);
	
	function rotate() {
		$('div.home-rotator-slogans div.item:nth-child(' + currentRotItem + ')').stop(true, true);
		$('div.home-rotator-images div.item:nth-child(' + currentRotItem + ')').stop(true, true);
		clearInterval(homeRotatorTimer);
		$('div.home-rotator-pager div.item').removeClass('active');
		$('div.home-rotator-slogans div.item:nth-child(' + currentRotItem + ')').fadeOut(600);
		$('div.home-rotator-images div.item:nth-child(' + currentRotItem + ')').fadeOut(600);
		currentRotItem++;
		if (currentRotItem > itemsCount) currentRotItem = 1;
		$('div.home-rotator-pager div.item:nth-child(' + currentRotItem + ')').addClass('active');
		$('div.home-rotator-slogans div.item:nth-child(' + currentRotItem + ')').fadeIn(600);
		$('div.home-rotator-images div.item:nth-child(' + currentRotItem + ')').fadeIn(600, 
			function() {
				isFading = false;
				homeRotatorTimer = setInterval(rotate, homeRotatorTime * 1000);
			}
		);
	}
}

function initHomeRotator() {
	$('div.home-rotator-images').after('<div class="images-dump"></div>');
	$('div.home-rotator-images div.item').each(function() {
		var imgPath;
		imgPath = $(this).css('background-image').split('"')[1];
		$('div.images-dump').append('<img src="' + imgPath + '" alt="" />');
	});
	makeHomeRotator();
}

/* ==== 2011 01 13 ==== */

$(function() {
	$('.article-quote-holder.has-video').find('.image').append('<span class="video-icon"><!--  --></span>');
});
