// JavaScript Document

Cufon.replace('ul.main-nav > li > a', { fontFamily: 'Heineken', hover:true });
Cufon.replace('.btn-yellow-arrow, .btn-green', { fontFamily: 'Heineken', textShadow: '0px 1px rgba(0, 0, 0, 0.4)' });
Cufon.replace('div.main-carousel-holder h2, div.locations-page h2', { fontFamily: 'HeinekenSansBlack' });
Cufon.replace('div.main-carousel-holder h3, div.main-billets-holder h2, .Heineken, div.h1-holder h1, div.programm-top-billet h2, div.training-holder h2, div.teckniek-holder h2, div.import-report h2, div.toetsen-holder h2, div.aanvragen-holder h2, div.programma-block div.block-content h2', { fontFamily: 'Heineken' });
Cufon.replace('div.h1-green-billet h1', { fontFamily: 'HeinekenSansBlack', color: '-linear-gradient(0.014=#fff, 0.015=#cfd2da, 0.24=#fbfbfc, 0.33=#d1d5dc)', textShadow: '1px 2px rgba(0, 0, 0, 0.4)' });

var homeCarouselCurrItem = 0;
var triviaCurrItem = 0;
var triviaNumber;

$(window).load(function() {
    $('.btn-yellow-arrow').append('<ins><!--  --></ins>');
    $('.btn-green').wrapInner('<span class="left-part"></span>');

	$('p.shadow-text').each(function() {
	    var text = $(this).text();
	    $(this).append('<span class="white-text">' + text + '</span>');
	    $('span.white-text', this).width($(this).width());
	});
	
	$('div.goed-getapte-page ul.main-nav > li').append('<ins class="l"><!--  --></ins><ins class="bl"><!--  --></ins><ins class="br"><!--  --></ins>').hover(
		function() {
			$(this).addClass('hover');
			$(this).find('ul').stop(true, true);
			$(this).find('ul').slideDown(150);
		},
		function() {
			$(this).removeClass('hover');
			$(this).find('ul').slideUp(70);
		}
	).each(function(i) {
	    $(this).css('z-index', (3000 - i));
	});
	
	$('div.goed-getapte-page ul.main-nav li ul').append('<li class="corner"><!--  --></li>').parent().find('ins.br, ins.bl').remove();	
	
	$('div.main-carousel-holder div.items-holder div.item:first').show();
	
	$('div.main-carousel-holder div.controls-wrapper div.item').hover(
		function() {
			if ($(this).hasClass('active')) { return };
			$('div.hover', this).stop(true, true);
			$('div.hover', this).animate({opacity:0}, 200);
		},
		function() {
			if ($(this).hasClass('active')) { return };
			$('div.hover', this).animate({opacity:0.66}, 200);
		}
	);
	$('div.main-carousel-holder div.controls-wrapper div.item').each(function(i) {
		$(this).click(function() {
			$('div.main-carousel-holder div.items-holder div.item:nth-child(' + (homeCarouselCurrItem + 1) + ')').fadeOut(300);
			$('div.main-carousel-holder div.items-holder div.item:nth-child(' + (i + 1) + ')').fadeIn(300);
			homeCarouselCurrItem = i;
			$('div.main-carousel-holder div.controls-wrapper div.active').removeClass('active').find('div.hover').animate({opacity:0.66}, 200);
			$(this).addClass('active').find('div.hover').animate({opacity:0}, 200);
			$('div.main-carousel-holder div.items-wrapper div.pointer').animate({left: (317 * homeCarouselCurrItem) + 'px'}, 300);
		});
	});
});

$.fn.delay = function(time, callback){
    jQuery.fx.step.delay = function(){};
    return this.animate({delay:1}, time, callback);
}

function showTrivia() {
	$('div.main-carousel-holder div.items-holder div.trivia-info').fadeOut(300);
	$('div.main-carousel-holder div.items-holder div.trivia-holder').fadeIn(300);
}

function closeTrivia() {
	$('div.main-carousel-holder div.items-holder div.trivia-info').fadeIn(300);
	$('div.main-carousel-holder div.items-holder div.trivia-holder').fadeOut(300);
}

function showFlashPlayer(fideoFile) {
    if (!$.trim(fideoFile)) return;
	$('div.flash-player-holder').css('background-color', '#012501').fadeIn(400, function() {
		var flashvars = {};
		flashvars._filePath = fideoFile;
		//flashvars._imagePath = '';
		flashvars._autoplay = 'true';
		var params = {};
		params.wmode = 'opaque';
		var attributes = {};
		attributes.id = 'flashPlayer';
		swfobject.embedSWF('/GGB/Content/Flashes/flvplayer.swf', 'flashPlayer', '100%', '100%', '9.0.0', false, flashvars, params, attributes);
	});
}

function closeFlashPlayer() {
    $('div.flash-player-holder').fadeOut(200, function() {
        $(this).html("<div id='flashPlayer' ><!--  --></div>" + 
                     "<a href='javascript:void(0)' class='close' onclick='closeFlashPlayer()'><!--  --></a>");
    });
}

function showTapcursus() {
    $('div.flash-player-holder').css('background-color', '#ffffff').fadeIn(400, function() {
        var flashvars = {};
        flashvars._httpPath = "/GGB/Content/Flashes/tapcursus/";
        var params = {};
        params.wmode = 'opaque';
        params.salign = "t";
        var attributes = {};
        attributes.id = 'flashPlayer';
        swfobject.embedSWF('/GGB/Content/Flashes/tapcursus/tapcursus.swf', 'flashPlayer', '100%', '100%', '9.0.0', false, flashvars, params, attributes);
    });
}
// moved to 'extras.js'
// function initTrivia() { }

function initGallery() {
    var currentPage = 0;

    var $regionsHolder = $('div.regions-holder div.regions-wrapper');
    var $thumbsHolder = $('div.gallery-holder div.thumbs');
    var $pagerHolder = $('div.gallery-holder div.pager-holder');
    var $bigImg = $('#galleryBigImg img');
    var pagesNumber = 1;

    var $pointer = $('div.regions-holder div.pointer');
    if ($('li.active', $regionsHolder).length) {
        var regionPointerY = 10 + $('li.active:last', $regionsHolder).offset().top + parseInt($('li.active:last', $regionsHolder).css('padding-top')) - $regionsHolder.offset().top;
        $pointer.css('top', regionPointerY);
    }
    
    var imgsNumber = $('li', $thumbsHolder).length;

    var $odd = $('li:odd', $thumbsHolder).clone();
    $('li:odd', $thumbsHolder).remove();
    $('ul', $thumbsHolder).append($odd);

    $('ul', $thumbsHolder).css('width', Math.ceil(imgsNumber / 2) * 81);
    if (imgsNumber > 6) {
        pagesNumber = Math.ceil(imgsNumber / 14);
        $pagerHolder.append('<ul></ul>');
        for (i = 0; i < pagesNumber; i++) {
            $('ul', $pagerHolder).append('<li><a href="javascript:void(0)" onfocus="this.blur()">' + (i + 1) + '<ins><!--  --></ins></a></li>');
        }
        $('li:nth-child(1)', $pagerHolder).addClass('active');
    }

    $('a', $pagerHolder).each(function(i) {
        $(this).click(function() {
            $('li', $pagerHolder).removeClass('active');
            $(this).parent('li').addClass('active');
            var thumbsY = -i * 546;
            currentPage = i;
            moveThumbs();
        });
    });
    
    $('li a', $thumbsHolder).click(function() {
        if ($(this).hasClass('active')) { return; }
        $('li a', $thumbsHolder).removeClass('active');
        $(this).addClass('active');
        imgSrc = $('img', this).attr('src').split('&thumb')[0];
        $bigImg.fadeOut(200, function() {
            $bigImg.attr('src', imgSrc);
        });
    });
	
    if ($('li:nth-child(1) a', $thumbsHolder).length) {
        $('li:nth-child(1) a', $thumbsHolder).addClass('active');
        var imgSrc = $('li:nth-child(1) img', $thumbsHolder).attr('src').split('&thumb')[0];
        $bigImg.load(function() {
            $(this).fadeIn(200);
            if ($.browser.msie && $.browser.version.substr(0, 1) < "8") {
                $(this).css('margin-top', (366 - $(this).height()) / 2);
            }
        }).attr('src', imgSrc);

        $('li a', $regionsHolder).click(function() {
            if ($(this).attr('accordable') != 'true') return;
            if ($(this).hasClass('active')) { return; }
            /*
            $('li a', $thumbsHolder).removeClass('active');
            $(this).addClass('active');
            imgSrc = $('img', this).attr('src').split('?')[0];
            $bigImg.fadeOut(200, function() {
            $bigImg.attr('src', imgSrc);
            });
            */
            if ($(this).parent().hasClass('opened')) {
                $('li.opened', $regionsHolder).removeClass('opened').find('ul').slideUp(200, function() {
                    regionPointerY = 10 + $('li.active:last', $regionsHolder).offset().top + parseInt($('li.active:last', $regionsHolder).css('padding-top')) - $regionsHolder.offset().top
                    $pointer.css('top', regionPointerY);
                });
            }
            else {
                $('li.opened', $regionsHolder).removeClass('opened').find('ul').slideUp(200);
                $(this).parent().addClass('opened').find('ul').slideDown(200, function() {
                    regionPointerY = 10 + $('li.active:last', $regionsHolder).offset().top + parseInt($('li.active:last', $regionsHolder).css('padding-top')) - $regionsHolder.offset().top
                    $pointer.css('top', regionPointerY);
                });
            }
            $(this).attr("href", "javascript://");
        });
    }

    function moveThumbs() {
        if (currentPage < 0) { currentPage = 0; }
        if (currentPage > pagesNumber - 1) { currentPage = pagesNumber - 1; }
        $('li', $pagerHolder).removeClass('active');
        $('li:nth-child(' + (currentPage + 1) + ')', $pagerHolder).addClass('active');
        var thumbsY = -currentPage * 546;
        $thumbsHolder.stop(true, true);
        $('ul', $thumbsHolder).animate({ left: thumbsY }, 400);
    }
}

function initAccordion() {
    $('ul.accordion a.label').click(function() {
        $(this).parent().toggleClass('active');
        $(this).next('div.description').slideToggle(200, function() {
            if ($.browser.msie) {
                $('.btn-green', $(this)).hide().show();
            }
        });
    });
}

function loadRegion(linkObj, htmlUrl) {
    $('div.locations-page div.map-area a').removeClass('active');
    $('#htmlObject').animate({ 'opacity': 0 }, 200, function() {
        $(this).load(htmlUrl, {}, function() {
            $(linkObj).addClass('active');
            $(this).animate({ 'opacity': 1 }, 200);
        });
    });
}
