function loadScripts(){
    now();

    $(document).ready(function(){
        onDomReady()
    });

    $(window).load(function () {
        onPageLoaded();
    });
}

function now(){
    Cufon.replace('.menu > li > a', { fontFamily: 'font01', hover: true });
    Cufon.replace('#footer span', { fontFamily: 'font01', hover: true });
    Cufon.replace('#footer a', { fontFamily: 'font01', hover: true });
    Cufon.replace('.menu > li > ul > li > a', { fontFamily: 'font01', hover: true });
    Cufon.replace('.menu > li > ul > li > ul > li > a', { fontFamily: 'font01', hover: true });
    Cufon.replace('.showcase', { fontFamily: 'font01', hover: true })
    Cufon.replace('#slogan > h1', { fontFamily: 'font01', hover: true })
}

function onDomReady(){
        nav();
        nospam();
        style();
        onResize();        
        scroller();
        $('.cb').colorbox();
        $(".youtube").colorbox({iframe:true, innerWidth:500, innerHeight:360});
        $('.scroller').jScrollPane(
        	{
        		verticalDragMinHeight: 60,
				verticalDragMaxHeight: 60
        	}
        );
    }

function onPageLoaded(){ 
        resizeBackground();
        $('.jThumbnailScroller').thumbnailScroller({
        scrollerType:"hoverPrecise",
        scrollerOrientation:"vertical",
        scrollSpeed:800,
        scrollEasing:"easeOutCirc",
        scrollEasingAmount:800,
        acceleration:4,
        noScrollCenterSpace:10,
        autoScrolling:0
    });
}

function onResize(){
   $(window).resize(function() {
        resizeBackground();
        $('.sub-menu').css({
            'height' : $(window).height()
        })            
   });    
}

function style(){
    $('.menu-nav-container').addClass('fullheight');
    $('.sub-menu .sub-menu').removeClass('sub-menu')
    $('.sub-menu').css({
            'height' : $(window).height()
        })
}

function resizeBackground() {
    var menuWidth = 0, footerHeight = 0;
    var bgWidth = $(window).width()- menuWidth, bgHeight = $(window).height()- footerHeight;
    $container = $("#background-container");
    $img = $("#background-container").find('img');  
    
    $container.css({
            "display":"block",
            "position":"fixed",
            "top":"0px",
            "left": menuWidth + "px",
            "z-index":"-1",
            "overflow":"hidden",
            "width": bgWidth + "px",
            "height": bgHeight + "px"
        });    
   
    var w_w = bgWidth,
        w_h = bgHeight,
        r_w = w_h / w_w,
	i_w = $img.width(), 
        i_h = $img.height(), 
        r_i = i_h / i_w,
	new_w,new_h;

	if(r_w > r_i){
            new_h = w_h;
            new_w = w_h / r_i;
	}
	 else{
            new_h = w_w * r_i;
            new_w = w_w;
	}

	$img.css({
            position : 'absolute',
            width : new_w + 'px',
            height : new_h + 'px',
            left : (w_w - new_w) / 2 + 'px',
            top	: (w_h - new_h) / 2 + 'px'
	});    
}



function nav(){
	$(".menu-item").addClass("no-child");
	$(".menu-item").has('.sub-menu').addClass("has-child").removeClass("no-child");
	
	$(".menu > li").hover(function(){
		$(this).addClass('selected')
		
	    $(this).find('.sub-menu').show().animate({width: '160px'}, 500);	            		
          
	}, function(){
		$(this).removeClass('selected');
		$(this).find('.sub-menu').stop(true,true).css({'width':'0px'}).hide();
	});
	

}


function scroller(){
    function loadImage(index){

        $path = $(".jThumbnailScroller a").eq(index).attr('href');
        $oldImg = $("#background-container").find('img');
        $newImg = "<img style='display:none' src='" + $path +  "'/>";
        $("#background-container").append($newImg)
            $oldImg.fadeOut(1000, function() {
                $(this).remove();
            })
        $("#background-container").find('img').fadeIn(1000);
        resizeBackground();
        $(".jThumbnailScroller .selected").removeClass('selected')
        $(".jThumbnailScroller a").eq(index).addClass('selected');
    }

    if ($(".jThumbnailScroller").length > 0){ 
            loadImage(0);
            $(".jThumbnailScroller a:last").addClass('last');
            $(".jThumbnailScroller a").click(function(event){
                event.preventDefault();
                $imgIndex = $(".jThumbnailScroller a").index($(this));
                loadImage($imgIndex);
            })
            
            $('.gallery-next').click(function(event){
                event.preventDefault(); 
                $indexSelected = $(".jThumbnailScroller a").index($('.selected'))
                $indexLast = $(".jThumbnailScroller a").index($('.last'))
                if ($indexSelected != $indexLast){
                                    loadImage($indexSelected + 1)    
                }else{
                                    loadImage(0)    
                }        
            })
            
            $('.gallery-prev').click(function(event){
                event.preventDefault(); 
                $indexSelected = $(".jThumbnailScroller a").index($('.selected'))
                $indexLast = $(".jThumbnailScroller a").index($('.last'))
                if ($indexSelected != 0){
                                    loadImage($indexSelected - 1)    
                }else{
                                    loadImage($indexLast)    
                }                        
            })
            
            $(".jThumbnailScroller a").hover(function(){
                $(this).find('div').fadeIn();
            },function(){
                $(this).find('div').fadeOut();
            })
            
    }        
    
}


