﻿
var lockmovement = 0;
var previtemnr = 1;
var gotoitemnr = 1;
var panelpos = 0;
var trailerplaying = 0;
var timer = 0;
var timerruns = 0;
var noactivitytimer = 0;
var preloading = 0;
var coverimages = new Array();
var coverimagesloaded = new Array();
var itemscount = 0;
var ipadoriphone = 0;

if (navigator.userAgent.match(/iPad/i) != null) ipadoriphone = 1;
if (navigator.userAgent.match(/iPhone/i) != null) ipadoriphone = 1;

function swapCovers() {

    var $activecover = $('#mrotator-cover' + previtemnr);
    var $nextcover = $('#mrotator-cover' + gotoitemnr);

    if (trailerplaying) {
        $activecover.hide();
        removeTrailer();
    }
    else {
        $activecover.fadeOut(500, 'swing');
    }
    $nextcover.fadeIn(500, 'swing', function () {
        lockmovement = 0;
    });
}

function openPanel(nr) {

    if (nr != undefined) setDetailPanelContent(nr);

    $('.mrotator-detailpanel').delay(150).animate({
        left: '20px'
    }, 350, 'swing', function () {
        panelpos = 1;
    });
}

function closePanel() {
    $('.mrotator-detailpanel').animate({
        left: '-220px'
    }, 150, 'swing', function () {
        panelpos = 0;
    });
}

function closeOpenPanel(nr) {
    $('.mrotator-detailpanel').animate({
        left: '-220px'
    }, 150, 'swing', function () {

        setDetailPanelContent(nr);

        $('.mrotator-detailpanel').delay(100).animate({
            left: '20px'
        }, 250, 'swing', function () {
            panelpos = 1;
        });
    });
}

function setDetailPanelContent(nr) {
    $('.mrotator-detailpanelcontent-holder div').each(function () {
        if ($(this).attr('id').replace('mrotator-detailpanelcontent', '') == nr) {
            $(this).show();
        }
        else {
            $(this).hide();
        }
    });
}

function removeTrailer() {
    lockmovement = 1;
    $('#mrotator-stage-trailer-holder').fadeOut(500, 'swing', function () {
        jwplayer("mrotator-stage-trailer").stop();
        jwplayer("mrotator-stage-trailer").remove();
        trailerplaying = 0;
        lockmovement = 0;
    });
}

function rotate() {

    if (lockmovement == 0) {

        lockmovement = 1;

        previtemnr = gotoitemnr;
        gotoitemnr++;
        if (gotoitemnr == itemscount + 1) gotoitemnr = 1;

        closePanel();

        moveselectbar(previtemnr, gotoitemnr);

        if (coverimagesloaded[gotoitemnr - 1] == 1) {
            swapCovers();
        }
        else {
            preloading = 1;
            $('<img />')
				.attr('src', coverimages[gotoitemnr - 1] + '?' + Math.random() * 99999)
				.attr('border', 0)
				.load(function () {
				    $('#mrotator-cover' + gotoitemnr).append($('<a />').attr('href', $('#mrotator-detailpanelcontent' + gotoitemnr).find('.mrotator-detailpanelcontent-watchnow').attr('href')));
				    $('#mrotator-cover' + gotoitemnr + ' a').append($(this));
				    swapCovers();
				    coverimagesloaded[gotoitemnr - 1] = 1;
				    preloading = 0;
				});
        }
    }
}

function moveselectbar(prev, next) {

    setTimeout(function () {
        $('#title' + prev + ' a').removeClass('active');
    }, 150);

    setTimeout(function () {
        $('#title' + next + ' a').addClass('active');
    }, 400);

    $('.mrotator-btn-movie-select a').attr('href', '#');

    $('.mrotator-btn-movie-select').animate({
        top: $('#title' + next).position().top + selecttopmargin + 'px'
    }, 500, function () {

        if (ipadoriphone == 0) {
            $('.mrotator-btn-movie-select a').attr('href', $('#mrotator-detailpanelcontent' + gotoitemnr).find('.mrotator-detailpanelcontent-watchnow').attr('href'));
        }
    });
}

function changeitem(nr) {

    if (lockmovement == 0 && gotoitemnr != nr) {

        lockmovement = 1;

        previtemnr = gotoitemnr;
        gotoitemnr = nr;

        moveselectbar(previtemnr, gotoitemnr);

        if (coverimagesloaded[gotoitemnr - 1] == 1) {
            swapCovers();

            if (panelpos == 1) closeOpenPanel(gotoitemnr);
            else openPanel(gotoitemnr);
        }
        else {
            preloading = 1;

            $('<img />')
				.attr('src', coverimages[gotoitemnr - 1] + '?' + Math.random() * 99999)
				.attr('border', 0)
				.load(function () {

				    $('#mrotator-cover' + gotoitemnr).append($('<a />').attr('style', 'outline:none').attr('href', $('#mrotator-detailpanelcontent' + gotoitemnr).find('.mrotator-detailpanelcontent-watchnow').attr('href')));
				    $('#mrotator-cover' + gotoitemnr + ' a').append($(this));

				    swapCovers();

				    if (panelpos == 1) closeOpenPanel(gotoitemnr);
				    else openPanel(gotoitemnr);

				    coverimagesloaded[gotoitemnr - 1] = 1;
				    preloading = 0;
				});
        }
    }
    else {

        if (lockmovement == 0) {

            if (gotoitemnr != nr) {

                if (panelpos == 1) closeOpenPanel(nr);
                else openPanel();
            }
            else {
                if (panelpos == 0) openPanel();
            }
        }
    }

}

function setTimer() {
    timerruns = 1;
    timer = setInterval(function () {
        if (trailerplaying == 0 && preloading == 0) rotate();
    }, 5000);
}

function stopTimer() {
    timerruns = 0;
    timer = clearInterval(timer);

    noactivitytimer = setInterval(function () {

        if (trailerplaying == 0 && timerruns == 0 && preloading == 0) {
            noactivitytimer = clearInterval(noactivitytimer);
            setTimer();
        }

    }, 10000);
}

$(document).ready(function () {

    var optclass = ' class="active"';
    var movietitle = '';
    var movieurl = '';
    var title_maxcharlength = 25;
    var detail_synopsis_maxcharlength = 135;


    // fill data
    $('.mrotator-detailpanelcontent-holder div').each(function () {

        itemscount++;
        movieurl = $(this).find('.mrotator-detailpanelcontent-watchnow').attr('href');

        $('.mrotator-btn-movie-bg').append('<li></li>');
        $('.mrotator-stage').append('<div id="mrotator-cover' + itemscount + '"></div>');

        // check title length
        movietitle = $.trim($(this).find('.mrotator-detailpanelcontent-title').html());


        if (movietitle.length > title_maxcharlength) {
            movietitle = movietitle.slice(0, title_maxcharlength);
            movietitle = movietitle.slice(0, movietitle.lastIndexOf(' '));
            movietitle = movietitle + '...';
        }
        $(this).find('.mrotator-detailpanelcontent-title').html(movietitle);

        if (itemscount > 1) optclass = '';
        $('.mrotator-btn-movie-title').append('<li id="title' + itemscount + '"><a href="' + movieurl + '"' + optclass + '>' + movietitle + '</a></li>');

        // check synopsis length
        movietitle = $.trim($(this).find('.mrotator-detailpanelcontent-synopsis').html());
        if (movietitle.length > detail_synopsis_maxcharlength) {
            movietitle = movietitle.slice(0, detail_synopsis_maxcharlength);
            movietitle = movietitle.slice(0, movietitle.lastIndexOf(' '));
            movietitle = movietitle + '...';
        }
        $(this).find('.mrotator-detailpanelcontent-synopsis').html(movietitle);

        // add to cover arrays
        coverimages.push($(this).find('.mrotator-detailpanelcontent-coverurl').html());
        coverimagesloaded.push(0);
    });


    selecttopmargin = $('.mrotator-btn-movie-select').position().top;

    // add click events to menuitems
    $('.mrotator-btn-movie-title li').each(function () {

        if (ipadoriphone == 1) {

            $(this).click(function () {
                stopTimer();
                changeitem($(this).attr('id').replace('title', ''));
                return false;
            });
        }
        else {

            hiConfig = {
                sensitivity: 3,
                interval: 100,
                timeout: 100,
                over: function () {
                    stopTimer();
                    changeitem($(this).attr('id').replace('title', ''));
                    return false;
                },
                out: function () {

                }
            }
            $(this).hoverIntent(hiConfig);
        }

    });

    // close panel button
    $('.mrotator-btnclosepanel').click(function () {
        closePanel();
        return false;
    });

    // trailer button
    $('.btn-icon-trailer').click(function () {

        trailerplaying = 1;

        var url = $(this).parent().find('.mrotator-detailpanelcontent-trailerurl').text();

        // setup jw player
        jwplayer("mrotator-stage-trailer").setup({
            autostart: true,
            flashplayer: "jwplayer/player.swf",
            file: url,
            height: 276,
            width: 578,
            provider: "video",
            stretching: "exactfit",
            skin: "jwplayer/beelden.zip"
        });

        $('#mrotator-stage-trailer-holder').show();

        jwplayer("mrotator-stage-trailer").onComplete(function () {
            jwplayer("mrotator-stage-trailer").stop();
            $('#mrotator-stage-trailer-holder').hide();
            trailerplaying = 0;
        });

        return false;
    });

    // preload first cover and start timer
    $('<img />')
		.attr('src', coverimages[0] + '?' + Math.random() * 99999)
		.attr('border', 0)
		.load(function () {
		    $('#mrotator-cover1').append($('<a />').attr('style','outline:none').attr('href', $('#mrotator-detailpanelcontent1').find('.mrotator-detailpanelcontent-watchnow').attr('href')));
		    $('#mrotator-cover1 a').append($(this));
		    coverimagesloaded[0] = 1;
		    $('#mrotator-cover1').show();
		    $('#mrotator-detailpanelcontent1').show();
		    setTimer();
		});

    $('#mrotator-detailpanel-holder-click').attr('href', $('#mrotator-detailpanelcontent1').find('.mrotator-detailpanelcontent-watchnow').attr('href'));

});
