/* JQUERY APP */
$(document).ready(function() {
    if($('body.adminPage').length < 1) {
        setupLayout();
        setupGallery();
    }
});

function setupLayout() {
    $('.contentContainer:not(.gallery)').css({
        'height':
        $(window).height()
            - $('.mainNavigationContainer .mainNavigationBox').outerHeight()
            - ($('.headerContainer').outerHeight() * 2),
        'margin-top': $('.mainNavigationContainer .mainNavigationBox').outerHeight() / 2
    });
    $('.contentContainer.gallery').css({
        'height':
        $(window).height()
            - $('.mainNavigationContainer .mainNavigationBox').outerHeight()
            - $('.headerContainer').outerHeight()
    });
    
    $('.galleryContainer').css({
        'margin-top': (($('.contentContainer').height() - $('.galleryContainer').height()) / 2) - $('.artistNaviationContainer').height()
    }).show();
    
    if($('.contentContainer .image img').load(function() {
        $('.contentContainer .image').css({
            'margin-top': ($('.contentContainer').height() - $('.contentContainer .image').height() - ($('.artistNaviationContainer, .currentNaviationContainer, .pastNaviationContainer').height() * 2)) / 2
        }).show();
    }));
    
    intCountMainNavBoxes = $('.mainNavigationContainer .mainNavigationBox').length;
    strMainNavWidth = Math.floor($('#mainContainer').width() / intCountMainNavBoxes);
    intTop = $(window).height() - ($('.mainNavigationContainer .mainNavigationBox').outerHeight());
    $('.mainNavigationContainer').css({
        'top': intTop,
        'width': $('#mainContainer').width()
    });
    $('.mainNavigationContainer .mainNavigationBox').width(strMainNavWidth + 'px');
    showMainNavPointRecursive($('.mainNavigationContainer div.mainNavigationBox').first());
    
    if($('.newsletterSubscribeContainer').length > 0) {
        $('#btnNewsletterSubscribe').click(function() {
            _strEmailAddress = $('#newsletterSubscribeEmailAddress').val();
            reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
            if(reg.test(_strEmailAddress) == false) {
                $('.newsletterSubscribeContainer .newsletterSubscribeError')
                    .html("Entered email address appears to be invalid. Try again.")
                    .slideDown('fast');
            } else {
                exeInterface({ '__ajax': 'newsletter-subscribe',  'e': _strEmailAddress });
                $('.newsletterSubscribeContainer')
                    .html('<div>Thanks for subscribing! You will receive the next issue soon.</div>');
            }
            
        })
    }
}

function setupGallery() {
    
    var intImagesCount = $('.galleryImage').length;
    var intImagesLoaded = 0;
    
    objRow = $('.galleryRow').data('w', 0);
    
    $('.galleryImage').load(function() {
        intWidth = $(this).width();
        intHeight = $(this).height();
        $(this).parent().find('.galleryLabel').css({"width":intWidth});
        objRow = $(this).parent().parent();
        objRow.data('w', objRow.data('w') + Math.ceil(intWidth / 4) + 10);
        objRow.css(
            { 'width': objRow.data('w') }
        );
        objGallery = $('.galleryContainer');
        intMaxHeight = objRow.height();
        $(this)
            .data('current', false)
            .data('w', intWidth)
            .data('h', intHeight)
            .click(function() {

                currentImage = $(this);
                
                if(!currentImage.data('current')) {
                    objRow.data('w', Math.ceil(objRow.data('w') - (currentImage.data('w') / 4) + currentImage.data('w')));
                    objRow.css(
                        { 'width': objRow.data('w') }
                    );
                }
                
                currentImage.animate({
                    'width': currentImage.data('w'),
                    'height': currentImage.data('h'),
                    'margin-top': (intMaxHeight / 2) - ($(this).data('h') / 2)
                }, function() {
                    currentImage
                        .data('current', true)
                        .parent().find('.galleryLabel').fadeIn('fast');
                });
                
                currentIndex = currentImage.parent().attr('id').split('-')[1];
                currentOffset = 0;

                $('.galleryImage').each(function(i, e) {
                    if(i < currentIndex) {
                        currentOffset -= $(this).data('current') ? ($(this).data('w')) + 10 : ($(this).data('w') / 4) + 10;
                    }
                });

                currentOffset += ($('.contentContainer').width() / 2) - (currentImage.data('w') / 2);
                if(currentOffset > 0) currentOffset = 0;

                objGallery.scrollTo(
                        (0 - currentOffset),
                        {
                            'duration': 400,
                            'axis' : 'x'
                        }
                    );

            }).css({
                'width': $(this).data('w') / 4,
                'height': $(this).data('h') / 4,
                'margin-top': (intMaxHeight / 2) - ($(this).data('h') / 8)
            }).show();
    });
    
}

function showMainNavPointRecursive (objCurrentNav) {
    $(objCurrentNav).fadeIn(100, function() {
        if($(this).next().length > 0) {
            showMainNavPointRecursive($(this).next());
        } else {
            if($('.artistNaviationContainer, .currentNaviationContainer, .pastNaviationContainer').length > 0) {
                intTop = $(window).height() - ($('.mainNavigationContainer .mainNavigationBox').outerHeight());
                $('.artistNaviationContainer').css({
                    'top': intTop - 12,
                    'margin-left': $('#page-artists a').position().left
                }).fadeIn(150);
                $('.currentNaviationContainer').css({
                    'top': intTop - 12,
                    'margin-left': $('#page-current a').position().left
                }).fadeIn(150);
                $('.pastNaviationContainer').css({
                    'top': intTop - 12,
                    'margin-left': $('#page-past a').position().left
                }).fadeIn(150);
                intHeight = $('.contentContainer').height();
                $('.contentContainer').css(
                    'height', intHeight - (10 * 2)
                );
            }
        }
    });
}

function exeInterface (objParams, postFunction) {
    $.post(_BASE_URL, objParams, function(data) {
        if(typeof postFunction !== "undefined") postFunction(data, objParams);
    }, 'json');
}
