﻿/*global jQuery, $LAB, $site */

var BrikCmsSite = (function () {

    function extendMediaCollections() {
        var root = $site.appPath;

        // add the needed CSSiframe
        var link = document.createElement("link");
        link.type = "text/css";
        link.rel = "stylesheet";
        link.href = root + "/Scripts/jquery-lightbox-0.5/css/jquery.lightbox-0.5.css";
        document.getElementsByTagName("head")[0].appendChild(link);

        // add the needed scripts
        $LAB.script(root + "/Scripts/jquery-lightbox-0.5/js/jquery.lightbox-0.5.min.js").wait(function () {
            // once loaded, lets activate the lightbox
            jQuery("a.lightbox").lightBox({
                imageLoading: root + "/Scripts/jquery-lightbox-0.5/" + "images/lightbox-ico-loading.gif",
                imageBtnPrev: root + "/Scripts/jquery-lightbox-0.5/" + "images/lightbox-btn-prev.gif",
                imageBtnNext: root + "/Scripts/jquery-lightbox-0.5/" + "images/lightbox-btn-next.gif",
                imageBtnClose: root + "/Scripts/jquery-lightbox-0.5/" + "images/lightbox-btn-close.gif",
                imageBlank: root + "/Scripts/jquery-lightbox-0.5/" + "images/lightbox-blank.gif",
                fixedNavigation: true
            });
        });

        $LAB.script(root + "/Scripts/flowplayer/example/flowplayer-3.2.4.min.js").script(root + "/Scripts/flowplayer/jquery.tools.min.js").wait(function () {
            // create overlay
            var overlayEl = document.createElement("div");
            overlayEl.id = "video_overlay";
            document.body.appendChild(overlayEl);

            var playerEl = document.createElement("div");
            playerEl.id = "video_player";
            overlayEl.appendChild(playerEl);

            var player = flowplayer(playerEl.id, root + "/Scripts/flowplayer/flowplayer-3.2.5.swf", {
                clip: {
                    autoPlay: true,
                    autoBuffering: true // <- do not place a comma here  
                }
            });

            jQuery("a.video").each(function () {
                var el = jQuery(this);
                el.attr("rel", "#video_overlay");
                var src = el.attr("href");
                el.append('<div class="icon-play"></div>');
                el.overlay({
                    // use the Apple effect for overlay
                    effect: 'apple',
                    // when overlay is opened, load our player
                    onLoad: function () {
                        player.load(function () {
                            player.play(src);
                        });
                    },
                    // when overlay is closed, unload our player
                    onClose: function () {
                        player.unload();
                    }
                });
                el = null;
            });
        });

        $LAB.script(root + "/Scripts/jquery.cycle.lite.js").wait(function () {
            jQuery(".image-slideshow").cycle({
                    fit      :    1,
                    height   :  370,
                    width    :  658,
                    speed    :  700,
                    timeout  : 5000,
                    slideExpr: ".slide",
                    next     : ".next",
                    prev     : ".prev"
            });
        });
    }

    return {
        extendMediaCollections: extendMediaCollections
    };
})();



jQuery(function(){
    function query(e){
        location.href = $site.appPath + "/Search.aspx?q=" + encodeURIComponent(document.getElementById("txtQuery").value);
        e.preventDefault();
        e.stopPropagation();
    }
    jQuery("#btnQuery").click(query);
    jQuery("#txtQuery").bind("keypress", function(e){
        if (e.keyCode == 13) {
            query(e);
        }
    });
});

