﻿$(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);

        //video or images
        if ($(this).hasClass('video')) {
            $("#largeMedia a").html('').append("<div id='flashplayer'></div>");
            writeFlash(newSrc, 'flashplayer', $(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 a").html('').append(image);

                $("#largeMedia a").attr("rel", rel);
                $("#largeMedia a").attr("title", title);
                $("#largeMedia a").attr("href", newSrc);

                $("#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 so = new SWFObject(videoroot + '/player/player.swf', 'vidplayer', $('#largeMedia').width(), $('#largeMedia').height(), '6', '#FFF');
    so.addVariable('file', videoroot+url);
    so.addParam('allowFullScreen', 'true');
    so.addParam('scale', 'noScale');
    if (thumbImage != null) {
        so.addVariable('image', thumbImage);
    }
    so.write(id);
}

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 + '...');
    }
}
