/* The object */
var imageStack = new Object();

imageStack  = {
    viewSize : '',
    filmstripSize : '',
    animateDiff : '',
    initialize: function(){
       
        imageStack.checkContent();
        imageStack.handleNavigation();

    },
    checkContent: function(){

        if($('.contentLeft').length){
            imageStack.checkForStack($('.contentLeft').children());
        }
        if($('.contentRight').length){
            imageStack.checkForStack($('.contentRight').children());
        } 
    },

    checkForStack: function(selectedDiv){

        var intBlocksAmount = $(selectedDiv).children().length;
        $(selectedDiv).children().each(function(){
            imageStack.stackImages(this,intBlocksAmount);
        });

    },
    stackImages: function(selectedDiv,intBlocksAmount){

        var imgHeight = 100;
        var imgWidth = 100;
        var imgRadius = 0;
        if(intBlocksAmount == 1){
           imgHeight = 218;
        }
        if(intBlocksAmount > 1){
            imgHeight = 130;
        }
	
        var intAmountImages = $(selectedDiv).children().children('img').length;

        var countLoop = 1;
        if(intAmountImages > 1){
            $(selectedDiv).children().children('img').each(function(){

                imgRadius = parseFloat($(this).width()) / parseFloat($(this).height());
                imgWidth = imgRadius * imgHeight;



                $(this).attr('width',imgWidth.toFixed(0));
                $(this).attr('height',imgHeight.toFixed(0));
                
                if(countLoop == 1){
                    $(this).parent().attr({'class' : 'imageStackContainer'});
                    $(this).parent().removeAttr('style');
                    //$(this).wrap('<div class="imageStackContainer" />');
                    $(this).wrap('<div class="imageStackContent" />');
                    $(this).wrap('<div class="imageStackImg" rel="' + countLoop + '"/>');    
                } else {
                    //alert($(this).parent().parent().html());
                    $(this).appendTo($(this).parent().parent().children('.imageStackContainer').children('.imageStackContent'));
                    $(this).wrap('<div class="imageStackImg" style="display:none;" rel="' + countLoop + '"/>');
                }
                countLoop++;
            });
            //alert($(selectedDiv).children('.imageStackContainer').html());
            var intAlignNavigation = (parseFloat($(selectedDiv).parent().width()) - 50) / 2;
            $(selectedDiv).children('.imageStackContainer').append('<div style="margin-left:' + intAlignNavigation + 'px;" class="imageStackNavigation"><div class="imageStackNavigationPrev"></div><div class="imageStackNavigationNext"></div></div>');
            $(selectedDiv).find('.imageFloatContainer').each(function(){
                $(this).remove();
            });
            $('p').each(function(){
                if($(this).html() == '' || $(this).html() == '&nbsp;'){
                    $(this).remove();
                }
            });
        }
    },
    handleNavigation: function(){
        $('.imageStackNavigationPrev').click(function(){
            var intCurrentImg = 1;
            var intLastImg = 0;
            $(this).parent().parent().children('.imageStackContent').children('.imageStackImg').each(function(){
                if($(this).css('display') != 'none'){
                    intCurrentImg = parseFloat($(this).attr('rel'));
                }
                intLastImg++;
            });
            if(intCurrentImg > 1){
                $(this).parent().parent().children('.imageStackContent').children('.imageStackImg[rel=' + intCurrentImg + ']').hide();
                $(this).parent().parent().children('.imageStackContent').children('.imageStackImg[rel=' + (intCurrentImg - 1) + ']').show();
            }
            if((intCurrentImg - 1) <= 1){
                $(this).hide();
            }
            if($(this).parent().children('.imageStackNavigationNext').css('display') == 'none'){
                $(this).parent().children('.imageStackNavigationNext').show();
            }

        });
        $('.imageStackNavigationNext').click(function(){
            var intCurrentImg = 1;
            var intLastImg = 0;
            $(this).parent().parent().children('.imageStackContent').children('.imageStackImg').each(function(){    
                if($(this).css('display') != 'none'){
                    intCurrentImg = parseFloat($(this).attr('rel'));

                }
                intLastImg++;
            });
            if(intCurrentImg < intLastImg){
                $(this).parent().parent().children('.imageStackContent').children('.imageStackImg[rel=' + intCurrentImg + ']').hide();
                $(this).parent().parent().children('.imageStackContent').children('.imageStackImg[rel=' + (intCurrentImg + 1) + ']').show();
            }
            if((intCurrentImg + 1) == intLastImg){
                $(this).hide();
            }
            if($(this).parent().children('.imageStackNavigationPrev').css('display') == 'none'){
                $(this).parent().children('.imageStackNavigationPrev').show();
            }
        });
        $('.imageStackNavigationPrev').hide();
    }

}
