﻿$(document).ready(function () {
    var constructDiv = document.getElementById('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');
        $(constructDiv).css('top', ((clientHeight - height) / 2) + 'px');
        $(constructDiv).css('z-index', 100);
    }

    $('#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 url = $(this).attr('src');
        var id = $(this).attr('id');

        writeFlash(url, id);
    });

    /*$('#largeMedia a').click(function(event) {
    event.stopPropagation();
    event.preventDefault();

    var rel = $(this).attr('rel');
    if (rel != null) {
    var pattern = /(\d+),(\d+)/g;
    var str = rel.match(pattern)[0];

    var newsId = str.split(',')[0];
    var mediaId = str.split(',')[1];

    showImagePopup(newsId, mediaId);
    }
    return false;
    });*/

    $("#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).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');

                image.alt = title;

                imageFix(image, $('#largeMedia').width(), $('#largeMedia').height());

                $("#largeMedia").html('<a href=\'\'></a>');
                $('#largeMedia a').append(image);

                $("#largeMedia a").attr("rel", rel);
                $("#largeMedia a").attr("title", title);
                $("#largeMedia a").attr("href", newSrc);
                $("#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) {
            submitSearchForm();
            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 () {
            $(img).css('position', 'absolute');
            $(img).css('left', (parent.width() - img.width) / 2);
            $(img).css('top', (parent.height() - img.height) / 2);

            $(img).click(function (event) {
                event.preventDefault();
                parent.find('.video').trigger('click');
            });

            parent.find('a').append(img);
        };
        img.src = '/images/playVideo.png';
    });

    //support the new photostyle enlargements
    $('#newscontent .mediaWrapper a').photoStyle();
});

function writeFlash(url, id, thumbImage) {
    var isMp4 = url.search(/\.mp4/) > -1;
    
    var flashPlayer = { type: 'flash', src:  videoroot + '/player5/player.swf' };
    var settings = {
        file: url,
        width: $('#largeMedia').width(),
        height: $('#largeMedia').height(),
        players : []
    };


    if (isMp4 && !($.browser.mozilla)) {
        settings.players.push({ type: 'html5' });
    }

    settings.players.push(flashPlayer);
                                            
    jwplayer(id).setup(settings);

}

function resize() {
    $('#largeMedia img').imageFix($('#largeMedia').width(), $('#largeMedia').height());
}


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 + '...');
    }
}

