﻿/************************************************************************  
VARS
**************************************************************************/

/************************************************************************ 
Page Element Enums
**************************************************************************/
var SecondIINoneVideoGalleryPageElements =
{
    Video: 'video',
    DdlVideos: 'ddlVideos',
    DivVideoGallery: 'divVideoGallery'
}

/************************************************************************ 
Functions 
**************************************************************************/
function pageLoad() {
    if (_globalVideos == null) {
        getVideosUsingService('1');
    }
    else {
        onGetVideosComplete(_globalVideos)
    }
}

function getVideosUsingService(categoryId) {
    showWaitDialog(InformationMessages.VideoRetrieving);

    SecondIINoneUIService.GetVideos(categoryId, onGetVideosComplete, onSecondIINoneVideoGalleryError);
}

function onGetVideosComplete(videos) {
    var divError = $get(LayoutElements.DivError);
    hideContent(divError);

    var divVideoGallery = $get(SecondIINoneVideoGalleryPageElements.DivVideoGallery);

    if (videos.length > 0) {
        _globalVideos = videos;

        var secondIInoneVideoGalleryString = '';

        for (var secondIInoneVideoGalleryRowCount = 0; secondIInoneVideoGalleryRowCount < videos.length; secondIInoneVideoGalleryRowCount++) {

            secondIInoneVideoGalleryString = secondIInoneVideoGalleryString + '<table cellpadding="0" cellspacing="15" class="secondIInoneVideoTable">';
            secondIInoneVideoGalleryString = secondIInoneVideoGalleryString + '<tr>';

            for (var i = 0; i < 3; i++) {
                if (secondIInoneVideoGalleryRowCount < (videos.length)) {

                    secondIInoneVideoGalleryString = secondIInoneVideoGalleryString + '<td>';

                    secondIInoneVideoGalleryString = secondIInoneVideoGalleryString + '<a onclick="javascript:showSelectedVideo(\'' + videos[secondIInoneVideoGalleryRowCount].Id + '\');">';
                    secondIInoneVideoGalleryString = secondIInoneVideoGalleryString + '<img src="' + videos[secondIInoneVideoGalleryRowCount].Thumbnail + '" />';
                    secondIInoneVideoGalleryString = secondIInoneVideoGalleryString + '</a>';
                    secondIInoneVideoGalleryString = secondIInoneVideoGalleryString + '<br/>';
                    secondIInoneVideoGalleryString = secondIInoneVideoGalleryString + '<span style="color:blue;font-weight:bold;">';
                    secondIInoneVideoGalleryString = secondIInoneVideoGalleryString + '<a onclick="javascript:showSelectedVideo(\'' + videos[secondIInoneVideoGalleryRowCount].Id + '\');">';
                    secondIInoneVideoGalleryString = secondIInoneVideoGalleryString + videos[secondIInoneVideoGalleryRowCount].Title;
                    secondIInoneVideoGalleryString = secondIInoneVideoGalleryString + '</a>';
                    secondIInoneVideoGalleryString = secondIInoneVideoGalleryString + '</span>';
                    secondIInoneVideoGalleryString = secondIInoneVideoGalleryString + '<br/>';
                    secondIInoneVideoGalleryString = secondIInoneVideoGalleryString + '<span style="color:burgandy;font-weight:bold;">';
                    secondIInoneVideoGalleryString = secondIInoneVideoGalleryString + videos[secondIInoneVideoGalleryRowCount].Duration;
                    secondIInoneVideoGalleryString = secondIInoneVideoGalleryString + '</span>';
                    secondIInoneVideoGalleryString = secondIInoneVideoGalleryString + '<br/>';
                    secondIInoneVideoGalleryString = secondIInoneVideoGalleryString + 'Views: &nbsp;&nbsp;<span style="color:grey;font-weight:bold;">' + videos[secondIInoneVideoGalleryRowCount].ViewCount + '</span>';
                    secondIInoneVideoGalleryString = secondIInoneVideoGalleryString + '</td>';
                }
                else {

                    secondIInoneVideoGalleryString = secondIInoneVideoGalleryString + '<td>';
                    secondIInoneVideoGalleryString = secondIInoneVideoGalleryString + '<p style="width: 107px;">&nbsp;</p>';
                    secondIInoneVideoGalleryString = secondIInoneVideoGalleryString + '</td>';
                }

                secondIInoneVideoGalleryRowCount++
            }

            secondIInoneVideoGalleryRowCount--

            secondIInoneVideoGalleryString = secondIInoneVideoGalleryString + '</tr>';
            secondIInoneVideoGalleryString = secondIInoneVideoGalleryString + '</table>';
        }

        divVideoGallery.innerHTML = secondIInoneVideoGalleryString;

        hideWaitDialog();

    }

    performPostLoadOperations();
}

function showSelectedVideo(videoId) {
    var videoString = ''

    videoString = videoString + '<div><object width="480" height="385">';
    videoString = videoString + '<param name="movie" value="http://www.youtube.com/v/' + videoId + '?fs=1&amp;hl=en_US"></param>';
    videoString = videoString + '<param name="allowFullScreen" value="true"></param>';
    videoString = videoString + '<param name="allowscriptaccess" value="always"></param>';
    videoString = videoString + '<embed src="http://www.youtube.com/v/' + videoId + '?fs=1&amp;hl=en_US" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed>';
    videoString = videoString + '</object><p>&nbsp;</p></div>';

    var videoDiv = $get(SecondIINoneVideoGalleryPageElements.Video);
    showContent(videoDiv);

    videoDiv.innerHTML = '';
    videoDiv.innerHTML = videoString;

    globalBackgroundPopup = '#backgroundVideoPopup';
    globalPopup = '#videoPopup';
    globalPopupClose = '#videoPopupClose'

    hideWaitDialog();

    //centering with css
    centerPopup();
    //load popup
    loadJQueryPopup();
}

function onSecondIINoneVideoGalleryError(result) {
    var problems = new Array();
    problems[problems.length] = result.get_message();
    setErrorInformation($get(LayoutElements.DivError), problems);

    hideWaitDialog();
}


