$(document).ready(function() {

    // Gallery
    $(".thumb").fancybox();

    // Footer
   // $(window).scroll(function() {
    //    $('#copyright').css('bottom', '-' + $(window).scrollTop() + 'px');
    //});

    // Reviews
    if (typeof Cms === 'undefined') {
        var reviews = $('.ContentItemAd');
        var thisReview = rand(0, reviews.length - 1);
        var updateReviews = function() {
            reviews.fadeOut(2000);
            window.setTimeout(function() {
                reviews.eq(thisReview).fadeIn(2000);
                thisReview += 1;
                if (thisReview >= reviews.length) thisReview = 0;
            }, 3000);
        }
        updateReviews();
        window.setInterval(updateReviews, 15000);
    }
    
    // Video
    $('.ContentItemDetailVideoPlayer .FormFieldFilePath').css('display', 'none');
    $('.ContentItemContentGroupVideoPlayer').before($('<div class="VideoPlayerWindow"></div>'));
    $('.ContentItemDetailVideoPlayer .FormFieldTitle a').live('click', function() {
        $('.VideoPlayerWindow').html(getVideoEmbed($(this).closest('.ContentItemVideoPlayer').find('.FilePath a').attr('href'), 198, 163));
        $('.ContentItemDetailVideoPlayer a').removeClass('active');
        $(this).addClass('active');
        return false;
    });
    $('.ContentItemDetailVideoPlayer .FormFieldTitle a').eq(0).click();
    
});

function rand(l, u) { return Math.floor((Math.random() * (u - l + 1)) + l); }

function getVideoEmbed(url, w, h) {
	var regexp = new RegExp('youtube\.com/watch.*?v=([a-zA-Z0-9\-]*)');
	var match = regexp.exec(url);
    if (match) {
        if (match.length > 0) 
            return '<iframe title="YouTube video player" width="' + w + '" height="' + h + '" src="http://www.youtube.com/embed/' + match[1] + '" frameborder="0" allowfullscreen></iframe>';
    }
	regexp = new RegExp('vimeo\.com/([a-zA-Z0-9]*)');
	match = regexp.exec(url);
    if (match) {
        if (match.length > 0) 
            return '<iframe src="http://player.vimeo.com/video/' + match[1] + '" width="' + w + '" height="' + h + '" frameborder="0"></iframe>';
    }
	return '';
}

