new jFrame({
    controllerPath : 'shares/js/',
    cache : false
}, 'jThutti');

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

var thutti = {
    _winHeight: 0,
    _headerHeight: 0,
    _footerHeight: 0,
    _bodyHeight: 0,
    _minimumHeight: 440,
    page: 0,
    initialize: function(){
        thutti._winWidth = $(window).width();
        thutti._winHeight = $(window).height();
        thutti._headerHeight = $('.HeaderContainer').eq(0).outerHeight();
        thutti._footerHeight = $('.FooterContainer').eq(0).outerHeight();
       
        thutti._bodyHeight = thutti._winHeight - (thutti._headerHeight + thutti._footerHeight);
       
        $('.BodyContainer').eq(0).css('height', thutti._bodyHeight); 
       
        var pd = parseInt($('.LeftContainer').eq(0).css('padding-top')) + parseInt($('.LeftContainer').eq(0).css('padding-bottom'));
        if(thutti._bodyHeight < thutti._minimumHeight){
            $('.MenuContainer').eq(0).css('height', thutti._minimumHeight);
            $('.RightContainer').eq(0).css('height', thutti._minimumHeight + pd);
        }else{
            $('.MenuContainer').eq(0).css('height', thutti._bodyHeight - pd);
            $('.RightContainer').eq(0).css('height', thutti._bodyHeight);
        }
        
        if($('#bxPortfolio')){	
            thutti.portfolio();
        }
    },
    portfolio_minwidth: 162,
    portfolio_itemperpage: 6,
    resize: function(){
        thutti._winWidth = $(window).width();
        thutti._winHeight = $(window).height();
        thutti._headerHeight = $('.HeaderContainer').eq(0).outerHeight();
        thutti._footerHeight = $('.FooterContainer').eq(0).outerHeight();
       
        thutti._bodyHeight = thutti._winHeight - (thutti._headerHeight + thutti._footerHeight);
       
        $('.BodyContainer').eq(0).css('height', thutti._bodyHeight); 
       
        var pd = parseInt($('.LeftContainer').eq(0).css('padding-top')) + parseInt($('.LeftContainer').eq(0).css('padding-bottom'));
        if(thutti._bodyHeight < thutti._minimumHeight){
            $('.MenuContainer').eq(0).css('height', thutti._minimumHeight);
            $('.RightContainer').eq(0).css('height', thutti._minimumHeight + pd);
        }else{
            $('.MenuContainer').eq(0).css('height', thutti._bodyHeight - pd);
            $('.RightContainer').eq(0).css('height', thutti._bodyHeight);
        }
        
        if($('#bxPortfolio')){	
            thutti.portfolio();
        }
    },
    portfolio: function(){  
        var parent_ele = $('#bxPortfolio-Image');
        
        parent_ele.css({
            'height': $('.RightContainer').eq(0).innerHeight(),
            'width': $('.RightContainer').eq(0).innerWidth() - ($('.bxPortfolio-Left-Landing').outerWidth() + $('.bxPortfolio-Right-Landing').outerWidth())
        });
        
        var child = $('ul', parent_ele).children('li').length;
        
        var li_width = parent_ele.innerWidth();// thutti.portfolio_itemperpage;
        var mgRight = parseInt($('ul li:first', parent_ele).css('margin-right'));
        var parentHeight = $('ul', parent_ele).parent().innerHeight();
        
        $('ul', parent_ele)
        .css({
            'height': parentHeight,
            'width': li_width * child
        })
        .find('li')
        .css({
            //'width': li_width - mgRight,
            'height': parentHeight
        });
        
        var x_height = 0;
        $('ul > li:first div', parent_ele).each(function(){
            x_height += parseInt($(this).outerHeight());
            x_height += parseInt($(this).css('margin-top'));
            x_height += parseInt($(this).css('margin-bottom'));
        });
        
        var total_ul_width = 0;
        $('ul li a', parent_ele).each(function(){
            $(this).css({
                'height': $(this).parent().outerHeight() - x_height
            });
            
            $(this).unbind('mouseenter').bind('mouseenter', function(){
                $(this).css('opacity', 1);
            });
            $(this).unbind('mouseleave')
            .bind('mouseleave', function(){
                $(this).css('opacity', 0.3);
            })
            .trigger('mouseleave')
            
            //$(this).find('img').attr('width', $('ul li a img', parent_ele).parent().innerWidth());
            $(this).find('img').attr('height', $('ul li a img', parent_ele).parent().innerHeight());
            $(this).parents('li').css({
                'width': $(this).find('img').width()
            });
            total_ul_width += $(this).parents('li').innerWidth();
        });
        
        $('ul', parent_ele).css({
            'width': total_ul_width
        });
        
        var fUl = $('ul', parent_ele);
        var total_width = parseInt(fUl.css('width'));
        //var show_width = parseInt(parent_ele.css('width'));
        var show_width = (parent_ele.innerWidth() / thutti.portfolio_itemperpage);//fUl.find('li:first-child').outerWidth();        
                
        $('a.previous').bind('click', function(e){
            e.stopPropagation();
            e.preventDefault();
            portfolio_previous();
        });
        
        $('a.next').bind('click', function(e){
            e.stopPropagation();
            e.preventDefault();
            portfolio_next();            
        });
        
        $('ul', parent_ele).css({
            'left': -($('ul', parent_ele).children('li').eq(thutti.page).position().left)
        });
                
        function portfolio_previous(){
            var position = fUl.position();
            if(position.left < 0){
                var newposition = position.left + $('ul li', parent_ele).eq(thutti.page-1).innerWidth();//show_width;
                
                if(newposition >= 0){
                    newposition = 0;
                }
                
                fUl.stop().animate({
                    'left': newposition
                }, 500, 'easeInOut', function(){
                    thutti.page--;
                    if(newposition == 0)
                        thutti.page = 0;
                });
            }
        }        
       
        function portfolio_next(){
            var position = fUl.position();
            var newposition = position.left - $('ul li', parent_ele).eq(thutti.page).innerWidth();//show_width;
            var maxposition = -(total_ul_width - $('ul li:last-child', parent_ele).innerWidth());
            
            if(newposition <= maxposition){
                newposition = maxposition;
            }
            
            fUl.stop().animate({
                'left': newposition
            }, 500, 'easeInOut',function(){
                thutti.page++;
                if(thutti.page == $('ul li', parent_ele).length)
                    thutti.page = $('ul li a', parent_ele).length - 1;
            });
        }
    }   
};

$(document).ready(function(){
    $(window).resize(thutti.resize);
    thutti.initialize();
});
