﻿var activePopup = null;

function openPopup(cfg) {

    if (activePopup != null) closePopup();

    var defaults = {

        Title: '',
        Url: null,
        Html: null,
        Modal: false,
        Draggable: false,
        Resizable: true,
        Maximize: false,
        Center: true,
        Width: null,
        Height: null,
        OnClose: null,
        OnLoad: null
    };

    cfg = $.extend(defaults, cfg);

    activePopup = $.telerik.window.create({
        title: cfg.Title,
        html: (cfg.Html) ? cfg.Html : '<iframe src="' + cfg.Url + '" frameborder="0" style="width:100%;height:100%"></iframe>',
        modal: cfg.Modal,
        resizable: cfg.Resizable,
        draggable: cfg.Draggable,
        width: cfg.Width,
        height: cfg.Height,
        onClose: function () {

            var data = $(this).data('tWindow');

            if (data.reloadOnClose) {
                window.location.reload();
            }

            if (cfg.OnClose != null) {
                cfg.OnClose($(this));
            }
        },
        onLoad: function () {

            if (cfg.Center) {
                $(this).data('tWindow').center();
            }

            if (cfg.Maximize) {
                $(this).data('tWindow').maximize();
            }

            if (cfg.OnLoad != null) {
                cfg.OnLoad($(this));
            }
        }
    });
}

function closePopup() {
    activePopup.data('tWindow').close();
    activePopup = null;
}

function refreshOnPopupClose() {
    activePopup.data('tWindow').reloadOnClose = true;
}

function redirectOnPopupClose(id) {
    activePopup.data('tWindow').newItemId = id;
    activePopup.data('tWindow').redirectOnClose = true;
}

function hideCartSummary() {

    $('.cart_summary').hide();
    $('.menu').removeClass('push_to_top')
}

function updateCartSummary(itemNum, total, productQuantityUpdated, actionTrigger) {

    if (itemNum < 1) {
        hideCartSummary();
        return;
    }

    var text = itemNum + ' item';
    if (itemNum > 1) text += 's';
    text += ', ' + total;

    $('.cart_summary .cart_link').text(text);
    $('.cart_summary').show();

    var menu = $('.menu');

    if (menu.hasClass('push_to_top') == false) {

        menu.addClass('push_to_top');
    }
}

function updateClock() {

    var date = new Date();
    var hours = date.getUTCHours();
    var minutes = date.getUTCMinutes();

    if (hours < 10) hours = "0" + hours;
    if (minutes < 10) minutes = "0" + minutes;
    $('#time_label').text(hours + ":" + minutes + " GMT");
    
    setTimeout("updateClock()", 60000)
}

function downloadFile(path) {
    console.log('downloading: ' + path);
    document.location.href = path;
}

function launchAudioPlayer(name, path) {

    var windowCfg = {
        Title: 'Now playing ' + name,
        Html: '<div id="audio" style="display:block;width:450px;height:30px;" href="' + path + '"></div>',
        Modal: true,
        Center: true,
        Width: 450,
        Draggable: false,
        Resizable: false,
        OnLoad: function (popup) {
            $f("audio", "http://releases.flowplayer.org/swf/flowplayer-3.2.7.swf", {

                // fullscreen button not needed here
                plugins: {
                    controls: {
                        fullscreen: false,
                        height: 30,
                        autoHide: false
                    }
                }
            });
        },
        OnClose: function (popup) {
            $('#audio').remove();
        }
    };

    openPopup(windowCfg);
}

function launchVideoPlayer(videoCfg) {

    var windowCfg;
    var content;

    if (videoCfg.IsEmbedCode) {

        content = '<div id="video">' + videoCfg.VideoContent + '"</div>';

        if (videoCfg.AdditionalContent != null) {
            content += "<div style='padding:10px;'>"+ videoCfg.AdditionalContent +"</div>";
        }

        windowCfg = {
            Title: 'Now playing ' + videoCfg.Name,
            Html: content,
            Modal: true,
            Center: true,
            Draggable: false,
            Resizable: false,
            OnClose: function (popup) {
                $('#video').remove();
            }
        };

    } else {

        var width = (videoCfg.Width != null)? videoCfg.Width : 640;
        var height = (videoCfg.Height != null)? videoCfg.Height : 480;

        content = '<div id="video" style="display:block;width:' + width + 'px;height:' + height + 'px;" href="' + videoCfg.VideoContent + '"></div>';

        if (videoCfg.AdditionalContent != null) {
            content += "<div style='padding:10px;'>" + videoCfg.AdditionalContent + "</div>";
        }

        windowCfg = {
            Title: 'Now playing ' + videoCfg.Name,
            Html: content,
            Modal: true,
            Center: true,
            Width: 640,
            Draggable: false,
            Resizable: false,
            OnLoad: function (popup) {
                $f("video", "http://releases.flowplayer.org/swf/flowplayer-3.2.7.swf");
            },
            OnClose: function (popup) {
                $('#video').remove();
            }
        };
    }

    openPopup(windowCfg);
}

