$(function () {
    jQuery.get('./scripts/counter.txt', function(data) {
    	$("#lblCount strong").html(data);
    });
 

    /* ---------------------------------------------
    popup windows
    --------------------------------------------- */

    var D = 550, A = 450, C = screen.height, B = screen.width, H = Math.round((B / 2) - (D / 2));
    if (C > A) { G = Math.round((C / 2) - (A / 2)); }
    $('.popup').click(function () {
        var url = $(this).attr('href');
        window.open(url, '', 'left=' + H + ',top=' + G + ',width=' + D + ',height=' + A + ',personalbar=0,toolbar=0,scrollbars=1,resizable=1');
        return false;
    });

    /* ---------------------------------------------
    popup DIV window (used for video player)
    --------------------------------------------- */

    /* onresizing or scrolling of window */
    $(window).bind('resize scroll', function () {
        var scrollTop = $(window).scrollTop();
        $('#shade').height($(window).height() + scrollTop);
        var currentItem = $('.popupGallery .content img:visible');
        resizePopup(currentItem.width(), currentItem.height());
    });

    function showPopup(id, isGallery) {
        //create and show shade
        var IE7 = (navigator.appVersion.indexOf('MSIE 7.') == -1) ? false : true;
        $('#shade').height($(document).height());
        if (IE7) {
            $('#shade').width($(document).width());
            $('#shade').css({ 'top': 0, 'left': 0 });
        }
        $('#shade').show();
        loadPopupContent(id, isGallery);
        $("body").css("overflow", "hidden");
        $('.popupGallery').fadeIn();
    }

    function positionPopup() {

        var scrollTop = $(window).scrollTop();
        var positionTop = (($(window).height() - $('.popupGallery').height()) / 2) + scrollTop;
        var positionLeft = ($(window).width() - $('.popupGallery').width()) / 2;

        //alert('scrollTop:' + scrollTop + '| positionTop:' + positionTop + '  positionLeft:' + positionLeft);
        $('.popupGallery').css({ 'top': positionTop, 'left': positionLeft });
    }

    function resizePopup(width, height) {
        if (width == null) { width = 640; }
        if (height == null) { height = 360; }
        var contentHeight = height - 102;
        $('.popupGallery').css({ 'width': width, 'height': height });
        $('.popupGallery .content').css({ 'width': width, 'height': contentHeight });
        positionPopup();
    }

    function loadPopupContent(id, isGallery) {
        var loading = '<div id="loadingPopup"><span>loading</span></div>';
        var top = (($(window).height() / 2) + $(window).scrollTop()) - 12;
        var left = ($(window).width() / 2) - 12;
        $('body').append(loading);
        $('#loadingPopup').css({ 'top': top, 'left': left });
        $.ajax({
            url: id,
            success: function (data) {
                $('.popupGallery .content').html(data);
                if (isGallery) {
                    $('.popupGallery .content img').hide();
                    var firstItem = $('.popupGallery .content img').first();
                    firstItem.show();
                    $(".left").show().fadeTo("fast", 1);
                    $(".right").show().fadeTo("fast", 1);
                    $(".left").fadeTo(3000, 0);
                    $(".right").fadeTo(3000, 0);
                    resizePopup(firstItem.width(), firstItem.height());
                }
                else {
                    var item = $('.popupGallery .content iframe');
                    $(".left").hide();
                    $(".right").hide();
                    resizePopup(item.width(), item.height());
                }
                if ($('.popupGallery').hasClass('en'))
                    $('.popupGallery .title p').text($('.popupGallery .content title').text());
                else
                    $('.popupGallery .title p').text($('.popupGallery .content title').attr('title'));
                $('#loadingPopup').remove();
            }
        });
    }

    function hidePopup() {
        $('.popupGallery').fadeOut(function () {
            $('.popupGallery iframe').remove();
            $('.popupGallery .content img').remove();
            $('#shade').hide();
            $("body").css("overflow", "auto");
        });
    }

    /* auto hide popups when clicking on the shade */
    $('#shade').live('click', function () {
        var id = "#" + $(this).next('.popupGallery').attr('id');
        hidePopup();
    });

    /* hide popup when clicking close */
    $('.popupGallery .title .close').click(function () {
        hidePopup();
        return false;
    });

    /* open popup on click */
    $('.inpopup').click(function () {
        showPopup($(this).attr('href'), $(this).hasClass('gallery'));
        return false;
    });

    /* load previous picture */
    $('.left').live('click', function () {
        showPrev();
    });

    /* load next picture */
    $('.right').live('click', function () {
        showNext();
    });

    $('.left').live('mouseleave', function () {
        $(this).fadeTo("slow", 0);
    });

    $('.right').live('mouseenter', function () {
        $(this).fadeTo("fast", 1);
    });

    $('.left').live('mouseenter', function () {
        $(this).fadeTo("fast", 1);
    });

    $('.right').live('mouseleave', function () {
        $(this).fadeTo("slow", 0);
    });

    function showPrev() {
        var currentItem = $('.popupGallery .content img:visible');
        var prevItem = currentItem.prev();
        if (!prevItem.hasClass('galleryItem'))
            prevItem = $('.popupGallery .content img').last();
        currentItem.hide();
        resizePopup(prevItem.width(), prevItem.height());
        prevItem.show();
    }

    function showNext() {
        var currentItem = $('.popupGallery .content img:visible');
        var nextItem = currentItem.next();
        if (!nextItem.hasClass('galleryItem'))
            nextItem = $('.popupGallery .content img').first();
        currentItem.hide();
        resizePopup(nextItem.width(), nextItem.height());
        nextItem.show();
    }
});
