﻿/// <reference path="~/Scripts/jquery-1.5.js"/>


//                "<script type=""text/javascript"">"& vbCrlf &_
//		            "function showImagePopup(newsId, mediaId) {"& vbCrlf &_
//		                "window.open('/nw-"&b_id&"-"&PAGEMETHOD_MEDIAPOPUP&"-' + newsId + '/"&KZN_newsUrlPart&"?mediaId=' + mediaId +'"&sec&"' , 'mediaPopup', 'menubar=no,location=no,resizable=no,width=625,height=600');" & vbCrLf &_
//                    "}" & vbCrLf &_



$(document).ready(function () {
    $.ajaxSetup({ cache: false });

    var constructDiv = $('#underconstructionDIV');
    if (constructDiv != null) {
        var width = constructDiv.width();
        var height = constructDiv.height();
        var clientWidth = $(window).width();
        var clientHeight = $(window).height();

        constructDiv.css({
            'left' : ((clientWidth - width) / 2) + 'px',
            'top': ((clientHeight - height) / 2) + 'px',
            'z-index': 100
        });
    }
});

var NewsModule = (function () {

    var postReaction = function (html) {
        $('#newsReaction').html(html);

        toggleReactionButton();
        clearReactionFields();
    };

    var toggleReactionButton = function () {
        $('#news-content #submitreaction').toggle();
    };

    var clearReactionFields = function () {
        $('#news-content #reactionForm input[type=text], #news-content #reactionForm textarea').val('');
    };

    var showOverlay = function (element) {
    }

    var init = function () {
        $('#news-content .reactionsNavigation a').live('click', function (e) {
            e.preventDefault();

            var url = $(this).attr('href');
            $.get(url, function (html) {
                $('#newsReaction').html(html);
            });
        });

        $('#sendNews').click(function (event) {
            event.preventDefault();
            event.stopPropagation();
            window.open($(this).attr('href'), 'sendNews', 'menubar = no, location = no, resizable = no, width = 625, height = 500');
            return false;
        });
        $('#printNews').click(function (event) {
            event.preventDefault();
            event.stopPropagation();
            window.open($(this).attr('href'));
            return false;
        });

        $("#media").jcarousel({ scroll: 2 });

        $("#largeMedia .video").each(function () {
            var $this = $(this);
            var url = $this.attr('src');
            var id = $this.attr('id');
            var thumb = $this.data('thumb');
            //remove the <a/> as this would result in a photostyle being applied to this video
            $this.unwrap();

            writeFlash(url, id, thumb);
        });

        $("#media .media").click(function (event) {
            event.preventDefault();
            event.stopPropagation();

            var newSrc = $(this).parent().attr("href");
            $("#largeMedia .media").hide();

            var original = $(this);

            //unload a previous video
            if ($('#flashplayer')[0]) {
                try {
                    jwplayer("flashplayer").destroy();
                }
                catch (e) { }
            }

            //video or images
            if ($(this).hasClass('video')) {
                $('#largeMedia').html('').append('<div id=\'video\'></div>');
                writeFlash(newSrc, 'video', $(this).data('thumb'));
                //writeFlash(newSrc, 'video', $(this).attr('src'));

                $("#largeMedia .media").show();
            } else if ($(this).hasClass('image')) {

                //Load the image
                var image = new Image();
                image.onload = function () {
                    var rel = original.parent().attr('rel');
                    var title = original.parent().attr('title');
                    var href = original.parent().attr('data-original');

                    image.alt = title;

                    $("#largeMedia").html('<a href=\'\'></a>');
                    $('#largeMedia a').append(image);

                    $("#largeMedia a").attr("rel", rel);
                    $("#largeMedia a").attr("title", title);
                    $("#largeMedia a").attr("href", href);
                    $("#largeMedia a").photoStyle();
                    $("#largeMedia .media").show();
                };
                image.src = newSrc;
            }
            return false;
        });

        /* Prevent the enter key of submitting the form*/
        $('#searchForm input').keypress(function (event) {
            if (event.keyCode == 13) {
                $('searchForm').submit();
                return false;
            }
        });

        //$('#searchSubmit').click(submitSearchForm);

        //add the videoplayback icon to every video thumbnail
        $("#media .video").each(function () {
            var parent = $(this).parents("div.carouselImageWrapper:eq(0)");
            var img = new Image();
            img.onload = function () {
                var $img = $(img);

                $img.css({
                    'position': 'absolute',
                    'left': (parent.width() - img.width) / 2 + 'px',
                    'top': (parent.height() - img.height) / 2 + 'px'
                }).click(function (event) {
                    event.preventDefault();
                    parent.find('.video').trigger('click');
                });

                parent.find('a').append($img);
            };
            img.src = '/images/playVideo.png';
        });

        $('#news-content .mediaWrapper a').photoStyle();
    };

    $(document).ready(function () {
        init();
    });

    return {
        postReaction: postReaction,
        toggleReactionButton: toggleReactionButton,
        clearReactionFields: clearReactionFields,
        showOverlay: showOverlay
    }
})();

function writeFlash(url, id, thumbImage) {
    var isMp4 = url.search(/\.mp4/) > -1;

    videoroot = 'http://video.ziber.nl'
    var flashPlayer = { type: 'flash', src: videoroot + '/player5/player.swf' };
    var settings = {
        file: url,
        width: $('#largeMedia').width(),
        height: $('#largeMedia').height(),
        image: thumbImage,
        players: []
    };


    if (isMp4 && !($.browser.mozilla)) {
        settings.players.push({ type: 'html5' });
    }

    settings.players.push(flashPlayer);

    jwplayer(id).setup(settings);

}

function ellipsis(e) {
    var w = e.width() - 10000;
    var t = e.html();

    e.html('<span>' + t + '</span>');
    e = e.children(":first");

    while (t.length > 0 && e.width() >= w) {
        t = t.substr(0, t.length - 1);
        e.html(t + '...');
    }
}

jQuery.fn.imageFix = function () {
    $(this).each(function () {
        var img = $(this);

        var parentWidth = $('#largeMedia').width();
        var parentHeight = $('#largeMedia').height()

        var width = img.width();
        var height = img.height();

        //`this` refers to the jQuery object, that object always is an array
        //to refer to the DOM object we need to do `this[0]`
        if (img[0].naturalWidth) { width = img[0].naturalWidth; }
        if (img[0].naturalHeight) { height = img[0].naturalHeight; }

        var topOffset = 0;
        if (height < parentHeight) {
            topOffset = (parentHeight - height) / 2;
        } else if (height > parentHeight) {
            topOffset = (height - parentHeight) / 2;
        }

        var leftOffset = 0;
        if (width < parentWidth) {
            leftOffset = (parentWidth - width) / 2;
        } else if (width > parentWidth) {
            leftOffset = (width - parentWidth) / 2;
        }

        img.css({
            'margin-top' : parseInt(topOffset, 10) + 'px',
            'margin-left' : parseInt(leftOffset, 10) + 'px'
        });
    });
};

