$(document).ready(function()
{
  // Init pdBox
  $('.thickbox').pdBox({minWidth: 400});
	
	// Tabs
	$('.tabs-box').pdTabs();
	  
	// Adjust heights
	function init(){
        $('.crossroad .reset').adjustHeights({
            elements : ['h2', 'div', '.desc'],
            vAlign : 'top'
        });
        /*$('.project-box .reset').adjustHeights({
            elements : ['.name'],
            vAlign : 'top'
        });*/
        
    }  
    $(document).fontSizeListener({
        element: '#main',
        callFunction : function(){init()}
    });
	
	// SCROLL 
    $('.small-imgs .wrap').each(function(){
        var $this = $(this)
        var scroll = {
            el: $('ul', this)
            ,elWidth: function(){return scroll.el.width();}
            ,elScroll: function(){return scroll.el[0].scrollWidth;}
            ,elScrollL: function(){return scroll.el[0].scrollLeft;}
            ,itemWidth: function(){return $('li', scroll.el).width();}
            ,coef: 1
            ,hover: function(e){
                e = e || window.event;
                if(e.type == 'mouseenter'){
                    $(e.target).addClass('hover');
                }
                else if(e.type == 'mouseleave'){
                    $(e.target).removeClass('hover');
                }   
            }
            ,controlN: function(){
                  if(left + scroll.elWidth() >= scroll.elScroll()){
                      $('.next', $this).fadeTo("fast", 0.33).addClass('disable').removeClass('hover').unbind();
                  }
                  if($('.prev', $this).is('.disable') && left > 0){
                      $('.prev', $this).fadeTo("fast", 1).removeClass('disable').click(scroll.prev).bind('mouseenter mouseleave', scroll.hover);
                  }
            }
            ,controlP: function(){
                if(left <= 0){
                    $('.prev', $this).fadeTo("fast", 0.33).addClass('disable').removeClass('hover').unbind();
                }
                if($('.next', $this).is('.disable') && left + scroll.elWidth() < scroll.elScroll()){
                    $('.next', $this).fadeTo("fast", 1).removeClass('disable').click(scroll.next).bind('mouseenter mouseleave', scroll.hover);
                }
            }
            ,next: function(){
                scroll.el.stop();
                var length = scroll.itemWidth() * scroll.coef;
                left = left + length;
                scroll.controlN();
                scroll.el.animate({
                    scrollLeft: left +'px'
                }, 250*scroll.coef);        
            }
            ,prev: function(){
                scroll.el.stop();
                var length = scroll.itemWidth() * scroll.coef;
                left = left - length;
                scroll.controlP();
                scroll.el.animate({
                    scrollLeft: left +'px'
                }, 300*scroll.coef);        
            }
        };
        if(scroll.elWidth() < scroll.elScroll() ){
            $this.append('<div class="nav"><span class="prev"></span><span class="next"></span></div>');
            var left = scroll.elScrollL();
            scroll.el.css('overflow-x', 'hidden');
            scroll.controlN();
            scroll.controlP();
            $('.next:not(.disable)', this).click(scroll.next).bind('mouseenter mouseleave', scroll.hover);
            $('.prev:not(.disable)', this).click(scroll.prev).bind('mouseenter mouseleave', scroll.hover);
        }
        else{
            $('.small-imgs .wrap').addClass('no-scroll').removeClass('wrap');
        }
    });
	
});


