﻿// Global variables
var FILMS_CYCLE_PERIOD_IN_S = 5;
var timerFilms = null;
var filmIds = null;
var enums = new Enum();

var screenAd = null;
var selectedId = null;
var selectedType = null;

// Get Site Uri
var siteUrl = document.URL;
siteUrl = siteUrl.substr(0, siteUrl.lastIndexOf("/") + 1);

// player -> provides an interface to the flash player
var player = null;

function Enum()
    {
        this.Video = new function() {
            this.Clip = 0;
            this.Film = 1;
            this.Advert = 2;
            this.ScreenAdvert = 3;
        }    
    }

function OnLoad() {

    // Flow Player
    if ($("PlayerDiv") != null) {
        InitializeFlowPlayer();
    }

    // Selected video
    if ($("SelectedVideoId") != null && $("SelectedVideoId").value != null) {
        selectedId = $("SelectedVideoId").value;
        selectedType = enums.Video.Clip;
    }
    
    // FilmIds
    if ($("FilmDiv") != null)
    {
        var images = $("FilmDiv").getElementsByTagName("img");
        filmIds = new Array();
        $A(images).each(function(img) {
            if (img.id != null)
            {
                filmIds.push(img.id.substr(10, img.id.length));
            }
        }); 
        // Start displaying films   
        if (filmIds != null && filmIds.length > 0) {
            // Start cycling films if there is no specific clip to view
            if (selectedId == null || selectedId == 0) {
                CycleFilms(0);
            }      
        }
    }
    // Highlite main menu item  
    var page = document.URL.substr(document.URL.lastIndexOf("/") + 1, document.URL.length);
    if (page.length == 0 || page == "default.aspx")
    {
        $("HomeLi").getElementsByTagName("a")[0].className = "selected";;
    }
    else
    {
        if (page == "contact.aspx")
        {
            $("ContactLi").getElementsByTagName("a")[0].className = "selected";
        }
        else if (page == "about.aspx")
        {
            $("AboutLi").getElementsByTagName("a")[0].className = "selected";
        }
    }

    // Columns height
    var contentHeight = $("ContentDiv").offsetHeight;
    var rightHeight = $("RigthColumn").offsetHeight;
    if (contentHeight < rightHeight) {
        $("ContentDiv").style.height = rightHeight + "px";
    } 
}

function InitializeFlowPlayer() {
    flowplayer("FlashPlayerDiv", { src: "Content/flash/flowplayer-3.1.2.swf", wmode: 'opaque' }, {
        clip:
                {
                    baseUrl: siteUrl,
                    autoPlay: true,
                    autoBuffering: true,
                    bufferLength: 4,
                    duration: null,
                    scaling: 'fit',
                    onStart: function(clip) {

                        // resize the screen based on the custom clip properties 
                        this.getScreen().animate({ width: clip.w, height: clip.h }, 3000);
                    }
                },
        /* Event handlers */
        onPlaylistReplace: function() {
            $("SmallLogoDiv").show();
            if (selectedType == enums.Video.Clip || selectedType == enums.Video.Film) {
                setTimeout("ShowAdvertOnScreen()", 1000);
            }
        },
        onVolume: function(level) {
        },
        onFinish: function() {
            this.unload();
        },
        onMouseOver: function() {
        },
        onMouseOut: function() {
        },
        /* Appearance */
        plugins:
                {
                    controls:
                    {
                        tooltips:
                        {
                            buttons: true,
                            pause: null,
                            play: 'Přehrát',
                            pause: null,
                            mute: 'Vypnout zvuk',
                            unmute: 'Zapnout zvuk',
                            fullscreen: 'Celá obrazovka',
                            fullscreenExit: 'Zpět do okna'
                        },
                        tooltipColor: '#112233',
                        tooltipTextColor: '#8899ff'
                    }
                }
    });
}



// JW player - When the browser has finished executing the creating script, the player calls this function
function playerReady(thePlayer) {
    player = window.document[thePlayer.id];
    //player.addControllerListener("LOAD", "PlayListener");
    player.sendEvent("PLAY", "true");
    $("SmallLogoDiv").show();
    
    // Show advert
    if (selectedType == enums.Video.Clip || selectedType == enums.Video.Film) {
        setTimeout("ShowAdvertOnScreen()", 1000);
    }
}

function PlayClip()
{
    StopCyclingFilms();
    $("SplashDiv").hide(); // hides a splash screen
    var format;
    var videoName;
    var videoDirUri;
    id = selectedId;
    type = selectedType;
    
    if (type == enums.Video.Film)
    {
        format = $("FilmFormat" + id).value;
        videoDirUri = "DB/Films/";
        videoName = id + ".flv";
    }
    else if (type == enums.Video.Clip)
    {
        format = $("VideoFormat" + id).value;
        videoDirUri = "DB/Videos/";
        videoName = id + ".flv";
    }
    else if (type == enums.Video.Advert)
    {
        format = $("AdvertFormat" + id).value;
        videoDirUri = "DB/Adverts/";
        videoName = id + ".flv";
    }
    else if (type == enums.Video.ScreenAdvert) {
        format = screenAd.Format;
        videoDirUri = "DB/Adverts/";
        videoName = screenAd.Id + ".flv";
    }
       
    var w = null;
    if (format == "f4x3")
    {
        w = 375;
    }
    else 
    {
        w = 500;
    }

    $("FlashPlayerDiv").show();
    flowplayer().show();
    flowplayer().play(videoDirUri + videoName);
    
//    var so = new SWFObject('Content/flash/player.swf','myplayer','500','305','9');
//    so.addParam('allowscriptaccess','always');
//    so.addParam('allowfullscreen', 'true');
//    so.addParam('menu', 'false');
//    //so.addParam('autostart', 'true');
//    so.addVariable('autostart', 'true');
//    so.addParam('bgcolor', '#000000');
//    so.addParam('flashvars', '&file=' + videoUri);
//    so.addParam("wmode", "opaque");
//    so.write('FlashPlayerDiv');    
}     


function ShowAdvertOnScreen()
{
    function GetRandomAdvertByAjaxCall() {
        PageMethods.GetRandomAdvert(GetRandomAdvert_OnSuccess, GetRandomAdvert_OnFail);
    }

    function GetRandomAdvert_OnSuccess(ad) {
        if (ad != null) {
            screenAd = ad;
            $("AdvertSplashDiv").show();
            $("AdvertImg").innerHTML = "<img src='DB/Images/Advert/thumb" + ad.Id + ".jpg' />";
            $("AdvertText").innerHTML = "<h2>" + ad.Title + (ad.Loc.length > 0 ? ", " + ad.Loc : "") + "</h2>";
            if (ad.Desc.length > 0) {
                $("AdvertText").innerHTML += ad.Desc;
            }
        }
    }

    function GetRandomAdvert_OnFail(error) {
    }

    GetRandomAdvertByAjaxCall(); 
}


// Selects a clip - changes the style of a video item and shows a splash screen
function SelectClip(id) {
    CloseAdvertBox();
    DeselectVideo(selectedId);
    selectedId = id;
    selectedType = enums.Video.Clip;
    SelectVideo(id);

    if ($("FlashPlayerDiv") != null) {
        flowplayer().close();
        flowplayer().hide();
        //$("FlashPlayerDiv").hide();
  
        var title = $("VideoTitle" + id).value;
        var desc = $("VideoDesc" + id).value;
        var imgPath = "DB/Images/Video/" + id + ".jpg";
        var format = $("VideoFormat" + id).value;
        var date = $("VideoDate" + id).value;
        var location = $("VideoLocation" + id).value;
        
        $("SplashDiv").show(); // shows a splash screen
        $("SplashImageDiv").innerHTML = "<img src='" + imgPath + "' class='" + format + "' />";
        var splashInfo = "<h2>" + title + "</h2>" + date;
        if (location.length > 0)
        {
            splashInfo += ", ";
        }
        splashInfo += location + "<br />" + 
                      desc;       
        $("SplashInfoDiv").innerHTML = splashInfo;
        
        $("SplashDiv").show();
        $("SelectedVideoId").value = id;  
    }  
}

// Selects a film - changes the style of a video item and shows a splash screen
function SelectFilm(id) {
    CloseAdvertBox();
    DeselectVideo(selectedId);    
    selectedId = id;
    selectedType = enums.Video.Film;
    SelectVideo(id);

    if ($("FlashPlayerDiv") != null) {
        flowplayer().close();
        flowplayer().hide();
        //$("FlashPlayerDiv").hide();
  
        var title = $("FilmTitle" + id).value;
        var desc = $("FilmDesc" + id).value;
        var imgPath = "DB/Images/Film/" + id + ".jpg";
        var format = $("FilmFormat" + id).value;
        var date = $("FilmDate" + id).value;
        var location = $("FilmLocation" + id).value;
        
        $("SplashDiv").show(); // shows a splash screen
        $("SplashImageDiv").innerHTML = "<img src='" + imgPath + "' class='" + format + "' />";
        var splashInfo = "<h2>" + title + "</h2>" + date;
        if (location.length > 0)
        {
            splashInfo += ", ";
        }
        splashInfo += location + "<br />" + 
                      desc;       
        $("SplashInfoDiv").innerHTML = splashInfo;
        
        $("SplashDiv").show();
        $("SelectedFilmId").value = id;  
        //ActivateFilmBtn(id);
    }  
}

// Selects a advert - changes the style of a video item (selects it) and shows a splash screen
function SelectAdvert(id) {
    CloseAdvertBox();
    DeselectVideo(selectedId);
    selectedId = id;
    selectedType = enums.Video.Advert;
    SelectVideo(id);

    if ($("FlashPlayerDiv") != null) {
        flowplayer().close();
        flowplayer().hide();
        //$("FlashPlayerDiv").hide();
  
        var title = $("AdvertTitle" + id).value;
        var desc = $("AdvertDesc" + id).value;
        var imgPath = "DB/Images/Advert/" + id + ".jpg";
        var format = $("AdvertFormat" + id).value;
        var date = $("AdvertDate" + id).value;
        var location = $("AdvertLocation" + id).value;
        
        $("SplashDiv").show(); // shows a splash screen
        $("SplashImageDiv").innerHTML = "<img src='" + imgPath + "' class='" + format + "' />";
        var splashInfo = "<h2>" + title + "</h2>" + date;
        if (location.length > 0)
        {
            splashInfo += ", ";
        }
        splashInfo += location + "<br />" + 
                      desc;       
        $("SplashInfoDiv").innerHTML = splashInfo;
        
        $("SplashDiv").show();
        $("SelectedVideoId").value = id;  
    }  
}

// Changes the css style of a video item (clip and also film)
function SelectVideo(id)
{
    if (selectedType == enums.Video.Clip)
    {
        $("ClipDiv" + id).addClassName("selected");    
    }
    else if (selectedType == enums.Video.Film)
    {
        $("PlayBtnImg" + id).src = "Content/images/playbtn_selected110x46.png";
    }
    else if (selectedType == enums.Video.Advert)
    {
        $("AdvertDiv" + id).addClassName("selected");    
    }
}

// Changes the css style of a video item (clip and also film)
function DeselectVideo(id)
{
    if (selectedType == enums.Video.Clip)
    {
        // If you select a clip (archiv) and than choose a location -> the clip is not on the page if it does not belong to the location
        if ($("ClipDiv" + id) != null)
        {
            $("ClipDiv" + id).removeClassName("selected");    
        }    
    }
    else if (selectedType == enums.Video.Film)
    {
        $("PlayBtnImg" + id).src = "Content/images/playbtn110x46.png";
    }
    else if (selectedType == enums.Video.Advert)
    {
        if ($("AdvertDiv" + id) != null)
        {
            $("AdvertDiv" + id).removeClassName("selected");    
        } 
    }
}

// Clip Icon (video item) - onclick
function ClipIcon_OnClick(id) {
    $("SmallLogoDiv").hide();
    StopCyclingFilms();
    SelectClip(id);
}

// Film Icon (video item) - onclick
function PlayBtn_OnClick(id) {
    $("SmallLogoDiv").hide();
    StopCyclingFilms();
    SelectFilm(id);
}

// Clip Icon (video item) - onclick
function AdvertIcon_OnClick(id) {
    $("SmallLogoDiv").hide();
    StopCyclingFilms();
    SelectAdvert(id);
}

// Cycles films (Repeats displaying films)
function CycleFilms(i)
{
    if (i == filmIds.length) {
        i = 0;
    }
    var filmId = filmIds[i]; 
    SelectFilm(filmId);
    i++;
    timerFilms = setTimeout("CycleFilms(" +  i + ")", FILMS_CYCLE_PERIOD_IN_S * 1000);
}

// Stops cycling (repeating) films
function StopCyclingFilms()
{
    clearTimeout(timerFilms);
}

function openWin(url){
    player=window.open(url,'player','width=300,height=100,left=0,top=0');
}

function CloseAdvertBox() {
    //Effect.SlideUp('AdvertSplashDiv');
    $("AdvertSplashDiv").hide();
}

function PlayAdvert() {
    CloseAdvertBox();
    DeselectVideo(selectedId);
    selectedType = enums.Video.ScreenAdvert;
    PlayClip();    
}

/*** HELPERS
*********************************************************************************************************************************/ 


/*** Class Extensions
/**********************/
// Date
// Converts a date to its string representation
Date.prototype.toMyString = function()
{
    var strDate = this.getDate() + "/" + this.getMonth() + "/" + this.getFullYear();
    return strDate;
}

function ArchivSortBtn_OnClick(el)
{
    var nodes = $("ArchivSortDiv").childNodes;
    // Erase css class of all link btns
    $A(nodes).each(function(node)
        {
            if (node.tagName != null && node.tagName.toLowerCase() == "a")
            {
                node.className = "";
            }
        });
    el.className = "selected";

}
