

/*
===   MODAL   ===
*/
function getWindowHeight() {
    var windowHeight;
    if (window.innerHeight && window.scrollMaxY) {
        windowHeight = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight) {
        windowHeight = document.body.scrollHeight;
    } else {
        windowHeight = document.body.offsetHeight;
    }
    return windowHeight;
}

function RefreshParent() {
    window.location.href = window.location.href;
}

function clientHeight() {
    var y = 0;
    if (self.pageYOffset) {
        y = self.pageYOffset;
    }
    else if (document.documentElement && document.documentElement.scrollTop) {

        y = document.documentElement.scrollTop;
    }

    if (y == getWindowHeight())
    { return 0; }
    else
    { return y; }


}

function Print(elementId) {
    $(elementId).printElement();
}

function GalleryLive() {

    var liWidth = $('.galleries_select ul li:first-child').outerWidth(true);

    var staticFirstLiRight = $('.galleries_select ul li:first-child').css('right');
    var staticLastLiRight = $('.galleries_select ul li:last-child').css('right');

    $('.galleries_select .next').live('click',function (e) {
        e.preventDefault();
        var newRight = parseInt($('.galleries_select ul li:first-child').css('right').replace('px', '')) - liWidth;
        $('.galleries_select ul li').animate({ right: newRight + 'px' }, { queue: false, duration: 500 });

    });

    $('.galleries_select .prev').live('click', function (e) {

        e.preventDefault();
        if (staticFirstLiRight != $('.galleries_select ul li:first-child').css('right')) {
            
            var newRight = parseInt($('.galleries_select ul li:first-child').css('right').replace('px', '')) + liWidth;
            $('.galleries_select ul li').animate({ right: newRight + 'px' }, { queue: false, duration: 500 });
            staticFirstLiRight
        }
       

    });
 }
//Main function , assigns behaiviors to different elements , when document is ready
$(document).ready(function () {
    var liWidth = $('.galleries_select ul li:first-child').outerWidth(true);
    $('.galleries_select .next').click(function (e) {
        e.preventDefault();
        var newRight = parseInt($('.galleries_select ul li:first-child').css('right').replace('px', '')) - liWidth;
        $('.galleries_select ul li').animate({ right: newRight + 'px' }, { queue: false, duration: 500 });

    });

    $('.galleries_select .prev').click(function (e) {
        e.preventDefault();
        var newRight = parseInt($('.galleries_select ul li:first-child').css('right').replace('px', '')) + liWidth;
        $('.galleries_select ul li').animate({ right: newRight + 'px' }, { queue: false, duration: 500 });

    });

    $('.list_expandable li span.expandable').toggle(function () {
        $(this).parent().find("div.ExpandableContent").slideDown('fast');
        $(this).attr("class", "expanded");
    }, function () {
        $(this).parent().find("div.ExpandableContent").slideUp('fast');
        $(this).attr("class", "expandable");
    });


    var targetLi;
    var parent;
    $('.gallery_thumbnail,.image_name').click(function (e) {

        e.preventDefault();
        $('.galleryPopupWindow>ul>li').hide();
        targetLi = $(this).attr('href');

        if ($(targetLi).next().attr('id') == null) {
            $('#sitePopupNext').hide();
        }
        else { $('#sitePopupNext').show(); }

        if ($(targetLi).prev().attr('id') == null) {
            $('#sitePopupPrev').hide();
        }
        else { $('#sitePopupPrev').show(); }


        parent = $('#sitePopup').parent();
        $('#sitePopup').appendTo('body').show();

        $(targetLi).show();
        $('#sitePopup').animate({ width: ($(targetLi + ' img').width()) + 'px', height: ($(targetLi + ' img').height()) + 'px' });

        var maskHeight = $(document).height();
        var maskWidth = $(window).width();

        $('#mask').css({ width: maskWidth, height: maskHeight, opacity: '0.5' });
        $('#mask').click(function () {
            $(this).hide();
            $('.galleryPopupWindow>ul>li').hide();
            $('#sitePopup').appendTo(parent).hide();
        });
        //transition effect		
        $('#mask').fadeIn(500);

        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();

        //Set the sitePopup window to center
        $('#sitePopup').css('top', (clientHeight() + 50));
        $('#sitePopup').css('left', winW / 2 - $(targetLi + ' img').width() / 2);

    });
    $('#sitePopupClose').click(function (e) {
        //Cancel the link behavior
        e.preventDefault();
        $('#mask').hide();
        $('.galleryPopupWindow>ul>li').hide();
        $('#sitePopup').appendTo(parent).hide();
    });

    $('#sitePopupNext').click(function (e) {
        $('.galleryPopupWindow>ul>li').hide();
        $('#sitePopupPrev').show();

        targetLi = '#' + $(targetLi).next().attr('id');

        if ($(targetLi).next().attr('id') == null) {
            $('#sitePopupNext').hide();
        }

        $(targetLi).show();
        $(targetLi + ' img').hide();
        var winW = $(window).width();
        var leftValue = winW / 2 - $(targetLi + ' img').width() / 2;
        $('#sitePopup').animate({ left: leftValue, width: parseInt($(targetLi + ' img').width()) + 'px', height: parseInt($(targetLi + ' img').height()) + 'px' }, 'slow', function (e) { $(targetLi + ' img').show(); });


    });
    $('#sitePopupPrev').click(function (e) {
        $('.galleryPopupWindow>ul>li').hide();
        $('#sitePopupNext').show();
        targetLi = '#' + $(targetLi).prev().attr('id');

        if ($(targetLi).prev().attr('id') == null) {
            $('#sitePopupPrev').hide();
        }

        $(targetLi).show();
        $(targetLi + ' img').hide();
        var winW = $(window).width();
        var leftValue = winW / 2 - $(targetLi + ' img').width() / 2;

        $('#sitePopup').animate({ left: leftValue, width: parseInt($(targetLi + ' img').width()) + 'px', height: parseInt($(targetLi + ' img').height()) + 'px' }, 'slow', function (e) { $(targetLi + ' img').show(); });


    });
});


