// JavaScript Document

var isIE;

$(document).ready(function () {
    isIE = $.browser.msie;

    fixLayout();

    inputs();
    if ($('#ageCheck').length) { }
    else { makeHomeRotator(0); }

    makeMainMenu();
    Cufon.replace('.cufonEx', { fontFamily: 'Helvetica Neue Ex', hover: true });
    Cufon.replace('.cufonBd', { fontFamily: 'Helvetica Neue Bd', hover: true });
    Cufon.replace('.cufonMd', { fontFamily: 'Helvetica Neue Md', hover: true });
    Cufon.replace('.cufonHV', { fontFamily: 'Helvetica Neue Hv', hover: true });
    Cufon.replace('.cufonTEx', { fontFamily: 'Helvetica Neue Ex', hover: true, textShadow: '-1px -1px rgba(0, 0, 0, 0.1), -2px -2px rgba(0, 0, 0, 0.1)' });
    Cufon.replace('.cufonTBd', { fontFamily: 'Helvetica Neue Bd', hover: true, textShadow: '-1px -1px rgba(0, 0, 0, 0.1), -2px -2px rgba(0, 0, 0, 0.1)' });
    Cufon.replace('a.custom-button', { fontFamily: 'Helvetica Neue Hv', hover: true, textShadow: '0px 1px rgba(0, 0, 0, 0.3)' });
    Cufon.replace('a.custom-button-big', { fontFamily: 'Helvetica Neue Bd', hover: true, textShadow: '-1px -1px rgba(0, 0, 0, 0.3)' });
    Cufon.replace('ul.subnav li > a', { fontFamily: 'Helvetica Neue Bd', hover: true, textShadow: '-1px -1px rgba(0, 0, 0, 0.1), -2px -2px rgba(0, 0, 0, 0.1)' });
    Cufon.replace('ul.nav > li.active > a', { fontFamily: 'Helvetica Neue Bd', textShadow: '-1px -1px rgba(0, 0, 0, 0.1), -2px -2px rgba(0, 0, 0, 0.1)' });
    Cufon.replace('span.pagecontainer a', { fontFamily: 'Helvetica Neue Hv', textShadow: '0px 1px rgba(0, 0, 0, 0.3)' });
})

$(window).load(function() {
    initCustomCheckboxes();
	initCustomRadio();
    $('div.agecheck-inputs-container span input')
		.focus(function() {
		    if ($(this).parent().hasClass('invalid')) { return; }
		    $(this).parent().addClass('focus');
		})
		.blur(function() {
		    $(this).parent().removeClass('focus');
		});
});

function inputs(){
	var $default;
	$('input[type="text"]').each(function(){
		$default = $(this).data('default');
		if ($(this).val() != $default){
			$(this).addClass('focus');
			}
		$(this).focus(function(){
			$default = $(this).data('default');
			if ($(this).val() == $default){
			$(this).val('').addClass('focus');
			}
		});
		$(this).blur(function(){
			if($(this).val() == ''){
			$(this).val($default).removeClass('focus');
			}
		});
	});
	$('.agecheck-inputs-container span:eq(0) input').focus();
}	


function autoTab(input, next, len, e) {
	var isNN = (navigator.appName.indexOf("Netscape") != -1);
	var keyCode = (isNN) ? e.which : e.keyCode;
	var filter = (isNN) ? [0, 8, 9] : [0, 8, 9, 16, 17, 18, 37, 38, 39, 40, 46];
	if (input.value.length >= len && !containsElement(filter, keyCode)) {
		input.value = input.value.slice(0, len);
	if (next) {
		next.focus();
	}
	else {
		input.form[(getIndex(input) + 1) % input.form.length].focus();
	}
	}
	function containsElement(arr, ele) {
	var found = false, index = 0;
	while (!found && index < arr.length)
	if (arr[index] == ele)
	found = true;
	else
	index++;
	return found;
	}
	function getIndex(input) {
	var index = -1, i = 0, found = false;
	while (i < input.form.length && index == -1)
	if (input.form[i] == input) index = i;
	else i++;
	return index;
	}
	return true;
}


function fixLayout(){
	var $heightCustom = 0;
	var step = 0;
	var $heightH = $('html').height();
	var $heightB = $('body').height();
	if ($heightH < $heightB){step = 6;}
	if($heightH > $heightB){$heightCustom = $heightH;}
	else {$heightCustom = $heightB;}
	if($('#ageCheck').is(':visible') && $('#ageCheck').length){$heightCustom = $heightH; $('html').addClass('over');}
	if (isIE && $.browser.version==7){
	$('div.main-layout').css('height', $heightCustom + step);
	$('div#pageLayout').css('height', $heightCustom + step);
	}
	else{
	$('div.main-layout').css('height', $heightCustom);
	$('div#pageLayout').css('height', $heightCustom);	
	}
	$(window).resize(function() {
		var $heightH = $('html').outerHeight();
		var $heightB = $('body').outerHeight();
		if($heightH > $heightB){$heightCustom = $heightH;}
		else {$heightCustom = $heightB;}
		$('div.main-layout').css('height', $heightCustom);
		return
	});
}

function initCustomCheckboxes() {
	$('input.custom-checkbox:not(.switched)').each(function() {
    	var $originalInput = $(this);
		
		var $spanInput = $originalInput.parent('span.custom-checkbox-holder');
		if ($originalInput[0].checked) { $spanInput.addClass('checked').next('label').addClass('checked'); }
		
		$spanInput.click(function() {
			if ($originalInput[0].checked) {
				$spanInput.removeClass('checked').next('label').removeClass('checked');
				$originalInput[0].checked = false;
			} else {
				$spanInput.addClass('checked').next('label').addClass('checked');
				$originalInput[0].checked = true;
			}
		});
		$originalInput.addClass('switched');
		
		$spanInput.next('label').click(function() {
			if ($originalInput[0].checked) {
				$spanInput.removeClass('checked').next('label').removeClass('checked');
				$originalInput[0].checked = false;
			} else {
				$spanInput.addClass('checked').next('label').addClass('checked');
				$originalInput[0].checked = true;
			}
		});
	});
}

function initCustomRadio(){
	$('span.custom-radio input').each(function() {
		var $originalInput = $(this);
		var $spanRadio = $originalInput.parent('span');
		if ($originalInput[0].checked) { $spanRadio.addClass('checked').addClass('checked'); }
		$spanRadio.click(function(){
			if ($originalInput[0].checked) {
			} else {
			$('span.custom-radio').removeClass('checked');
			$spanRadio.addClass('checked');
			$originalInput[0].checked = true;
			}
		});
	});
	}

function onFocus(inputObj, defaultText) {
    if ($.trim($(inputObj).val()) == defaultText) {
        $(inputObj).val("");
    }
}

function onBlur(inputObj, defaultText) {
    if (($.trim($(inputObj).val()) == "") || ($(inputObj).val().toLowerCase() == defaultText.toLowerCase())) {
        $(inputObj).val(defaultText);
    }
}

function validation(){
	$('div.agecheck-form-container').disable();
}

function validationTemp(){
	$('div.agecheck-layout').disable();
}

function openSIte(){
	$('div#ageCheck').hide();
	$('div#pageLayout').show();
	makeHomeRotator(0);
}

	
function makeHomeRotator(RotItem) {
	var rotObj = new Object();
	var itemsCount = $('div#carousel-container ul li').length;
	var currentRotItem;
	var isFading = false;
	var homeRotatorTime = 3;
	var homeRotatorTimer;
	var notGo = false;
	var delayTime = 4000;
	var delayTimer;
	if (itemsCount > 1){$('span.left-button, span.right-button').addClass('active');}
	for (var i = 0; i < itemsCount; i++) {
		$('span.center-pagination').append('<span class="dot-carousel"><!--  --></span>');
	}
	$('img', 'div#carousel-container').each(function(){
	var top = $(this).data('top');
	var left = $(this).data('left');
	$(this).css('top', top).css('left', left);
	});
	$('div.blockcarousel', 'div#carousel-container').each(function(){
	var top = $(this).data('top');
	var left = $(this).data('left');
	$(this).css('top', top).css('left', left);
	});
	$('span.additional-text', 'div#carousel-container').each(function(){
	if ($(this).data('back') == 1){$(this).addClass('back');}
});
    rotate();
	$('span.dot-carousel').each(function(i) {
		$(this)
		.click(
			function() {
				notGo = false;
				if ($(this).hasClass('active')) return;
				if (isFading) return;
				isFading = true;
				$('span.center-pagination span.dot-carousel').removeClass('active');
				$(this).addClass('active');
				clearInterval(homeRotatorTimer);
				$('div#carousel-container ul li:nth-child(' + currentRotItem + ')').fadeOut(600);
				currentRotItem = i + 1;
				$('div#carousel-container ul li:nth-child(' + currentRotItem + ')').fadeIn(600, 
					function() {
						isFading = false;
					}
				);
			}
		)
	});
	if (itemsCount == 1){clearInterval(homeRotatorTimer);}
	if (RotItem == 0) {
		currentRotItem = 1; 
		$('span.center-pagination span.dot-carousel:nth-child(1)').addClass('active');
		$('div#carousel-container ul li:nth-child(1)').show();
		}
	else {
		currentRotItem = RotItem;
		$('span.center-pagination span.dot-carousel:nth-child(' + currentRotItem + ')').addClass('active');
		$('div#carousel-container ul li:nth-child(' + currentRotItem + ')').show();
		clearInterval(homeRotatorTimer);
		}
	function rotate() {
		clearInterval(homeRotatorTimer);
		homeRotatorTimer = setInterval(function() { 
		            $('div#carousel-container ul li:nth-child(' + currentRotItem + ')').stop(true, true);
    	            $('span.center-pagination span.dot-carousel').removeClass('active');
    	            $('div#carousel-container li:nth-child(' + currentRotItem + ')').fadeOut(600);
		            currentRotItem++;
		            if (currentRotItem > itemsCount) currentRotItem = 1;
		            $('span.center-pagination span.dot-carousel:nth-child(' + currentRotItem + ')').addClass('active');
		            $('div#carousel-container ul li:nth-child(' + currentRotItem + ')').fadeIn(600, 
			            function() {
				            isFading = false;
			            }
		            );
		}, delayTime);	
	};
	$('span.left-button.active').click(function() {
	clearInterval(homeRotatorTimer);
	rotate();
    if (!($('#carousel-container li').is(':animated'))){
	        notGo = false;
	        $('div#carousel-container ul li:nth-child(' + currentRotItem + ')').fadeOut(600);
	        currentRotItem = currentRotItem - 1;
	        if (currentRotItem == 0) { currentRotItem = itemsCount; }
	        $('span.center-pagination span.dot-carousel').removeClass('active');
	        $('span.center-pagination span.dot-carousel:nth-child(' + currentRotItem + ')').addClass('active');
	        $('div#carousel-container ul li:nth-child(' + currentRotItem + ')').fadeIn(600,
		function() {
		    isFading = false;
		}
		);
};
	});
	$('span.right-button.active').click(function() {
	    clearInterval(homeRotatorTimer);
           rotate();
    if (!($('#carousel-container li').is(':animated'))){
        notGo = false;
        $('div#carousel-container ul li:nth-child(' + currentRotItem + ')').fadeOut(600);
        currentRotItem = currentRotItem + 1;
        if (currentRotItem > itemsCount) { currentRotItem = 1; }
        $('span.center-pagination span.dot-carousel').removeClass('active');
        $('span.center-pagination span.dot-carousel:nth-child(' + currentRotItem + ')').addClass('active');
        $('div#carousel-container ul li:nth-child(' + currentRotItem + ')').fadeIn(600,
		    function() {
		        isFading = false;
		    }
		    );
	}
    });
	$('div.news-brief').click(function(){
		clearInterval(homeRotatorTimer);		   
	});
	$('input', 'div.news-brief').focus(function(){
		clearInterval(homeRotatorTimer);
		notGo = true;
	});
	$('span.custom-radio', 'div.news-brief').click(function(){
		clearInterval(homeRotatorTimer);
		notGo = true;
	});
	$('div#carousel-container ul li').hover(function(){
		clearInterval(homeRotatorTimer);
	},
	function(){
		if(itemsCount == 1){return}
		if(notGo == true){return}
	    rotate();
		}
	);
}

function makeMainMenu(){
	var $mainmenu = $('ul.nav');
	var $submenu = $('ul.subnav');
	var $activeMain = $('ul.nav > li.active');
	if ($activeMain.length){
		$activeMain.nextAll().addClass('pass');
		$activeMain.prevAll().addClass('pass');
		Cufon.replace('ul.nav > li.pass > a', { fontFamily: 'Helvetica Neue Bd' });
		}
	else{
		Cufon.replace('ul.nav > li > a', { fontFamily: 'Helvetica Neue Bd' });
		$('li', $mainmenu).each(function(){
			$(this).addClass('pass');
		});
		}

	$('ul.nav > li').hover(
	function(){
		if ($(this).hasClass('active')) return;
		else{
			$('ul.nav > li.active').addClass('passd');
			$(this).addClass('hover');
				Cufon.replace('ul.nav > li.pass > a', { fontFamily: 'Helvetica Neue Bd'});
				Cufon.replace('ul.nav > li.hover > a', { fontFamily: 'Helvetica Neue Bd', color: '#FFF', textShadow: '-1px -1px rgba(0, 0, 0, 0.1), -2px -2px rgba(0, 0, 0, 0.1)'  });
			}
		},
	function(){
		if ($(this).hasClass('active')) return;
			$(this).removeClass('hover');
			$('ul.nav > li.active').removeClass('passd');
			Cufon.replace('ul.nav > li.pass > a', { fontFamily: 'Helvetica Neue Bd', color: '#333333'});
		}
	);
	$('ul.subnav').hover(
	function(){
		$(this).parent('li').addClass('triger');
		},
	function(){
		$(this).parent('li').removeClass('triger');
		}
	);
}

function mailtoJ(){
	$('div.overlay, div.popup').show();
	$('a.close').click(function(){
		$('div.overlay').hide();
		$('div.popup').hide();
	});
	$('div.overlay').click(function(){
		$('div.overlay').hide();
		$('div.popup').hide();
	});
	}

