﻿window.addEvent('domready', function() {
    /*
    if (typeof sIFR == "function") {
        sIFR.replaceElement(named({ sSelector: ".HomepageContentShowtimesHeaderTitle", sFlashSrc: "Flash/tradegothic.swf", sColor: "#ffffff", sLinkColor: "#ffffff", sBgColor: "#FFFFFF", sHoverColor: "#ffffff", sCase: "upper", sWmode: "transparent" }));
    }
    */

    /*
    $('HomepageContentShowtimesHeaderSelect').set('tween', { duration: 'short' });
    $('HomepageContentShowtimesHeaderSelect').addEvent('mouseenter', function() {
        $('HomepageContentShowtimesHeaderSelect').tween('height', 60);
    });
    $('HomepageContentShowtimesHeaderSelect').addEvent('mouseleave', function() {
        $('HomepageContentShowtimesHeaderSelect').tween('height', 0);
    });
    */
    
    $$('.HomepageOneClipListLinkOff').each(function(element) {
        element.addEvent('mouseenter', function() {
            element.set('styles', { 'color': '#ffffff', 'background-color': '#265c80' });
        });
        element.addEvent('mouseleave', function() {
            element.set('styles', { 'color': '#494e51', 'background-color': '#b5c2ca' });
        });
    });
});

function changeClass(element) {
    //$(element).set('tween', { duration: 5000 });
    //$(element).tween('background-color', '#265c80', '#b5c2ca');
}

function enableScrollButton(buttonName) {
    var scrollButton = $(buttonName)
    //scrollButton.setStyle('background-position', 'right top');
    scrollButton.setStyle('cursor', 'pointer');
}

function disableScrollButton(buttonName) {
    var scrollButton = $(buttonName)
    //scrollButton.setStyle('background-position', 'left top');
    scrollButton.setStyle('cursor', 'default');
}

function setToNowPlaying(item) {
    var playlistItem = $('PlaylistItem' + item);
    playlistItem.className = 'HomepagePlaylistItemNow';
    playlistItem.set('clipState', '1');
    document.getElementById('HomepagePlaylistItemType' + item).innerHTML = "NOW PLAYING";
}

function setToUpNext(item) {
    var playlistItem = $('PlaylistItem' + item);
    playlistItem.className = 'HomepagePlaylistItemNext';
    playlistItem.set('clipStatus', '0');
    document.getElementById('HomepagePlaylistItemType' + item).innerHTML = "UP NEXT";
}

function nextPlaylistItem() {
    if (homepagePlaylistCurrent < (homepagePlaylistCount - 1)) {
        var leftValue = $('HomepagePlaylistDisplayItemsContainer').getStyle('left').toInt();
        playlistTweenFxNext.start('left', leftValue, leftValue - 258);
        //$('HomepagePlaylistDisplayItemsContainer').tween('left', [leftValue, leftValue - 258]);       
    }
}

function advancePlaylistCounter() {
    homepagePlaylistCurrent++;
    setPlaylistNavigationButtons();
}

function previousPlaylistItem() {
    if (homepagePlaylistCurrent > 0) {
        var leftValue = $('HomepagePlaylistDisplayItemsContainer').getStyle('left').toInt();
        playlistTweenFxPrevious.start('left', leftValue, leftValue + 258);
        //$('HomepagePlaylistDisplayItemsContainer').tween('left', [leftValue, leftValue + 258]);
    }
}

function recedePlaylistCounter() {
    homepagePlaylistCurrent--;
    setPlaylistNavigationButtons();
}

function setPlaylistNavigationButtons() {
    if (homepagePlaylistCurrent > 0) {
        enableScrollButton('HomepagePlaylistPrevious');
    }
    else {
        disableScrollButton('HomepagePlaylistPrevious');
    }
    if (homepagePlaylistCurrent < (homepagePlaylistCount - 1)) {
        enableScrollButton('HomepagePlaylistNext');
    }
    else {
        disableScrollButton('HomepagePlaylistNext');
    }
}

/*
function playlistPlayClip(itemNum, clipId, clipTitle) {
    if (itemNum != homepagePlaylistNowPlaying) {
        alert(itemNum - homepagePlaylistNowPlaying);
        Playlist.GetInstance().MovePlayhead(itemNum - homepagePlaylistNowPlaying);
        //Playlist.GetInstance().Play(new Video({ ClipId: clipId, Format: 'FLV', Title: clipTitle }));
        setToNowPlaying(itemNum);
        setToUpNext(homepagePlaylistNowPlaying);
        homepagePlaylistNowPlaying = itemNum;
    }
}
*/

function playlistPlayClip(itemNum, clipId, clipTitle) {
    if (itemNum != homepagePlaylistNowPlaying) {
        /*
        if (Playlist.GetInstance().Current != null) {
        alert("Moving playhead by " + (itemNum - homepagePlaylistNowPlaying));
        Playlist.GetInstance().MovePlayhead(itemNum - homepagePlaylistNowPlaying);
        }
        else {
        Playlist.GetInstance().Play(new Video({ ClipId: clipId, Format: 'FLV', Title: clipTitle }));
        }
        */
        Playlist.GetInstance().Play(new Video({ ClipId: clipId, Format: 'FLV', Title: clipTitle }));
        setToUpNext(homepagePlaylistNowPlaying);
        setToNowPlaying(homepagePlaylistCurrent);
        homepagePlaylistNowPlaying = homepagePlaylistCurrent;
    }
}

function playlistOnClipEnded() {
    Player.EndClip();
    Playlist.GetInstance().Next();
    var newLocation = getPlaylistLocationById(Playlist.GetInstance().Current.ClipId);
    if (newLocation != -1) {
        setToUpNext(homepagePlaylistNowPlaying);
        var leftValue = $('HomepagePlaylistDisplayItemsContainer').getStyle('left').toInt();
        //alert("Setting offset");
        var offset = (newLocation - homepagePlaylistCurrent) * 258;
        //alert("Offset set: " + offset);
        $('HomepagePlaylistDisplayItemsContainer').tween('left', [leftValue, leftValue - offset]);
        homepagePlaylistCurrent = homepagePlaylistNowPlaying = newLocation;
        setToNowPlaying(homepagePlaylistNowPlaying);
        setPlaylistNavigationButtons();
    }
}

function getPlaylistLocationById(clipId) {
    //alert("Finding location for " + clipId);
    for (var i in playlistArray) {
        if (i == clipId) {
            //alert("Found: " + playlistArray[i]);
            return playlistArray[i];
            break;
        }
    }
    //alert("Not found");
    return -1;
}

/*
function playlistOnClipEnded() {
    if (!Playlist.GetInstance().Current.IsAd && homepagePlaylistNowPlaying < (homepagePlaylistCount - 1)) {
        setToUpNext(homepagePlaylistNowPlaying);
        homepagePlaylistNowPlaying++;
        setToNowPlaying(homepagePlaylistNowPlaying);
        var leftValue = $('HomepagePlaylistDisplayItemsContainer').getStyle('left').toInt();
        var offset = (homepagePlaylistNowPlaying - homepagePlaylistCurrent) * 258;
        $('HomepagePlaylistDisplayItemsContainer').tween('left', [leftValue, leftValue - offset]);
        homepagePlaylistCurrent = homepagePlaylistNowPlaying;
        setPlaylistNavigationButtons();
    }
    //last clip end
    else if (!Playlist.GetInstance().Current.IsAd && homepagePlaylistNowPlaying == (homepagePlaylistCount - 1)) {
        setToUpNext(homepagePlaylistNowPlaying);
        homepagePlaylistNowPlaying = 0;
        setToNowPlaying(homepagePlaylistNowPlaying);
        var leftValue = $('HomepagePlaylistDisplayItemsContainer').getStyle('left').toInt();
        $('HomepagePlaylistDisplayItemsContainer').tween('left', [leftValue, 0]);
        homepagePlaylistCurrent = 0;
        setPlaylistNavigationButtons();
    }
    Player.EndClip();
    Playlist.GetInstance().Next();
}
*/

function playerEndClip() {
    if (Playlist.GetInstance().Current != null) {
        if (getPlaylistLocationById(Playlist.GetInstance().Current.ClipId) == (homepagePlaylistCount - 1)) {
            setToUpNext(homepagePlaylistNowPlaying);
            var leftValue = $('HomepagePlaylistDisplayItemsContainer').getStyle('left').toInt();
            $('HomepagePlaylistDisplayItemsContainer').tween('left', [leftValue, 0]);
            setToNowPlaying(0);
            homepagePlaylistNowPlaying = homepagePlaylistCurrent = 0;
            setPlaylistNavigationButtons();
        }
    }

    var _Playlist = Playlist.GetInstance();

    if (_Playlist.Current) {
        if (Player.GetInstance().Controller && Player.GetInstance().Controller.Loading) {
            Player.GetInstance().Controller.Loading();
        }

        Metrics.EndedClip(_Playlist.Current);

        if (_Playlist.Current.OnEnd) {
            _Playlist.Current.OnEnd();
        }

        //  _Playlist.Current = null;
    }
}
